Mercurial > xib
annotate bot.py @ 247:303e571bc104
catch ServerNotConnectedError in Participant.say_on_irc_to
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Mon, 08 Mar 2010 18:47:07 +0100 |
parents | 5e65f52be453 |
children | 5d6ff929e6bc |
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 |
201
de574314990e
log some more things when Bot.debug is False
Charly COSTE <changaco@changaco.net>
parents:
200
diff
changeset
|
65 def error(self, importance, message, debug=False, no_debug_add='', 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.""" |
246
5e65f52be453
make sure Bot.error doesn't raise a transcoding exception and uses repr() when there is a problem
Charly COSTE <changaco@changaco.net>
parents:
243
diff
changeset
|
67 try: |
5e65f52be453
make sure Bot.error doesn't raise a transcoding exception and uses repr() when there is a problem
Charly COSTE <changaco@changaco.net>
parents:
243
diff
changeset
|
68 message = message.encode('utf-8') |
5e65f52be453
make sure Bot.error doesn't raise a transcoding exception and uses repr() when there is a problem
Charly COSTE <changaco@changaco.net>
parents:
243
diff
changeset
|
69 except: |
5e65f52be453
make sure Bot.error doesn't raise a transcoding exception and uses repr() when there is a problem
Charly COSTE <changaco@changaco.net>
parents:
243
diff
changeset
|
70 message = repr(message) |
201
de574314990e
log some more things when Bot.debug is False
Charly COSTE <changaco@changaco.net>
parents:
200
diff
changeset
|
71 if not self.debug: |
de574314990e
log some more things when Bot.debug is False
Charly COSTE <changaco@changaco.net>
parents:
200
diff
changeset
|
72 message += no_debug_add |
86
bfa32b017fc9
First hack at an error notification system
Charly COSTE <changaco@changaco.net>
parents:
79
diff
changeset
|
73 if send_to_admins == True: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
74 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
|
75 if importance == -1: |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
76 return |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
77 if not debug: |
246
5e65f52be453
make sure Bot.error doesn't raise a transcoding exception and uses repr() when there is a problem
Charly COSTE <changaco@changaco.net>
parents:
243
diff
changeset
|
78 self.error_fd.write(self.format_message(importance, message)+'\n') |
201
de574314990e
log some more things when Bot.debug is False
Charly COSTE <changaco@changaco.net>
parents:
200
diff
changeset
|
79 elif self.debug: |
246
5e65f52be453
make sure Bot.error doesn't raise a transcoding exception and uses repr() when there is a problem
Charly COSTE <changaco@changaco.net>
parents:
243
diff
changeset
|
80 self.error_fd.write('='*importance+'> '+message+'\n') |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
81 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
82 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
83 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
|
84 """[Internal] XMPP infinite loop.""" |
75 | 85 i = 1 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
86 while True: |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
87 if self.halt: |
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
88 s = len(self.xmpp_connections) |
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
89 for i in range(s): |
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
90 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
|
91 break |
79
73a30fc1922b
Fixed the XMPP loop (release the lock)
Charly COSTE <changaco@changaco.net>
parents:
78
diff
changeset
|
92 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
|
93 try: |
51
2737ed5b9003
Tried to improve the XMPP loop, again.
Charly COSTE <changaco@changaco.net>
parents:
50
diff
changeset
|
94 if len(self.xmpp_connections) == 1: |
2737ed5b9003
Tried to improve the XMPP loop, again.
Charly COSTE <changaco@changaco.net>
parents:
50
diff
changeset
|
95 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
|
96 for j, c in enumerate(self.xmpp_connections.itervalues()): |
75 | 97 i += 1 |
51
2737ed5b9003
Tried to improve the XMPP loop, again.
Charly COSTE <changaco@changaco.net>
parents:
50
diff
changeset
|
98 if hasattr(c, 'lock'): |
49
714e78fb912e
Tried to improve thread-safety during XMPP connection closing.
Charly COSTE <changaco@changaco.net>
parents:
48
diff
changeset
|
99 c.lock.acquire() |
75 | 100 if i == j: |
101 ping = xmpp.protocol.Iq(typ='get') | |
102 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
|
103 self.error(1, 'sending XMPP ping', debug=True) |
75 | 104 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
|
105 if hasattr(c, 'Process'): |
5aabf124c78d
Fixed XMPP connection closing and the XMPP loop
Charly COSTE <changaco@changaco.net>
parents:
51
diff
changeset
|
106 c.Process(0.01) |
5aabf124c78d
Fixed XMPP connection closing and the XMPP loop
Charly COSTE <changaco@changaco.net>
parents:
51
diff
changeset
|
107 c.lock.release() |
75 | 108 if i > 5000: |
109 i = 0 | |
51
2737ed5b9003
Tried to improve the XMPP loop, again.
Charly COSTE <changaco@changaco.net>
parents:
50
diff
changeset
|
110 except RuntimeError: |
2737ed5b9003
Tried to improve the XMPP loop, again.
Charly COSTE <changaco@changaco.net>
parents:
50
diff
changeset
|
111 pass |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
23
diff
changeset
|
112 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
|
113 self.error(1, 'invalid stanza', debug=True) |
101
29d3b85c6286
Minor fixes/improvements
Charly COSTE <changaco@changaco.net>
parents:
100
diff
changeset
|
114 self.reopen_xmpp_connection(c) |
79
73a30fc1922b
Fixed the XMPP loop (release the lock)
Charly COSTE <changaco@changaco.net>
parents:
78
diff
changeset
|
115 unlock = True |
91
69e4fc0f015c
Tried to fix XMPP «Replaced by new connection» problem
Charly COSTE <changaco@changaco.net>
parents:
90
diff
changeset
|
116 except xmpp.Conflict: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
117 self.error(1, 'conflict', debug=True) |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
118 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
|
119 unlock = True |
51
2737ed5b9003
Tried to improve the XMPP loop, again.
Charly COSTE <changaco@changaco.net>
parents:
50
diff
changeset
|
120 except: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
121 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
|
122 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
|
123 unlock = True |
73a30fc1922b
Fixed the XMPP loop (release the lock)
Charly COSTE <changaco@changaco.net>
parents:
78
diff
changeset
|
124 if unlock == True: |
73a30fc1922b
Fixed the XMPP loop (release the lock)
Charly COSTE <changaco@changaco.net>
parents:
78
diff
changeset
|
125 c.lock.release() |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
126 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
127 |
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
|
128 def _xmpp_presence_handler(self, dispatcher, presence): |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
129 """[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
|
130 |
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
|
131 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
|
132 |
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
|
133 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
|
134 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
|
135 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
|
136 |
202
2a1ee46f86af
don't log presence from self
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
137 from_ = xmpp.protocol.JID(presence.getFrom()) |
2a1ee46f86af
don't log presence from self
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
138 bare_jid = unicode(from_.getNode()+'@'+from_.getDomain()) |
2a1ee46f86af
don't log presence from self
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
139 |
2a1ee46f86af
don't log presence from self
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
140 if bare_jid == self.bare_jid: |
2a1ee46f86af
don't log presence from self
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
141 self.error(1, 'Ignoring XMPP presence from self', debug=True) |
2a1ee46f86af
don't log presence from self
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
142 return |
2a1ee46f86af
don't log presence from self
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
143 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
144 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
|
145 |
201
de574314990e
log some more things when Bot.debug is False
Charly COSTE <changaco@changaco.net>
parents:
200
diff
changeset
|
146 no_debug_add = '\n'+presence.__str__(fancy=1) |
de574314990e
log some more things when Bot.debug is False
Charly COSTE <changaco@changaco.net>
parents:
200
diff
changeset
|
147 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
148 for bridge in self.bridges: |
103 | 149 if bare_jid == bridge.xmpp_room_jid: |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
150 # presence comes from a muc |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
151 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
|
152 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
153 if resource == '': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
154 # presence comes from the muc itself |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
155 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
|
156 |
108
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
157 elif resource == xmpp_c.nickname: |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
158 # presence comes from self |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
159 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
|
160 if x: |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
161 d = x.getTag('destroy') |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
162 if d: |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
163 # room was destroyed |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
164 # 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
|
165 # 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
|
166 reason = d.getTag('reason') |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
167 if reason: |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
168 r = reason.getData() |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
169 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
|
170 # MUC server is going down, try to restart the bridges in 1 minute |
216
d527d7b75f56
merged Bot.find_bridges and Bot.get_bridges into Bot.iter_bridges, btw we don't need to lock Bot.bridges because lists don't raise RuntimeError
Charly COSTE <changaco@changaco.net>
parents:
213
diff
changeset
|
171 bridges = self.iter_bridges(patterns=[from_.getDomain()]) |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
172 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' |
190
6db99e458168
fixed calls to Bot.restart_bridges_delayed(), arguments changed in f52cb2f6e273328e73d75ab4ecf214c8dc9dfe1b
Charly COSTE <changaco@changaco.net>
parents:
188
diff
changeset
|
173 error = [say_levels.warning, m] |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
174 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
|
175 return |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
176 elif r == '': |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
177 r = 'None given' |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
178 else: |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
179 r = 'None given' |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
180 |
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
181 # 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
|
182 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) |
193
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
190
diff
changeset
|
183 bridge.stop(message='The MUC room has been destroyed with reason "'+r+'", stopping the bridge') |
108
dff9746aa508
XMPP room deletion (and server shut down) handling
Charly COSTE <changaco@changaco.net>
parents:
105
diff
changeset
|
184 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
185 else: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
186 # 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
|
187 |
103 | 188 x = presence.getTag('x', namespace='http://jabber.org/protocol/muc#user') |
189 item = None | |
190 if x: | |
191 item = x.getTag('item') | |
192 | |
93
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
193 if presence.getType() == 'unavailable': |
96 | 194 try: |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
202
diff
changeset
|
195 p = bridge.get_participant(resource) |
124 | 196 except Bridge.NoSuchParticipantException: |
96 | 197 p = None |
198 | |
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 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
|
200 # participant changed its nickname |
93
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
201 if p == None: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
202 return |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
203 if p.protocol != 'xmpp': |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
204 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
|
205 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
|
206 if not item: |
201
de574314990e
log some more things when Bot.debug is False
Charly COSTE <changaco@changaco.net>
parents:
200
diff
changeset
|
207 self.error(say_levels.debug, 'bad stanza, no item element', no_debug_add=no_debug_add) |
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
|
208 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
|
209 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
|
210 if not new_nick: |
201
de574314990e
log some more things when Bot.debug is False
Charly COSTE <changaco@changaco.net>
parents:
200
diff
changeset
|
211 self.error(say_levels.debug, 'bad stanza, new nick is not given', no_debug_add=no_debug_add) |
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
|
212 return |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
202
diff
changeset
|
213 p.change_nickname(new_nick, 'irc') |
93
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
214 |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
215 elif x and x.getTag('status', attrs={'code': '307'}): |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
216 # participant was kicked |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
217 if p == None: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
218 bridge.xmpp_room.rejoin() |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
219 return |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
220 if isinstance(p.xmpp_c, xmpp.client.Client): |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
221 p.muc.rejoin() |
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 if item: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
224 reason = item.getTag('reason') |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
225 actor = item.getTag('actor') |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
226 if actor and actor.has_attr('jid'): |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
227 kicker = actor.getAttr('jid') |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
228 s1 = 'Kicked by '+kicker |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
229 else: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
230 s1 = 'Kicked from XMPP' |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
231 if reason: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
232 s2 = ' with reason: '+reason.getData() |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
233 else: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
234 s2 = ' (no reason was given)' |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
235 else: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
236 s1 = 'Kicked from XMPP' |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
237 s2 = ' (no reason was given)' |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
238 |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
202
diff
changeset
|
239 bridge.remove_participant('xmpp', p.nickname, s1+s2) |
93
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
240 |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
241 elif x and x.getTag('status', attrs={'code': '301'}): |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
242 # participant was banned |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
243 if p == None: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
244 bridge.say(say_levels.error, 'bot got banned from XMPP', on_xmpp=False, send_to_admins=True) |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
202
diff
changeset
|
245 self.remove_bridge(bridge) |
93
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
246 return |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
247 if item: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
248 reason = item.getTag('reason') |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
249 actor = item.getTag('actor') |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
250 if actor and actor.has_attr('jid'): |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
251 kicker = actor.getAttr('jid') |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
252 s1 = 'Banned by '+kicker |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
253 else: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
254 s1 = 'Banned from XMPP' |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
255 if reason: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
256 s2 = ' with reason: '+reason.getData() |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
257 else: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
258 s2 = ' (no reason was given)' |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
259 else: |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
260 s1 = 'Banned from XMPP' |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
261 s2 = ' (no reason was given)' |
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
262 |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
202
diff
changeset
|
263 bridge.remove_participant('xmpp', p.nickname, s1+s2) |
93
c95fe0b319d9
XMPP kick and ban handling
Charly COSTE <changaco@changaco.net>
parents:
92
diff
changeset
|
264 |
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
|
265 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
|
266 # participant left |
99
5390e9abfa44
Don't try to remove a participant that does not exist
Charly COSTE <changaco@changaco.net>
parents:
96
diff
changeset
|
267 if p != None: |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
202
diff
changeset
|
268 bridge.remove_participant('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
|
269 |
111
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
270 elif presence.getType() == 'error': |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
271 error = presence.getTag('error') |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
272 if error: |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
273 for c in error.getChildren(): |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
274 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
|
275 err = error.getAttr('type')+' '+c.getName() |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
276 if err == 'cancel remote-server-not-found': |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
277 # Remote server not found |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
278 # Stop bridges that depend on this server |
216
d527d7b75f56
merged Bot.find_bridges and Bot.get_bridges into Bot.iter_bridges, btw we don't need to lock Bot.bridges because lists don't raise RuntimeError
Charly COSTE <changaco@changaco.net>
parents:
213
diff
changeset
|
279 bridges = self.iter_bridges(patterns=[from_.getDomain()]) |
190
6db99e458168
fixed calls to Bot.restart_bridges_delayed(), arguments changed in f52cb2f6e273328e73d75ab4ecf214c8dc9dfe1b
Charly COSTE <changaco@changaco.net>
parents:
188
diff
changeset
|
280 error = [say_levels.error, 'XMPP Remote server not found: '+from_.getDomain()] |
6db99e458168
fixed calls to Bot.restart_bridges_delayed(), arguments changed in f52cb2f6e273328e73d75ab4ecf214c8dc9dfe1b
Charly COSTE <changaco@changaco.net>
parents:
188
diff
changeset
|
281 self.restart_bridges_delayed(bridges, 60, error) |
111
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
282 else: |
59401ac0f47a
handle XMPP remote-server-not-found
Charly COSTE <changaco@changaco.net>
parents:
110
diff
changeset
|
283 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
|
284 |
96 | 285 elif resource != bridge.bot.nickname: |
103 | 286 real_jid = None |
287 if item and item.has_attr('jid'): | |
288 real_jid = item.getAttr('jid') | |
289 | |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
290 p = bridge.add_participant('xmpp', resource, real_jid=real_jid) |
103 | 291 |
292 # if we have the real jid check if the participant is a bot admin | |
124 | 293 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
|
294 for admin in self.admins: |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
295 if xmpp.protocol.JID(admin.jid).bareMatch(real_jid): |
103 | 296 p.bot_admin = True |
297 break | |
298 | |
96 | 299 return |
300 | |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
301 return |
200
740effa74c18
bugfix for previous commit, forgot "self" argument for say_on_XMPP_through_bridge
Charly COSTE <changaco@changaco.net>
parents:
199
diff
changeset
|
302 |
201
de574314990e
log some more things when Bot.debug is False
Charly COSTE <changaco@changaco.net>
parents:
200
diff
changeset
|
303 self.error(say_levels.debug, 'Unhandled XMPP presence', no_debug_add='\n'+presence.__str__(fancy=1)) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
304 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
305 |
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
|
306 def _xmpp_iq_handler(self, dispatcher, iq): |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
307 """[Internal] Manage XMPP IQs.""" |
75 | 308 |
309 xmpp_c = dispatcher._owner | |
310 | |
311 # Ignore pongs | |
312 if iq.getType() in ['result', 'error'] and iq.getID() in xmpp_c.pings: | |
313 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
|
314 self.error(1, 'received XMPP pong', debug=True) |
75 | 315 return |
316 | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
317 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
|
318 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
319 |
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
|
320 def _xmpp_message_handler(self, dispatcher, message): |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
321 """[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
|
322 |
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
|
323 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
|
324 |
34
57d0e66378b0
Fixed XMPP message handling and removed disconnect handler.
Charly COSTE <changaco@changaco.net>
parents:
33
diff
changeset
|
325 if message.getBody() == None: |
57d0e66378b0
Fixed XMPP message handling and removed disconnect handler.
Charly COSTE <changaco@changaco.net>
parents:
33
diff
changeset
|
326 return |
57d0e66378b0
Fixed XMPP message handling and removed disconnect handler.
Charly COSTE <changaco@changaco.net>
parents:
33
diff
changeset
|
327 |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
328 |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
329 # Private message |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
330 if message.getType() == 'chat': |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
331 from_bare_jid = unicode(message.getFrom().getNode()+'@'+message.getFrom().getDomain()) |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
332 |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
333 try: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
334 bridge = self.get_bridge(xmpp_room_jid=from_bare_jid) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
335 except KeyError: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
336 bridge = None |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
337 |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
338 if bridge: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
339 # message comes from a room participant |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
340 self.error(2, 'Received XMPP chat message.\n'+message.__str__(fancy=1), debug=True) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
341 |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
342 try: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
343 from_ = bridge.get_participant(message.getFrom().getResource()) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
344 to_ = bridge.get_participant(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
|
345 |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
346 from_.say_on_irc_to(to_.nickname, message.getBody()) |
103 | 347 |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
348 except Bridge.NoSuchParticipantException: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
349 if xmpp_c.nickname == self.nickname: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
350 r = self.respond(str(message.getBody()), participant=from_) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
351 if isinstance(r, basestring) and len(r) > 0: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
352 s = xmpp.protocol.Message(to=message.getFrom(), body=r, typ='chat') |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
353 self.error(2, 'Sending\n'+s.__str__(fancy=1), debug=True) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
354 xmpp_c.send(s) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
355 else: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
356 self.error(1, 'won\'t answer.', debug=True) |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
357 return |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
358 self.error(say_levels.debug, 'XMPP chat message not relayed', no_debug_add='\n'+message.__str__(fancy=1)) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
359 return |
103 | 360 |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
361 else: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
362 # message does not come from a room participant |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
363 if xmpp_c.nickname != self.nickname: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
364 self.error(1, 'Ignoring XMPP chat message not received on bot connection.', debug=True) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
365 return |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
366 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
367 self.error(2, 'Received XMPP chat message.\n'+message.__str__(fancy=1), debug=True) |
103 | 368 |
369 # Find out if the message comes from a bot admin | |
370 bot_admin = False | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
371 for admin in self.admins: |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
372 if xmpp.protocol.JID(admin.jid).bareMatch(message.getFrom()): |
103 | 373 bot_admin = True |
374 break | |
375 | |
376 # Respond | |
377 r = self.respond(str(message.getBody()), bot_admin=bot_admin) | |
378 if isinstance(r, basestring) and len(r) > 0: | |
379 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
|
380 self.error(2, 'Sending\n'+s.__str__(fancy=1), debug=True) |
103 | 381 xmpp_c.send(s) |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
382 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
383 |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
384 # MUC message |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
385 elif message.getType() == 'groupchat': |
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
|
386 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
387 for child in message.getChildren(): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
388 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
|
389 # MUC delayed message |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
390 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
|
391 |
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
|
392 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
|
393 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
|
394 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
|
395 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
396 from_ = xmpp.protocol.JID(message.getFrom()) |
23 | 397 |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
398 resource = unicode(from_.getResource()) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
399 |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
400 if resource == self.nickname: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
401 self.error(1, 'Ignoring XMPP MUC message sent by self.', debug=True) |
23 | 402 return |
403 | |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
404 room_jid = unicode(from_.getNode()+'@'+from_.getDomain()) |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
405 bridge = self.get_bridge(xmpp_room_jid=room_jid) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
406 |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
407 if resource == '': |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
408 # message comes from the room itself |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
409 self.error(1, 'Ignoring XMPP groupchat message sent by the room.', debug=True) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
410 return |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
411 else: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
412 # message comes from a participant of the room |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
413 self.error(2, 'Received XMPP groupchat message.\n'+message.__str__(fancy=1), debug=True) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
414 |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
415 try: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
416 participant = bridge.get_participant(resource) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
417 participant.say_on_irc(message.getBody()) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
418 except Bridge.NoSuchParticipantException: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
419 bridge.say_on_behalf(resource, message.getBody(), 'irc', action=(message.getBody()[:4] == '/me ')) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
420 |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
421 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
|
422 |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
423 |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
424 # Error message |
105
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
425 elif message.getType() == 'error': |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
426 try: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
427 b = self.get_bridge(xmpp_room_jid=message.getFrom()) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
428 except KeyError: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
429 self.error(say_levels.debug, 'received unknown error message\n'+message.__str__(fancy=1)) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
430 return |
105
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
431 |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
432 for c in message.getChildren(): |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
433 if c.getName() == 'error': |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
434 for cc in c.getChildren(): |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
435 if cc.getNamespace() == 'urn:ietf:params:xml:ns:xmpp-stanzas' and cc.getName() != 'text': |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
436 err = cc.getName() |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
437 if err in ['not-acceptable', 'not-allowed']: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
438 # we sent a message to a room we are not in |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
439 # can be due to a MUC server restart |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
440 # can be a concurrency bug |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
441 if xmpp_c.nickname == self.nickname: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
442 b.restart(message='Automatic restart of bridge') |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
443 else: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
444 try: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
445 p = b.get_participant(xmpp_c.nickname) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
446 p.say_on_XMPP_through_bridge(message.getBody()) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
447 except Bridge.NoSuchParticipantException: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
448 b.restart(message='Automatic restart of bridge') |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
449 |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
450 elif err == 'forbidden': |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
451 # we don't have the permission to speak |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
452 # let's remove the bridge and tell admins |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
453 self.error(say_levels.error, 'Not allowed to speak on the XMPP MUC of bridge '+str(b)+', stopping it', send_to_admins=True) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
454 b.stop(message='Not allowed to speak on the XMPP MUC, stopping the bridge') |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
455 else: |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
456 self.error(say_levels.debug, 'recevied unknown error message\n'+message.__str__(fancy=1)) |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
457 |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
458 return |
105
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
459 |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
460 |
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
461 # Unknown message type |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
462 else: |
200
740effa74c18
bugfix for previous commit, forgot "self" argument for say_on_XMPP_through_bridge
Charly COSTE <changaco@changaco.net>
parents:
199
diff
changeset
|
463 self.error(say_levels.debug, 'Received XMPP message of unknown type "'+str(message.getType())+'".\n'+message.__str__(fancy=1)) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
464 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
465 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
466 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
|
467 """[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
|
468 |
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
|
469 # Answer ping |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
470 if event.eventtype() == 'ping': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
471 connection.pong(connection.get_server_name()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
472 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
|
473 |
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 |
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 # Events we always want to ignore |
166
0fc24e232997
some fixes related to IRC nicknames
Charly COSTE <changaco@changaco.net>
parents:
156
diff
changeset
|
476 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
|
477 return |
243
c9652650d5b5
ignore "noorigin" IRC event
Charly COSTE <changaco@changaco.net>
parents:
242
diff
changeset
|
478 if event.eventtype() in ['pong', 'privnotice', 'ctcp', 'nochanmodes', 'notexttosend', 'currenttopic', 'topicinfo', '328', 'pubnotice', '042', 'umode', 'welcome', 'yourhost', 'created', 'myinfo', 'featurelist', 'luserclient', 'luserop', 'luserchannels', 'luserme', 'n_local', 'n_global', 'endofnames', 'luserunknown', 'luserconns', 'inviteonlychan', 'bannedfromchan', 'channelisfull', 'badchannelkey', 'topic', 'noorigin']: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
479 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
|
480 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
|
481 |
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
|
482 |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
483 source_nickname = None |
220
5726a0833537
minor syntax change in bot.py
Charly COSTE <changaco@changaco.net>
parents:
219
diff
changeset
|
484 if event.source() and '!' in event.source(): |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
485 source_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
|
486 |
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
|
487 |
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
|
488 # A string representation of the event |
201
de574314990e
log some more things when Bot.debug is False
Charly COSTE <changaco@changaco.net>
parents:
200
diff
changeset
|
489 event_str = '\nconnection='+connection.__str__()+'\neventtype='+event.eventtype()+'\nsource='+repr(event.source())+'\ntarget='+repr(event.target())+'\narguments='+repr(event.arguments()) |
de574314990e
log some more things when Bot.debug is False
Charly COSTE <changaco@changaco.net>
parents:
200
diff
changeset
|
490 debug_str = 'Received IRC event.'+event_str |
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
|
491 |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
492 handled = 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
|
493 |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
494 # Private message |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
495 if event.eventtype() in ['privmsg', 'action']: |
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
|
496 |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
497 if event.target() == self.nickname: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
498 # message is for the bot |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
499 connection.privmsg(source_nickname, self.respond(event.arguments()[0])) |
179
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
500 return |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
501 |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
502 elif not irclib.is_channel(event.target()[0]): |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
503 # search if the IRC user who sent the message is in one of the bridges |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
504 for bridge in self.iter_bridges(irc_server=connection.server): |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
505 try: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
506 from_ = bridge.get_participant(source_nickname) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
507 # he is, forward the message on XMPP |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
508 if event.eventtype() == 'action': |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
509 action = True |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
510 else: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
511 action = False |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
512 from_.say_on_xmpp_to(connection.nickname, event.arguments()[0], action=action) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
513 return |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
514 except Bridge.NoSuchParticipantException: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
515 continue |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
516 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
517 # he isn't, send an error |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
518 connection.privmsg(source_nickname, 'XIB error: you cannot send a private message to an XMPP user if you are not in one of the chans he is in') |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
519 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
520 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
521 # Connection errors |
152
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
151
diff
changeset
|
522 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
|
523 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
|
524 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
|
525 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
|
526 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
|
527 return |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
528 |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
177
diff
changeset
|
529 |
225
da8fbaf69242
fixed 'cannotsendtochan' and 'notonchannel' handling, ignore 'inviteonlychan', 'bannedfromchan', 'channelisfull' and 'badchannelkey' (handled via join callbacks)
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
530 # Chan errors |
da8fbaf69242
fixed 'cannotsendtochan' and 'notonchannel' handling, ignore 'inviteonlychan', 'bannedfromchan', 'channelisfull' and 'badchannelkey' (handled via join callbacks)
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
531 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
|
532 self.error(2, debug_str, debug=True) |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
533 |
225
da8fbaf69242
fixed 'cannotsendtochan' and 'notonchannel' handling, ignore 'inviteonlychan', 'bannedfromchan', 'channelisfull' and 'badchannelkey' (handled via join callbacks)
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
534 bridge = self.get_bridge(irc_room=event.arguments()[0], irc_server=connection.server) |
da8fbaf69242
fixed 'cannotsendtochan' and 'notonchannel' handling, ignore 'inviteonlychan', 'bannedfromchan', 'channelisfull' and 'badchannelkey' (handled via join callbacks)
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
535 |
da8fbaf69242
fixed 'cannotsendtochan' and 'notonchannel' handling, ignore 'inviteonlychan', 'bannedfromchan', 'channelisfull' and 'badchannelkey' (handled via join callbacks)
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
536 if event.eventtype() == 'cannotsendtochan': |
236
8acbfda313b9
use ServerConnection.nickname instead of .real_nickname in most cases, make sure we put a string in .real_nickname
Charly COSTE <changaco@changaco.net>
parents:
235
diff
changeset
|
537 if connection.nickname == self.nickname: |
225
da8fbaf69242
fixed 'cannotsendtochan' and 'notonchannel' handling, ignore 'inviteonlychan', 'bannedfromchan', 'channelisfull' and 'badchannelkey' (handled via join callbacks)
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
538 bridge._join_irc_failed(event.eventtype()) |
da8fbaf69242
fixed 'cannotsendtochan' and 'notonchannel' handling, ignore 'inviteonlychan', 'bannedfromchan', 'channelisfull' and 'badchannelkey' (handled via join callbacks)
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
539 else: |
236
8acbfda313b9
use ServerConnection.nickname instead of .real_nickname in most cases, make sure we put a string in .real_nickname
Charly COSTE <changaco@changaco.net>
parents:
235
diff
changeset
|
540 p = bridge.get_participant(connection.nickname) |
225
da8fbaf69242
fixed 'cannotsendtochan' and 'notonchannel' handling, ignore 'inviteonlychan', 'bannedfromchan', 'channelisfull' and 'badchannelkey' (handled via join callbacks)
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
541 p._close_irc_connection(event.eventtype()) |
da8fbaf69242
fixed 'cannotsendtochan' and 'notonchannel' handling, ignore 'inviteonlychan', 'bannedfromchan', 'channelisfull' and 'badchannelkey' (handled via join callbacks)
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
542 p.irc_connection = event.eventtype() |
da8fbaf69242
fixed 'cannotsendtochan' and 'notonchannel' handling, ignore 'inviteonlychan', 'bannedfromchan', 'channelisfull' and 'badchannelkey' (handled via join callbacks)
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
543 |
da8fbaf69242
fixed 'cannotsendtochan' and 'notonchannel' handling, ignore 'inviteonlychan', 'bannedfromchan', 'channelisfull' and 'badchannelkey' (handled via join callbacks)
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
544 elif event.eventtype() == 'notonchannel': |
236
8acbfda313b9
use ServerConnection.nickname instead of .real_nickname in most cases, make sure we put a string in .real_nickname
Charly COSTE <changaco@changaco.net>
parents:
235
diff
changeset
|
545 if connection.nickname == self.nickname: |
225
da8fbaf69242
fixed 'cannotsendtochan' and 'notonchannel' handling, ignore 'inviteonlychan', 'bannedfromchan', 'channelisfull' and 'badchannelkey' (handled via join callbacks)
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
546 bridge.restart(message='Restarting bridge because we received the IRC event '+event.eventtype()) |
da8fbaf69242
fixed 'cannotsendtochan' and 'notonchannel' handling, ignore 'inviteonlychan', 'bannedfromchan', 'channelisfull' and 'badchannelkey' (handled via join callbacks)
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
547 else: |
236
8acbfda313b9
use ServerConnection.nickname instead of .real_nickname in most cases, make sure we put a string in .real_nickname
Charly COSTE <changaco@changaco.net>
parents:
235
diff
changeset
|
548 p = bridge.get_participant(connection.nickname) |
225
da8fbaf69242
fixed 'cannotsendtochan' and 'notonchannel' handling, ignore 'inviteonlychan', 'bannedfromchan', 'channelisfull' and 'badchannelkey' (handled via join callbacks)
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
549 p.irc_connection.join(bridge.irc_room) |
da8fbaf69242
fixed 'cannotsendtochan' and 'notonchannel' handling, ignore 'inviteonlychan', 'bannedfromchan', 'channelisfull' and 'badchannelkey' (handled via join callbacks)
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
550 |
152
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
151
diff
changeset
|
551 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
|
552 |
58
92341dbfb8b7
Handle bannedfromchan IRC event
Charly COSTE <changaco@changaco.net>
parents:
56
diff
changeset
|
553 |
187 | 554 # Ignore events not received on bot connection |
236
8acbfda313b9
use ServerConnection.nickname instead of .real_nickname in most cases, make sure we put a string in .real_nickname
Charly COSTE <changaco@changaco.net>
parents:
235
diff
changeset
|
555 if connection.nickname != self.nickname: |
187 | 556 self.error(1, 'ignoring IRC '+event.eventtype()+' not received on bridge connection', debug=True) |
557 return | |
558 | |
559 | |
242
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
560 # Server events |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
561 if event.eventtype() in ['quit', 'nick']: |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
562 for bridge in self.iter_bridges(irc_server=connection.server): |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
563 |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
564 try: |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
565 from_ = bridge.get_participant(source_nickname) |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
566 except Bridge.NoSuchParticipantException: |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
567 continue |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
568 |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
569 handled = True |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
570 |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
571 # Quit event |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
572 if event.eventtype() == 'quit': |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
573 if len(event.arguments()) > 0: |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
574 leave_message = event.arguments()[0] |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
575 else: |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
576 leave_message = 'Left server.' |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
577 bridge.remove_participant('irc', from_.nickname, leave_message) |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
578 continue |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
579 |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
580 # Nickname change |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
581 if event.eventtype() == 'nick': |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
582 from_.change_nickname(event.target(), 'xmpp') |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
583 continue |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
584 |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
585 if handled: |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
586 return |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
587 |
60645c505f11
ignore IRC server-wide events not received on bot connection
Charly COSTE <changaco@changaco.net>
parents:
241
diff
changeset
|
588 |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
589 # Chan events |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
590 if event.eventtype() in ['pubmsg', 'action', 'part', 'kick', 'mode', 'join']: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
591 |
238
a6c37733357a
some more fixes for Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
236
diff
changeset
|
592 if event.eventtype() in ['pubmsg', 'action', 'part', 'kick', 'join'] and not source_nickname: |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
593 self.error(say_levels.debug, 'a source is needed for a '+event.eventtype()+' event', no_debug_add=event_str) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
594 return |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
595 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
596 if event.eventtype() == 'kick' and len(event.arguments()) == 0: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
597 self.error(say_levels.debug, 'at least 1 argument is needed for a '+event.eventtype()+' event', no_debug_add=event_str) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
598 return |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
599 |
238
a6c37733357a
some more fixes for Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
236
diff
changeset
|
600 if event.eventtype() == 'mode' and len(event.arguments()) < 2: |
a6c37733357a
some more fixes for Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
236
diff
changeset
|
601 self.error(say_levels.debug, '2 arguments are needed for a '+event.eventtype()+' event'+event_str) |
a6c37733357a
some more fixes for Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
236
diff
changeset
|
602 return |
a6c37733357a
some more fixes for Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
236
diff
changeset
|
603 |
a6c37733357a
some more fixes for Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
236
diff
changeset
|
604 chan = event.target().lower() |
a6c37733357a
some more fixes for Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
236
diff
changeset
|
605 |
a6c37733357a
some more fixes for Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
236
diff
changeset
|
606 bridge = self.get_bridge(irc_room=chan, irc_server=connection.server) |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
607 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
608 try: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
609 from_ = bridge.get_participant(source_nickname) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
610 except Bridge.NoSuchParticipantException: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
611 from_ = None |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
612 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
613 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
614 # Join event |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
615 if event.eventtype() == 'join': |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
616 bridge.add_participant('irc', source_nickname) |
187 | 617 return |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
618 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
619 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
620 # kick handling |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
621 if event.eventtype() == 'kick': |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
622 try: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
623 kicked = bridge.get_participant(event.arguments()[0]) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
624 except Bridge.NoSuchParticipantException: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
625 self.error(2, debug_str, debug=True) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
626 self.error(say_levels.debug, 'a participant that was not here has been kicked ? WTF ?', no_debug_add=event_str) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
627 return |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
628 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
629 leave_message = 'kicked by '+nickname |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
630 if len(event.arguments()) > 1: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
631 leave_message += ' with reason: '+event.arguments()[1] |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
632 else: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
633 leave_message += ' (no reason was given)' |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
634 log_message = '"'+kicked.nickname+'" has been '+leave_message |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
635 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
636 self.error(say_levels.warning, log_message) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
637 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
638 if isinstance(kicked.irc_connection, irclib.ServerConnection): |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
639 # an IRC duplicate of an XMPP user has been kicked, auto-rejoin |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
640 kicked.irc_connection.join(bridge.irc_room) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
641 elif isinstance(kicked.xmpp_c, xmpp.client.Client): |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
642 # an IRC user has been kicked, make its duplicate leave |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
643 kicked.leave(m) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
644 else: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
645 # an IRC user with no duplicate on XMPP has been kicked, say it on XMPP |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
646 bridge.say(say_levels.warning, log_message, on_irc=False) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
647 return |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
648 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
649 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
650 # Part event |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
651 if event.eventtype() == 'part': |
235 | 652 if not from_: |
238
a6c37733357a
some more fixes for Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
236
diff
changeset
|
653 self.error(say_levels.debug, 'a participant that wasn\'t here left:'+event_str) |
235 | 654 return |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
655 if len(event.arguments()) > 0: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
656 leave_message = event.arguments()[0] |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
657 else: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
658 leave_message = 'Left channel.' |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
659 bridge.remove_participant('irc', from_.nickname, leave_message) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
660 return |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
661 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
662 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
663 # Chan message |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
664 if event.eventtype() in ['pubmsg', 'action']: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
665 message = event.arguments()[0] |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
666 if event.eventtype() == 'action': |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
667 action = True |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
668 else: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
669 action = False |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
670 if isinstance(from_, Participant): |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
671 from_.say_on_xmpp(message, action=action) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
672 else: |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
673 bridge.say_on_behalf(source_nickname, message, 'xmpp', action=action) |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
674 return |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
675 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
676 |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
677 # Mode event |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
678 if event.eventtype() == 'mode': |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
679 if event.arguments()[1] != self.nickname or not 'o' in event.arguments()[0]: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
680 self.error(1, 'ignoring IRC mode "'+event.arguments()[0]+'" for "'+event.arguments()[1]+'"', debug=True) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
681 return |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
682 if re.search('\+[^\-]*o', event.arguments()[0]): |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
683 # bot is channel operator |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
684 bridge.irc_op = True |
238
a6c37733357a
some more fixes for Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
236
diff
changeset
|
685 self.error(say_levels.notice, 'bot has IRC operator privileges in '+chan) |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
686 elif re.search('\-[^\+]*o', event.arguments()[0]): |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
687 # bot lost channel operator privileges |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
688 if bridge.irc_op: |
238
a6c37733357a
some more fixes for Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
236
diff
changeset
|
689 self.error(say_levels.notice, 'bot lost IRC operator privileges in '+chan, send_to_admins=True) |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
690 bridge.irc_op = False |
187 | 691 return |
692 | |
693 | |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
694 # Namreply event |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
695 if event.eventtype() == 'namreply': |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
696 bridge = self.get_bridge(irc_room=event.arguments()[1].lower(), irc_server=connection.server) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
697 for nickname in re.split('(?:^[&@\+%]?|(?: [&@\+%]?)*)', event.arguments()[2].strip()): |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
698 if nickname == '' or nickname == self.nickname: |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
699 continue |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
700 bridge.add_participant('irc', nickname) |
187 | 701 return |
702 | |
703 | |
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
|
704 # Unhandled events |
230
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
705 self.error(1, 'event not handled', debug=True) |
d5a126cf119c
reorganized and fixed many things in Bot._irc_event_handler
Charly COSTE <changaco@changaco.net>
parents:
228
diff
changeset
|
706 self._send_message_to_admins(say_levels.debug, 'The following IRC event was not handled:'+event_str) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
707 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
708 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
709 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
|
710 """[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
|
711 for admin in self.admins: |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
712 if importance != -1: |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
713 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
|
714 continue |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
715 message = self.format_message(importance, message) |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
716 |
86
bfa32b017fc9
First hack at an error notification system
Charly COSTE <changaco@changaco.net>
parents:
79
diff
changeset
|
717 try: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
718 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
|
719 except: |
bfa32b017fc9
First hack at an error notification system
Charly COSTE <changaco@changaco.net>
parents:
79
diff
changeset
|
720 pass |
bfa32b017fc9
First hack at an error notification system
Charly COSTE <changaco@changaco.net>
parents:
79
diff
changeset
|
721 |
bfa32b017fc9
First hack at an error notification system
Charly COSTE <changaco@changaco.net>
parents:
79
diff
changeset
|
722 |
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
|
723 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
|
724 """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
|
725 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
|
726 self.bridges.append(b) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
727 return b |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
728 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
729 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
730 def format_message(self, importance, message): |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
731 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
|
732 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
|
733 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
|
734 |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
735 |
211
3fe9fed07b64
created Bot.get_bridge(), it calls Bot.get_bridges() and raises exceptions when there are 0 or more than 1 matches
Charly COSTE <changaco@changaco.net>
parents:
207
diff
changeset
|
736 def get_bridge(self, **kwargs): |
216
d527d7b75f56
merged Bot.find_bridges and Bot.get_bridges into Bot.iter_bridges, btw we don't need to lock Bot.bridges because lists don't raise RuntimeError
Charly COSTE <changaco@changaco.net>
parents:
213
diff
changeset
|
737 """Calls self.iter_bridges and raises exceptions when there are 0 or more than 1 matches |
211
3fe9fed07b64
created Bot.get_bridge(), it calls Bot.get_bridges() and raises exceptions when there are 0 or more than 1 matches
Charly COSTE <changaco@changaco.net>
parents:
207
diff
changeset
|
738 |
216
d527d7b75f56
merged Bot.find_bridges and Bot.get_bridges into Bot.iter_bridges, btw we don't need to lock Bot.bridges because lists don't raise RuntimeError
Charly COSTE <changaco@changaco.net>
parents:
213
diff
changeset
|
739 See Bot.iter_bridges for the list of args""" |
211
3fe9fed07b64
created Bot.get_bridge(), it calls Bot.get_bridges() and raises exceptions when there are 0 or more than 1 matches
Charly COSTE <changaco@changaco.net>
parents:
207
diff
changeset
|
740 |
216
d527d7b75f56
merged Bot.find_bridges and Bot.get_bridges into Bot.iter_bridges, btw we don't need to lock Bot.bridges because lists don't raise RuntimeError
Charly COSTE <changaco@changaco.net>
parents:
213
diff
changeset
|
741 bridges = [b for b in self.iter_bridges(**kwargs)] |
211
3fe9fed07b64
created Bot.get_bridge(), it calls Bot.get_bridges() and raises exceptions when there are 0 or more than 1 matches
Charly COSTE <changaco@changaco.net>
parents:
207
diff
changeset
|
742 if len(bridges) == 0: |
232
02c58515edde
improved Bot._xmpp_message_handler
Charly COSTE <changaco@changaco.net>
parents:
230
diff
changeset
|
743 raise KeyError, 'no bridge matching '+str(kwargs) |
211
3fe9fed07b64
created Bot.get_bridge(), it calls Bot.get_bridges() and raises exceptions when there are 0 or more than 1 matches
Charly COSTE <changaco@changaco.net>
parents:
207
diff
changeset
|
744 elif len(bridges) > 1: |
3fe9fed07b64
created Bot.get_bridge(), it calls Bot.get_bridges() and raises exceptions when there are 0 or more than 1 matches
Charly COSTE <changaco@changaco.net>
parents:
207
diff
changeset
|
745 raise Exception, 'more than one bridge matching '+str(kwargs)+'\n'+'\n'.join([str(b) for b in bridges]) |
3fe9fed07b64
created Bot.get_bridge(), it calls Bot.get_bridges() and raises exceptions when there are 0 or more than 1 matches
Charly COSTE <changaco@changaco.net>
parents:
207
diff
changeset
|
746 return bridges[0] |
3fe9fed07b64
created Bot.get_bridge(), it calls Bot.get_bridges() and raises exceptions when there are 0 or more than 1 matches
Charly COSTE <changaco@changaco.net>
parents:
207
diff
changeset
|
747 |
3fe9fed07b64
created Bot.get_bridge(), it calls Bot.get_bridges() and raises exceptions when there are 0 or more than 1 matches
Charly COSTE <changaco@changaco.net>
parents:
207
diff
changeset
|
748 |
216
d527d7b75f56
merged Bot.find_bridges and Bot.get_bridges into Bot.iter_bridges, btw we don't need to lock Bot.bridges because lists don't raise RuntimeError
Charly COSTE <changaco@changaco.net>
parents:
213
diff
changeset
|
749 def iter_bridges(self, irc_room=None, irc_server=None, xmpp_room_jid=None, patterns=None): |
d527d7b75f56
merged Bot.find_bridges and Bot.get_bridges into Bot.iter_bridges, btw we don't need to lock Bot.bridges because lists don't raise RuntimeError
Charly COSTE <changaco@changaco.net>
parents:
213
diff
changeset
|
750 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
|
751 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
|
752 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
|
753 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
|
754 continue |
103 | 755 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
|
756 continue |
216
d527d7b75f56
merged Bot.find_bridges and Bot.get_bridges into Bot.iter_bridges, btw we don't need to lock Bot.bridges because lists don't raise RuntimeError
Charly COSTE <changaco@changaco.net>
parents:
213
diff
changeset
|
757 if patterns != None: |
d527d7b75f56
merged Bot.find_bridges and Bot.get_bridges into Bot.iter_bridges, btw we don't need to lock Bot.bridges because lists don't raise RuntimeError
Charly COSTE <changaco@changaco.net>
parents:
213
diff
changeset
|
758 for pattern in patterns: |
d527d7b75f56
merged Bot.find_bridges and Bot.get_bridges into Bot.iter_bridges, btw we don't need to lock Bot.bridges because lists don't raise RuntimeError
Charly COSTE <changaco@changaco.net>
parents:
213
diff
changeset
|
759 if not pattern in str(bridge): |
d527d7b75f56
merged Bot.find_bridges and Bot.get_bridges into Bot.iter_bridges, btw we don't need to lock Bot.bridges because lists don't raise RuntimeError
Charly COSTE <changaco@changaco.net>
parents:
213
diff
changeset
|
760 continue |
d527d7b75f56
merged Bot.find_bridges and Bot.get_bridges into Bot.iter_bridges, btw we don't need to lock Bot.bridges because lists don't raise RuntimeError
Charly COSTE <changaco@changaco.net>
parents:
213
diff
changeset
|
761 yield bridge |
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 |
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
|
763 |
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
|
764 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
|
765 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
|
766 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
|
767 c.used_by += 1 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
768 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
|
769 return c |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
770 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
|
771 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
|
772 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
|
773 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
|
774 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
|
775 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
|
776 c.nickname = nickname |
90
965dd6f5fcc3
Tried to improve XMPP disconnections handling
Charly COSTE <changaco@changaco.net>
parents:
88
diff
changeset
|
777 c.mucs = [] |
75 | 778 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
|
779 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
|
780 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
|
781 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
|
782 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
|
783 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
|
784 c.sendInitPresence() |
103 | 785 if nickname == self.nickname: |
786 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
|
787 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
|
788 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
|
789 |
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
|
790 |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
791 def reopen_xmpp_connection(self, c): |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
792 if not isinstance(c, xmpp.client.Client): |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
793 return |
103 | 794 bot_connection = False |
795 if c == self.xmpp_c: | |
796 bot_connection = True | |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
797 mucs = c.mucs |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
798 nickname = c.nickname |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
799 used_by = c.used_by |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
800 participants = [] |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
801 for b in self.bridges: |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
802 for p in b.participants: |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
803 if p.xmpp_c == c: |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
804 participants.append(p) |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
805 p.xmpp_c = None |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
806 self.error(3, 'reopening XMPP connection for "'+nickname+'"', debug=True) |
122 | 807 if self.xmpp_connections.has_key(nickname): |
808 self.xmpp_connections.pop(nickname) | |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
809 c.send(xmpp.protocol.Presence(typ='unavailable')) |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
810 del c |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
811 c = self.get_xmpp_connection(nickname) |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
812 c.used_by = used_by |
103 | 813 if bot_connection: |
814 self.xmpp_c = c | |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
815 for p in participants: |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
816 p.xmpp_c = c |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
817 c.mucs = mucs |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
818 for m in c.mucs: |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
819 m.rejoin() |
122 | 820 return c |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
821 |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
99
diff
changeset
|
822 |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
823 def close_xmpp_connection(self, nickname, force=False): |
29 | 824 if not self.xmpp_connections.has_key(nickname): |
825 return | |
37 | 826 c = self.xmpp_connections[nickname] |
42
b8c9ddf320f8
Thread safety for bot.close_xmpp_connection().
Charly COSTE <changaco@changaco.net>
parents:
41
diff
changeset
|
827 c.lock.acquire() |
37 | 828 c.used_by -= 1 |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
829 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
|
830 self.error(3, 'closing XMPP connection for "'+nickname+'"', debug=True) |
37 | 831 self.xmpp_connections.pop(nickname) |
52
5aabf124c78d
Fixed XMPP connection closing and the XMPP loop
Charly COSTE <changaco@changaco.net>
parents:
51
diff
changeset
|
832 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
|
833 c.lock.release() |
37 | 834 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
|
835 else: |
42
b8c9ddf320f8
Thread safety for bot.close_xmpp_connection().
Charly COSTE <changaco@changaco.net>
parents:
41
diff
changeset
|
836 c.lock.release() |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
837 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
|
838 |
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
|
839 |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
202
diff
changeset
|
840 def remove_bridge(self, bridge, message='Removing bridge', log=True): |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
841 self.bridges.remove(bridge) |
193
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
190
diff
changeset
|
842 bridge.stop(message=message, log=log) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
843 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
844 |
124 | 845 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
|
846 if isinstance(participant, Participant): |
931a5edc7971
moved commands to a separate file
Charly COSTE <changaco@changaco.net>
parents:
133
diff
changeset
|
847 bridge = participant.bridge |
931a5edc7971
moved commands to a separate file
Charly COSTE <changaco@changaco.net>
parents:
133
diff
changeset
|
848 if bot_admin != participant.bot_admin: |
931a5edc7971
moved commands to a separate file
Charly COSTE <changaco@changaco.net>
parents:
133
diff
changeset
|
849 bot_admin = participant.bot_admin |
931a5edc7971
moved commands to a separate file
Charly COSTE <changaco@changaco.net>
parents:
133
diff
changeset
|
850 else: |
931a5edc7971
moved commands to a separate file
Charly COSTE <changaco@changaco.net>
parents:
133
diff
changeset
|
851 bridge = None |
103 | 852 |
134
931a5edc7971
moved commands to a separate file
Charly COSTE <changaco@changaco.net>
parents:
133
diff
changeset
|
853 return commands.execute(self, message, bot_admin, bridge) |
103 | 854 |
855 | |
207
a97a7e930697
added optionnal "message" argument to Bot.restart()
Charly COSTE <changaco@changaco.net>
parents:
203
diff
changeset
|
856 def restart(self, message='Restarting bot'): |
103 | 857 # Stop the bridges |
858 for b in self.bridges: | |
207
a97a7e930697
added optionnal "message" argument to Bot.restart()
Charly COSTE <changaco@changaco.net>
parents:
203
diff
changeset
|
859 b.stop(message=message, log=False) |
103 | 860 |
861 # Reopen the bot's XMPP connection | |
862 self.reopen_xmpp_connection(self.xmpp_c) | |
863 | |
864 # Restart the bridges | |
865 for b in self.bridges: | |
866 b.init2() | |
867 | |
207
a97a7e930697
added optionnal "message" argument to Bot.restart()
Charly COSTE <changaco@changaco.net>
parents:
203
diff
changeset
|
868 self.error(-1, 'Bot restarted with message: '+message, send_to_admins=True) |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
869 |
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
|
870 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
871 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
|
872 if len(bridges) > 0: |
195
e4ceefab8ba5
fixed Bot.restart_bridges_delayed(), don't send the message to admins more than once
Charly COSTE <changaco@changaco.net>
parents:
193
diff
changeset
|
873 found = False |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
874 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
|
875 for b in bridges: |
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
|
876 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
|
877 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
|
878 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
|
879 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
|
880 |
195
e4ceefab8ba5
fixed Bot.restart_bridges_delayed(), don't send the message to admins more than once
Charly COSTE <changaco@changaco.net>
parents:
193
diff
changeset
|
881 if not b.reconnecting: |
e4ceefab8ba5
fixed Bot.restart_bridges_delayed(), don't send the message to admins more than once
Charly COSTE <changaco@changaco.net>
parents:
193
diff
changeset
|
882 found = True |
e4ceefab8ba5
fixed Bot.restart_bridges_delayed(), don't send the message to admins more than once
Charly COSTE <changaco@changaco.net>
parents:
193
diff
changeset
|
883 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
|
884 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
|
885 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
|
886 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
|
887 |
193
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
190
diff
changeset
|
888 b.stop(message=leave_message, log=False) |
121
765e460bc20a
Restart all bridges connected to a MUC server when it goes down
Charly COSTE <changaco@changaco.net>
parents:
119
diff
changeset
|
889 |
195
e4ceefab8ba5
fixed Bot.restart_bridges_delayed(), don't send the message to admins more than once
Charly COSTE <changaco@changaco.net>
parents:
193
diff
changeset
|
890 if found: |
e4ceefab8ba5
fixed Bot.restart_bridges_delayed(), don't send the message to admins more than once
Charly COSTE <changaco@changaco.net>
parents:
193
diff
changeset
|
891 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
|
892 |
765e460bc20a
Restart all bridges connected to a MUC server when it goes down
Charly COSTE <changaco@changaco.net>
parents:
119
diff
changeset
|
893 |
129
b13ca9cefe1c
tried to fix the halt command (added Bot.stop())
Charly COSTE <changaco@changaco.net>
parents:
127
diff
changeset
|
894 def stop(self, message='Stopping bot'): |
193
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
190
diff
changeset
|
895 self.error(-1, message, send_to_admins=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
|
896 for bridge in self.bridges: |
193
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
190
diff
changeset
|
897 bridge.stop(message=message, log=False) |
129
b13ca9cefe1c
tried to fix the halt command (added Bot.stop())
Charly COSTE <changaco@changaco.net>
parents:
127
diff
changeset
|
898 |
b13ca9cefe1c
tried to fix the halt command (added Bot.stop())
Charly COSTE <changaco@changaco.net>
parents:
127
diff
changeset
|
899 |
b13ca9cefe1c
tried to fix the halt command (added Bot.stop())
Charly COSTE <changaco@changaco.net>
parents:
127
diff
changeset
|
900 def __del__(self): |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
901 for bridge in self.bridges: |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
202
diff
changeset
|
902 self.remove_bridge(bridge, message='Stopping bot', log=False) |
155
63db565438bd
fixed the halt command
Charly COSTE <changaco@changaco.net>
parents:
152
diff
changeset
|
903 self.halt = True |