comparison bridge.py @ 124:99f3dee1fad7

code cleaning Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Wed, 13 Jan 2010 23:08:44 +0100
parents 75a03f10a863
children efdc038e757a
comparison
equal deleted inserted replaced
123:75a03f10a863 124:99f3dee1fad7
13 # 13 #
14 # You should have received a copy of the GNU General Public License 14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>. 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16
17 17
18 import re
19 import threading
20 import traceback
21
22 from encoding import *
23 from irclib import ServerConnection
18 import muc 24 import muc
19 xmpp = muc.xmpp 25 xmpp = muc.xmpp
20 del muc 26 del muc
21 from participant import * 27
22 from encoding import * 28 from participant import Participant
23 from irclib import ServerConnection 29
24 import traceback 30
25 import re 31 class Bridge:
26 import threading
27
28
29 class NoSuchParticipantException(Exception): pass
30
31
32 class bridge:
33 32
34 _all = 0 33 _all = 0
35 _info = 1 34 _info = 1
36 _notice = 2 35 _notice = 2
37 _warning = 3 36 _warning = 3
38 _error = 4 37 _error = 4
39 _nothing = 5 38 _nothing = 5
40 _say_levels = ['all', 'info', 'notice', 'warning', 'error', 'nothing'] 39 _say_levels = ['all', 'info', 'notice', 'warning', 'error', 'nothing']
41 _modes = ['normal', 'bypass', 'limited', 'minimal'] 40 _modes = ['normal', 'bypass', 'limited', 'minimal']
41
42
43 class NoSuchParticipantException(Exception): pass
42 44
43 45
44 def __init__(self, owner_bot, xmpp_room_jid, irc_room, irc_server, mode, say_level, irc_port=6667): 46 def __init__(self, owner_bot, xmpp_room_jid, irc_room, irc_server, mode, say_level, irc_port=6667):
45 """Create a new bridge.""" 47 """Create a new bridge."""
46 self.bot = owner_bot 48 self.bot = owner_bot
145 if irc_id: 147 if irc_id:
146 p.irc_connection.irc_id = irc_id 148 p.irc_connection.irc_id = irc_id
147 return p 149 return p
148 p.set_both_sides() 150 p.set_both_sides()
149 return p 151 return p
150 except NoSuchParticipantException: 152 except self.NoSuchParticipantException:
151 pass 153 pass
152 154
153 if nickname == 'ChanServ' and from_protocol == 'irc': 155 if nickname == 'ChanServ' and from_protocol == 'irc':
154 return 156 return
155 157
156 self.lock.acquire() 158 self.lock.acquire()
157 self.bot.error('===> Debug: adding participant "'+nickname+'" from "'+from_protocol+'" to bridge "'+str(self)+'"', debug=True) 159 self.bot.error('===> Debug: adding participant "'+nickname+'" from "'+from_protocol+'" to bridge "'+str(self)+'"', debug=True)
158 try: 160 try:
159 p = participant(self, from_protocol, nickname, real_jid=real_jid) 161 p = Participant(self, from_protocol, nickname, real_jid=real_jid)
160 except IOError: 162 except IOError:
161 self.bot.error('===> Debug: IOError while adding participant "'+nickname+'" from "'+from_protocol+'" to bridge "'+str(self)+'", reconnectiong ...', debug=True) 163 self.bot.error('===> Debug: IOError while adding participant "'+nickname+'" from "'+from_protocol+'" to bridge "'+str(self)+'", reconnectiong ...', debug=True)
162 p.xmpp_c.reconnectAndReauth() 164 p.xmpp_c.reconnectAndReauth()
163 except: 165 except:
164 self.bot.error('===> Debug: unknown error while adding participant "'+nickname+'" from "'+from_protocol+'" to bridge "'+str(self)+'"', debug=True) 166 self.bot.error('===> Debug: unknown error while adding participant "'+nickname+'" from "'+from_protocol+'" to bridge "'+str(self)+'"', debug=True)
240 for p in self.participants: 242 for p in self.participants:
241 if nickname in [p.nickname, p.duplicate_nickname]: 243 if nickname in [p.nickname, p.duplicate_nickname]:
242 self.lock.release() 244 self.lock.release()
243 return p 245 return p
244 self.lock.release() 246 self.lock.release()
245 raise NoSuchParticipantException('there is no participant using the nickname "'+nickname+'" in this bridge') 247 raise self.NoSuchParticipantException('there is no participant using the nickname "'+nickname+'" in this bridge')
246 248
247 249
248 def get_participants_nicknames_list(self, protocols=['irc', 'xmpp']): 250 def get_participants_nicknames_list(self, protocols=['irc', 'xmpp']):
249 """Returns a list of the nicknames of the bridge's participants that are connected on the XMPP side.""" 251 """Returns a list of the nicknames of the bridge's participants that are connected on the XMPP side."""
250 self.lock.acquire() 252 self.lock.acquire()
258 260
259 def hasParticipant(self, nickname): 261 def hasParticipant(self, nickname):
260 try: 262 try:
261 self.getParticipant(nickname) 263 self.getParticipant(nickname)
262 return True 264 return True
263 except NoSuchParticipantException: 265 except self.NoSuchParticipantException:
264 return False 266 return False
265 267
266 268
267 def removeParticipant(self, left_protocol, nickname, leave_message): 269 def removeParticipant(self, left_protocol, nickname, leave_message):
268 """Remove the participant using nickname from the bridge. Raises a NoSuchParticipantException if nickname is not used in the bridge.""" 270 """Remove the participant using nickname from the bridge. Raises a NoSuchParticipantException if nickname is not used in the bridge."""