Mercurial > xib
annotate bot.py @ 11:79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Mon, 17 Aug 2009 00:30:37 +0200 |
parents | 7cb790f5f243 |
children | 1a1f2a0d35c7 |
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 # *** CONTRIBUTORS *** |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
19 # Contributor: Changaco <changaco@changaco.net> |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
20 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
21 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
22 # *** Changelog *** |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
23 # 0.1: First release |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
24 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
25 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
26 # *** Versioning *** |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
27 # Major will pass to 1 when xib will be considered fault-tolerant |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
28 # After that major will only be changed if the new version is not retro-compatible (e.g. requires changes in config file) |
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 version = 0, 1 |
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 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
33 import irclib |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
34 import xmppony as xmpp |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
35 from threading import Thread |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
36 from bridge import * |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
37 from time import sleep |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
38 import re |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
39 import sys |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
40 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
41 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
42 class bot(Thread): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
43 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
44 def __init__(self, jid, password, nickname, error_fd=sys.stderr, debug=False): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
45 Thread.__init__(self) |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
46 self.commands = ['!xmpp_participants'] |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
47 self.jid = xmpp.protocol.JID(jid=jid) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
48 self.nickname = nickname |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
49 self.password = password |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
50 self.error_fd = error_fd |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
51 self.debug = debug |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
52 self.bridges = [] |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
53 self.irc = irclib.IRC() |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
54 self.irc.add_global_handler('all_events', self._irc_event_handler) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
55 self.irc_thread = Thread(target=self.irc.process_forever) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
56 self.irc_thread.start() |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
57 # Open connection with XMPP server |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
58 try: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
59 self.xmpp_c = xmpp.client.Client(self.jid.getDomain(), debug=[]) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
60 self.xmpp_c.connect() |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
61 if self.jid.getResource() == '': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
62 self.jid.setResource('xib-bot') |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
63 self.xmpp_c.auth(self.jid.getNode(), password, resource=self.jid.getResource()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
64 self.xmpp_c.RegisterHandler('presence', self._xmpp_presence_handler) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
65 self.xmpp_c.RegisterHandler('iq', self._xmpp_iq_handler) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
66 self.xmpp_c.RegisterHandler('message', self._xmpp_message_handler) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
67 self.xmpp_c.sendInitPresence() |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
68 except: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
69 self.error('Error: XMPP Connection failed') |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
70 raise |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
71 self.xmpp_thread = Thread(target=self._xmpp_loop) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
72 self.xmpp_thread.start() |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
73 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
74 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
75 def error(self, s, debug=False): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
76 if not debug or debug and self.debug: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
77 try: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
78 self.error_fd.write(auto_encode(s)+"\n") |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
79 except EncodingException: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
80 self.error_fd.write('Error message cannot be transcoded.\n') |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
81 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
82 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
83 def _xmpp_loop(self): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
84 while True: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
85 self.xmpp_c.Process(5) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
86 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
87 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
88 def _xmpp_presence_handler(self, xmpp_c, presence): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
89 """[Internal] Manage XMPP presence.""" |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
90 self.error('==> Debug: Received XMPP presence.', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
91 self.error(presence.__str__(fancy=1), debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
92 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
93 if presence.getTo() != self.jid: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
94 #self.error('=> Debug: Skipping XMPP presence not received on bot connection.', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
95 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
96 from_ = xmpp.protocol.JID(presence.getFrom()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
97 bare_jid = unicode(from_.getNode()+'@'+from_.getDomain()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
98 for bridge in self.bridges: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
99 if bare_jid == bridge.xmpp_room.room_jid: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
100 # presence comes from a muc |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
101 resource = unicode(from_.getResource()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
102 if resource == '': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
103 # presence comes from the muc itself |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
104 # TODO: handle room deletion and muc server reboot |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
105 pass |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
106 else: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
107 # presence comes from a participant of the muc |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
108 try: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
109 p = bridge.getParticipant(resource) |
3
3f651f4fdb4f
Added IRC "part" event handling
Charly COSTE <changaco@changaco.net>
parents:
2
diff
changeset
|
110 if p.protocol in ['xmpp', 'both']: |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
111 if presence.getType() == 'unavailable': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
112 x = presence.getTag('x', namespace='http://jabber.org/protocol/muc#user') |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
113 if x and x.getTag('status', attrs={'code': '303'}): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
114 # participant changed its nickname |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
115 item = x.getTag('item') |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
116 if not item: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
117 self.error('Debug: bad stanza, no item element', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
118 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
119 new_nick = item.getAttr('nick') |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
120 if not new_nick: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
121 self.error('Debug: bad stanza, new nick is not given', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
122 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
123 p.changeNickname(new_nick, 'irc') |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
124 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
125 # participant left |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
126 bridge.removeParticipant('xmpp', resource, presence.getStatus()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
127 except NoSuchParticipantException: |
3
3f651f4fdb4f
Added IRC "part" event handling
Charly COSTE <changaco@changaco.net>
parents:
2
diff
changeset
|
128 if presence.getType() != 'unavailable': |
3f651f4fdb4f
Added IRC "part" event handling
Charly COSTE <changaco@changaco.net>
parents:
2
diff
changeset
|
129 try: |
3f651f4fdb4f
Added IRC "part" event handling
Charly COSTE <changaco@changaco.net>
parents:
2
diff
changeset
|
130 bridge.addParticipant('xmpp', resource) |
3f651f4fdb4f
Added IRC "part" event handling
Charly COSTE <changaco@changaco.net>
parents:
2
diff
changeset
|
131 except Exception: |
3f651f4fdb4f
Added IRC "part" event handling
Charly COSTE <changaco@changaco.net>
parents:
2
diff
changeset
|
132 pass |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
133 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
134 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
135 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
136 def _xmpp_iq_handler(self, xmpp_c, iq): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
137 """[Internal] Manage XMPP IQs.""" |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
138 self.error('=> Debug: Received XMPP iq.', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
139 self.error(iq.__str__(fancy=1), debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
140 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
141 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
142 def _xmpp_message_handler(self, xmpp_c, message): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
143 """[Internal] Manage XMPP messages.""" |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
144 if message.getType() == 'chat': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
145 self.error('==> Debug: Received XMPP message.', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
146 self.error(message.__str__(fancy=1), debug=True) |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
147 from_bare_jid = unicode(message.getFrom().getNode()+'@'+message.getFrom().getDomain()) |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
148 for bridge in self.bridges: |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
149 if from_bare_jid == bridge.xmpp_room.room_jid: |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
150 # message comes from a room participant |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
151 try: |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
152 from_ = bridge.getParticipant(message.getFrom().getResource()) |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
153 to_ = bridge.getParticipant(message.getTo().getResource()) |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
154 except NoSuchParticipantException: |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
155 if message.getTo() == self.jid: |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
156 xmpp_c.send(xmpp.protocol.Message(to=message.getFrom(), body=self.respond(message.getBody(), from_), typ='chat')) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
157 return |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
158 self.error('==> Debug: XMPP chat message not relayed, from_bare_jid='+from_bare_jid+' to='+str(message.getTo().getResource())+' from='+message.getFrom().getResource(), debug=True) |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
159 return |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
160 if from_.protocol in ['xmpp', 'both']: |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
161 from_.sayOnIRCTo(to_.nickname, message.getBody()) |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
162 else: |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
163 self.error('==> Debug: received XMPP chat message from a non-XMPP participant, WTF ?', debug=True) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
164 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
165 elif message.getType() == 'groupchat': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
166 # message comes from a room |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
167 if message.getTo() != self.jid: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
168 self.error('=> Debug: Skipping XMPP MUC message not received on bot connection.', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
169 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
170 for child in message.getChildren(): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
171 if child.getName() == 'delay': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
172 self.error('=> Debug: Skipping XMPP MUC delayed message.', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
173 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
174 self.error('==> Debug: Received XMPP message.', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
175 self.error(message.__str__(fancy=1), debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
176 from_ = xmpp.protocol.JID(message.getFrom()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
177 room_jid = unicode(from_.getNode()+'@'+from_.getDomain()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
178 for bridge in self.bridges: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
179 if room_jid == bridge.xmpp_room.room_jid: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
180 resource = unicode(from_.getResource()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
181 if resource == '': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
182 # message comes from the room itself |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
183 pass |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
184 else: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
185 # message comes from a participant of the room |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
186 try: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
187 participant_ = bridge.getParticipant(resource) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
188 except NoSuchParticipantException: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
189 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
190 if participant_.protocol == 'xmpp': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
191 participant_.sayOnIRC(message.getBody()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
192 elif participant_.protocol == 'both': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
193 bridge.irc_connection.privmsg(bridge.irc_room, '<'+participant_.nickname+'> '+message.getBody()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
194 else: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
195 self.error('==> Debug: Received XMPP message.', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
196 self.error(message.__str__(fancy=1), debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
197 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
198 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
199 def _irc_event_handler(self, connection, event): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
200 """[internal] Manage IRC events""" |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
201 if not connection.bridge in self.bridges: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
202 # Not for us, ignore |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
203 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
204 if 'all' in event.eventtype(): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
205 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
206 if 'motd' in event.eventtype(): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
207 self.error('=> Debug: ignoring event containing "motd" in the eventtype ('+event.eventtype()+')', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
208 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
209 if event.eventtype() in ['pong', 'welcome', 'yourhost', 'created', 'myinfo', 'featurelist', 'luserclient', 'luserop', 'luserchannels', 'luserme', 'n_local', 'n_global', 'endofnames', 'luserunknown']: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
210 self.error('=> Debug: ignoring '+event.eventtype(), debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
211 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
212 if event.eventtype() == 'pubmsg' and connection.get_nickname() != connection.bridge.irc_connection.get_nickname(): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
213 self.error('=> Debug: ignoring IRC pubmsg not received on bridge connection', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
214 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
215 if event.eventtype() == 'ping': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
216 connection.pong(connection.get_server_name()) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
217 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
218 self.error('==> Debug: Received IRC event.', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
219 self.error('server='+connection.get_server_name(), debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
220 self.error('eventtype='+event.eventtype(), debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
221 self.error('source='+str(event.source()), debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
222 self.error('target='+str(event.target()), debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
223 self.error('arguments='+str(event.arguments()), debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
224 if event.eventtype() == 'disconnect': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
225 if connection.get_nickname() == connection.bridge.irc_connection.get_nickname(): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
226 # Lost bridge IRC connection, we must reconnect if we want the bridge to work |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
227 self.recreate_bridge(connection.bridge) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
228 return |
5
cb0daec4b778
Added support for IRC "nick" event, fixed participant.changeNickname(), fixed handling of IRC "namreply" event, removed muc._check() because waiting does not solve the problem if it is blocking incoming messages handling
Charly COSTE <changaco@changaco.net>
parents:
3
diff
changeset
|
229 if connection.bridge.mode == 'normal' and connection.closing == False: |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
230 connection.bridge.switchToLimitedMode() |
5
cb0daec4b778
Added support for IRC "nick" event, fixed participant.changeNickname(), fixed handling of IRC "namreply" event, removed muc._check() because waiting does not solve the problem if it is blocking incoming messages handling
Charly COSTE <changaco@changaco.net>
parents:
3
diff
changeset
|
231 if connection.closing == True: |
cb0daec4b778
Added support for IRC "nick" event, fixed participant.changeNickname(), fixed handling of IRC "namreply" event, removed muc._check() because waiting does not solve the problem if it is blocking incoming messages handling
Charly COSTE <changaco@changaco.net>
parents:
3
diff
changeset
|
232 connection.close() |
cb0daec4b778
Added support for IRC "nick" event, fixed participant.changeNickname(), fixed handling of IRC "namreply" event, removed muc._check() because waiting does not solve the problem if it is blocking incoming messages handling
Charly COSTE <changaco@changaco.net>
parents:
3
diff
changeset
|
233 return |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
234 elif event.eventtype() == 'nicknameinuse': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
235 if connection.nick_callback: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
236 connection.nick_callback('nicknameinuse') |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
237 else: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
238 self.error('=> Debug: no nick callback for "'+str(event.target())+'"', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
239 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
240 elif event.eventtype() == 'erroneusnickname': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
241 if connection.nick_callback: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
242 connection.nick_callback('erroneusnickname') |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
243 else: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
244 self.error('=> Debug: no nick callback for "'+str(event.target())+'"', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
245 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
246 elif event.eventtype() == 'umode': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
247 if connection.nick_callback: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
248 connection.nick_callback(None) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
249 else: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
250 self.error('=> Debug: no nick callback for "'+str(event.target())+'"', debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
251 self.error('connection.nick_callback='+str(connection.nick_callback), debug=True) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
252 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
253 elif event.eventtype() == 'namreply': |
5
cb0daec4b778
Added support for IRC "nick" event, fixed participant.changeNickname(), fixed handling of IRC "namreply" event, removed muc._check() because waiting does not solve the problem if it is blocking incoming messages handling
Charly COSTE <changaco@changaco.net>
parents:
3
diff
changeset
|
254 for nickname in re.split('(?:^[@\+]?|(?: [@\+]?)*)', event.arguments()[2].strip()): |
cb0daec4b778
Added support for IRC "nick" event, fixed participant.changeNickname(), fixed handling of IRC "namreply" event, removed muc._check() because waiting does not solve the problem if it is blocking incoming messages handling
Charly COSTE <changaco@changaco.net>
parents:
3
diff
changeset
|
255 if nickname == '': |
cb0daec4b778
Added support for IRC "nick" event, fixed participant.changeNickname(), fixed handling of IRC "namreply" event, removed muc._check() because waiting does not solve the problem if it is blocking incoming messages handling
Charly COSTE <changaco@changaco.net>
parents:
3
diff
changeset
|
256 continue |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
257 try: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
258 connection.bridge.addParticipant('irc', nickname) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
259 except: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
260 pass |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
261 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
262 elif event.eventtype() == 'join': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
263 nickname = event.source().split('!')[0] |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
264 if nickname == self.nickname: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
265 pass |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
266 else: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
267 try: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
268 connection.bridge.getParticipant(nickname) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
269 except NoSuchParticipantException: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
270 connection.bridge.addParticipant('irc', nickname) |
5
cb0daec4b778
Added support for IRC "nick" event, fixed participant.changeNickname(), fixed handling of IRC "namreply" event, removed muc._check() because waiting does not solve the problem if it is blocking incoming messages handling
Charly COSTE <changaco@changaco.net>
parents:
3
diff
changeset
|
271 return |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
272 try: |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
273 if event.source() == None or not '!' in event.source(): |
10 | 274 return |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
275 from_ = connection.bridge.getParticipant(event.source().split('!')[0]) |
3
3f651f4fdb4f
Added IRC "part" event handling
Charly COSTE <changaco@changaco.net>
parents:
2
diff
changeset
|
276 if event.eventtype() == 'quit' or event.eventtype() == 'part' and event.target() == connection.bridge.irc_room: |
3f651f4fdb4f
Added IRC "part" event handling
Charly COSTE <changaco@changaco.net>
parents:
2
diff
changeset
|
277 if from_.protocol in ['irc', 'both']: |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
278 connection.bridge.removeParticipant('irc', from_.nickname, event.arguments()[0]) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
279 return |
10 | 280 if event.eventtype() == 'nick' and from_.protocol in ['irc', 'both']: |
5
cb0daec4b778
Added support for IRC "nick" event, fixed participant.changeNickname(), fixed handling of IRC "namreply" event, removed muc._check() because waiting does not solve the problem if it is blocking incoming messages handling
Charly COSTE <changaco@changaco.net>
parents:
3
diff
changeset
|
281 from_.changeNickname(event.target(), 'xmpp') |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
282 except NoSuchParticipantException: |
10 | 283 self.error('===> Debug: NoSuchParticipantException "'+event.source().split('!')[0]+'"', debug=True) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
284 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
285 if event.eventtype() == 'pubmsg': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
286 if from_.protocol == 'irc' or from_.protocol == 'both': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
287 from_.sayOnXMPP(event.arguments()[0]) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
288 elif event.eventtype() == 'privmsg': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
289 if event.target() == None: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
290 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
291 try: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
292 to_ = connection.bridge.getParticipant(event.target().split('!')[0]) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
293 except NoSuchParticipantException: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
294 if event.target().split('!')[0] == self.nickname: |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
295 connection.privmsg(from_.nickname, self.respond(event.arguments()[0], from_)) |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
296 return |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
297 if to_.protocol == 'xmpp': |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
298 from_.sayOnXMPPTo(to_.nickname, event.arguments()[0]) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
299 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
300 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
301 def new_bridge(self, xmpp_room, irc_room, irc_server, irc_port=6667): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
302 """Create a bridge between xmpp_room and irc_room at irc_server.""" |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
303 b = bridge(self, xmpp_room, irc_room, irc_server, irc_port=irc_port) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
304 self.bridges.append(b) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
305 return b |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
306 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
307 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
308 def recreate_bridge(self, bridge): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
309 """Disconnect and reconnect.""" |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
310 self.new_bridge(bridge.xmpp_room.room_jid, bridge.irc_room, bridge.irc_server) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
311 self.bridges.remove(bridge) |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
312 del bridge |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
313 |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
314 |
11
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
315 def respond(self, message, participant): |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
316 if message.strip() == '!xmpp_participants': |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
317 xmpp_participants_nicknames = participant.bridge.get_xmpp_participants_nicknames_list() |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
318 return 'participants on '+participant.bridge.xmpp_room.room_jid+': '+' '.join(xmpp_participants_nicknames) |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
319 else: |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
320 return 'commands: '+' '.join(self.commands) |
79b0a7f48a3e
Introduced the command mechanism and fixed a bug
Charly COSTE <changaco@changaco.net>
parents:
10
diff
changeset
|
321 |
0
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
322 def __del__(self): |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
323 for bridge in bridges: |
4c842d23d4ce
Initial commit, version 0.1
Charly COSTE <changaco@changaco.net>
parents:
diff
changeset
|
324 del bridge |