comparison bridge.py @ 21:801160b4136f

Introduced a bridge's "say" attribute to offer more flexibility in what the bot says. Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Thu, 20 Aug 2009 15:17:20 +0200
parents c1b84196c100
children e2bd4de698e5
comparison
equal deleted inserted replaced
20:08cde283621a 21:801160b4136f
18 import muc 18 import muc
19 xmpp = muc.xmpp 19 xmpp = muc.xmpp
20 del muc 20 del muc
21 from participant import * 21 from participant import *
22 from encoding import * 22 from encoding import *
23 import traceback
24 import re
23 25
24 26
25 class NoSuchParticipantException(Exception): pass 27 class NoSuchParticipantException(Exception): pass
26 28
27 29
28 class bridge: 30 class bridge:
29 def __init__(self, owner_bot, xmpp_room_jid, irc_room, irc_server, mode, say_participants_list, irc_port=6667): 31
32 _all = 0
33 _info = 1
34 _notice = 2
35 _warning = 3
36 _error = 4
37 _nothing = 5
38
39
40 def __init__(self, owner_bot, xmpp_room_jid, irc_room, irc_server, mode, say_level, irc_port=6667):
30 """Create a new bridge.""" 41 """Create a new bridge."""
31 self.bot = owner_bot 42 self.bot = owner_bot
32 self.irc_server = irc_server 43 self.irc_server = irc_server
33 self.irc_port = irc_port 44 self.irc_port = irc_port
34 self.irc_room = irc_room 45 self.irc_room = irc_room
35 self.say_participants_list = say_participants_list 46 if hasattr(self.__class__, '_'+say_level):
47 self.say_level = getattr(self.__class__, '_'+say_level)
48 else:
49 raise Exception('[Error] "'+say_level+'" is not a correct value for a bridge\'s "say_level" attribute')
36 self.participants = [] 50 self.participants = []
37 if mode not in ['normal', 'limited', 'minimal']: 51 if mode not in ['normal', 'limited', 'minimal']:
38 raise Exception('Error: "'+mode+'" is not a correct value for a bridge\'s "mode" attribute') 52 raise Exception('[Error] "'+mode+'" is not a correct value for a bridge\'s "mode" attribute')
39 self.mode = mode 53 self.mode = mode
40 54
41 # Join XMPP room 55 # Join XMPP room
42 try: 56 try:
43 self.xmpp_room = xmpp.muc(xmpp_room_jid) 57 self.xmpp_room = xmpp.muc(xmpp_room_jid)
44 self.xmpp_room.join(self.bot.xmpp_c, self.bot.nickname, callback=self._xmpp_join_callback) 58 self.xmpp_room.join(self.bot.xmpp_c, self.bot.nickname, callback=self._xmpp_join_callback)
45 except: 59 except:
46 self.bot.error('Error: joining XMPP room failed') 60 self.bot.error('[Error] joining XMPP room failed')
47 raise 61 raise
48 62
49 # Join IRC room 63 # Join IRC room
50 try: 64 try:
51 self.irc_connection = self.bot.irc.server(irc_server, irc_port, self.bot.nickname) 65 self.irc_connection = self.bot.irc.server(irc_server, irc_port, self.bot.nickname)
52 self.irc_connection.connect(nick_callback=self._irc_nick_callback) 66 self.irc_connection.connect(nick_callback=self._irc_nick_callback)
53 except: 67 except:
54 self.bot.error('Error: joining IRC room failed') 68 self.bot.error('[Error] joining IRC room failed')
55 raise 69 raise
56 self.bot.error('[Notice] bridge "'+str(self)+'" is running in '+self.mode+' mode') 70 self.bot.error('[Notice] bridge "'+str(self)+'" is running in '+self.mode+' mode')
57 71
58 72
59 def _irc_nick_callback(self, error, arguments=[]): 73 def _irc_nick_callback(self, error, arguments=[]):
101 raise Exception('[Internal Error] bad protocol') 115 raise Exception('[Internal Error] bad protocol')
102 return 116 return
103 except NoSuchParticipantException: 117 except NoSuchParticipantException:
104 pass 118 pass
105 self.bot.error('===> Debug: adding participant "'+nickname+'" from "'+protocol+'" to bridge "'+str(self)+'"', debug=True) 119 self.bot.error('===> Debug: adding participant "'+nickname+'" from "'+protocol+'" to bridge "'+str(self)+'"', debug=True)
106 p = participant(self, protocol, nickname) 120 try:
121 p = participant(self, protocol, nickname)
122 except:
123 self.bot.error('===> Debug: unknown error while adding participant "'+nickname+'" from "'+protocol+'" to bridge "'+str(self)+'"', debug=True)
124 traceback.print_exc()
125 return
107 self.participants.append(p) 126 self.participants.append(p)
108 if self.mode != 'normal' and protocol == 'xmpp': 127 if self.mode != 'normal' and protocol == 'xmpp':
109 xmpp_participants_nicknames = self.get_participants_nicknames_list(protocols=['xmpp']) 128 xmpp_participants_nicknames = self.get_participants_nicknames_list(protocols=['xmpp'])
110 self.say('[Info] Participants on XMPP: '+' '.join(xmpp_participants_nicknames), on_xmpp=False) 129 self.say('[Info] Participants on XMPP: '+' '.join(xmpp_participants_nicknames), on_xmpp=False)
111 return p 130 return p
185 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) 204 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)
186 205
187 206
188 def say(self, message, on_irc=True, on_xmpp=True): 207 def say(self, message, on_irc=True, on_xmpp=True):
189 """Make the bot say something.""" 208 """Make the bot say something."""
209 if message[0] != '[':
210 raise Exception('[Internal Error] message does not start with "["')
211 if self.say_level == self.__class__._nothing:
212 return
213 level = re.findall('^\[(Info|Notice|Warning|Error)\]', message)
214 if len(level) == 0:
215 raise Exception('[Internal Error] unknown message importance "'+re.findall('^\[([^[\]]+)', message)[0]+'"')
216 level = level[0].lower()
217 if getattr(self.__class__, '_'+level) < self.say_level:
218 return
190 if on_xmpp == True: 219 if on_xmpp == True:
191 self.xmpp_room.say(message) 220 self.xmpp_room.say(message)
192 if on_irc == True: 221 if on_irc == True:
193 self.irc_connection.privmsg(self.irc_room, auto_encode(message)) 222 self.irc_connection.privmsg(self.irc_room, auto_encode(message))
194 223