mod_bridge.py

Sun, 19 Apr 2015 20:52:49 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 19 Apr 2015 20:52:49 +0300
changeset 127
66b206bd9510
parent 124
7b2cd8b1ba86
child 146
c17b82b1f573
permissions
-rw-r--r--

Cranked up the runaway (8 was only for testing)

124
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
1 '''
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
2 Copyright 2014-2015 Teemu Piippo
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
3 All rights reserved.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
4
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
5 Redistribution and use in source and binary forms, with or without
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
6 modification, are permitted provided that the following conditions
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
7 are met:
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
8
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
9 1. Redistributions of source code must retain the above copyright
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
10 notice, this list of conditions and the following disclaimer.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
12 notice, this list of conditions and the following disclaimer in the
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
13 documentation and/or other materials provided with the distribution.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
14 3. The name of the author may not be used to endorse or promote products
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
15 derived from this software without specific prior written permission.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
16
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
27 '''
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
28
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 import re
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 from configfile import Config
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 import irc as Irc
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 ModuleData = {
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 'commands':
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 [
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 'name': 'bridge',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 'description': 'Creates a bridge between two channels',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 'args': '<destination>',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 'level': 'admin',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 },
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42
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 'name': 'unbridge',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 'description': 'Removes a bridge between two channels',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 'args': '<destination>',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 'level': 'admin',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 },
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
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 'hooks':
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 'chanmsg': ['hook_chanmsg'],
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 }
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 def get_destination (bot, target, destination, error):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 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
59 if not match:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 error ('malformed destination')
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 destHost, destChannel = match.group (1, 2)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 dest = None
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 # ensure we know this target
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 for conndata in Config.get_nodelist ('connections'):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 if conndata.get_value('name') == destHost:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 break
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 else:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 error ('unknown connection %s' % destHost)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 for channel in conndata.get_value('channels'):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 if channel['name'] == destChannel:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 break
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 else:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 error ('unknown channel %s' % destChannel)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 if destHost == bot.name and destChannel == target:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 error ('cannot establish a bridge to self!')
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 return (destHost, destChannel)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 def cmd_bridge (bot, target, args, reply, error, **rest):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 source = (bot.name, target)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 dest = get_destination (bot, target, args['destination'], error)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 sourceName = '%s/%s' % source
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 destName = '%s/%s' % dest
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 bridges = Config.get_node('bridges')
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 if destName in bridges.get_value (sourceName, []) \
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 or sourceName in bridges.get_value (destName, []):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 error ('bridge to %s already established' % destName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 bridges.append_value ('%s/%s' % source, '%s/%s' % dest)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 bridges.append_value ('%s/%s' % dest, '%s/%s' % source)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 reply ('bridge to %s established' % destName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 def cmd_unbridge (bot, target, args, reply, error, **rest):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 source = (bot.name, target)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 dest = get_destination (bot, target, args['destination'], error)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 sourceName = '%s/%s' % source
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 destName = '%s/%s' % dest
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 bridges = Config.get_node('bridges')
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 if destName in bridges.get_value (sourceName, []) \
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 or sourceName in bridges.get_value (destName, []):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 try:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 bridges.get_value (sourceName, []).remove (destName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 bridges.get_value (destName, []).remove (sourceName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 except ValueError:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 pass
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 reply ('bridge to %s dropped' % destName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 else:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 error ('no bridge to %s established' % destName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 def hook_chanmsg (bot, channel, sender, message, **rest):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 sourceName = '%s/%s' % (bot.name, channel)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 for dest in Config.get_node('bridges').get_value (sourceName, []):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 try:
119
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
121 # Stuff some control characters into the name so that this doesn't cause people who
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
122 # are on both ends of the bridge to highlight themselves
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
123 # 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
124 # 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
125 sender = sender[:len(sender)/2] + '`' + sender[len(sender)/2:]
119
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
126
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 clientName, channelName = dest.split ('/')
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 Irc.get_client (clientName).privmsg (channelName,
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 '[%s/%s] <%s> %s' % (bot.name, channel, sender, message))
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 except Exception as e:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 Irc.broadcast ('Error while bridging from %s to %s: %s' %
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 (sourceName, dest, e))
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 pass

mercurial