aboutsummaryrefslogtreecommitdiff
path: root/remind.tcl
blob: edd6fd7f3185a170078912491e04773382952e2f (about) (plain) (blame)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
################################################
#################### ABOUT #####################
################################################
#
# Reminder-0.2 by Fredrik Bostrom
# for Eggdrop IRC bot
#
# Usage:
# !remind nick time message
#   - creates a new reminder for nick in the 
#     current channel at time with message
#   - time is a format parseable by the tcl 
#     command 'clock scan'. If the time consists
#     of several words, it has to be enclosed
#     in "".
#   - examples: 
#     !remind morbaq "tomorrow 10:00" Call Peter 
#     !remind morbaq "2009-12-31 23:59" Happy new year!
#
# !reminders 
#   - lists all active reminders
#
# !cancelReminder id
#   - cancels the reminder with id
#   - the id is the number preceeding the 
#     reminder in the list produced by 
#     !reminders
#   - note: the id may change as new reminders 
#     are added or old reminders removed. Always
#     check the id just before cancelling
#
#
################################################
################ CONFIGURATION #################
################################################

set datafile "scripts/reminders.dat"

################################################
######## DON'T EDIT BEOYND THIS LINE! ##########
################################################

bind pub - "!clockscan" pub:clockscan
bind pub - "!remind" pub:newReminder
bind pub - "!remindme" pub:newReminderfornick
bind pub - "!reminders" pub:getReminders
bind pub n "!cancelReminder" pub:cancelReminder
bind pub n "\$inspectReminders" pub:inspectReminders

array set reminders {}

# save to file
proc saveReminders {} {
    global reminders
    global datafile

    set file [open $datafile w+]
    puts $file [array get reminders] 
    close $file
}

# the run-at-time procedure
proc at {time args} {
    if {[llength $args]==1} {
	set args [lindex $args 0]
    }
    set dt [expr {($time - [clock seconds])*1000}]
    return [after $dt $args]
}

proc printReminder {reminderId {tonick ""} {fire "false"}} {
    global reminders

    # get the reminder
    set reminder $reminders($reminderId)

    set when [clock format [lindex $reminder 0] -format "%Y-%m-%dT%H:%M:%SZ"]
    set chan [lindex $reminder 1]
    set who [lindex $reminder 2]
    set timer [lindex $reminder 3]
    set what [lindex $reminder 4]

    if {$fire} {
	putserv "PRIVMSG $chan :\[remind\] $who: $what"
    } else {
	putserv "NOTICE $tonick :\[remind\] $reminderId: for $who at $when: $what"
    }
}

proc fireReminder {reminderId} {
    global reminders

    printReminder $reminderId "" "true"
    unset reminders($reminderId)
    saveReminders
}


proc pub:clockscan {nick host handle chan text} {
    set timeError ""
    # parse parameters
    set curTime [clock format [clock seconds] -format "%H:%M:%SZ" -gmt 1]
    regsub -all {([0-9]+)y$} $text "\\1 years $curTime" text
    regsub -all {([0-9]+)mo$} $text "\\1 months $curTime" text
    regsub -all {([0-9]+)w$} $text "\\1 weeks $curTime" text
    regsub -all {([0-9]+)d$} $text "\\1 days $curTime" text
    regsub -all {([0-9]+)hr?$} $text {\1 hours} text
    regsub -all {([0-9]+)m$} $text {\1 minutes} text
    regsub -all {([0-9]+)s$} $text {\1 seconds} text
    set time [catch {clock scan $text} timeResult]

    if {$time != 0} {
	    putserv "NOTICE $chan :\[remind\] unable to parse time: $timeResult"
	    return 1
    }

    set ISOTimeResult [clock format $timeResult -format "%Y-%m-%dT%H:%M:%SZ" -gmt 1]

    if {[clock seconds] > $timeResult} {
	    putserv "NOTICE $chan :\[clockscan\] error: \"$text\" (parsed as $timeResult → $ISOTimeResult) is in the past"
            return 1
    }

    putserv "NOTICE $chan :\[clockscan\] parsed \"$text\" as $timeResult → $ISOTimeResult"
}


proc pub:newReminderfornick {nick host handle chan text} {
    global reminders


    set timeError ""
    # parse parameters
    set id [clock seconds]
    set who $nick
    set when [lindex $text 0]
    set curTime [clock format [clock seconds] -format "%H:%M:%SZ" -gmt 1]
    regsub -all {([0-9]+)y$} $when "\\1 years $curTime" when
    regsub -all {([0-9]+)mo$} $when "\\1 months $curTime" when
    regsub -all {([0-9]+)w$} $when "\\1 weeks $curTime" when
    regsub -all {([0-9]+)d$} $when "\\1 days $curTime" when
    regsub -all {([0-9]+)hr?$} $when {\1 hours} when
    regsub -all {([0-9]+)m$} $when {\1 minutes} when
    regsub -all {([0-9]+)s$} $when {\1 seconds} when
    set time [catch {clock scan $when} timeResult]
    set what [lrange $text 1 end]

    if {$when == ""} {
	    putserv "NOTICE $chan :\[remind\] !remindme \"<time>\" <message> (time is a format parseable by the TCL command 'clock scan' - https://www.tcl.tk/man/tcl8.6/TclCmd/clock.html)"
	    return 0
    }

    if {$time != 0} {
	    putserv "NOTICE $chan :\[remind\] unable to parse time: $timeResult"
	    return 1
    }

    set ISOTimeResult [clock format $timeResult -format "%Y-%m-%dT%H:%M:%SZ" -gmt 1]

    if {[clock seconds] > $timeResult} {
	    putserv "NOTICE $chan :\[remind\] error: \"$when\" (parsed as $timeResult → $ISOTimeResult) is in the past"
            return 1
    }

    # create new entry
    set new [list $timeResult $chan $who null $what]

    # activate the event
    set timer [at $timeResult fireReminder $id]

    # putlog "new timer: $timer"

    # set the timer associated with this reminder
    set new [lreplace $new 3 3 $timer]
    # putlog "new reminder: $new"

    set reminders($id) $new
    saveReminders

    putserv "NOTICE $chan :\[remind\] ok, i'll remind you at $ISOTimeResult"
}

proc pub:newReminder {nick host handle chan text} {
    global reminders

    # parse parameters
    set id [clock seconds]
    set who [lindex $text 0]
    set when [lindex $text 1]
    set curTime [clock format [clock seconds] -format "%H:%M:%SZ" -gmt 1]
    regsub -all {([0-9]+)y$} $when "\\1 years $curTime" when
    regsub -all {([0-9]+)mo$} $when "\\1 months $curTime" when
    regsub -all {([0-9]+)w$} $when "\\1 weeks $curTime" when
    regsub -all {([0-9]+)d$} $when "\\1 days $curTime" when
    regsub -all {([0-9]+)hr?$} $when {\1 hours} when
    regsub -all {([0-9]+)m$} $when {\1 minutes} when
    regsub -all {([0-9]+)s$} $when {\1 seconds} when
    set time [catch {clock scan $when} timeResult]
    set what [lrange $text 2 end]

    if {$who == ""} {
	    putserv "NOTICE $chan :\[remind\] !remind <nick> \"<time>\" <message> (time is a format parseable by the TCL command 'clock scan' - https://www.tcl.tk/man/tcl8.6/TclCmd/clock.html)"
	    return 0
    }

    if {$time != 0} {
	    putserv "NOTICE $chan :\[remind\] unable to parse time: $timeResult"
	    return 1
    }

    set ISOTimeResult [clock format $timeResult -format "%Y-%m-%dT%H:%M:%SZ" -gmt 1]

    if {[clock seconds] > $timeResult} {
	    putserv "NOTICE $chan :\[remind\] error: \"$when\" (parsed as $ISOTimeResult) is in the past"
            return 1
    }

    # create new entry
    set new [list $timeResult $chan $who null $what]

    # activate the event
    set timer [at $timeResult fireReminder $id]

    # putlog "new timer: $timer"

    # set the timer associated with this reminder
    set new [lreplace $new 3 3 $timer]
    # putlog "new reminder: $new"

    set reminders($id) $new
    saveReminders

    putserv "NOTICE $chan :\[remind\] ok, i'll remind $who at $ISOTimeResult"
}

proc pub:getReminders {nick host handle chan text} {
    global reminders
    set chanReminders {}
    
    # count all reminders for this channel
    foreach {key value} [array get reminders] {
	if {[lindex $value 1] == $chan} {
	    lappend chanReminders $key
	}
    }

    # count the reminders
    set howMany [llength $chanReminders]

    # do we have reminders?
    if {$howMany < 1} {
	putserv "NOTICE $nick :\[remind\] no active reminders"
	return
    }

    # print reminders for this channel
    putserv "NOTICE $nick :\[remind\] $howMany active reminder(s):"
    foreach key $chanReminders {
	printReminder $key $nick
    }
}

proc pub:cancelReminder {nick host handle chan text} {
    global reminders

    set reminder $reminders($text)
    set timer [lindex $reminder 3]

    # putlog "Cancelling timer: $timer"
    after cancel $timer
    unset reminders($text)
    putserv "PRIVMSG $chan :Removed reminder with id $text"

    saveReminders
}

proc pub:inspectReminders {nick host handle chan text} {
    global reminders

    set timerString [after info]
    set reminderString [array get reminders]

    putserv "NOTICE $nick :Reminders: $reminderString"
    putserv "NOTICE $nick :Timers: $timerString"
}

proc initReminders {} {
    global reminders
    
    set reminderString [array get reminders]
    # putlog "Initiating reminders: $reminderString"

    # get current time
    set time [clock seconds]

    # get active timers
    set activeTimers [after info]

    # check for expired reminders and fire them
    foreach {key value} [array get reminders] {
	if {[lindex $value 0] < $time} {
	    fireReminder $key
	} elseif {[lsearch $activeTimers [lindex $value 3]] == -1} {
	    # if the reminder hasn't expired, check if the timer is already set
	    set timerId [at [lindex $value 0] fireReminder $key]
	    set reminders($key) [lreplace $value 3 3 $timerId]
	}
    }
    saveReminders
}


# read the old if they exist
set file [open $datafile]
set content [read $file]
close $file

if {$content == ""} {
    set content {}
}

array set reminders $content

initReminders


###################################
putlog "Reminder script loaded!"
###################################