5
""" handle txt alarms """
7
__copyright__ = 'this file is in the public domain'
9
from gozerbot.generic import handle_exception, rlog, strtotime, striptime
10
from gozerbot.persist import Persist
11
from gozerbot.commands import cmnds
12
from gozerbot.examples import examples
13
from gozerbot.nextid import nextid
14
from gozerbot.fleet import fleet
15
from gozerbot.datadir import datadir
16
from gozerbot.plughelp import plughelp
17
from gozerbot.aliases import aliases
18
from gozerbot.periodical import periodical
21
plughelp.add('alarm', 'the alarm plugin allows for alarms which /msg the user \
22
giving the command at a certain time or at number of seconds from now')
24
class Alarmitem(object):
26
""" item holding alarm data """
28
def __init__(self, botname, i, nick, ttime, txt, printto=None):
29
self.botname = botname
34
self.printto = printto or ""
37
result = "%s %s %s %s %s" % (self.botname, self.idnr, self.nick, \
41
class Alarms(Persist):
43
""" class that holds the alarms """
45
def __init__(self, fname):
46
Persist.__init__(self, fname)
52
except AttributeError:
53
setattr(i, 'printto', "")
54
periodical.addjob(i.time - time.time(), 1, self.alarmsay, i.nick, i)
57
""" return number of alarms """
60
def bynick(self, nick):
61
""" get alarms by nick """
69
def alarmsay(self, item):
71
bot = fleet.byname(item.botname)
74
bot.say(item.printto, "[%s] %s" % (item.nick, item.txt))
76
bot.say(item.nick, item.txt)
77
self.delete(item.idnr)
79
def add(self, botname, nick, ttime, txt, printto=None):
82
nrid = nextid.next('alarms')
83
item = Alarmitem(botname, nrid, nick, ttime, txt, printto=printto)
84
pid = periodical.addjob(ttime - time.time(), 1, self.alarmsay, nick, \
87
self.data.append(item)
91
def delete(self, idnr):
92
""" delete alarmnr """
93
for i in range(len(self.data)-1, -1, -1):
94
if self.data[i].idnr == idnr:
96
if periodical.killjob(idnr):
100
alarms = Alarms(datadir + os.sep + 'alarms')
106
""" return number of alarms """
109
def handle_alarmadd(bot, ievent):
110
""" alarm <txt-with-time> | <+delta> <txt> .. add an alarm """
111
if ievent.cmnd == 'DCC':
112
ievent.reply("sorry can't run alarm from dcc chat")
115
ievent.reply('alarm <txt-with-time> or alarm <+delta> <txt>')
118
alarmtxt = ievent.rest
119
# see if alarm time is given as delta aka starts with +
120
if alarmtxt[0] == '+':
122
sec = int(ievent.args[0][1:]) # get nr of seconds to sleep
124
ievent.reply('use +nrofsecondstosleep')
126
if len(ievent.args) < 2:
127
ievent.reply('i need txt to remind you')
130
ttime = time.time() + sec
131
# check for time overflow
133
ievent.reply("time overflow")
136
nrid = alarms.add(bot.name, ievent.nick, ttime, \
137
' '.join(ievent.args[1:]), ievent.printto)
138
ievent.reply("alarm %s set at %s" % (nrid, time.ctime(ttime)))
140
except Exception, ex:
141
handle_exception(ievent)
143
# see if we can determine time from txt
144
alarmtime = strtotime(alarmtxt)
146
ievent.reply("can't detect time")
148
# check if alarm txt is provided
149
txt = striptime(alarmtxt).strip()
151
ievent.reply('i need txt to remind you')
153
if time.time() > alarmtime:
154
ievent.reply("we are already past %s" % time.ctime(alarmtime))
157
nrid = alarms.add(bot.name, ievent.nick, alarmtime, txt, ievent.printto)
158
ievent.reply("alarm %s set at %s" % (nrid, time.ctime(alarmtime)))
160
cmnds.add('alarm', handle_alarmadd, 'USER', allowqueue=False)
161
examples.add('alarm', 'say txt at a specific time or time diff', \
162
'1) alarm 12:00 lunchtime 2) alarm 3-11-2007 0:01 birthday ! 3) alarm +180 \
165
def handle_alarmlist(bot, ievent):
166
""" alarm-list .. list all alarms """
168
for i in alarms.data:
169
result.append("%s) %s: %s - %s " % (i.idnr, i.nick, \
170
time.ctime(i.time), i.txt))
172
ievent.reply("alarmlist: ", result, dot=True)
174
ievent.reply('no alarms')
176
cmnds.add('alarm-list', handle_alarmlist, 'OPER')
177
examples.add('alarm-list', 'list current alarms', 'alarm-list')
179
def handle_myalarmslist(bot, ievent):
180
""" alarm-mylist .. show alarms of user giving the command """
182
nick = ievent.nick.lower()
183
for i in alarms.data:
185
result.append("%s) %s - %s " % (i.idnr, time.ctime(i.time), i.txt))
187
ievent.reply("your alarms: ", result, dot=True)
189
ievent.reply('no alarms')
191
cmnds.add('alarm-mylist', handle_myalarmslist, 'USER')
192
examples.add('alarm-mylist', 'list alarms of user giving the commands', \
194
aliases.data['myalarms'] = 'alarm-mylist'
196
def handle_alarmdel(bot, ievent):
197
""" alarm-del <nr> .. delete alarm """
199
alarmnr = int(ievent.args[0])
201
ievent.missing('<nr>')
204
ievent.reply('argument needs to be an integer')
206
if alarms.delete(alarmnr):
207
ievent.reply('alarm with id %s deleted' % alarmnr)
209
ievent.reply('failed to delete alarm with id %s' % alarmnr)
211
cmnds.add('alarm-del', handle_alarmdel, 'OPER')
212
examples.add('alarm-del', 'delete alarm with id <nr>', 'alarm-del 1')