mod_bridge.py

Sun, 19 Apr 2015 19:45:42 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 19 Apr 2015 19:45:42 +0300
changeset 123
aeb0d0788869
parent 120
9880bb697149
child 124
7b2cd8b1ba86
permissions
-rw-r--r--

- added commits.db and mercurial support restructure
- added new, from-scratch calculator for .calc

118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 import re
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 from configfile import Config
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 import irc as Irc
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 ModuleData = {
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 'commands':
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 [
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 {
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 'name': 'bridge',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 'description': 'Creates a bridge between two channels',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 'args': '<destination>',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 'level': 'admin',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 },
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 {
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 'name': 'unbridge',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 'description': 'Removes a bridge between two channels',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 'args': '<destination>',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 'level': 'admin',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 },
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 ],
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 'hooks':
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 {
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 'chanmsg': ['hook_chanmsg'],
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 }
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 }
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 def get_destination (bot, target, destination, error):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 match = re.match (r'^([A-Za-z0-9_]+)/(#[#A-Za-z0-9_]*)$', destination)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 if not match:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 error ('malformed destination')
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 destHost, destChannel = match.group (1, 2)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 dest = None
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 # ensure we know this target
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 for conndata in Config.get_nodelist ('connections'):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 if conndata.get_value('name') == destHost:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 break
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 else:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 error ('unknown connection %s' % destHost)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 for channel in conndata.get_value('channels'):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 if channel['name'] == destChannel:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 break
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 else:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 error ('unknown channel %s' % destChannel)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 if destHost == bot.name and destChannel == target:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 error ('cannot establish a bridge to self!')
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 return (destHost, destChannel)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 def cmd_bridge (bot, target, args, reply, error, **rest):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 source = (bot.name, target)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 dest = get_destination (bot, target, args['destination'], error)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 sourceName = '%s/%s' % source
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 destName = '%s/%s' % dest
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 bridges = Config.get_node('bridges')
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 if destName in bridges.get_value (sourceName, []) \
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 or sourceName in bridges.get_value (destName, []):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 error ('bridge to %s already established' % destName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 bridges.append_value ('%s/%s' % source, '%s/%s' % dest)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 bridges.append_value ('%s/%s' % dest, '%s/%s' % source)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 reply ('bridge to %s established' % destName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 def cmd_unbridge (bot, target, args, reply, error, **rest):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 source = (bot.name, target)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 dest = get_destination (bot, target, args['destination'], error)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 sourceName = '%s/%s' % source
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 destName = '%s/%s' % dest
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 bridges = Config.get_node('bridges')
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 if destName in bridges.get_value (sourceName, []) \
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 or sourceName in bridges.get_value (destName, []):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 try:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 bridges.get_value (sourceName, []).remove (destName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 bridges.get_value (destName, []).remove (sourceName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 except ValueError:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 pass
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 reply ('bridge to %s dropped' % destName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 else:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 error ('no bridge to %s established' % destName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 def hook_chanmsg (bot, channel, sender, message, **rest):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 sourceName = '%s/%s' % (bot.name, channel)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 for dest in Config.get_node('bridges').get_value (sourceName, []):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 try:
119
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
93 # Stuff some control characters into the name so that this doesn't cause people who
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
94 # are on both ends of the bridge to highlight themselves
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
95 # sender = sender[0:len(sender)/2] + (Irc.BoldChar * 2)
120
9880bb697149 - added doomseeker support, fixed up mod_bridge, now shortens new commit links with bit.ly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
96 # sender += '_'
9880bb697149 - added doomseeker support, fixed up mod_bridge, now shortens new commit links with bit.ly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
97 sender = sender[:len(sender)/2] + '`' + sender[len(sender)/2:]
119
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
98
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 clientName, channelName = dest.split ('/')
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 Irc.get_client (clientName).privmsg (channelName,
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 '[%s/%s] <%s> %s' % (bot.name, channel, sender, message))
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 except Exception as e:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 Irc.broadcast ('Error while bridging from %s to %s: %s' %
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 (sourceName, dest, e))
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 pass

mercurial