conf: use shutil.move instead of os.rename for saving
[urk.git] / scripts / ctcp.py
blob09f35d3f00c190e9fa9be67ad7858d09232bdb57
1 import time
3 import events
4 import urk
5 import windows
7 def ctcp(network, user, msg):
8 param_list = msg.split(' ')
9 param_list[0] = param_list[0].upper()
10 network.raw('PRIVMSG %s :\x01%s\x01' % (user,' '.join(param_list)))
12 def ctcp_reply(network, user, msg):
13 param_list = msg.split(' ')
14 param_list[0] = param_list[0].upper()
15 network.raw('NOTICE %s :\x01%s\x01' % (user,' '.join(param_list)))
17 def emote(network, user, msg):
18 network.raw("PRIVMSG %s :\x01ACTION %s\x01" % (user, msg))
19 events.trigger(
20 'OwnAction', network=network, window=windows.get_default(network),
21 source=network.me, target=str(user), text=msg
24 def onCommandMe(e):
25 if isinstance(e.window, windows.ChannelWindow) or isinstance(e.window, windows.QueryWindow):
26 #kludge around switches so we don't lose the -> in /me -> something
27 #emote(e.network, e.window.id, ' '.join(e.args))
28 command, text = e.text.split(" ",1)
29 emote(e.network, e.window.id, text)
30 else:
31 raise events.CommandError("There's no one here to speak to.")
33 def onCommandCtcp(e):
34 ctcp(e.network, e.args[0], ' '.join(e.args[1:]))
36 def onCommandPing(e):
37 ctcp(e.network, e.args[0], 'PING %s' % time.time())
39 def onCommandCtcpreply(e):
40 ctcp_reply(e.network, e.args[0], ' '.join(e.args[1:]))
42 def setupRaw(e):
43 if not e.done and e.msg[1] in ('PRIVMSG', 'NOTICE') and \
44 e.text.startswith('\x01') and e.text.endswith('\x01'):
45 e_data = events.data(**e.__dict__)
46 e_data.text = e.text[1:-1]
47 tokens = e_data.text.split(' ')
48 e_data.name = tokens[0].upper()
49 e_data.args = tokens[1:]
50 if e.msg[1] == 'PRIVMSG':
51 events.trigger('Ctcp', e_data)
52 else:
53 events.trigger('CtcpReply', e_data)
54 e.done = True
56 def setupCtcpReply(e):
57 if e.name == 'PING':
58 try:
59 elapsed_time = "%0.2f seconds" % (time.time() - float(e.args[0]))
60 e.old_args = e.args
61 e.args = [elapsed_time]
62 except:
63 pass
65 def setupCtcp(e):
66 if not e.done:
67 if e.name == 'ACTION':
68 e_data = events.data(**e.__dict__)
69 e_data.text = ' '.join(e.args)
70 events.trigger('Action', e_data)
71 e.done = True
72 e.quiet = True
73 elif e.name == 'PING':
74 ctcp_reply(e.network, e.source, e.text)
75 e.done = True
76 elif e.name == 'VERSION':
77 ctcp_reply(e.network, e.source, 'VERSION %s - %s' % (urk.long_version, urk.website))
78 e.done = True
79 elif e.name == 'TIME':
80 ctcp_reply(e.network, e.source, 'TIME %s' % time.asctime())
81 e.done = True
83 events.load(__name__)