Mercurial > xib
annotate bot.py @ 188:bd185885d4ca
minor fixes in bot.py, error messages that weren't flagged as debug
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Sat, 20 Feb 2010 19:03:12 +0100 |
parents | 594f570f4657 |
children | 6db99e458168 |
rev | line source |
---|---|
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
3 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
4 # This program is free software: you can redistribute it and/or modify |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
5 # it under the terms of the GNU General Public License as published by |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
6 # the Free Software Foundation, either version 3 of the License, or |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
7 # (at your option) any later version. |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
8 # |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
9 # This program is distributed in the hope that it will be useful, |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
12 # GNU General Public License for more details. |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
13 # |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
14 # You should have received a copy of the GNU General Public License |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
15 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
16 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
17 |
124 | 18 import re |
19 import sys | |
20 import threading | |
21 from time import sleep | |
22 import traceback | |
23 import xml.parsers.expat | |
24 | |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
25 import irclib |
75 | 26 import muc |
27 xmpp = muc.xmpp | |
28 del muc | |
124 | 29 |
30 from bridge import Bridge | |
31 from participant import Participant | |
134
931a5edc7971
moved commands to a separate file
Charly COSTE <changaco@changaco.net>
parents:
133
diff
changeset
|
32 import commands |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
33 import say_levels |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
34 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
35 |
124 | 36 class Bot(threading.Thread): |
37 | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
38 def __init__(self, jid, password, nickname, admins=[], error_fd=sys.stderr, debug=False): |
124 | 39 threading.Thread.__init__(self) |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
40 self.halt = False |
171
489c157d9e82
display error when bot creation fails
Charly COSTE <changaco@changaco.net>
parents:
166
diff
changeset
|
41 self.bridges = [] |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
42 self.bare_jid = xmpp.protocol.JID(jid=jid) |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
43 self.bare_jid.setResource('') |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
44 self.nickname = nickname |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
45 self.password = password |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
46 self.error_fd = error_fd |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
47 self.debug = debug |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
48 self.admins = admins |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
49 self.xmpp_connections = {} |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
50 self.irc = irclib.IRC() |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
51 self.irc.bot = self |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
52 self.irc.add_global_handler('all_events', self._irc_event_handler) |
124 | 53 self.irc_thread = threading.Thread(target=self.irc.process_forever) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
54 self.irc_thread.start() |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
55 # Open connection with XMPP server |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
56 try: |
22
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
57 self.xmpp_c = self.get_xmpp_connection(self.nickname) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
58 except: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
59 self.error(say_levels.error, 'XMPP Connection failed') |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
60 raise |
124 | 61 self.xmpp_thread = threading.Thread(target=self._xmpp_loop) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
62 self.xmpp_thread.start() |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
63 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
64 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
65 def error(self, importance, message, debug=False, send_to_admins=False): |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
66 """Output an error message.""" |
86
bfa32b017fc9
First hack at an error notification system
Charly COSTE <changaco@changaco.net>
parents:
79
diff
changeset
|
67 if send_to_admins == True: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
68 self._send_message_to_admins(importance, message) |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
69 if importance == -1: |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
70 return |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
71 if not debug: |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
72 self.error_fd.write(self.format_message(importance, message).encode('utf-8')+'\n') |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
73 if debug and self.debug: |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
74 self.error_fd.write('='*importance+'> '+message.encode('utf-8')+'\n') |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
75 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
76 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
77 def _xmpp_loop(self): |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
78 """[Internal] XMPP infinite loop.""" |
75 | 79 i = 1 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
80 while True: |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
81 if self.halt: |
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
82 s = len(self.xmpp_connections) |
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
83 for i in range(s): |
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
84 self.close_xmpp_connection(self.xmpp_connections.keys()[s-i-1], force=True) |
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
85 break |
79
73a30fc1922b
Fixed the XMPP loop (release the lock)
Charly COSTE <changaco@changaco.net>
parents:
78
diff
changeset
|
86 unlock = False |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
87 try: |
51
2737ed5b9003
Tried to improve the XMPP loop, again.
Charly COSTE <changaco@changaco.net>
parents:
50
diff
changeset
|
88 if len(self.xmpp_connections) == 1: |
2737ed5b9003
Tried to improve the XMPP loop, again.
Charly COSTE <changaco@changaco.net>
parents:
50
diff
changeset
|
89 sleep(0.5) # avoid bot connection being locked all the time |
101
29d3b85c6286
Minor fixes/improvements
Charly COSTE <changaco@changaco.net>
parents:
100
diff
changeset
|
90 for j, c in enumerate(self.xmpp_connections.itervalues()): |
75 | 91 i += 1 |
51
2737ed5b9003
Tried to improve the XMPP loop, again.
Charly COSTE <changaco@changaco.net>
parents:
50
diff
changeset
|
92 if hasattr(c, 'lock'): |
49
714e78fb912e
Tried to improve thread-safety during XMPP connection closing.
Charly COSTE <changaco@changaco.net>
parents:
48
diff
changeset
|
93 c.lock.acquire() |
75 | 94 if i == j: |
95 ping = xmpp.protocol.Iq(typ='get') | |
96 ping.addChild(name='ping', namespace='urn:xmpp:ping') | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
97 self.error(1, 'sending XMPP ping', debug=True) |
75 | 98 c.pings.append(c.send(ping)) |
52
5aabf124c78d
Fixed XMPP connection closing and the XMPP loop
Charly COSTE <changaco@changaco.net>
parents:
51
diff
changeset
|
99 if hasattr(c, 'Process'): |
5aabf124c78d
Fixed XMPP connection closing and the XMPP loop
Charly COSTE <changaco@changaco.net>
parents:
51
diff
changeset
|
100 c.Process(0.01) |
5aabf124c78d
Fixed XMPP connection closing and the XMPP loop
Charly COSTE <changaco@changaco.net>
parents:
51
diff
changeset
|
101 c.lock.release() |
75 | 102 if i > 5000: |
103 i = 0 | |
51
2737ed5b9003
Tried to improve the XMPP loop, again.
Charly COSTE <changaco@changaco.net>
parents:
50
diff
changeset
|
104 except RuntimeError: |
2737ed5b9003
Tried to improve the XMPP loop, again.
Charly COSTE <changaco@changaco.net>
parents:
50
diff
changeset
|
105 pass |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
23
diff
changeset
|
106 except (xml.parsers.expat.ExpatError, xmpp.protocol.XMLNotWellFormed): |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
107 self.error(1, 'invalid stanza', debug=True) |
101
29d3b85c6286
Minor fixes/improvements
Charly COSTE <changaco@changaco.net>
parents:
100
diff
changeset
|
108 self.reopen_xmpp_connection(c) |
79
73a30fc1922b
Fixed the XMPP loop (release the lock)
Charly COSTE <changaco@changaco.net>
parents:
78
diff
changeset
|
109 unlock = True |
91
69e4fc0f015c
Tried to fix XMPP «Replaced by new connection» problem
Charly COSTE <changaco@changaco.net>
parents:
90
diff
changeset
|
110 except xmpp.Conflict: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
111 self.error(1, 'conflict', debug=True) |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
112 self.reopen_xmpp_connection(c) |
91
69e4fc0f015c
Tried to fix XMPP «Replaced by new connection» problem
Charly COSTE <changaco@changaco.net>
parents:
90
diff
changeset
|
113 unlock = True |
51
2737ed5b9003
Tried to improve the XMPP loop, again.
Charly COSTE <changaco@changaco.net>
parents:
50
diff
changeset
|
114 except: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
115 error = 'Unknown exception on XMPP thread:\n'+traceback.format_exc() |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
116 self.error(say_levels.error, error, send_to_admins=True) |
79
73a30fc1922b
Fixed the XMPP loop (release the lock)
Charly COSTE <changaco@changaco.net>
parents:
78
diff
changeset
|
117 unlock = True |
73a30fc1922b
Fixed the XMPP loop (release the lock)
Charly COSTE <changaco@changaco.net>
parents:
78
diff
changeset
|
118 if unlock == True: |
73a30fc1922b
Fixed the XMPP loop (release the lock)
Charly COSTE <changaco@changaco.net>
parents:
78
diff
changeset
|
119 c.lock.release() |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
120 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
121 |
22
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
122 def _xmpp_presence_handler(self, dispatcher, presence): |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
123 """[Internal] Manage XMPP presence.""" |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
124 |
22
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
125 xmpp_c = dispatcher._owner |
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
126 |
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
127 if xmpp_c.nickname != self.nickname: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
128 self.error(1, 'Skipping XMPP presence not received on bot connection.', debug=True) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
129 return |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
130 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
131 self.error(2, 'Received XMPP presence.\n'+presence.__str__(fancy=1), debug=True) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
132 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
133 from_ = xmpp.protocol.JID(presence.getFrom()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
134 bare_jid = unicode(from_.getNode()+'@'+from_.getDomain()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
135 for bridge in self.bridges: |
103 | 136 if bare_jid == bridge.xmpp_room_jid: |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
137 # presence comes from a muc |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
138 resource = unicode(from_.getResource()) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
139 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
140 if resource == '': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
141 # presence comes from the muc itself |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
142 pass |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
143 |
108
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
144 elif resource == xmpp_c.nickname: |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
145 # presence comes from self |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
146 x = presence.getTag('x', namespace='http://jabber.org/protocol/muc#user') |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
147 if x: |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
148 d = x.getTag('destroy') |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
149 if d: |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
150 # room was destroyed |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
151 # problem is that this is used by some MUC servers when they shut down or restart |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
152 # considering this lack of semantic we have no choice but to do a check on the reason |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
153 reason = d.getTag('reason') |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
154 if reason: |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
155 r = reason.getData() |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
156 if r == 'The conference component is shutting down': |
121
765e460bc20a
Restart all bridges connected to a MUC server when it goes down
Charly COSTE <changaco@changaco.net>
parents:
119
diff
changeset
|
157 # MUC server is going down, try to restart the bridges in 1 minute |
765e460bc20a
Restart all bridges connected to a MUC server when it goes down
Charly COSTE <changaco@changaco.net>
parents:
119
diff
changeset
|
158 bridges = self.findBridges([from_.getDomain()]) |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
159 m = 'The MUC server '+from_.getDomain()+' seems to be going down, the bot will try to recreate all bridges related to this server in 1 minute' |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
160 error = (say_levels.warning, m) |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
161 self.restart_bridges_delayed(bridges, 60, error) |
108
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
162 return |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
163 elif r == '': |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
164 r = 'None given' |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
165 else: |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
166 r = 'None given' |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
167 |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
168 # room has been destroyed, stop the bridge |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
169 self.error(say_levels.error, 'The MUC room of the bridge '+str(bridge)+' has been destroyed with reason "'+r+'", stopping the bridge', send_to_admins=True) |
108
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
170 bridge.stop(message='The MUC room of the bridge has been destroyed with reason "'+r+'", stopping the bridge') |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
171 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
172 else: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
173 # presence comes from a participant of the muc |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
174 |
103 | 175 x = presence.getTag('x', namespace='http://jabber.org/protocol/muc#user') |
176 item = None | |
177 if x: | |
178 item = x.getTag('item') | |
179 | |
93
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
180 if presence.getType() == 'unavailable': |
96 | 181 try: |
182 p = bridge.getParticipant(resource) | |
124 | 183 except Bridge.NoSuchParticipantException: |
96 | 184 p = None |
185 | |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
186 if x and x.getTag('status', attrs={'code': '303'}): |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
187 # participant changed its nickname |
93
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
188 if p == None: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
189 return |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
190 if p.protocol != 'xmpp': |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
191 return |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
192 item = x.getTag('item') |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
193 if not item: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
194 self.error(1, 'bad stanza, no item element', debug=True) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
195 return |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
196 new_nick = item.getAttr('nick') |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
197 if not new_nick: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
198 self.error(1, 'bad stanza, new nick is not given', debug=True) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
199 return |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
200 p.changeNickname(new_nick, 'irc') |
93
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
201 |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
202 elif x and x.getTag('status', attrs={'code': '307'}): |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
203 # participant was kicked |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
204 if p == None: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
205 bridge.xmpp_room.rejoin() |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
206 return |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
207 if isinstance(p.xmpp_c, xmpp.client.Client): |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
208 p.muc.rejoin() |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
209 else: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
210 if item: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
211 reason = item.getTag('reason') |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
212 actor = item.getTag('actor') |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
213 if actor and actor.has_attr('jid'): |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
214 kicker = actor.getAttr('jid') |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
215 s1 = 'Kicked by '+kicker |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
216 else: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
217 s1 = 'Kicked from XMPP' |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
218 if reason: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
219 s2 = ' with reason: '+reason.getData() |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
220 else: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
221 s2 = ' (no reason was given)' |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
222 else: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
223 s1 = 'Kicked from XMPP' |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
224 s2 = ' (no reason was given)' |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
225 |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
226 bridge.removeParticipant('xmpp', p.nickname, s1+s2) |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
227 |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
228 elif x and x.getTag('status', attrs={'code': '301'}): |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
229 # participant was banned |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
230 if p == None: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
231 bridge.say(say_levels.error, 'bot got banned from XMPP', on_xmpp=False, send_to_admins=True) |
93
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
232 self.removeBridge(bridge) |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
233 return |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
234 if item: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
235 reason = item.getTag('reason') |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
236 actor = item.getTag('actor') |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
237 if actor and actor.has_attr('jid'): |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
238 kicker = actor.getAttr('jid') |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
239 s1 = 'Banned by '+kicker |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
240 else: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
241 s1 = 'Banned from XMPP' |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
242 if reason: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
243 s2 = ' with reason: '+reason.getData() |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
244 else: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
245 s2 = ' (no reason was given)' |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
246 else: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
247 s1 = 'Banned from XMPP' |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
248 s2 = ' (no reason was given)' |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
249 |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
250 bridge.removeParticipant('xmpp', p.nickname, s1+s2) |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
251 |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
252 else: |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
253 # participant left |
99
5390e9abfa44
Don't try to remove a participant that does not exist
Charly COSTE <changaco@changaco.net>
parents:
96
diff
changeset
|
254 if p != None: |
5390e9abfa44
Don't try to remove a participant that does not exist
Charly COSTE <changaco@changaco.net>
parents:
96
diff
changeset
|
255 bridge.removeParticipant('xmpp', resource, presence.getStatus()) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
256 |
111
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
257 elif presence.getType() == 'error': |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
258 error = presence.getTag('error') |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
259 if error: |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
260 for c in error.getChildren(): |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
261 if c.getNamespace() == 'urn:ietf:params:xml:ns:xmpp-stanzas' and c.getName() != 'text': |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
262 err = error.getAttr('type')+' '+c.getName() |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
263 if err == 'cancel remote-server-not-found': |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
264 # Remote server not found |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
265 # Stop bridges that depend on this server |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
266 bridges = self.findBridges([from_.getDomain()]) |
121
765e460bc20a
Restart all bridges connected to a MUC server when it goes down
Charly COSTE <changaco@changaco.net>
parents:
119
diff
changeset
|
267 error_message = '[Error] XMPP Remote server not found: '+from_.getDomain() |
765e460bc20a
Restart all bridges connected to a MUC server when it goes down
Charly COSTE <changaco@changaco.net>
parents:
119
diff
changeset
|
268 self.restart_bridges_delayed(bridges, 60, error_message) |
111
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
269 else: |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
270 raise Exception(presence.__str__(fancy=1).encode('utf-8')) |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
271 |
96 | 272 elif resource != bridge.bot.nickname: |
103 | 273 real_jid = None |
274 if item and item.has_attr('jid'): | |
275 real_jid = item.getAttr('jid') | |
276 | |
277 p = bridge.addParticipant('xmpp', resource, real_jid) | |
278 | |
279 # if we have the real jid check if the participant is a bot admin | |
124 | 280 if real_jid and isinstance(p, Participant): |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
281 for admin in self.admins: |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
282 if xmpp.protocol.JID(admin.jid).bareMatch(real_jid): |
103 | 283 p.bot_admin = True |
284 break | |
285 | |
96 | 286 return |
287 | |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
288 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
289 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
290 |
22
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
291 def _xmpp_iq_handler(self, dispatcher, iq): |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
292 """[Internal] Manage XMPP IQs.""" |
75 | 293 |
294 xmpp_c = dispatcher._owner | |
295 | |
296 # Ignore pongs | |
297 if iq.getType() in ['result', 'error'] and iq.getID() in xmpp_c.pings: | |
298 xmpp_c.pings.remove(iq.getID()) | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
299 self.error(1, 'received XMPP pong', debug=True) |
75 | 300 return |
301 | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
302 self.error(2, 'Received XMPP iq.\n'+iq.__str__(fancy=1), debug=True) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
303 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
304 |
22
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
305 def _xmpp_message_handler(self, dispatcher, message): |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
306 """[Internal] Manage XMPP messages.""" |
22
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
307 |
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
308 xmpp_c = dispatcher._owner |
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
309 |
34
57d0e66378b0
Fixed XMPP message handling and removed disconnect handler.
Charly COSTE <changaco@changaco.net>
parents:
33
diff
changeset
|
310 if message.getBody() == None: |
57d0e66378b0
Fixed XMPP message handling and removed disconnect handler.
Charly COSTE <changaco@changaco.net>
parents:
33
diff
changeset
|
311 return |
57d0e66378b0
Fixed XMPP message handling and removed disconnect handler.
Charly COSTE <changaco@changaco.net>
parents:
33
diff
changeset
|
312 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
313 if message.getType() == 'chat': |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
314 from_bare_jid = unicode(message.getFrom().getNode()+'@'+message.getFrom().getDomain()) |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
315 for bridge in self.bridges: |
103 | 316 if from_bare_jid == bridge.xmpp_room_jid: |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
317 # message comes from a room participant |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
318 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
319 self.error(2, 'Received XMPP chat message.\n'+message.__str__(fancy=1), debug=True) |
103 | 320 |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
321 try: |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
322 from_ = bridge.getParticipant(message.getFrom().getResource()) |
27
9b7a628ca612
Fixed XMPP message handling, again.
Charly COSTE <changaco@changaco.net>
parents:
25
diff
changeset
|
323 to_ = bridge.getParticipant(xmpp_c.nickname) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
324 |
34
57d0e66378b0
Fixed XMPP message handling and removed disconnect handler.
Charly COSTE <changaco@changaco.net>
parents:
33
diff
changeset
|
325 from_.sayOnIRCTo(to_.nickname, message.getBody()) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
326 |
124 | 327 except Bridge.NoSuchParticipantException: |
22
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
328 if xmpp_c.nickname == self.nickname: |
124 | 329 r = self.respond(str(message.getBody()), participant=from_) |
103 | 330 if isinstance(r, basestring) and len(r) > 0: |
331 s = xmpp.protocol.Message(to=message.getFrom(), body=r, typ='chat') | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
332 self.error(2, 'Sending\n'+s.__str__(fancy=1), debug=True) |
103 | 333 xmpp_c.send(s) |
334 else: | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
335 self.error(1, 'won\'t answer.', debug=True) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
336 return |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
337 self.error(1, 'XMPP chat message not relayed', debug=True) |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
338 return |
103 | 339 |
340 # message does not come from a room | |
341 if xmpp_c.nickname == self.nickname: | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
342 self.error(2, 'Received XMPP chat message.\n'+message.__str__(fancy=1), debug=True) |
103 | 343 |
344 # Find out if the message comes from a bot admin | |
345 bot_admin = False | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
346 for admin in self.admins: |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
347 if xmpp.protocol.JID(admin.jid).bareMatch(message.getFrom()): |
103 | 348 bot_admin = True |
349 break | |
350 | |
351 # Respond | |
352 r = self.respond(str(message.getBody()), bot_admin=bot_admin) | |
353 if isinstance(r, basestring) and len(r) > 0: | |
354 s = xmpp.protocol.Message(to=message.getFrom(), body=r, typ='chat') | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
355 self.error(2, 'Sending\n'+s.__str__(fancy=1), debug=True) |
103 | 356 xmpp_c.send(s) |
357 | |
358 else: | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
359 self.error(1, 'Ignoring XMPP chat message not received on bot connection.', debug=True) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
360 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
361 elif message.getType() == 'groupchat': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
362 # message comes from a room |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
363 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
364 for child in message.getChildren(): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
365 if child.getName() == 'delay': |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
366 # MUC delayed message |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
367 return |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
368 |
22
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
369 if xmpp_c.nickname != self.nickname: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
370 self.error(1, 'Ignoring XMPP MUC message not received on bot connection.', debug=True) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
371 return |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
372 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
373 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
374 from_ = xmpp.protocol.JID(message.getFrom()) |
23 | 375 |
376 if unicode(from_.getResource()) == self.nickname: | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
377 self.error(1, 'Ignoring XMPP MUC message sent by self.', debug=True) |
23 | 378 return |
379 | |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
380 room_jid = unicode(from_.getNode()+'@'+from_.getDomain()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
381 for bridge in self.bridges: |
103 | 382 if room_jid == bridge.xmpp_room_jid: |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
383 resource = unicode(from_.getResource()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
384 if resource == '': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
385 # message comes from the room itself |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
386 self.error(1, 'Ignoring XMPP groupchat message sent by the room.', debug=True) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
387 return |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
388 else: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
389 # message comes from a participant of the room |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
390 self.error(2, 'Received XMPP groupchat message.\n'+message.__str__(fancy=1), debug=True) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
391 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
392 try: |
124 | 393 participant = bridge.getParticipant(resource) |
394 except Bridge.NoSuchParticipantException: | |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
395 if resource != self.nickname: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
396 self.error(1, 'NoSuchParticipantException "'+resource+'" on "'+str(bridge)+'", WTF ?', debug=True) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
397 return |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
398 |
124 | 399 participant.sayOnIRC(message.getBody()) |
37 | 400 return |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
401 |
105
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
402 elif message.getType() == 'error': |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
403 for b in self.bridges: |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
404 if message.getFrom() == b.xmpp_room_jid: |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
405 # message comes from a room |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
406 for c in message.getChildren(): |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
407 if c.getName() == 'error': |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
408 for cc in c.getChildren(): |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
409 if cc.getNamespace() == 'urn:ietf:params:xml:ns:xmpp-stanzas' and cc.getName() != 'text': |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
410 err = cc.getName() |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
411 if err == 'not-acceptable': |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
412 # we sent a message to a room we are not in |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
413 # probable cause is a MUC server restart |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
414 # let's restart the bot |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
415 self.restart() |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
416 elif err == 'forbidden': |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
417 # we don't have the permission to speak |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
418 # let's remove the bridge and tell admins |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
419 self.error(say_levels.error, 'Not allowed to speak on the XMPP MUC of bridge '+str(b)+', stopping it', send_to_admins=True) |
112
a817ad05dd1d
stop bridge on error instead of removing it
Charly COSTE <changaco@changaco.net>
parents:
111
diff
changeset
|
420 b.stop(message='Not allowed to speak on the XMPP MUC, stopping bridge.') |
105
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
421 else: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
422 self.error(2, 'recevied unknown error message\n'+message.__str__(fancy=1), debug=True) |
105
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
423 return |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
424 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
425 self.error(2, 'recevied unknown error message\n'+message.__str__(fancy=1), debug=True) |
105
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
426 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
427 else: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
428 self.error(2, 'Received XMPP message of unknown type "'+str(message.getType())+'".\n'+message.__str__(fancy=1), debug=True) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
429 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
430 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
431 def _irc_event_handler(self, connection, event): |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
432 """[Internal] Manage IRC events""" |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
433 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
434 # Answer ping |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
435 if event.eventtype() == 'ping': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
436 connection.pong(connection.get_server_name()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
437 return |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
438 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
439 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
440 # Events we always want to ignore |
166
0fc24e232997
some fixes related to IRC nicknames
Charly COSTE <changaco@changaco.net>
parents:
156
diff
changeset
|
441 if 'all' in event.eventtype() or 'motd' in event.eventtype() or event.eventtype() in ['nicknameinuse', 'nickcollision', 'erroneusnickname']: |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
442 return |
184
1e5308a0db55
added 2 IRC events to ignore, 042 and pubnotice
Charly COSTE <changaco@changaco.net>
parents:
183
diff
changeset
|
443 if event.eventtype() in ['pong', 'privnotice', 'ctcp', 'nochanmodes', 'notexttosend', 'currenttopic', 'topicinfo', '328', 'pubnotice', '042']: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
444 self.error(1, 'ignoring IRC '+event.eventtype(), debug=True) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
445 return |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
446 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
447 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
448 nickname = None |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
23
diff
changeset
|
449 if event.source() != None: |
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
23
diff
changeset
|
450 if '!' in event.source(): |
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
23
diff
changeset
|
451 nickname = event.source().split('!')[0] |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
452 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
453 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
454 # Events that we want to ignore only in some cases |
14
1a1f2a0d35c7
Fixed bug that prevented the bot from connecting to freenode and all other servers that don't send "umode"
Charly COSTE <changaco@changaco.net>
parents:
11
diff
changeset
|
455 if event.eventtype() in ['umode', 'welcome', 'yourhost', 'created', 'myinfo', 'featurelist', 'luserclient', 'luserop', 'luserchannels', 'luserme', 'n_local', 'n_global', 'endofnames', 'luserunknown', 'luserconns']: |
1a1f2a0d35c7
Fixed bug that prevented the bot from connecting to freenode and all other servers that don't send "umode"
Charly COSTE <changaco@changaco.net>
parents:
11
diff
changeset
|
456 if connection.really_connected == False: |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
457 if event.target() == connection.nickname: |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
458 connection.really_connected = True |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
459 connection._call_nick_callbacks(None) |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
460 elif len(connection.nick_callbacks) > 0: |
188
bd185885d4ca
minor fixes in bot.py, error messages that weren't flagged as debug
Charly COSTE <changaco@changaco.net>
parents:
187
diff
changeset
|
461 self.error(3, 'event target ('+event.target()+') and connection nickname ('+connection.nickname+') don\'t match', debug=True) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
462 connection._call_nick_callbacks('nicknametoolong', arguments=[len(event.target())]) |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
463 self.error(1, 'ignoring '+event.eventtype(), debug=True) |
14
1a1f2a0d35c7
Fixed bug that prevented the bot from connecting to freenode and all other servers that don't send "umode"
Charly COSTE <changaco@changaco.net>
parents:
11
diff
changeset
|
464 return |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
465 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
466 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
467 # A string representation of the event |
179
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
468 event_str = 'connection='+connection.__str__()+'\neventtype='+event.eventtype()+'\nsource='+repr(event.source())+'\ntarget='+repr(event.target())+'\narguments='+repr(event.arguments()) |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
469 debug_str = 'Received IRC event.\n'+event_str |
179
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
470 printed_event = False |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
471 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
472 |
53
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
473 if event.eventtype() in ['pubmsg', 'action', 'privmsg', 'quit', 'part', 'nick', 'kick']: |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
474 if nickname == None: |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
475 return |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
476 |
39 | 477 handled = False |
478 | |
179
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
479 if event.eventtype() in ['quit', 'part'] and nickname == self.nickname: |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
480 return |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
481 |
53
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
482 if event.eventtype() in ['quit', 'part', 'nick', 'kick']: |
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
483 if connection.get_nickname() != self.nickname: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
484 self.error(1, 'ignoring IRC '+event.eventtype()+' not received on bot connection', debug=True) |
53
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
485 return |
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
486 else: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
487 self.error(2, debug_str, debug=True) |
179
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
488 printed_event = True |
53
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
489 |
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
490 if event.eventtype() == 'kick' and len(event.arguments()) < 1: |
187 | 491 self.error(1, 'at least 1 argument is needed for a '+event.eventtype()+' event', debug=True) |
53
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
492 return |
39 | 493 |
54
2507f424773a
Fixed a bug introduced in previous commit
Charly COSTE <changaco@changaco.net>
parents:
53
diff
changeset
|
494 if event.eventtype() in ['pubmsg', 'action']: |
55 | 495 if connection.get_nickname() != self.nickname: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
496 self.error(1, 'ignoring IRC '+event.eventtype()+' not received on bot connection', debug=True) |
54
2507f424773a
Fixed a bug introduced in previous commit
Charly COSTE <changaco@changaco.net>
parents:
53
diff
changeset
|
497 return |
2507f424773a
Fixed a bug introduced in previous commit
Charly COSTE <changaco@changaco.net>
parents:
53
diff
changeset
|
498 if nickname == self.nickname: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
499 self.error(1, 'ignoring IRC '+event.eventtype()+' sent by self', debug=True) |
54
2507f424773a
Fixed a bug introduced in previous commit
Charly COSTE <changaco@changaco.net>
parents:
53
diff
changeset
|
500 return |
50
dbb7c29a71e2
Ignore messages sent by self
Charly COSTE <changaco@changaco.net>
parents:
49
diff
changeset
|
501 |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
502 # TODO: lock self.bridges for thread safety |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
503 for bridge in self.bridges: |
32
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
504 if connection.server != bridge.irc_server: |
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
505 continue |
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
506 |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
507 try: |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
508 from_ = bridge.getParticipant(nickname) |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
509 |
124 | 510 except Bridge.NoSuchParticipantException: |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
511 continue |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
512 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
513 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
514 # Private message |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
515 if event.eventtype() == 'privmsg': |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
516 if event.target() == None: |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
517 return |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
518 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
519 try: |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
520 to_ = bridge.getParticipant(event.target().split('!')[0]) |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
521 self.error(2, debug_str, debug=True) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
522 from_.sayOnXMPPTo(to_.nickname, event.arguments()[0]) |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
523 return |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
524 |
124 | 525 except Bridge.NoSuchParticipantException: |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
526 if event.target().split('!')[0] == self.nickname: |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
527 # Message is for the bot |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
528 self.error(2, debug_str, debug=True) |
18
3cdf7bb580da
Fixed bot.respond(), boosted XMPP thread to lower latency, fixed mode notice.
Charly COSTE <changaco@changaco.net>
parents:
17
diff
changeset
|
529 connection.privmsg(from_.nickname, self.respond(event.arguments()[0])) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
530 return |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
531 else: |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
532 continue |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
533 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
534 |
92
aae8870b3727
Better handling of IRC kick
Charly COSTE <changaco@changaco.net>
parents:
91
diff
changeset
|
535 # kick handling |
53
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
536 if event.eventtype() == 'kick': |
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
537 if event.target().lower() == bridge.irc_room: |
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
538 try: |
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
539 kicked = bridge.getParticipant(event.arguments()[0]) |
72
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
70
diff
changeset
|
540 if isinstance(kicked.irc_connection, irclib.ServerConnection): |
53
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
541 kicked.irc_connection.join(bridge.irc_room) |
93
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
542 else: |
92
aae8870b3727
Better handling of IRC kick
Charly COSTE <changaco@changaco.net>
parents:
91
diff
changeset
|
543 if len(event.arguments()) > 1: |
aae8870b3727
Better handling of IRC kick
Charly COSTE <changaco@changaco.net>
parents:
91
diff
changeset
|
544 bridge.removeParticipant('irc', kicked.nickname, 'Kicked by '+nickname+' with reason: '+event.arguments()[1]) |
aae8870b3727
Better handling of IRC kick
Charly COSTE <changaco@changaco.net>
parents:
91
diff
changeset
|
545 else: |
aae8870b3727
Better handling of IRC kick
Charly COSTE <changaco@changaco.net>
parents:
91
diff
changeset
|
546 bridge.removeParticipant('irc', kicked.nickname, 'Kicked by '+nickname+' (no reason was given)') |
58
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
547 return |
124 | 548 except Bridge.NoSuchParticipantException: |
188
bd185885d4ca
minor fixes in bot.py, error messages that weren't flagged as debug
Charly COSTE <changaco@changaco.net>
parents:
187
diff
changeset
|
549 self.error(1, 'a participant that was not here has been kicked ? WTF ?', debug=True) |
53
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
550 return |
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
551 else: |
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
552 continue |
a2258a705a17
Handle kick (by simply rejoining) on IRC side
Charly COSTE <changaco@changaco.net>
parents:
52
diff
changeset
|
553 |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
554 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
555 # Leaving events |
45
41394ddb3aff
Lower before comparing strings.
Charly COSTE <changaco@changaco.net>
parents:
44
diff
changeset
|
556 if event.eventtype() == 'quit' or event.eventtype() == 'part' and event.target().lower() == bridge.irc_room: |
30
c0fb916cb0a0
Fixed IRC leaving events handling.
Charly COSTE <changaco@changaco.net>
parents:
29
diff
changeset
|
557 if len(event.arguments()) > 0: |
c0fb916cb0a0
Fixed IRC leaving events handling.
Charly COSTE <changaco@changaco.net>
parents:
29
diff
changeset
|
558 leave_message = event.arguments()[0] |
c0fb916cb0a0
Fixed IRC leaving events handling.
Charly COSTE <changaco@changaco.net>
parents:
29
diff
changeset
|
559 elif event.eventtype() == 'quit': |
c0fb916cb0a0
Fixed IRC leaving events handling.
Charly COSTE <changaco@changaco.net>
parents:
29
diff
changeset
|
560 leave_message = 'Left server.' |
c0fb916cb0a0
Fixed IRC leaving events handling.
Charly COSTE <changaco@changaco.net>
parents:
29
diff
changeset
|
561 elif event.eventtype() == 'part': |
c0fb916cb0a0
Fixed IRC leaving events handling.
Charly COSTE <changaco@changaco.net>
parents:
29
diff
changeset
|
562 leave_message = 'Left channel.' |
c0fb916cb0a0
Fixed IRC leaving events handling.
Charly COSTE <changaco@changaco.net>
parents:
29
diff
changeset
|
563 else: |
c0fb916cb0a0
Fixed IRC leaving events handling.
Charly COSTE <changaco@changaco.net>
parents:
29
diff
changeset
|
564 leave_message = '' |
c0fb916cb0a0
Fixed IRC leaving events handling.
Charly COSTE <changaco@changaco.net>
parents:
29
diff
changeset
|
565 bridge.removeParticipant('irc', from_.nickname, leave_message) |
39 | 566 handled = True |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
567 continue |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
568 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
569 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
570 # Nickname change |
41
4c4f4ee72e17
Fixed IRC "nick" event handling. Fixed bot's XMPP infinite loop.
Charly COSTE <changaco@changaco.net>
parents:
39
diff
changeset
|
571 if event.eventtype() == 'nick': |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
572 from_.changeNickname(event.target(), 'xmpp') |
39 | 573 handled = True |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
574 continue |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
575 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
576 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
577 # Chan message |
32
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
578 if event.eventtype() in ['pubmsg', 'action']: |
45
41394ddb3aff
Lower before comparing strings.
Charly COSTE <changaco@changaco.net>
parents:
44
diff
changeset
|
579 if bridge.irc_room == event.target().lower() and bridge.irc_server == connection.server: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
580 self.error(2, debug_str, debug=True) |
32
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
581 message = event.arguments()[0] |
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
582 if event.eventtype() == 'action': |
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
583 message = '/me '+message |
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
584 from_.sayOnXMPP(message) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
585 return |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
586 else: |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
587 continue |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
588 |
179
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
589 if handled: |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
590 return |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
591 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
592 |
58
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
593 # Handle bannedfromchan |
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
594 if event.eventtype() == 'bannedfromchan': |
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
595 if len(event.arguments()) < 1: |
188
bd185885d4ca
minor fixes in bot.py, error messages that weren't flagged as debug
Charly COSTE <changaco@changaco.net>
parents:
187
diff
changeset
|
596 self.error(1, 'length of arguments should be greater than 0 for a '+event.eventtype()+' event', debug=True) |
58
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
597 return |
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
598 |
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
599 for bridge in self.bridges: |
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
600 if connection.server != bridge.irc_server or event.arguments()[0].lower() != bridge.irc_room: |
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
601 continue |
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
602 |
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
603 if event.target() == self.nickname: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
604 self.error(say_levels.error, 'the nickname "'+event.target()+'" is banned from the IRC chan of bridge "'+str(bridge)+'"') |
58
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
605 raise Exception('[Error] the nickname "'+event.target()+'" is banned from the IRC chan of bridge "'+str(bridge)+'"') |
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
606 else: |
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
607 try: |
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
608 banned = bridge.getParticipant(event.target()) |
72
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
70
diff
changeset
|
609 if banned.irc_connection != 'bannedfromchan': |
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
70
diff
changeset
|
610 banned.irc_connection = 'bannedfromchan' |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
611 self.error(2, debug_str, debug=True) |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
612 bridge.say(say_levels.warning, 'the nickname "'+event.target()+'" is banned from the IRC chan', log=True) |
58
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
613 else: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
614 self.error(1, 'ignoring '+event.eventtype(), debug=True) |
124 | 615 except Bridge.NoSuchParticipantException: |
188
bd185885d4ca
minor fixes in bot.py, error messages that weren't flagged as debug
Charly COSTE <changaco@changaco.net>
parents:
187
diff
changeset
|
616 self.error(1, 'no such participant. WTF ?', debug=True) |
58
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
617 return |
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
618 |
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
619 return |
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
620 |
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
621 |
152
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
151
diff
changeset
|
622 if event.eventtype() in ['disconnect', 'kill', 'error']: |
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
151
diff
changeset
|
623 if len(event.arguments()) > 0 and event.arguments()[0] == 'Connection reset by peer': |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
624 self.error(2, debug_str, debug=True) |
152
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
151
diff
changeset
|
625 else: |
188
bd185885d4ca
minor fixes in bot.py, error messages that weren't flagged as debug
Charly COSTE <changaco@changaco.net>
parents:
187
diff
changeset
|
626 self.error(say_levels.debug, debug_str, send_to_admins=True) |
179
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
627 return |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
628 |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
629 |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
630 if event.eventtype() in ['cannotsendtochan', 'notonchannel']: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
631 self.error(2, debug_str, debug=True) |
179
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
632 bridges = self.getBridges(irc_room=event.arguments()[0], irc_server=connection.server) |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
633 if len(bridges) > 1: |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
634 raise Exception, 'more than one bridge for one irc chan, WTF ?' |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
635 bridge = bridges[0] |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
636 if connection.get_nickname() == self.nickname: |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
637 bridge._join_irc_failed() |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
638 else: |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
639 p = bridge.getParticipant(connection.get_nickname()) |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
640 p._close_irc_connection('') |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
641 p.irc_connection = error |
152
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
151
diff
changeset
|
642 return |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
643 |
58
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
644 |
187 | 645 # Ignore events not received on bot connection |
646 if connection.get_nickname() != self.nickname: | |
647 self.error(1, 'ignoring IRC '+event.eventtype()+' not received on bridge connection', debug=True) | |
648 return | |
649 | |
650 | |
651 # Joining events | |
652 if event.eventtype() in ['namreply', 'join']: | |
653 if event.eventtype() == 'namreply': | |
654 for bridge in self.getBridges(irc_room=event.arguments()[1].lower(), irc_server=connection.server): | |
655 for nickname in re.split('(?:^[&@\+%]?|(?: [&@\+%]?)*)', event.arguments()[2].strip()): | |
656 if nickname == '' or nickname == self.nickname: | |
657 continue | |
658 bridge.addParticipant('irc', nickname) | |
659 return | |
660 elif event.eventtype() == 'join': | |
661 bridges = self.getBridges(irc_room=event.target().lower(), irc_server=connection.server) | |
662 if len(bridges) == 0: | |
663 self.error(2, debug_str, debug=True) | |
664 self.error(3, 'no bridge found for "'+event.target().lower()+' at '+connection.server+'"', debug=True) | |
665 return | |
666 for bridge in bridges: | |
667 bridge.addParticipant('irc', nickname, irc_id=event.source()) | |
668 return | |
669 | |
670 | |
671 # Mode event | |
672 if event.eventtype() == 'mode': | |
673 if len(event.arguments()) < 2: | |
674 self.error(2, debug_str, debug=True) | |
675 self.error(1, '2 arguments are needed for a '+event.eventtype()+' event', debug=True) | |
676 return | |
677 if event.arguments()[1] != self.nickname or not 'o' in event.arguments()[0]: | |
678 self.error(1, 'ignoring IRC mode "'+event.arguments()[0]+'" for "'+event.arguments()[1]+'"', debug=True) | |
679 return | |
680 self.error(2, debug_str, debug=True) | |
681 bridges = self.getBridges(irc_room=event.target(), irc_server=connection.server) | |
682 if len(bridges) > 1: | |
683 raise Exception, 'more than one bridge for one irc chan, WTF ?' | |
684 bridge = bridges[0] | |
685 if re.search('\+[^\-]*o', event.arguments()[0]): | |
686 # bot is channel operator | |
687 bridge.irc_op = True | |
688 self.error(say_levels.notice, 'bot has IRC operator privileges in '+event.target()) | |
689 elif re.search('\-[^\+]*o', event.arguments()[0]): | |
690 # bot lost channel operator privileges | |
691 if bridge.irc_op: | |
692 self.error(say_levels.notice, 'bot lost IRC operator privileges in '+event.target(), send_to_admins=True) | |
693 bridge.irc_op = False | |
694 return | |
695 | |
696 | |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
697 # Unhandled events |
179
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
698 if not printed_event: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
699 self.error(say_levels.debug, 'The following IRC event was not handled:\n'+event_str+'\n', send_to_admins=True) |
179
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
700 else: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
701 self.error(1, 'event not handled', debug=True) |
181
803e00d72cb7
minor fix in bot.py, a line I forgot to change in the previous patch
Charly COSTE <changaco@changaco.net>
parents:
180
diff
changeset
|
702 self._send_message_to_admins(say_levels.debug, 'The following IRC event was not handled:\n'+event_str) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
703 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
704 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
705 def _send_message_to_admins(self, importance, message): |
86
bfa32b017fc9
First hack at an error notification system
Charly COSTE <changaco@changaco.net>
parents:
79
diff
changeset
|
706 """[Internal] Send XMPP Message to bot admin(s)""" |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
707 for admin in self.admins: |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
708 if importance != -1: |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
709 if admin.say_level == say_levels.nothing or importance < admin.say_level: |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
710 continue |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
711 message = self.format_message(importance, message) |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
712 |
86
bfa32b017fc9
First hack at an error notification system
Charly COSTE <changaco@changaco.net>
parents:
79
diff
changeset
|
713 try: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
714 self.xmpp_c.send(xmpp.protocol.Message(to=admin.jid, body=message, typ='chat')) |
86
bfa32b017fc9
First hack at an error notification system
Charly COSTE <changaco@changaco.net>
parents:
79
diff
changeset
|
715 except: |
bfa32b017fc9
First hack at an error notification system
Charly COSTE <changaco@changaco.net>
parents:
79
diff
changeset
|
716 pass |
bfa32b017fc9
First hack at an error notification system
Charly COSTE <changaco@changaco.net>
parents:
79
diff
changeset
|
717 |
bfa32b017fc9
First hack at an error notification system
Charly COSTE <changaco@changaco.net>
parents:
79
diff
changeset
|
718 |
172
64a0e9636ae6
removed encoding.py, charsets handling is now in irclib and custom charsets can be set per server in the configuration file
Charly COSTE <changaco@changaco.net>
parents:
171
diff
changeset
|
719 def new_bridge(self, xmpp_room, irc_room, irc_server, mode, say_level, irc_port=6667, irc_connection_interval=1, irc_charsets=None): |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
720 """Create a bridge between xmpp_room and irc_room at irc_server.""" |
172
64a0e9636ae6
removed encoding.py, charsets handling is now in irclib and custom charsets can be set per server in the configuration file
Charly COSTE <changaco@changaco.net>
parents:
171
diff
changeset
|
721 b = Bridge(self, xmpp_room, irc_room, irc_server, mode, say_level, irc_port=irc_port, irc_connection_interval=irc_connection_interval, irc_charsets=irc_charsets) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
722 self.bridges.append(b) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
723 return b |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
724 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
725 |
103 | 726 def findBridges(self, str_array): |
727 # TODO: lock self.bridges for thread safety | |
728 bridges = [b for b in self.bridges] | |
183
8f67684fee23
minor fix to Bot.findBridges(), memory usage improvement
Charly COSTE <changaco@changaco.net>
parents:
181
diff
changeset
|
729 for bridge in self.bridges: |
103 | 730 for s in str_array: |
731 if not s in str(bridge): | |
732 bridges.remove(bridge) | |
733 break | |
734 return bridges | |
735 | |
736 | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
737 def format_message(self, importance, message): |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
738 if importance < 0 or importance >= len(say_levels.levels): |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
739 raise Exception('[Internal Error] unknown message importance') |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
740 return'['+str(say_levels.get(importance))+'] '+message |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
741 |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
742 |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
743 def getBridges(self, irc_room=None, irc_server=None, xmpp_room_jid=None): |
96 | 744 # TODO: lock self.bridges for thread safety |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
745 bridges = [b for b in self.bridges] |
32
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
746 for bridge in [b for b in bridges]: |
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
747 if irc_room != None and bridge.irc_room != irc_room: |
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
748 bridges.remove(bridge) |
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
749 continue |
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
750 if irc_server != None and bridge.irc_server != irc_server: |
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
751 bridges.remove(bridge) |
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
752 continue |
103 | 753 if xmpp_room_jid != None and bridge.xmpp_room_jid != xmpp_room_jid: |
32
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
754 bridges.remove(bridge) |
8aa261545662
Implemented "/me" in the IRC->XMPP way (ie "action" event is now handled). Re-written bot.getBridges because the old one didn't always work and was slow.
Charly COSTE <changaco@changaco.net>
parents:
30
diff
changeset
|
755 continue |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
756 return bridges |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
757 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
758 |
22
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
759 def get_xmpp_connection(self, nickname): |
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
760 if self.xmpp_connections.has_key(nickname): |
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
761 c = self.xmpp_connections[nickname] |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
762 c.used_by += 1 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
763 self.error(3, 'using existing XMPP connection for "'+nickname+'", now used by '+str(c.used_by)+' bridges', debug=True) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
764 return c |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
765 self.error(3, 'opening new XMPP connection for "'+nickname+'"', debug=True) |
22
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
766 c = xmpp.client.Client(self.bare_jid.getDomain(), debug=[]) |
35
ebf516b2e5c9
Switched from Lock() to RLock().
Charly COSTE <changaco@changaco.net>
parents:
34
diff
changeset
|
767 c.lock = threading.RLock() |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
23
diff
changeset
|
768 c.lock.acquire() |
22
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
769 self.xmpp_connections[nickname] = c |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
770 c.used_by = 1 |
22
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
771 c.nickname = nickname |
90
965dd6f5fcc3
Tried to improve XMPP disconnections handling
Charly COSTE <changaco@changaco.net>
parents:
88
diff
changeset
|
772 c.mucs = [] |
75 | 773 c.pings = [] |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
774 c.connect() |
22
e2bd4de698e5
Solved an XMPP resource conflict that would have happened when someone on IRC changed its nickname and later its old nickname would be used again. In other words, the bot no longer uses nicknames as XMPP resources.
Charly COSTE <changaco@changaco.net>
parents:
21
diff
changeset
|
775 c.auth(self.bare_jid.getNode(), self.password) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
776 c.RegisterHandler('presence', self._xmpp_presence_handler) |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
777 c.RegisterHandler('iq', self._xmpp_iq_handler) |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
778 c.RegisterHandler('message', self._xmpp_message_handler) |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
779 c.sendInitPresence() |
103 | 780 if nickname == self.nickname: |
781 c.send(xmpp.protocol.Presence(priority=127)) | |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
23
diff
changeset
|
782 c.lock.release() |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
783 return c |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
784 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
785 |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
786 def reopen_xmpp_connection(self, c): |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
787 if not isinstance(c, xmpp.client.Client): |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
788 return |
103 | 789 bot_connection = False |
790 if c == self.xmpp_c: | |
791 bot_connection = True | |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
792 mucs = c.mucs |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
793 nickname = c.nickname |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
794 used_by = c.used_by |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
795 participants = [] |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
796 for b in self.bridges: |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
797 for p in b.participants: |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
798 if p.xmpp_c == c: |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
799 participants.append(p) |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
800 p.xmpp_c = None |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
801 self.error(3, 'reopening XMPP connection for "'+nickname+'"', debug=True) |
122 | 802 if self.xmpp_connections.has_key(nickname): |
803 self.xmpp_connections.pop(nickname) | |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
804 c.send(xmpp.protocol.Presence(typ='unavailable')) |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
805 del c |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
806 c = self.get_xmpp_connection(nickname) |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
807 c.used_by = used_by |
103 | 808 if bot_connection: |
809 self.xmpp_c = c | |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
810 for p in participants: |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
811 p.xmpp_c = c |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
812 c.mucs = mucs |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
813 for m in c.mucs: |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
814 m.rejoin() |
122 | 815 return c |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
816 |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
817 |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
818 def close_xmpp_connection(self, nickname, force=False): |
29 | 819 if not self.xmpp_connections.has_key(nickname): |
820 return | |
37 | 821 c = self.xmpp_connections[nickname] |
42
b8c9ddf320f8
Thread safety for bot.close_xmpp_connection().
Charly COSTE <changaco@changaco.net>
parents:
41
diff
changeset
|
822 c.lock.acquire() |
37 | 823 c.used_by -= 1 |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
824 if c.used_by < 1 or force: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
825 self.error(3, 'closing XMPP connection for "'+nickname+'"', debug=True) |
37 | 826 self.xmpp_connections.pop(nickname) |
52
5aabf124c78d
Fixed XMPP connection closing and the XMPP loop
Charly COSTE <changaco@changaco.net>
parents:
51
diff
changeset
|
827 c.send(xmpp.protocol.Presence(typ='unavailable')) |
49
714e78fb912e
Tried to improve thread-safety during XMPP connection closing.
Charly COSTE <changaco@changaco.net>
parents:
48
diff
changeset
|
828 c.lock.release() |
37 | 829 del c |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
830 else: |
42
b8c9ddf320f8
Thread safety for bot.close_xmpp_connection().
Charly COSTE <changaco@changaco.net>
parents:
41
diff
changeset
|
831 c.lock.release() |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
832 self.error(3, 'XMPP connection for "'+nickname+'" is now used by '+str(c.used_by)+' bridges', debug=True) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
833 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
834 |
105
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
835 def removeBridge(self, bridge, message='Removing bridge'): |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
836 self.bridges.remove(bridge) |
105
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
837 bridge.stop(message) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
838 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
839 |
124 | 840 def respond(self, message, participant=None, bot_admin=False): |
134
931a5edc7971
moved commands to a separate file
Charly COSTE <changaco@changaco.net>
parents:
133
diff
changeset
|
841 if isinstance(participant, Participant): |
931a5edc7971
moved commands to a separate file
Charly COSTE <changaco@changaco.net>
parents:
133
diff
changeset
|
842 bridge = participant.bridge |
931a5edc7971
moved commands to a separate file
Charly COSTE <changaco@changaco.net>
parents:
133
diff
changeset
|
843 if bot_admin != participant.bot_admin: |
931a5edc7971
moved commands to a separate file
Charly COSTE <changaco@changaco.net>
parents:
133
diff
changeset
|
844 bot_admin = participant.bot_admin |
931a5edc7971
moved commands to a separate file
Charly COSTE <changaco@changaco.net>
parents:
133
diff
changeset
|
845 else: |
931a5edc7971
moved commands to a separate file
Charly COSTE <changaco@changaco.net>
parents:
133
diff
changeset
|
846 bridge = None |
103 | 847 |
134
931a5edc7971
moved commands to a separate file
Charly COSTE <changaco@changaco.net>
parents:
133
diff
changeset
|
848 return commands.execute(self, message, bot_admin, bridge) |
103 | 849 |
850 | |
851 def restart(self): | |
852 # Stop the bridges | |
853 for b in self.bridges: | |
105
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
854 b.stop(message='Restarting bot') |
103 | 855 |
856 # Reopen the bot's XMPP connection | |
857 self.reopen_xmpp_connection(self.xmpp_c) | |
858 | |
859 # Restart the bridges | |
860 for b in self.bridges: | |
861 b.init2() | |
862 | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
863 self.error(-1, 'Bot restarted.', send_to_admins=True) |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
864 |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
865 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
866 def restart_bridges_delayed(self, bridges, delay, error, protocol='xmpp'): |
121
765e460bc20a
Restart all bridges connected to a MUC server when it goes down
Charly COSTE <changaco@changaco.net>
parents:
119
diff
changeset
|
867 if len(bridges) > 0: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
868 error[1] += '\nThese bridges will be stopped:' |
121
765e460bc20a
Restart all bridges connected to a MUC server when it goes down
Charly COSTE <changaco@changaco.net>
parents:
119
diff
changeset
|
869 for b in bridges: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
870 error[1] += '\n'+str(b) |
177
d1686164b9e3
improved Bot.restart_bridges_delayed(), it can now be used when the IRC server is the problem
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
871 |
d1686164b9e3
improved Bot.restart_bridges_delayed(), it can now be used when the IRC server is the problem
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
872 if protocol == 'xmpp': |
d1686164b9e3
improved Bot.restart_bridges_delayed(), it can now be used when the IRC server is the problem
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
873 leave_message = 'Could not connect to the MUC server ('+b.xmpp_room_jid+')' |
121
765e460bc20a
Restart all bridges connected to a MUC server when it goes down
Charly COSTE <changaco@changaco.net>
parents:
119
diff
changeset
|
874 else: |
177
d1686164b9e3
improved Bot.restart_bridges_delayed(), it can now be used when the IRC server is the problem
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
875 leave_message = 'Could not connect to the IRC server ('+b.irc_connection._server_str()+')' |
d1686164b9e3
improved Bot.restart_bridges_delayed(), it can now be used when the IRC server is the problem
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
876 |
d1686164b9e3
improved Bot.restart_bridges_delayed(), it can now be used when the IRC server is the problem
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
877 if not hasattr(b, 'reconnecting'): |
d1686164b9e3
improved Bot.restart_bridges_delayed(), it can now be used when the IRC server is the problem
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
878 leave_message += 'will try to recreate the bridge in '+str(delay)+' seconds' |
d1686164b9e3
improved Bot.restart_bridges_delayed(), it can now be used when the IRC server is the problem
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
879 b.reconnecting = True |
121
765e460bc20a
Restart all bridges connected to a MUC server when it goes down
Charly COSTE <changaco@changaco.net>
parents:
119
diff
changeset
|
880 self.irc.execute_delayed(delay, b.init2) |
177
d1686164b9e3
improved Bot.restart_bridges_delayed(), it can now be used when the IRC server is the problem
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
881 |
121
765e460bc20a
Restart all bridges connected to a MUC server when it goes down
Charly COSTE <changaco@changaco.net>
parents:
119
diff
changeset
|
882 b.stop(message=leave_message) |
765e460bc20a
Restart all bridges connected to a MUC server when it goes down
Charly COSTE <changaco@changaco.net>
parents:
119
diff
changeset
|
883 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
884 self.error(error[0], error[1], send_to_admins=True) |
121
765e460bc20a
Restart all bridges connected to a MUC server when it goes down
Charly COSTE <changaco@changaco.net>
parents:
119
diff
changeset
|
885 |
765e460bc20a
Restart all bridges connected to a MUC server when it goes down
Charly COSTE <changaco@changaco.net>
parents:
119
diff
changeset
|
886 |
129
b13ca9cefe1c
tried to fix the halt command (added Bot.stop())
Charly COSTE <changaco@changaco.net>
parents:
127
diff
changeset
|
887 def stop(self, message='Stopping bot'): |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
14
diff
changeset
|
888 for bridge in self.bridges: |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
889 bridge.stop(message=message) |
129
b13ca9cefe1c
tried to fix the halt command (added Bot.stop())
Charly COSTE <changaco@changaco.net>
parents:
127
diff
changeset
|
890 |
b13ca9cefe1c
tried to fix the halt command (added Bot.stop())
Charly COSTE <changaco@changaco.net>
parents:
127
diff
changeset
|
891 |
b13ca9cefe1c
tried to fix the halt command (added Bot.stop())
Charly COSTE <changaco@changaco.net>
parents:
127
diff
changeset
|
892 def __del__(self): |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
893 for bridge in self.bridges: |
156
3f299ad4d452
oops, fix for the halt command
Charly COSTE <changaco@changaco.net>
parents:
155
diff
changeset
|
894 self.removeBridge(bridge, message='Stopping bot') |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
895 self.halt = True |