Mercurial > xib
annotate bridge.py @ 275:09a5bde70919 default tip master
minor fix in Participant
Signed-off-by: Changaco <changaco ατ changaco δοτ net>
author | Changaco <changaco ατ changaco δοτ net> |
---|---|
date | Wed, 05 May 2010 23:36:13 +0200 |
parents | fa5375057658 |
children |
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 threading | |
20 import traceback | |
21 | |
260
9a2302e8382b
(irclib) created a "Channel" class
Charly COSTE <changaco@changaco.net>
parents:
259
diff
changeset
|
22 import irclib |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
23 import muc |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
24 xmpp = muc.xmpp |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
25 del muc |
124 | 26 |
27 from participant import Participant | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
28 import say_levels |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
29 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
30 |
124 | 31 class Bridge: |
21
801160b4136f
Introduced a bridge's "say" attribute to offer more flexibility in what the bot says.
Charly COSTE <changaco@changaco.net>
parents:
19
diff
changeset
|
32 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
33 modes = ['bypass', 'normal', 'limited', 'minimal'] |
21
801160b4136f
Introduced a bridge's "say" attribute to offer more flexibility in what the bot says.
Charly COSTE <changaco@changaco.net>
parents:
19
diff
changeset
|
34 |
124 | 35 class NoSuchParticipantException(Exception): pass |
36 | |
37 | |
174
c158ad24ef3c
moved irc connection interval handling to irclib
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
38 def __init__(self, owner_bot, xmpp_room_jid, irc_room, irc_server, mode, say_level, irc_port=6667, irc_connection_interval=None, irc_charsets=None): |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
39 """Create a new bridge.""" |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
40 self.bot = owner_bot |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
41 self.irc_server = irc_server |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
42 self.irc_port = irc_port |
45
41394ddb3aff
Lower before comparing strings.
Charly COSTE <changaco@changaco.net>
parents:
40
diff
changeset
|
43 self.irc_room = irc_room.lower() |
150
e0eea72ea493
new bridge attribute "irc_connection_interval" (in seconds)
Charly COSTE <changaco@changaco.net>
parents:
139
diff
changeset
|
44 self.irc_connection_interval = irc_connection_interval |
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:
169
diff
changeset
|
45 self.irc_charsets = irc_charsets |
187 | 46 self.irc_op = False |
103 | 47 self.xmpp_room_jid = xmpp_room_jid |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
48 self.say_level = say_level |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
49 self.participants = [] |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
50 if mode not in self.__class__.modes: |
21
801160b4136f
Introduced a bridge's "say" attribute to offer more flexibility in what the bot says.
Charly COSTE <changaco@changaco.net>
parents:
19
diff
changeset
|
51 raise Exception('[Error] "'+mode+'" is not a correct value for a bridge\'s "mode" attribute') |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
52 self.mode = mode |
269
abef394fded5
fix in Bridge, don't change mode to None otherwise restart cannot work
Charly COSTE <changaco@changaco.net>
parents:
268
diff
changeset
|
53 self.stopped = False |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
54 |
40
faa468731d8a
Tried to get thread safety and handle disconnections in muc.py
Charly COSTE <changaco@changaco.net>
parents:
38
diff
changeset
|
55 self.lock = threading.RLock() |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
22
diff
changeset
|
56 |
103 | 57 self.init2() |
58 | |
59 | |
60 def init2(self): | |
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
|
61 self.reconnecting = False |
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
|
62 |
249
430eb5052e4d
minor changes in Bridge
Charly COSTE <changaco@changaco.net>
parents:
240
diff
changeset
|
63 self.bot.error(say_levels.notice, 'starting bridge "'+str(self)+'" with mode="'+self.mode+'" and say_level="'+str(self.say_level)+'"') |
430eb5052e4d
minor changes in Bridge
Charly COSTE <changaco@changaco.net>
parents:
240
diff
changeset
|
64 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
65 # Join XMPP room |
174
c158ad24ef3c
moved irc connection interval handling to irclib
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
66 self.xmpp_room = xmpp.muc(self.xmpp_room_jid) |
c158ad24ef3c
moved irc connection interval handling to irclib
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
67 self.xmpp_room.join(self.bot.xmpp_c, self.bot.nickname, callback=self._xmpp_join_callback) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
68 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
69 # Join IRC room |
174
c158ad24ef3c
moved irc connection interval handling to irclib
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
70 self.irc_connection = self.bot.irc.open_connection(self.irc_server, self.irc_port, self.bot.nickname, delay=self.irc_connection_interval) |
c158ad24ef3c
moved irc connection interval handling to irclib
Charly COSTE <changaco@changaco.net>
parents:
172
diff
changeset
|
71 self.irc_connection.connect(nick_callback=self._irc_nick_callback, charsets=self.irc_charsets) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
72 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
73 |
193
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
192
diff
changeset
|
74 def _join_irc_failed(self, reason): |
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
192
diff
changeset
|
75 self.bot.error(say_levels.error, 'failed to connect to the IRC chan of bridge '+str(self)+'\nreason: '+reason, send_to_admins=True) |
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
192
diff
changeset
|
76 self.stop(message='Failed to connect to the IRC chan, stopping bridge', log=False) |
179
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
176
diff
changeset
|
77 |
f6c6708c6c0e
handle IRC events cannotsendtochan and notonchannel, send unhandled events to bot admins
Charly COSTE <changaco@changaco.net>
parents:
176
diff
changeset
|
78 |
231
5f1e9211af03
fix for 48ad162772b79740e8ff580bde2c250b9957c126, we do need arguments in nick callbacks
Charly COSTE <changaco@changaco.net>
parents:
226
diff
changeset
|
79 def _irc_nick_callback(self, error, arguments=None): |
226
0d85049ac68d
switch to IRC join callbacks in bridge.py and participant.py
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
80 if not error: |
269
abef394fded5
fix in Bridge, don't change mode to None otherwise restart cannot work
Charly COSTE <changaco@changaco.net>
parents:
268
diff
changeset
|
81 if self.stopped: |
85
dfa030c141f1
Fixed callbacks in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
84
diff
changeset
|
82 return |
226
0d85049ac68d
switch to IRC join callbacks in bridge.py and participant.py
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
83 self.irc_connection.join(self.irc_room, callback=self._irc_join_callback) |
0d85049ac68d
switch to IRC join callbacks in bridge.py and participant.py
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
84 |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
85 else: |
269
abef394fded5
fix in Bridge, don't change mode to None otherwise restart cannot work
Charly COSTE <changaco@changaco.net>
parents:
268
diff
changeset
|
86 self.stopped = True |
192
12fa9bb73b1d
check connection in Bridge.say(), use Bridge.say() in Participant.sayOn{IRC,XMPP}()
Charly COSTE <changaco@changaco.net>
parents:
191
diff
changeset
|
87 self.say(say_levels.error, 'failed to connect to the IRC chan, leaving ...', on_irc=False) |
193
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
192
diff
changeset
|
88 if error in ['nicknameinuse', 'nickcollision']: |
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
192
diff
changeset
|
89 reason = '"'+self.bot.nickname+'" is already used or reserved on the IRC server' |
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
192
diff
changeset
|
90 elif error == 'erroneusnickname': |
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
192
diff
changeset
|
91 reason = '"'+self.bot.nickname+'" got "erroneusnickname"' |
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
192
diff
changeset
|
92 elif error == 'nicknametoolong': |
219
63289aa1dea7
moved "nicknametoolong" and ServerConnection.really_connected handling to irclib, renamed ServerConnection.really_connected to .logged_in
Charly COSTE <changaco@changaco.net>
parents:
216
diff
changeset
|
93 reason = '"'+self.bot.nickname+'" got "nicknametoolong", limit seems to be '+str(len(self.irc_connection.real_nickname)) |
193
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
192
diff
changeset
|
94 else: |
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
192
diff
changeset
|
95 reason = error |
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
192
diff
changeset
|
96 self._join_irc_failed(reason) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
97 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
98 |
226
0d85049ac68d
switch to IRC join callbacks in bridge.py and participant.py
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
99 def _irc_join_callback(self, channel, error): |
0d85049ac68d
switch to IRC join callbacks in bridge.py and participant.py
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
100 if not error: |
0d85049ac68d
switch to IRC join callbacks in bridge.py and participant.py
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
101 self.bot.error(3, 'successfully joined IRC side of bridge "'+str(self)+'"', debug=True) |
0d85049ac68d
switch to IRC join callbacks in bridge.py and participant.py
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
102 self.say(say_levels.notice, 'bridge "'+str(self)+'" is running in '+self.mode+' mode', on_xmpp=False) |
0d85049ac68d
switch to IRC join callbacks in bridge.py and participant.py
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
103 if self.mode not in ['normal', 'bypass']: |
0d85049ac68d
switch to IRC join callbacks in bridge.py and participant.py
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
104 self.show_participants_list_on(protocols=['irc']) |
0d85049ac68d
switch to IRC join callbacks in bridge.py and participant.py
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
105 |
0d85049ac68d
switch to IRC join callbacks in bridge.py and participant.py
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
106 else: |
0d85049ac68d
switch to IRC join callbacks in bridge.py and participant.py
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
107 self._join_irc_failed(error) |
0d85049ac68d
switch to IRC join callbacks in bridge.py and participant.py
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
108 |
0d85049ac68d
switch to IRC join callbacks in bridge.py and participant.py
Charly COSTE <changaco@changaco.net>
parents:
221
diff
changeset
|
109 |
135
922858915907
handle xmpp.muc.RemoteServerNotFound
Charly COSTE <changaco@changaco.net>
parents:
132
diff
changeset
|
110 def _RemoteServerNotFound_handler(self): |
922858915907
handle xmpp.muc.RemoteServerNotFound
Charly COSTE <changaco@changaco.net>
parents:
132
diff
changeset
|
111 server = xmpp.protocol.JID(self.xmpp_room_jid).getDomain() |
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:
215
diff
changeset
|
112 bridges = self.bot.iter_bridges(patterns=[server]) |
262
db77ef37ff2c
fixed Bot.restart_bridges_delayed: removed buggy and useless test and improved error messages
Charly COSTE <changaco@changaco.net>
parents:
260
diff
changeset
|
113 m = 'The MUC server '+server+' seems to be down' |
db77ef37ff2c
fixed Bot.restart_bridges_delayed: removed buggy and useless test and improved error messages
Charly COSTE <changaco@changaco.net>
parents:
260
diff
changeset
|
114 self.bot.restart_bridges_delayed(bridges, 300, say_levels.warning, m) |
135
922858915907
handle xmpp.muc.RemoteServerNotFound
Charly COSTE <changaco@changaco.net>
parents:
132
diff
changeset
|
115 |
922858915907
handle xmpp.muc.RemoteServerNotFound
Charly COSTE <changaco@changaco.net>
parents:
132
diff
changeset
|
116 |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
117 def _xmpp_join_callback(self, errors): |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
118 """Called by muc._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:
15
diff
changeset
|
119 if len(errors) == 0: |
206
ba5ecd71065f
bugfix, we don't delete Bridge.reconnecting since 3518e463b82185bde91a61a810d38dbf58e55147
Charly COSTE <changaco@changaco.net>
parents:
205
diff
changeset
|
120 self.reconnecting = False |
269
abef394fded5
fix in Bridge, don't change mode to None otherwise restart cannot work
Charly COSTE <changaco@changaco.net>
parents:
268
diff
changeset
|
121 if self.stopped: |
85
dfa030c141f1
Fixed callbacks in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
84
diff
changeset
|
122 return |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
123 self.bot.error(3, 'succesfully connected on XMPP side of bridge "'+str(self)+'"', debug=True) |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
124 self.say(say_levels.notice, 'bridge "'+str(self)+'" is running in '+self.mode+' mode', on_irc=False) |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
125 else: |
269
abef394fded5
fix in Bridge, don't change mode to None otherwise restart cannot work
Charly COSTE <changaco@changaco.net>
parents:
268
diff
changeset
|
126 self.stopped = True |
192
12fa9bb73b1d
check connection in Bridge.say(), use Bridge.say() in Participant.sayOn{IRC,XMPP}()
Charly COSTE <changaco@changaco.net>
parents:
191
diff
changeset
|
127 self.say(say_levels.error, 'failed to connect to the XMPP room, leaving ...', on_xmpp=False) |
274
fa5375057658
fixed an UnboundLocalError in Bridge._xmpp_join_callback
Charly COSTE <changaco@changaco.net>
parents:
270
diff
changeset
|
128 trace = '' |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
129 for error in errors: |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
130 try: |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
131 raise error |
135
922858915907
handle xmpp.muc.RemoteServerNotFound
Charly COSTE <changaco@changaco.net>
parents:
132
diff
changeset
|
132 except xmpp.muc.RemoteServerNotFound: |
922858915907
handle xmpp.muc.RemoteServerNotFound
Charly COSTE <changaco@changaco.net>
parents:
132
diff
changeset
|
133 self._RemoteServerNotFound_handler() |
274
fa5375057658
fixed an UnboundLocalError in Bridge._xmpp_join_callback
Charly COSTE <changaco@changaco.net>
parents:
270
diff
changeset
|
134 return |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
135 except: |
274
fa5375057658
fixed an UnboundLocalError in Bridge._xmpp_join_callback
Charly COSTE <changaco@changaco.net>
parents:
270
diff
changeset
|
136 trace += '\n'+traceback.format_exc().splitlines()[-1] |
fa5375057658
fixed an UnboundLocalError in Bridge._xmpp_join_callback
Charly COSTE <changaco@changaco.net>
parents:
270
diff
changeset
|
137 self.bot.error(say_levels.error, 'failed to connect to the XMPP room of bridge "'+str(self)+'", stopping bridge'+trace, send_to_admins=True) |
251
49c57daff4d7
minor fix in Bridge, don't log the same error twice
Charly COSTE <changaco@changaco.net>
parents:
249
diff
changeset
|
138 self.stop(message='Failed to connect to the XMPP room, stopping bridge', log=False) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
139 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
140 |
221
1a82d5d40d90
moved irc_id handling to irclib
Charly COSTE <changaco@changaco.net>
parents:
219
diff
changeset
|
141 def add_participant(self, from_protocol, nickname, real_jid=None): |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
142 """Add a participant to the bridge.""" |
175
8378da7844f2
minor fix in Bridge.addParticipant(), don't rely on Bridge.irc_connection or Bridge.xmpp_room when not necessary
Charly COSTE <changaco@changaco.net>
parents:
174
diff
changeset
|
143 if (from_protocol == 'irc' and nickname == self.bot.nickname) or (from_protocol == 'xmpp' and nickname == self.bot.nickname): |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
144 return |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
145 try: |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
146 p = self.get_participant(nickname) |
72
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
147 if p.protocol != from_protocol: |
260
9a2302e8382b
(irclib) created a "Channel" class
Charly COSTE <changaco@changaco.net>
parents:
259
diff
changeset
|
148 if from_protocol == 'irc' and isinstance(p.irc_connection, irclib.ServerConnection) and p.irc_connection.channels.has_key(self.irc_room) and p.irc_connection.channels[self.irc_room].state >= irclib.JOINING or from_protocol == 'xmpp' and isinstance(p.muc, xmpp.muc) and p.muc.state >= p.muc.JOINING: |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
149 return p |
123
75a03f10a863
Code refactoring (new method participant.set_both_sides())
Charly COSTE <changaco@changaco.net>
parents:
122
diff
changeset
|
150 p.set_both_sides() |
109 | 151 return p |
124 | 152 except self.NoSuchParticipantException: |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
153 pass |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
154 |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
155 if nickname == 'ChanServ' and from_protocol == 'irc': |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
156 return |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
157 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
158 self.bot.error(3, 'adding participant "'+nickname+'" from "'+from_protocol+'" to bridge "'+str(self)+'"', debug=True) |
21
801160b4136f
Introduced a bridge's "say" attribute to offer more flexibility in what the bot says.
Charly COSTE <changaco@changaco.net>
parents:
19
diff
changeset
|
159 try: |
124 | 160 p = Participant(self, from_protocol, nickname, real_jid=real_jid) |
21
801160b4136f
Introduced a bridge's "say" attribute to offer more flexibility in what the bot says.
Charly COSTE <changaco@changaco.net>
parents:
19
diff
changeset
|
161 except: |
201
de574314990e
log some more things when Bot.debug is False
Charly COSTE <changaco@changaco.net>
parents:
197
diff
changeset
|
162 self.bot.error(say_levels.debug, 'unknown error while adding participant "'+nickname+'" from "'+from_protocol+'" to bridge "'+str(self)+'"\n'+traceback.format_exc()) |
21
801160b4136f
Introduced a bridge's "say" attribute to offer more flexibility in what the bot says.
Charly COSTE <changaco@changaco.net>
parents:
19
diff
changeset
|
163 return |
191
ac89a4a72a62
removed buggy exception catch in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
190
diff
changeset
|
164 self.lock.acquire() |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
165 self.participants.append(p) |
97
c71861491968
bridge.addParticipant improvements
Charly COSTE <changaco@changaco.net>
parents:
86
diff
changeset
|
166 self.lock.release() |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
167 if self.mode not in ['normal', 'bypass']: |
102
b3eba9489329
In minimal mode, display the participants' list on XMPP when someone joins on IRC
Charly COSTE <changaco@changaco.net>
parents:
101
diff
changeset
|
168 if from_protocol == 'xmpp': |
152
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
169 self.show_participants_list_on(protocols=['irc']) |
102
b3eba9489329
In minimal mode, display the participants' list on XMPP when someone joins on IRC
Charly COSTE <changaco@changaco.net>
parents:
101
diff
changeset
|
170 elif self.mode == 'minimal' and from_protocol == 'irc': |
152
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
171 self.show_participants_list_on(protocols=['xmpp']) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
172 return p |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
173 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
174 |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
175 def create_duplicates_on(self, protocols): |
132
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
176 for p in self.participants: |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
177 if p.protocol == 'xmpp' and 'irc' in protocols: |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
178 p.create_duplicate_on_irc() |
132
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
179 elif p.protocol == 'irc' and 'xmpp' in protocols: |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
180 p.create_duplicate_on_xmpp() |
132
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
181 |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
182 |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
183 def change_mode(self, new_mode): |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
184 if new_mode == self.mode: |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
185 return 'Mode is already equal to '+self.mode |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
186 |
132
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
187 old_mode = self.mode |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
188 self.mode = new_mode |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
189 |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
190 unhandled = False |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
191 |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
192 if new_mode in ['normal', 'bypass']: |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
193 |
152
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
194 if old_mode == 'limited': |
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
195 # From limited to {normal,bypass} |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
196 self.create_duplicates_on(['irc']) |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
197 |
132
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
198 elif old_mode in ['minimal', 'normal']: |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
199 # From {minimal,normal} to {normal,bypass} |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
200 self.create_duplicates_on(['irc', 'xmpp']) |
132
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
201 |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
202 elif old_mode == 'bypass': |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
203 # From bypass to normal |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
204 pass # Handled below |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
205 |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
206 else: |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
207 # Unhandled mode changing |
132
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
208 unhandled = True |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
209 |
152
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
210 elif new_mode == 'limited': |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
211 |
139
38eb220142a1
fixed minimal→*limited mode changing
Charly COSTE <changaco@changaco.net>
parents:
137
diff
changeset
|
212 if old_mode == 'minimal': |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
213 self.create_duplicates_on(['xmpp']) |
139
38eb220142a1
fixed minimal→*limited mode changing
Charly COSTE <changaco@changaco.net>
parents:
137
diff
changeset
|
214 |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
215 i = 0 |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
216 for p in self.participants: |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
217 if p.protocol == 'xmpp': |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
218 i += 1 |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
219 p._close_irc_connection('Bridge is switching to limited mode') |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
220 |
152
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
221 self.show_participants_list_on(protocols=['irc']) |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
222 |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
223 elif new_mode == 'minimal': |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
224 for p in self.participants: |
132
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
225 p.leave('Bridge is switching to minimal mode') |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
226 |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
227 else: |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
228 # Unhandled mode changing |
132
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
229 unhandled = True |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
230 |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
231 if unhandled: |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
232 self.mode = old_mode |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
233 return 'Error: unhandled mode changing from '+self.mode+' to '+new_mode |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
234 |
132
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
235 if old_mode == 'bypass': |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
236 # From bypass to * |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
237 for p in self.participants: |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
238 if p.nickname != p.duplicate_nickname: |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
239 p.leave('Bridge is switching to '+new_mode+' mode') |
6a6885dbed25
handle more mode changing cases (added bridge.createDuplicatesOn())
Charly COSTE <changaco@changaco.net>
parents:
131
diff
changeset
|
240 |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
241 self.say(say_levels.notice, 'Bridge is switching from '+old_mode+' to '+new_mode+' mode.', log=True) |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
242 |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
243 |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
244 def get_participant(self, nickname): |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
245 """Returns a participant object if there is a participant using nickname in the bridge. Raises a NoSuchParticipantException otherwise.""" |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
22
diff
changeset
|
246 self.lock.acquire() |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
247 for p in self.participants: |
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
248 if nickname in [p.nickname, p.duplicate_nickname]: |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
22
diff
changeset
|
249 self.lock.release() |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
250 return p |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
22
diff
changeset
|
251 self.lock.release() |
124 | 252 raise self.NoSuchParticipantException('there is no participant using the nickname "'+nickname+'" in this bridge') |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
253 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
254 |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
255 def get_participants_nicknames_list(self, protocols=['irc', 'xmpp']): |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
256 """Returns a list of the nicknames of the bridge's participants that are connected on the XMPP side.""" |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
22
diff
changeset
|
257 self.lock.acquire() |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
258 participants_nicknames = [] |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
259 for p in self.participants: |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
260 if p.protocol in protocols: |
18
3cdf7bb580da
Fixed bot.respond(), boosted XMPP thread to lower latency, fixed mode notice.
Charly COSTE <changaco@changaco.net>
parents:
17
diff
changeset
|
261 participants_nicknames.append('"'+p.nickname+'"') |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
22
diff
changeset
|
262 self.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:
15
diff
changeset
|
263 return participants_nicknames |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
264 |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
265 |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
266 def has_participant(self, nickname): |
62
61491895c607
Added bridge.hasParticipant() and fixed participant.changeNickname()
Charly COSTE <changaco@changaco.net>
parents:
60
diff
changeset
|
267 try: |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
268 self.get_participant(nickname) |
62
61491895c607
Added bridge.hasParticipant() and fixed participant.changeNickname()
Charly COSTE <changaco@changaco.net>
parents:
60
diff
changeset
|
269 return True |
124 | 270 except self.NoSuchParticipantException: |
62
61491895c607
Added bridge.hasParticipant() and fixed participant.changeNickname()
Charly COSTE <changaco@changaco.net>
parents:
60
diff
changeset
|
271 return False |
61491895c607
Added bridge.hasParticipant() and fixed participant.changeNickname()
Charly COSTE <changaco@changaco.net>
parents:
60
diff
changeset
|
272 |
61491895c607
Added bridge.hasParticipant() and fixed participant.changeNickname()
Charly COSTE <changaco@changaco.net>
parents:
60
diff
changeset
|
273 |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
274 def remove_participant(self, left_protocol, nickname, leave_message): |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
275 """Remove the participant using nickname from the bridge. Raises a NoSuchParticipantException if nickname is not used in the bridge.""" |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
276 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
277 was_on_both = None |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
278 p = self.get_participant(nickname) |
189
e04410e7e527
split the leaving process in two, first call to Bridge.removeParticipant() calls Participant.leave(), second call (when the bot receives the part or quit event) removes it from Bridge.participants
Charly COSTE <changaco@changaco.net>
parents:
187
diff
changeset
|
279 |
e04410e7e527
split the leaving process in two, first call to Bridge.removeParticipant() calls Participant.leave(), second call (when the bot receives the part or quit event) removes it from Bridge.participants
Charly COSTE <changaco@changaco.net>
parents:
187
diff
changeset
|
280 if p.left: |
e04410e7e527
split the leaving process in two, first call to Bridge.removeParticipant() calls Participant.leave(), second call (when the bot receives the part or quit event) removes it from Bridge.participants
Charly COSTE <changaco@changaco.net>
parents:
187
diff
changeset
|
281 self.lock.acquire() |
e04410e7e527
split the leaving process in two, first call to Bridge.removeParticipant() calls Participant.leave(), second call (when the bot receives the part or quit event) removes it from Bridge.participants
Charly COSTE <changaco@changaco.net>
parents:
187
diff
changeset
|
282 self.participants.remove(p) |
e04410e7e527
split the leaving process in two, first call to Bridge.removeParticipant() calls Participant.leave(), second call (when the bot receives the part or quit event) removes it from Bridge.participants
Charly COSTE <changaco@changaco.net>
parents:
187
diff
changeset
|
283 del p |
e04410e7e527
split the leaving process in two, first call to Bridge.removeParticipant() calls Participant.leave(), second call (when the bot receives the part or quit event) removes it from Bridge.participants
Charly COSTE <changaco@changaco.net>
parents:
187
diff
changeset
|
284 self.lock.release() |
e04410e7e527
split the leaving process in two, first call to Bridge.removeParticipant() calls Participant.leave(), second call (when the bot receives the part or quit event) removes it from Bridge.participants
Charly COSTE <changaco@changaco.net>
parents:
187
diff
changeset
|
285 return |
e04410e7e527
split the leaving process in two, first call to Bridge.removeParticipant() calls Participant.leave(), second call (when the bot receives the part or quit event) removes it from Bridge.participants
Charly COSTE <changaco@changaco.net>
parents:
187
diff
changeset
|
286 |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
287 if p.protocol == 'xmpp': |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
288 if p.irc_connection == 'both': |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
289 was_on_both = True |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
290 if left_protocol == 'xmpp': |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
291 p.protocol = 'irc' |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
292 p.create_duplicate_on_xmpp() |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
293 elif left_protocol == 'irc': |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
294 p.create_duplicate_on_irc() |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
295 else: |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
296 if left_protocol == 'xmpp': |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
297 was_on_both = False |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
298 elif left_protocol == 'irc': |
189
e04410e7e527
split the leaving process in two, first call to Bridge.removeParticipant() calls Participant.leave(), second call (when the bot receives the part or quit event) removes it from Bridge.participants
Charly COSTE <changaco@changaco.net>
parents:
187
diff
changeset
|
299 # got disconnected somehow |
260
9a2302e8382b
(irclib) created a "Channel" class
Charly COSTE <changaco@changaco.net>
parents:
259
diff
changeset
|
300 if isinstance(p.irc_connection, irclib.ServerConnection): |
233
4ad8e985c7d3
tried to fix Bridge.remove_participant
Charly COSTE <changaco@changaco.net>
parents:
231
diff
changeset
|
301 if p.irc_connection.socket == 'closed': |
4ad8e985c7d3
tried to fix Bridge.remove_participant
Charly COSTE <changaco@changaco.net>
parents:
231
diff
changeset
|
302 return |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
303 p.irc_connection.join(self.irc_room) |
131
46af7f2744a9
added irclib.ServerConnection.left_channels
Charly COSTE <changaco@changaco.net>
parents:
125
diff
changeset
|
304 else: |
46af7f2744a9
added irclib.ServerConnection.left_channels
Charly COSTE <changaco@changaco.net>
parents:
125
diff
changeset
|
305 c = self.bot.irc.get_connection(self.irc_server, self.irc_port, p.duplicate_nickname) |
260
9a2302e8382b
(irclib) created a "Channel" class
Charly COSTE <changaco@changaco.net>
parents:
259
diff
changeset
|
306 if not (c and c.channels.has_key(self.irc_room) and c.channels[self.irc_room].state <= irclib.LEAVING): |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
307 p.create_duplicate_on_irc() |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
308 return |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
309 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
310 elif p.protocol == 'irc': |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
311 if p.xmpp_c == 'both': |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
312 was_on_both = True |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
313 if left_protocol == 'irc': |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
314 p.protocol = 'xmpp' |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
315 p.create_duplicate_on_irc() |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
316 elif left_protocol == 'xmpp': |
203
2a81c480439a
renamed camelCased functions
Charly COSTE <changaco@changaco.net>
parents:
201
diff
changeset
|
317 p.create_duplicate_on_xmpp() |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
318 else: |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
319 if left_protocol == 'irc': |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
320 was_on_both = False |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
321 elif left_protocol == 'xmpp': |
259
3b318108e9e0
(muc) created "state" attribute to replace the "connected" one
Charly COSTE <changaco@changaco.net>
parents:
251
diff
changeset
|
322 if isinstance(p.muc, xmpp.muc) and p.muc.state <= p.muc.LEAVING: |
233
4ad8e985c7d3
tried to fix Bridge.remove_participant
Charly COSTE <changaco@changaco.net>
parents:
231
diff
changeset
|
323 return |
189
e04410e7e527
split the leaving process in two, first call to Bridge.removeParticipant() calls Participant.leave(), second call (when the bot receives the part or quit event) removes it from Bridge.participants
Charly COSTE <changaco@changaco.net>
parents:
187
diff
changeset
|
324 # got disconnected somehow |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
325 if isinstance(p.xmpp_c, xmpp.client.Client): |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
326 self.bot.reopen_xmpp_connection(p.xmpp_c) |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
327 return |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
328 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
329 else: |
19
c1b84196c100
Changed format of non-debug error messages, fixed IRC namreply handling, prevented crash when receiving bad XMPP stanza.
Charly COSTE <changaco@changaco.net>
parents:
18
diff
changeset
|
330 raise Exception('[Internal Error] bad protocol') |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
331 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
332 if was_on_both == True: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
333 self.bot.error(3, '"'+nickname+'" was on both sides of bridge "'+str(self)+'" but left '+left_protocol, 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:
15
diff
changeset
|
334 |
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
335 elif was_on_both == False: |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
336 self.bot.error(3, 'removing participant "'+nickname+'" from bridge "'+str(self)+'"', debug=True) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
337 p.leave(leave_message) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
338 if left_protocol == 'xmpp': |
118
b29fd5696a78
new mode bypass and new method bridge.changeMode
Charly COSTE <changaco@changaco.net>
parents:
115
diff
changeset
|
339 if self.mode not in ['normal', 'bypass']: |
152
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
340 self.show_participants_list_on(protocols=['irc']) |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
341 elif left_protocol == 'irc': |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
22
diff
changeset
|
342 if self.mode == 'minimal': |
152
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
343 self.show_participants_list_on(protocols=['xmpp']) |
189
e04410e7e527
split the leaving process in two, first call to Bridge.removeParticipant() calls Participant.leave(), second call (when the bot receives the part or quit event) removes it from Bridge.participants
Charly COSTE <changaco@changaco.net>
parents:
187
diff
changeset
|
344 |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
345 else: |
201
de574314990e
log some more things when Bot.debug is False
Charly COSTE <changaco@changaco.net>
parents:
197
diff
changeset
|
346 self.bot.error(say_levels.debug, 'Bad decision tree, p.protocol='+p.protocol+' left_protocol='+left_protocol+'\np.xmpp_c='+str(p.xmpp_c)+'\np.irc_connection='+str(p.irc_connection), send_to_admins=True) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
347 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
348 |
205
85a8b457c4b4
bugfix, added optional "message" argument to Bridge.restart(), needed since fa73f1292eafabf2ef868c2b056fe045a0704e97
Charly COSTE <changaco@changaco.net>
parents:
203
diff
changeset
|
349 def restart(self, message='Restarting bridge', log=True): |
103 | 350 """Restart the bridge""" |
351 | |
352 # Stop the bridge | |
205
85a8b457c4b4
bugfix, added optional "message" argument to Bridge.restart(), needed since fa73f1292eafabf2ef868c2b056fe045a0704e97
Charly COSTE <changaco@changaco.net>
parents:
203
diff
changeset
|
353 self.stop(message=message, log=log) |
103 | 354 |
355 # Recreate the bridge | |
356 self.init2() | |
357 | |
358 | |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
359 def say(self, importance, message, on_irc=True, on_xmpp=True, log=False, send_to_admins=False): |
17
32a35f7eff70
Rewrote/modified many things, multiple bridges should now work and are preferred over multiple bots.
Charly COSTE <changaco@changaco.net>
parents:
15
diff
changeset
|
360 """Make the bot say something.""" |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
361 if log or send_to_admins: |
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
362 self.bot.error(importance, message+' ('+str(self)+')', send_to_admins=send_to_admins) |
197
4a379eb359dd
bugfix, forgot "importance" argument of Bridge.say() in c5922031a4ac09c95713c4a4a18b1e0a0aad3e54
Charly COSTE <changaco@changaco.net>
parents:
195
diff
changeset
|
363 if importance != -1: |
4a379eb359dd
bugfix, forgot "importance" argument of Bridge.say() in c5922031a4ac09c95713c4a4a18b1e0a0aad3e54
Charly COSTE <changaco@changaco.net>
parents:
195
diff
changeset
|
364 message = self.bot.format_message(importance, message) |
4a379eb359dd
bugfix, forgot "importance" argument of Bridge.say() in c5922031a4ac09c95713c4a4a18b1e0a0aad3e54
Charly COSTE <changaco@changaco.net>
parents:
195
diff
changeset
|
365 if importance < self.say_level: |
4a379eb359dd
bugfix, forgot "importance" argument of Bridge.say() in c5922031a4ac09c95713c4a4a18b1e0a0aad3e54
Charly COSTE <changaco@changaco.net>
parents:
195
diff
changeset
|
366 return |
213
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
367 if on_xmpp: |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
368 self._say_on_xmpp(message) |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
369 if on_irc: |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
370 self._say_on_irc(message) |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
371 |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
372 |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
373 def say_on_behalf(self, nickname, message, on_protocol, action=False): |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
374 if action: |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
375 message = '* '+nickname+' '+message |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
376 else: |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
377 message = '<'+nickname+'> '+message |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
378 |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
379 if on_protocol == 'xmpp': |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
380 self._say_on_xmpp(message) |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
381 elif on_protocol == 'irc': |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
382 self._say_on_irc(message) |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
383 |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
384 |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
385 def _say_on_irc(self, message): |
263
73d6e284c18d
handle irclib.ServerNotConnectedError in Bridge._say_on_irc
Charly COSTE <changaco@changaco.net>
parents:
262
diff
changeset
|
386 try: |
73d6e284c18d
handle irclib.ServerNotConnectedError in Bridge._say_on_irc
Charly COSTE <changaco@changaco.net>
parents:
262
diff
changeset
|
387 self.irc_connection.privmsg(self.irc_room, message) |
267
f861a7038ad3
minor fix in Bridge, forgot namespace in bca57d434e135f3a482b11c3ea490266a187612f
Charly COSTE <changaco@changaco.net>
parents:
265
diff
changeset
|
388 except irclib.ServerNotConnectedError: |
263
73d6e284c18d
handle irclib.ServerNotConnectedError in Bridge._say_on_irc
Charly COSTE <changaco@changaco.net>
parents:
262
diff
changeset
|
389 bridges = self.bot.iter_bridges(irc_server=self.irc_server) |
73d6e284c18d
handle irclib.ServerNotConnectedError in Bridge._say_on_irc
Charly COSTE <changaco@changaco.net>
parents:
262
diff
changeset
|
390 self.bot.restart_bridges_delayed(bridges, 0, say_levels.error, 'Lost bot IRC connection', protocol='irc') |
213
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
391 |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
392 |
fbe40b397f67
created Bridge.say_on_behalf in order to avoid code repetition in participant.py
Charly COSTE <changaco@changaco.net>
parents:
206
diff
changeset
|
393 def _say_on_xmpp(self, message): |
270
7640a3feddf5
catch xmpp.muc.NotConnected in Bridge._say_on_xmpp
Charly COSTE <changaco@changaco.net>
parents:
269
diff
changeset
|
394 try: |
7640a3feddf5
catch xmpp.muc.NotConnected in Bridge._say_on_xmpp
Charly COSTE <changaco@changaco.net>
parents:
269
diff
changeset
|
395 self.xmpp_room.say(message) |
7640a3feddf5
catch xmpp.muc.NotConnected in Bridge._say_on_xmpp
Charly COSTE <changaco@changaco.net>
parents:
269
diff
changeset
|
396 except xmpp.muc.NotConnected: |
7640a3feddf5
catch xmpp.muc.NotConnected in Bridge._say_on_xmpp
Charly COSTE <changaco@changaco.net>
parents:
269
diff
changeset
|
397 # TODO: implement a message queue to improve this |
7640a3feddf5
catch xmpp.muc.NotConnected in Bridge._say_on_xmpp
Charly COSTE <changaco@changaco.net>
parents:
269
diff
changeset
|
398 if self.xmpp_room.state <= xmpp.muc.LEAVING: |
7640a3feddf5
catch xmpp.muc.NotConnected in Bridge._say_on_xmpp
Charly COSTE <changaco@changaco.net>
parents:
269
diff
changeset
|
399 self.bot.error(say_levels.debug, 'Ignoring xmpp.muc.NotConnected exception because we are leaving ('+str(self)+')', send_to_admins=True) |
7640a3feddf5
catch xmpp.muc.NotConnected in Bridge._say_on_xmpp
Charly COSTE <changaco@changaco.net>
parents:
269
diff
changeset
|
400 elif self.xmpp_room.state >= xmpp.muc.JOINING: |
7640a3feddf5
catch xmpp.muc.NotConnected in Bridge._say_on_xmpp
Charly COSTE <changaco@changaco.net>
parents:
269
diff
changeset
|
401 self.bot.error(say_levels.debug, 'Dropping a message because we are not in the XMPP room yet ('+str(self)+')', send_to_admins=True) |
7640a3feddf5
catch xmpp.muc.NotConnected in Bridge._say_on_xmpp
Charly COSTE <changaco@changaco.net>
parents:
269
diff
changeset
|
402 else: |
7640a3feddf5
catch xmpp.muc.NotConnected in Bridge._say_on_xmpp
Charly COSTE <changaco@changaco.net>
parents:
269
diff
changeset
|
403 self.bot.restart(message='Lost bot XMPP connection') |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
404 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
405 |
152
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
406 def show_participants_list_on(self, protocols=[]): |
219
63289aa1dea7
moved "nicknametoolong" and ServerConnection.really_connected handling to irclib, renamed ServerConnection.really_connected to .logged_in
Charly COSTE <changaco@changaco.net>
parents:
216
diff
changeset
|
407 if 'irc' in protocols and self.irc_connection.logged_in: |
152
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
408 xmpp_participants_nicknames = self.get_participants_nicknames_list(protocols=['xmpp']) |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
409 self.say(say_levels.info, 'Participants on XMPP: '+' '.join(xmpp_participants_nicknames), on_xmpp=False) |
152
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
410 if 'xmpp' in protocols: |
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
411 irc_participants_nicknames = self.get_participants_nicknames_list(protocols=['irc']) |
180
102f895347ff
added a required "importance" argument to Bot.error()
Charly COSTE <changaco@changaco.net>
parents:
179
diff
changeset
|
412 self.say(say_levels.info, 'Participants on IRC: '+' '.join(irc_participants_nicknames), on_irc=False) |
152
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
413 |
17305e57f71d
removed automatic switching to limited mode (created Bridge.show_participants_list_on())
Charly COSTE <changaco@changaco.net>
parents:
150
diff
changeset
|
414 |
237
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
415 def soft_restart(self, message='Softly restarting bridge', log=True): |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
416 """Make the bot leave and rejoin, avoid presence flood but can leave ghosts""" |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
417 |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
418 if log: |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
419 self.bot.error(-1, message+' '+str(self), send_to_admins=True) |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
420 |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
421 # Leave |
260
9a2302e8382b
(irclib) created a "Channel" class
Charly COSTE <changaco@changaco.net>
parents:
259
diff
changeset
|
422 if isinstance(self.irc_connection, irclib.ServerConnection) and isinstance(self.xmpp_room, xmpp.muc): |
268
7602a0898d19
catch irclib.UnknownChannel in Bridge.stop and Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
267
diff
changeset
|
423 try: |
7602a0898d19
catch irclib.UnknownChannel in Bridge.stop and Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
267
diff
changeset
|
424 self.irc_connection.part(self.irc_room, message=message) |
7602a0898d19
catch irclib.UnknownChannel in Bridge.stop and Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
267
diff
changeset
|
425 except irclib.UnknownChannel: |
7602a0898d19
catch irclib.UnknownChannel in Bridge.stop and Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
267
diff
changeset
|
426 pass |
237
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
427 self.xmpp_room.leave(message=message) |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
428 else: |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
429 self.stop() |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
430 self.init2() |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
431 return |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
432 |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
433 # Rejoin |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
434 self.irc_connection.join(self.irc_room, callback=self._irc_join_callback) |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
435 self.xmpp_room.rejoin(callback=self._xmpp_join_callback) |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
436 |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
437 |
193
c2a8586e64b5
handle IRC inviteonlychan event, improved error messages
Charly COSTE <changaco@changaco.net>
parents:
192
diff
changeset
|
438 def stop(self, message='Stopping bridge', log=True): |
103 | 439 """Stop the bridge""" |
440 | |
237
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
441 if log: |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
442 self.bot.error(-1, message+' '+str(self), send_to_admins=True) |
71c23e30cd6c
created Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
233
diff
changeset
|
443 |
103 | 444 # Close IRC connection if not used by an other bridge, just leave the room otherwise |
260
9a2302e8382b
(irclib) created a "Channel" class
Charly COSTE <changaco@changaco.net>
parents:
259
diff
changeset
|
445 if isinstance(self.irc_connection, irclib.ServerConnection): |
109 | 446 self.irc_connection.used_by -= 1 |
447 if self.irc_connection.used_by < 1: | |
448 self.irc_connection.close(message) | |
449 else: | |
268
7602a0898d19
catch irclib.UnknownChannel in Bridge.stop and Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
267
diff
changeset
|
450 try: |
7602a0898d19
catch irclib.UnknownChannel in Bridge.stop and Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
267
diff
changeset
|
451 self.irc_connection.part(self.irc_room, message=message) |
7602a0898d19
catch irclib.UnknownChannel in Bridge.stop and Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
267
diff
changeset
|
452 except irclib.UnknownChannel: |
7602a0898d19
catch irclib.UnknownChannel in Bridge.stop and Bridge.soft_restart
Charly COSTE <changaco@changaco.net>
parents:
267
diff
changeset
|
453 pass |
109 | 454 self.irc_connection = None |
103 | 455 |
456 # Leave the MUC | |
109 | 457 if isinstance(self.xmpp_room, xmpp.muc): |
458 self.xmpp_room.leave(message=message) | |
459 self.xmpp_room.__del__() | |
460 self.xmpp_room = None | |
103 | 461 |
462 # Delete participants objects | |
463 for p in self.participants: | |
464 p.leave(message) | |
465 self.participants = [] | |
466 | |
467 | |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
468 def __str__(self): |
103 | 469 return self.irc_room+'@'+self.irc_server+' <-> '+self.xmpp_room_jid |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
470 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
471 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
472 def __del__(self): |
103 | 473 self.stop(message='Removing bridge') |