-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatch.tcl
More file actions
176 lines (153 loc) · 4.48 KB
/
match.tcl
File metadata and controls
176 lines (153 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# match.tcl
#
# This script was originally created to save clan matches in a file,
# to show all the saved matches and to be able to remove them from
# the file again. But it could be used for storing, showing and
# deleting arbitrary lines of text in a file.
#
# Usage:
# !addmatch match add match to the file/list.
# !showmatch show the saved matches.
# !delmatch number remove match with number (as shown
# by !showmatch) from the file/list.
# The numbers of remaining matches
# might change.
#
# The command names can be changed in the config section below.
#
# Enable for a channel with: .chanset #channel +match
# Disable for a channel with: .chanset #channel -match
#
# See https://github.com/hwipl/eggdrop-scripts for the latest version and
# additional information including the license (MIT).
# tested versions, might run on earlier versions
package require Tcl 8.6
package require eggdrop 1.8.4
# Config:
namespace eval ::match {
# channel flag for enabling/disabling
setudef flag match
# Name and/or path of the file you want to store the matches in and its
# backup file. Channel name will be prepended to file name
variable filename "matches.lst"
variable filenamebak "matches.lst.bak"
# Names of the commands for adding, deleting and showing
variable addcommand "!addmatch"
variable delcommand "!delmatch"
variable showcommand "!showmatch"
}
# End of Config
# this procedure shows the saved matches:
proc ::match::show {nick host hand chan arg} {
variable filename
# check channel flag if enabled in this channel
if {![channel get $chan match]} {
return 0
}
# check if file exists and contains matches
set mfile ${chan}.${filename}
set nomatches "No matches found."
if {![file exists $mfile] || [file size $mfile] == 0} {
puthelp "PRIVMSG $chan :$nomatches"
return 0
}
# read all matches from file
if {[catch {open $mfile r} input]} {
puthelp "PRIVMSG $nick :Error opening file: $input"
return 0
}
while {[gets $input line] >= 0} {
lappend matches $line
}
close $input
# show each match as a message in the channel
puthelp "PRIVMSG $chan :*** Match List ***"
for { set i 0 } { $i < [llength $matches] } { incr i } {
puthelp "PRIVMSG $chan :([expr $i +1]) [lindex $matches $i]"
}
puthelp "PRIVMSG $chan :*** End of Match List ***"
return 1
}
# this procedure deletes saved matches:
proc ::match::del {nick host hand chan arg} {
variable filename
variable filenamebak
# check channel flag if enabled in this channel
if {![channel get $chan match]} {
return 0
}
# arg containing the match id must be present
if {$arg == ""} {
return 0
}
# check if file exists and contains matches
set mfile ${chan}.${filename}
set mfilebak ${chan}.${filenamebak}
set noexist "Match does not exist."
if {![file exists $mfile] || [file size $mfile] == 0} {
puthelp "PRIVMSG $nick :$noexist"
return 0
}
# read all matches from file
if {[catch {open $mfile r} input]} {
puthelp "PRIVMSG $nick :Error opening file: $input"
return 0
}
while {[gets $input line] >= 0} {
lappend matches $line
}
close $input
# backup matches file
file copy -force $mfile $mfilebak
# write matches back to file
if {[catch {open $mfile w} output]} {
putshelp "PRIVMSG $nick :Error opening file: $output"
return 0
}
set deleted 0
for { set i 0 } { $i < [llength $matches] } { incr i } {
# omit the match that should be deleted
if {[expr $i +1] == $arg} {
set deleted 1
continue
}
puts $output "[lindex $matches $i]"
}
close $output
# send result back to caller
if {$deleted == 0} {
puthelp "PRIVMSG $nick :$noexist"
return 0
}
puthelp "NOTICE $nick :Match number $arg deleted."
return 1
}
# this procedure adds matches to the list:
proc ::match::add {nick host hand chan arg} {
variable filename
# check channel flag if enabled in this channel
if {![channel get $chan match]} {
return 0
}
# arg containing the match must be present
if {$arg == ""} {
return 0
}
# write match to file
set mfile ${chan}.${filename}
if {[catch {open $mfile a} output]} {
puthelp "PRIVMSG $nick :Error opening file: $output"
return 0
}
puts $output "$nick: $arg"
close $output
puthelp "NOTICE $nick :Match added."
return 1
}
namespace eval ::match {
bind pub - $showcommand ::match::show
bind pub - $addcommand ::match::add
# bind pub o|o $delcommand ::match::del
bind pub - $delcommand ::match::del
putlog "Loaded match.tcl"
}