Mercurial > xib
annotate bridge.py @ 105:d8acff763731
Handle MUC error messages
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Sat, 21 Nov 2009 18:23:41 +0100 |
parents | 23416c27b592 |
children | 970a6b4ac6ce |
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 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
18 import muc |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
19 xmpp = muc.xmpp |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
20 del muc |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
21 from participant import * |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
22 from encoding import * |
72
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
23 from irclib import ServerConnection |
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
|
24 import traceback |
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
|
25 import re |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
22
diff
changeset
|
26 import threading |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
27 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
28 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
29 class NoSuchParticipantException(Exception): pass |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
30 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
31 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
32 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
|
33 |
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 _all = 0 |
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
|
35 _info = 1 |
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
|
36 _notice = 2 |
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
|
37 _warning = 3 |
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
|
38 _error = 4 |
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
|
39 _nothing = 5 |
103 | 40 _say_levels = ['all', 'info', 'notice', 'warning', 'error', 'nothing'] |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
22
diff
changeset
|
41 _modes = ['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
|
42 |
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
|
43 |
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
|
44 def __init__(self, owner_bot, xmpp_room_jid, irc_room, irc_server, mode, say_level, irc_port=6667): |
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
|
45 """Create a new bridge.""" |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
46 self.bot = owner_bot |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
47 self.irc_server = irc_server |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
48 self.irc_port = irc_port |
45
41394ddb3aff
Lower before comparing strings.
Charly COSTE <changaco@changaco.net>
parents:
40
diff
changeset
|
49 self.irc_room = irc_room.lower() |
103 | 50 self.xmpp_room_jid = xmpp_room_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
|
51 if hasattr(self.__class__, '_'+say_level): |
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
|
52 self.say_level = getattr(self.__class__, '_'+say_level) |
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
|
53 else: |
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
|
54 raise Exception('[Error] "'+say_level+'" is not a correct value for a bridge\'s "say_level" attribute') |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
55 self.participants = [] |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
22
diff
changeset
|
56 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
|
57 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
|
58 self.mode = mode |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
59 |
40
faa468731d8a
Tried to get thread safety and handle disconnections in muc.py
Charly COSTE <changaco@changaco.net>
parents:
38
diff
changeset
|
60 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
|
61 |
103 | 62 self.init2() |
63 | |
64 | |
65 def init2(self): | |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
66 # Join XMPP room |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
67 try: |
103 | 68 self.xmpp_room = xmpp.muc(self.xmpp_room_jid) |
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
|
69 self.xmpp_room.join(self.bot.xmpp_c, self.bot.nickname, callback=self._xmpp_join_callback) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
70 except: |
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
|
71 self.bot.error('[Error] joining XMPP room failed') |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
72 raise |
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
|
73 |
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
|
74 # Join IRC room |
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
|
75 try: |
38
984e1e5c5e51
Initialize bridge.irc_connections_limit to -1.
Charly COSTE <changaco@changaco.net>
parents:
24
diff
changeset
|
76 self.irc_connections_limit = -1 |
103 | 77 self.irc_connection = self.bot.irc.server(self.irc_server, self.irc_port, 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
|
78 self.irc_connection.connect(nick_callback=self._irc_nick_callback) |
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
|
79 except: |
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
|
80 self.bot.error('[Error] joining IRC room failed') |
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
|
81 raise |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
22
diff
changeset
|
82 |
103 | 83 self.bot.error('[Notice] bridge "'+str(self)+'" is running in '+self.mode+' mode and a say_level of "'+self._say_levels[self.say_level]+'"') |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
84 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
85 |
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
|
86 def _irc_nick_callback(self, error, arguments=[]): |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
87 if error == None: |
85
dfa030c141f1
Fixed callbacks in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
84
diff
changeset
|
88 if self.mode == None: |
dfa030c141f1
Fixed callbacks in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
84
diff
changeset
|
89 return |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
90 self.irc_connection.join(self.irc_room) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
91 self.bot.error('===> Debug: successfully connected on IRC side of bridge "'+str(self)+'"', debug=True) |
18
3cdf7bb580da
Fixed bot.respond(), boosted XMPP thread to lower latency, fixed mode notice.
Charly COSTE <changaco@changaco.net>
parents:
17
diff
changeset
|
92 self.say('[Notice] bridge "'+str(self)+'" is running in '+self.mode+' mode', on_xmpp=False) |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
93 else: |
85
dfa030c141f1
Fixed callbacks in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
84
diff
changeset
|
94 self.mode = None |
101
29d3b85c6286
Minor fixes/improvements
Charly COSTE <changaco@changaco.net>
parents:
100
diff
changeset
|
95 if self.xmpp_room.connected == True: |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
96 self.say('[Error] failed to connect to the IRC chan, leaving ...', on_irc=False) |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
97 try: |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
98 if error == 'nicknameinuse': |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
99 raise Exception('[Error] "'+self.bot.nickname+'" is already used in the IRC chan or reserved on the IRC server of bridge "'+str(self)+'"') |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
100 elif error == 'nickcollision': |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
101 raise Exception('[Error] "'+self.bot.nickname+'" is already used or reserved on the IRC server of bridge "'+str(self)+'"') |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
102 elif error == 'erroneusnickname': |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
103 raise Exception('[Error] "'+self.bot.nickname+'" got "erroneusnickname" on bridge "'+str(self)+'"') |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
104 elif error == 'nicknametoolong': |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
105 raise Exception('[Error] "'+self.bot.nickname+'" got "nicknametoolong" on bridge "'+str(self)+'", limit seems to be '+str(arguments[0])) |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
106 else: |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
107 raise Exception('[Error] unknown error for "'+self.bot.nickname+'" on bridge "'+str(self)+'", limit seems to be '+str(arguments[0])) |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
108 except: |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
109 traceback.print_exc() |
86
bfa32b017fc9
First hack at an error notification system
Charly COSTE <changaco@changaco.net>
parents:
85
diff
changeset
|
110 self.bot.error('[Error] failed to connect to the IRC chan of bridge "'+str(self)+'", removing bridge', send_to_admins=True) |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
111 self.bot.removeBridge(self) |
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
|
112 |
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
|
113 |
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
|
114 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
|
115 """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
|
116 if len(errors) == 0: |
85
dfa030c141f1
Fixed callbacks in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
84
diff
changeset
|
117 if self.mode == None: |
dfa030c141f1
Fixed callbacks in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
84
diff
changeset
|
118 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
|
119 self.bot.error('===> Debug: succesfully connected on XMPP side of bridge "'+str(self)+'"', debug=True) |
18
3cdf7bb580da
Fixed bot.respond(), boosted XMPP thread to lower latency, fixed mode notice.
Charly COSTE <changaco@changaco.net>
parents:
17
diff
changeset
|
120 self.say('[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
|
121 else: |
85
dfa030c141f1
Fixed callbacks in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
84
diff
changeset
|
122 self.mode = None |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
123 if self.irc_connection.really_connected == True: |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
124 self.say('[Error] failed to connect to the XMPP room, leaving ...', on_xmpp=False) |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
125 for error in errors: |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
126 try: |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
127 raise error |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
128 except: |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
129 traceback.print_exc() |
86
bfa32b017fc9
First hack at an error notification system
Charly COSTE <changaco@changaco.net>
parents:
85
diff
changeset
|
130 self.bot.error('[Error] failed to connect to the XMPP room of bridge "'+str(self)+'", removing bridge', send_to_admins=True) |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
131 self.bot.removeBridge(self) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
132 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
133 |
103 | 134 def addParticipant(self, from_protocol, nickname, real_jid=None): |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
135 """Add a participant to the bridge.""" |
72
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
136 if (from_protocol == 'irc' and nickname == self.irc_connection.get_nickname()) or (from_protocol == 'xmpp' and nickname == self.xmpp_room.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
|
137 self.bot.error('===> Debug: not adding self ('+self.bot.nickname+') to bridge "'+str(self)+'"', debug=True) |
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
|
138 return |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
139 try: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
140 p = self.getParticipant(nickname) |
72
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
141 if p.protocol != from_protocol: |
97
c71861491968
bridge.addParticipant improvements
Charly COSTE <changaco@changaco.net>
parents:
86
diff
changeset
|
142 if from_protocol == 'irc' and isinstance(p.irc_connection, ServerConnection) and p.irc_connection.really_connected == True or from_protocol == 'xmpp' and isinstance(p.xmpp_c, xmpp.client.Client) and isinstance(p.muc, xmpp.muc): |
72
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
143 return |
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
144 self.bot.error('===> Debug: "'+nickname+'" is on both sides of bridge "'+str(self)+'"', debug=True) |
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
145 self.say('[Warning] The nickname "'+nickname+'" is used on both sides of the bridge, please avoid that if possible') |
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
146 if isinstance(p.irc_connection, ServerConnection): |
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
147 p.irc_connection.close('') |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
148 if p.irc_connection != 'both': |
72
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
149 p.irc_connection = 'both' |
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
150 if isinstance(p.muc, xmpp.muc): |
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
151 p.muc.leave('') |
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
152 self.bot.close_xmpp_connection(p.nickname) |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
153 if p.xmpp_c != 'both': |
72
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
154 p.xmpp_c = 'both' |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
155 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
156 except NoSuchParticipantException: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
157 pass |
97
c71861491968
bridge.addParticipant improvements
Charly COSTE <changaco@changaco.net>
parents:
86
diff
changeset
|
158 self.lock.acquire() |
72
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
159 self.bot.error('===> Debug: 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
|
160 try: |
103 | 161 p = participant(self, from_protocol, nickname, real_jid=real_jid) |
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
|
162 except IOError: |
72
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
163 self.bot.error('===> Debug: IOError while adding participant "'+nickname+'" from "'+from_protocol+'" to bridge "'+str(self)+'", reconnectiong ...', 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
|
164 p.xmpp_c.reconnectAndReauth() |
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
|
165 except: |
72
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
166 self.bot.error('===> Debug: unknown error while 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
|
167 traceback.print_exc() |
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
|
168 return |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
169 self.participants.append(p) |
97
c71861491968
bridge.addParticipant improvements
Charly COSTE <changaco@changaco.net>
parents:
86
diff
changeset
|
170 self.lock.release() |
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
|
171 if self.mode != 'normal': |
b3eba9489329
In minimal mode, display the participants' list on XMPP when someone joins on IRC
Charly COSTE <changaco@changaco.net>
parents:
101
diff
changeset
|
172 if from_protocol == 'xmpp': |
b3eba9489329
In minimal mode, display the participants' list on XMPP when someone joins on IRC
Charly COSTE <changaco@changaco.net>
parents:
101
diff
changeset
|
173 xmpp_participants_nicknames = self.get_participants_nicknames_list(protocols=['xmpp']) |
b3eba9489329
In minimal mode, display the participants' list on XMPP when someone joins on IRC
Charly COSTE <changaco@changaco.net>
parents:
101
diff
changeset
|
174 self.say('[Info] Participants on XMPP: '+' '.join(xmpp_participants_nicknames), on_xmpp=False) |
b3eba9489329
In minimal mode, display the participants' list on XMPP when someone joins on IRC
Charly COSTE <changaco@changaco.net>
parents:
101
diff
changeset
|
175 elif self.mode == 'minimal' and from_protocol == 'irc': |
b3eba9489329
In minimal mode, display the participants' list on XMPP when someone joins on IRC
Charly COSTE <changaco@changaco.net>
parents:
101
diff
changeset
|
176 irc_participants_nicknames = self.get_participants_nicknames_list(protocols=['irc']) |
b3eba9489329
In minimal mode, display the participants' list on XMPP when someone joins on IRC
Charly COSTE <changaco@changaco.net>
parents:
101
diff
changeset
|
177 self.say('[Info] Participants on IRC: '+' '.join(irc_participants_nicknames), on_irc=False) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
178 return p |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
179 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
180 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
181 def getParticipant(self, nickname): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
182 """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
|
183 self.lock.acquire() |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
184 for participant_ in self.participants: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
185 if participant_.nickname == nickname: |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
22
diff
changeset
|
186 self.lock.release() |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
187 return participant_ |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
22
diff
changeset
|
188 self.lock.release() |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
189 raise NoSuchParticipantException('there is no participant using the nickname "'+nickname+'" in this bridge') |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
190 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
191 |
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
|
192 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
|
193 """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
|
194 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
|
195 participants_nicknames = [] |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
196 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
|
197 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
|
198 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
|
199 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
|
200 return participants_nicknames |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
201 |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
202 |
62
61491895c607
Added bridge.hasParticipant() and fixed participant.changeNickname()
Charly COSTE <changaco@changaco.net>
parents:
60
diff
changeset
|
203 def hasParticipant(self, nickname): |
61491895c607
Added bridge.hasParticipant() and fixed participant.changeNickname()
Charly COSTE <changaco@changaco.net>
parents:
60
diff
changeset
|
204 try: |
61491895c607
Added bridge.hasParticipant() and fixed participant.changeNickname()
Charly COSTE <changaco@changaco.net>
parents:
60
diff
changeset
|
205 self.getParticipant(nickname) |
61491895c607
Added bridge.hasParticipant() and fixed participant.changeNickname()
Charly COSTE <changaco@changaco.net>
parents:
60
diff
changeset
|
206 return True |
61491895c607
Added bridge.hasParticipant() and fixed participant.changeNickname()
Charly COSTE <changaco@changaco.net>
parents:
60
diff
changeset
|
207 except NoSuchParticipantException: |
61491895c607
Added bridge.hasParticipant() and fixed participant.changeNickname()
Charly COSTE <changaco@changaco.net>
parents:
60
diff
changeset
|
208 return False |
61491895c607
Added bridge.hasParticipant() and fixed participant.changeNickname()
Charly COSTE <changaco@changaco.net>
parents:
60
diff
changeset
|
209 |
61491895c607
Added bridge.hasParticipant() and fixed participant.changeNickname()
Charly COSTE <changaco@changaco.net>
parents:
60
diff
changeset
|
210 |
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
|
211 def removeParticipant(self, left_protocol, nickname, leave_message): |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
212 """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
|
213 |
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
|
214 was_on_both = None |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
215 p = self.getParticipant(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
|
216 if p.protocol == 'xmpp': |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
217 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
|
218 was_on_both = True |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
219 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
|
220 p.protocol = 'irc' |
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
|
221 p.createDuplicateOnXMPP() |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
222 elif left_protocol == 'irc': |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
223 p.createDuplicateOnIRC() |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
224 else: |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
225 if left_protocol == 'xmpp': |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
226 was_on_both = False |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
227 elif left_protocol == 'irc': |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
228 try: |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
229 p.irc_connection.join(self.irc_room) |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
230 except: |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
231 p._close_irc_connection() |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
232 p.createDuplicateOnIRC() |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
233 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
|
234 |
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
|
235 elif p.protocol == 'irc': |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
236 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
|
237 was_on_both = True |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
238 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
|
239 p.protocol = '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
|
240 p.createDuplicateOnIRC() |
84
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
241 elif left_protocol == 'xmpp': |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
242 p.createDuplicateOnXMPP() |
844ccdcf66be
Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py
Charly COSTE <changaco@changaco.net>
parents:
72
diff
changeset
|
243 else: |
100
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
244 if left_protocol == 'irc': |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
245 was_on_both = False |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
246 elif left_protocol == 'xmpp': |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
247 if isinstance(p.xmpp_c, xmpp.client.Client): |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
248 self.bot.reopen_xmpp_connection(p.xmpp_c) |
6289ac5a2db7
Reconnect when a connection is closed
Charly COSTE <changaco@changaco.net>
parents:
97
diff
changeset
|
249 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
|
250 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
251 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
|
252 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
|
253 |
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
|
254 if was_on_both == True: |
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 self.bot.error('===> Debug: "'+nickname+'" was on both sides of bridge "'+str(self)+'" but left '+left_protocol, debug=True) |
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 |
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
|
257 elif was_on_both == False: |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
22
diff
changeset
|
258 self.lock.acquire() |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
259 self.bot.error('===> Debug: removing participant "'+nickname+'" from bridge "'+str(self)+'"', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
260 self.participants.remove(p) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
261 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
|
262 del p |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
22
diff
changeset
|
263 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
|
264 if left_protocol == 'xmpp': |
86
bfa32b017fc9
First hack at an error notification system
Charly COSTE <changaco@changaco.net>
parents:
85
diff
changeset
|
265 xmpp_participants_nicknames = self.get_participants_nicknames_list(protocols=['xmpp']) |
bfa32b017fc9
First hack at an error notification system
Charly COSTE <changaco@changaco.net>
parents:
85
diff
changeset
|
266 if self.irc_connections_limit != -1 and self.irc_connections_limit > len(xmpp_participants_nicknames): |
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
|
267 self.switchFromLimitedToNormalMode() |
24
4e1f27ea527b
First hack at locks for thread safety. Some other minor changes.
Charly COSTE <changaco@changaco.net>
parents:
22
diff
changeset
|
268 if self.mode != 'normal': |
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
|
269 self.say('[Info] Participants on XMPP: '+' '.join(xmpp_participants_nicknames), on_xmpp=False) |
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
|
270 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
|
271 if self.mode == 'minimal': |
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
|
272 irc_participants_nicknames = self.get_participants_nicknames_list(protocols=['irc']) |
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
|
273 self.say('[Info] Participants on IRC: '+' '.join(irc_participants_nicknames), on_irc=False) |
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
|
274 |
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
|
275 else: |
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 self.bot.error('=> 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), debug=True) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
277 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
278 |
103 | 279 def restart(self): |
280 """Restart the bridge""" | |
281 | |
105
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
282 # Tell admins |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
283 self.bot.error('Restarting bridge '+str(self), send_to_admins=True) |
d8acff763731
Handle MUC error messages
Charly COSTE <changaco@changaco.net>
parents:
103
diff
changeset
|
284 |
103 | 285 # Stop the bridge |
286 self.stop(message='Restarting bridge') | |
287 | |
288 # Recreate the bridge | |
289 self.init2() | |
290 | |
291 | |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
292 def say(self, message, on_irc=True, on_xmpp=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
|
293 """Make the bot say something.""" |
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
|
294 if message[0] != '[': |
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
|
295 raise Exception('[Internal Error] message does not start with "["') |
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
|
296 if self.say_level == self.__class__._nothing: |
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
|
297 return |
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
|
298 level = re.findall('^\[(Info|Notice|Warning|Error)\]', message) |
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
|
299 if len(level) == 0: |
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
|
300 raise Exception('[Internal Error] unknown message importance "'+re.findall('^\[([^[\]]+)', message)[0]+'"') |
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
|
301 level = level[0].lower() |
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
|
302 if getattr(self.__class__, '_'+level) < self.say_level: |
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
|
303 return |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
304 if on_xmpp == True: |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
305 self.xmpp_room.say(message) |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
306 if on_irc == True: |
56 | 307 self.irc_connection.privmsg(self.irc_room, message) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
308 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
309 |
103 | 310 def stop(self, message='Stopping bridge'): |
311 """Stop the bridge""" | |
312 | |
313 # Close IRC connection if not used by an other bridge, just leave the room otherwise | |
314 self.irc_connection.used_by -= 1 | |
315 if self.irc_connection.used_by < 1: | |
316 self.irc_connection.close(message) | |
317 else: | |
318 self.irc_connection.part(self.irc_room, message=message) | |
319 | |
320 # Leave the MUC | |
321 self.xmpp_room.leave(message=message) | |
322 self.xmpp_room.__del__() | |
323 del self.xmpp_room | |
324 | |
325 # Delete participants objects | |
326 for p in self.participants: | |
327 p.leave(message) | |
328 del p | |
329 self.participants = [] | |
330 | |
331 | |
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
|
332 def switchFromLimitedToNormalMode(self): |
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
|
333 if self.mode != 'normal-limited': |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
334 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
|
335 self.bot.error('===> Bridge is switching to normal mode.') |
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
|
336 self.say('[Notice] Bridge is switching to normal mode.') |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
337 self.mode = 'normal' |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
338 for p in self.participants: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
339 if p.protocol == 'xmpp': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
340 p.createDuplicateOnIRC() |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
341 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
342 |
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
|
343 def switchFromNormalToLimitedMode(self): |
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
|
344 if self.mode != 'normal': |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
345 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
|
346 self.mode = 'normal-limited' |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
347 i = 0 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
348 for p in self.participants: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
349 if p.protocol == 'xmpp': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
350 i += 1 |
72
6c4b841144f6
Better handling of participants
Charly COSTE <changaco@changaco.net>
parents:
64
diff
changeset
|
351 if isinstance(self.irc_connection, ServerConnection): |
64
8fc496eaa17b
Handle IRC "nickcollision" event
Charly COSTE <changaco@changaco.net>
parents:
63
diff
changeset
|
352 p.irc_connection.close('Bridge is switching to limited mode') |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
353 p.irc_connection = None |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
354 self.irc_connections_limit = i |
12
fd695e2b5283
Fixed bridge mode-switching
Charly COSTE <changaco@changaco.net>
parents:
11
diff
changeset
|
355 self.bot.error('===> Bridge is switching to limited mode. Limit seems to be '+str(self.irc_connections_limit)+' on "'+self.irc_server+'".') |
fd695e2b5283
Fixed bridge mode-switching
Charly COSTE <changaco@changaco.net>
parents:
11
diff
changeset
|
356 self.say('[Warning] Bridge is switching to limited mode, it means that it will be transparent for XMPP users but not for IRC users, this is due to the IRC servers\' per-IP-address connections\' limit number which seems to be '+str(self.irc_connections_limit)+' on "'+self.irc_server+'".') |
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
|
357 xmpp_participants_nicknames = self.get_participants_nicknames_list(protocols=['xmpp']) |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
358 self.say('[Info] Participants on XMPP: '+' '.join(xmpp_participants_nicknames), on_xmpp=False) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
359 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
360 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
361 def __str__(self): |
103 | 362 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
|
363 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
364 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
365 def __del__(self): |
103 | 366 self.stop(message='Removing bridge') |