mod_bridge.py

Tue, 11 Aug 2015 19:12:30 +0300

author
Teemu Piippo <tsapii@utu.fi>
date
Tue, 11 Aug 2015 19:12:30 +0300
changeset 151
e24793fae424
parent 146
c17b82b1f573
permissions
-rw-r--r--

Yet more fixes

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
146
c17b82b1f573 Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents: 124
diff changeset
29 from __future__ import print_function
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 import re
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 from configfile import Config
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 import irc as Irc
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 ModuleData = {
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 'commands':
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 {
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 'name': 'bridge',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 'description': 'Creates a bridge between two channels',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 'args': '<destination>',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 'level': 'admin',
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 {
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 'name': 'unbridge',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 'description': 'Removes a bridge between two channels',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 'args': '<destination>',
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 'level': 'admin',
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
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 'hooks':
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 {
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 'chanmsg': ['hook_chanmsg'],
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
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 def get_destination (bot, target, destination, error):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 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
60 if not match:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 error ('malformed destination')
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 destHost, destChannel = match.group (1, 2)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 dest = None
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 # ensure we know this target
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 for conndata in Config.get_nodelist ('connections'):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 if conndata.get_value('name') == destHost:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 break
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 else:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 error ('unknown connection %s' % destHost)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 for channel in conndata.get_value('channels'):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 if channel['name'] == destChannel:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 break
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 else:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 error ('unknown channel %s' % destChannel)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 if destHost == bot.name and destChannel == target:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 error ('cannot establish a bridge to self!')
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 return (destHost, destChannel)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 def cmd_bridge (bot, target, args, reply, error, **rest):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 source = (bot.name, target)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 dest = get_destination (bot, target, args['destination'], error)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 sourceName = '%s/%s' % source
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 destName = '%s/%s' % dest
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 bridges = Config.get_node('bridges')
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 if destName in bridges.get_value (sourceName, []) \
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 or sourceName in bridges.get_value (destName, []):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 error ('bridge to %s already established' % destName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 bridges.append_value ('%s/%s' % source, '%s/%s' % dest)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 bridges.append_value ('%s/%s' % dest, '%s/%s' % source)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 reply ('bridge to %s established' % destName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 def cmd_unbridge (bot, target, args, reply, error, **rest):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 source = (bot.name, target)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 dest = get_destination (bot, target, args['destination'], error)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 sourceName = '%s/%s' % source
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 destName = '%s/%s' % dest
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 bridges = Config.get_node('bridges')
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 if destName in bridges.get_value (sourceName, []) \
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 or sourceName in bridges.get_value (destName, []):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 try:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 bridges.get_value (sourceName, []).remove (destName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 bridges.get_value (destName, []).remove (sourceName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 except ValueError:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 pass
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 reply ('bridge to %s dropped' % destName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 else:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 error ('no bridge to %s established' % destName)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 def hook_chanmsg (bot, channel, sender, message, **rest):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 sourceName = '%s/%s' % (bot.name, channel)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 for dest in Config.get_node('bridges').get_value (sourceName, []):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 try:
119
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
122 # Stuff some control characters into the name so that this doesn't cause people who
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
123 # are on both ends of the bridge to highlight themselves
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
124 # 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
125 # 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
126 sender = sender[:len(sender)/2] + '`' + sender[len(sender)/2:]
119
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
127
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 clientName, channelName = dest.split ('/')
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 Irc.get_client (clientName).privmsg (channelName,
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 '[%s/%s] <%s> %s' % (bot.name, channel, sender, message))
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 except Exception as e:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 Irc.broadcast ('Error while bridging from %s to %s: %s' %
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 (sourceName, dest, e))
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 pass

mercurial