Index: trunk/extensions/XMLRC/bridge/udp2xmpp.py |
— | — | @@ -44,15 +44,16 @@ |
45 | 45 | self.config = config |
46 | 46 | |
47 | 47 | def get_wikis( self ): |
48 | | - self.sections() |
| 48 | + return self.config.sections() |
49 | 49 | |
50 | 50 | def get_wiki_property( self, wiki, prop ): |
51 | | - opt = self.config.options( wiki ) |
| 51 | + if not self.config.has_section( wiki ): |
| 52 | + return None |
52 | 53 | |
53 | | - if opt is None: |
54 | | - return False |
| 54 | + if not self.config.has_option( wiki, prop ): |
| 55 | + return None |
55 | 56 | |
56 | | - return opt.get( prop ) |
| 57 | + return self.config.get( wiki, prop ) |
57 | 58 | |
58 | 59 | def get_wiki_page_url( self, wiki ): |
59 | 60 | return self.get_wiki_property( wiki, 'page-url' ) |
— | — | @@ -70,6 +71,8 @@ |
71 | 72 | self.loglevel = LOG_VERBOSE |
72 | 73 | self.wiki_info = wiki_info; |
73 | 74 | |
| 75 | + #FIXME: test unicode! |
| 76 | + |
74 | 77 | def warn(self, message): |
75 | 78 | if self.loglevel >= LOG_QUIET: |
76 | 79 | sys.stderr.write( "WARNING: %s\n" % ( message.encode( self.console_encoding ) ) ) |
— | — | @@ -83,7 +86,7 @@ |
84 | 87 | sys.stderr.write( "DEBUG: %s\n" % ( message.encode( self.console_encoding ) ) ) |
85 | 88 | |
86 | 89 | def get_all_channels(self): |
87 | | - return self.targets.values() |
| 90 | + return self.channels.values() |
88 | 91 | |
89 | 92 | def get_channel( self, name ): |
90 | 93 | return self.channels.get( name ) |
— | — | @@ -94,15 +97,15 @@ |
95 | 98 | |
96 | 99 | def create_channels( self, names, factories ): |
97 | 100 | for wiki in names: |
98 | | - t = self.wiki_info.get_wiki_channel_type( name ) |
99 | | - x = self.wiki_info.get_wiki_channel_spec( name ) |
| 101 | + t = self.wiki_info.get_wiki_channel_type( wiki ) |
| 102 | + x = self.wiki_info.get_wiki_channel_spec( wiki ) |
100 | 103 | |
101 | 104 | f = factories[ t ] |
102 | 105 | channel = f( x ) |
103 | 106 | |
104 | 107 | channel.join() #FIXME: error detection / recovery! |
105 | 108 | |
106 | | - self.add_channel( name, channel ) |
| 109 | + self.add_channel( wiki, channel ) |
107 | 110 | |
108 | 111 | def broadcast_message( self, message, xml = None ): |
109 | 112 | targets = self.get_all_channels() |
— | — | @@ -112,9 +115,11 @@ |
113 | 116 | |
114 | 117 | def process_command(self, line): |
115 | 118 | args = line.split() |
116 | | - command = args[0] |
| 119 | + command = args[0][1:] |
117 | 120 | args = args[1:] |
118 | 121 | |
| 122 | + self.debug( "processing command: %s" % command ) |
| 123 | + |
119 | 124 | if ( command == 'quit' ): |
120 | 125 | self.online = False |
121 | 126 | #elif ( command.startswith( '/' ) ): |
— | — | @@ -127,6 +132,8 @@ |
128 | 133 | self.loglevel = LOG_VERBOSE |
129 | 134 | elif ( command == 'quiet' ): |
130 | 135 | self.loglevel = LOG_QUIET |
| 136 | + else: |
| 137 | + self.warn( "unknwon command: %s" % command ) |
131 | 138 | |
132 | 139 | def get_rc_text( self, rc ): |
133 | 140 | for k, v in rc.items(): |
— | — | @@ -182,7 +189,7 @@ |
183 | 190 | u = self.wiki_info.get_wiki_page_url( wikiid ) |
184 | 191 | if not u: return False |
185 | 192 | |
186 | | - return u.sub( '$1', urllib.quote( page ) ) |
| 193 | + return u.replace( '$1', urllib.quote( page ) ) |
187 | 194 | |
188 | 195 | def relay_rc_message( self, rc ): |
189 | 196 | w = rc['wikiid'] |
— | — | @@ -242,6 +249,7 @@ |
243 | 250 | def __init__( self, relay, message_encoding = 'utf-8' ): |
244 | 251 | super( XmppConnection, self ).__init__( relay ) |
245 | 252 | self.message_encoding = message_encoding |
| 253 | + self.jid = None |
246 | 254 | |
247 | 255 | def process( self ): |
248 | 256 | self.jabber.Process(1) |
— | — | @@ -260,20 +268,22 @@ |
261 | 269 | return JabberChannel( self, jid ) |
262 | 270 | |
263 | 271 | def make_muc_channel( self, room_jid, room_nick = None ): |
| 272 | + if type(room_jid) != object: |
| 273 | + room_jid = xmpp.protocol.JID( room_jid ) |
264 | 274 | |
265 | 275 | if not room_nick: |
266 | 276 | room_nick = room_jid.getResource() |
267 | 277 | |
268 | 278 | if not room_nick: |
269 | | - room_nick = connection.jid.getNode() |
| 279 | + room_nick = self.jid.getNode() |
270 | 280 | |
271 | 281 | return MucChannel( self, room_jid, room_nick ) |
272 | 282 | |
273 | 283 | def process_message(self, con, message): |
274 | 284 | if (message.getError()): |
275 | | - self.warn("received %s error from <%s>: %s\n" % (message.getType(), message.getError(), message.getFrom() )) |
| 285 | + self.warn("received %s error from <%s>: %s" % (message.getType(), message.getError(), message.getFrom() )) |
276 | 286 | elif message.getBody(): |
277 | | - self.debug("discarding %s message from <%s>: %s\n" % (message.getType(), message.getFrom(), message.getBody() )) |
| 287 | + self.debug("discarding %s message from <%s>: %s" % (message.getType(), message.getFrom(), message.getBody().strip() )) |
278 | 288 | |
279 | 289 | def register_handlers(self): |
280 | 290 | self.jabber.RegisterHandler( 'message', self.process_message ) |
— | — | @@ -334,7 +344,7 @@ |
335 | 345 | self.socket.close() |
336 | 346 | |
337 | 347 | def process(self): |
338 | | - msg = self.socket.readline().sub('^\\s+|\\s+$', '') |
| 348 | + msg = self.socket.readline().strip() |
339 | 349 | |
340 | 350 | if (msg.startswith('/')): |
341 | 351 | self.process_command( msg ) |
— | — | @@ -373,10 +383,12 @@ |
374 | 384 | def connect( self, port, interface = '0.0.0.0' ): |
375 | 385 | self.socket = socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) |
376 | 386 | self.socket.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 ) |
| 387 | + self.socket.setblocking( 0 ) |
377 | 388 | |
378 | 389 | self.debug( "binding to UDP %s:%d" % (interface, port) ) |
| 390 | + self.socket.bind( (interface, port) ) |
379 | 391 | |
380 | | - if not self.socket.bind( (interface, port) ): |
| 392 | + if not self.socket.fileno(): |
381 | 393 | self.warn( "failed to bind to UDP %s:%d" % (interface, port) ) |
382 | 394 | return False |
383 | 395 | |
— | — | @@ -398,6 +410,10 @@ |
399 | 411 | class JabberChannel (Channel): |
400 | 412 | def __init__( self, connection, jid ): |
401 | 413 | super( JabberChannel, self ).__init__( connection ) |
| 414 | + |
| 415 | + if type( jid ) != object: |
| 416 | + jid = xmpp.protocol.JID( jid ) |
| 417 | + |
402 | 418 | self.connection = connection |
403 | 419 | self.jid = jid |
404 | 420 | self.message_type = 'chat' |
— | — | @@ -410,7 +426,7 @@ |
411 | 427 | if mtype is None: |
412 | 428 | mtype = self.message_type |
413 | 429 | |
414 | | - message = xmpp.protocol.Message( jid, body= message, type= mtype ) |
| 430 | + message = xmpp.protocol.Message( self.jid, body= message, typ= mtype ) |
415 | 431 | |
416 | 432 | if xml: |
417 | 433 | message.addChild( node = xml ) |
— | — | @@ -430,6 +446,9 @@ |
431 | 447 | |
432 | 448 | class MucChannel (JabberChannel): |
433 | 449 | def __init__( self, connection, room_jid, room_nick ): |
| 450 | + if type( room_jid ) != object: |
| 451 | + room_jid = xmpp.protocol.JID( room_jid ) |
| 452 | + |
434 | 453 | super( MucChannel, self ).__init__( connection, room_jid.getStripped() ) |
435 | 454 | |
436 | 455 | self.nick = room_nick |
— | — | @@ -437,7 +456,7 @@ |
438 | 457 | |
439 | 458 | def join(self): |
440 | 459 | # use our own desired nickname as resource part of the group's JID |
441 | | - jid = self.jid.__str__( wresource= 0 ) + "/" + self.nick; |
| 460 | + jid = self.jid.getStripped() + "/" + self.nick; |
442 | 461 | |
443 | 462 | #create presence stanza |
444 | 463 | join = xmpp.Presence( to= jid ) |
— | — | @@ -447,7 +466,7 @@ |
448 | 467 | |
449 | 468 | self.connection.jabber.send( join ) |
450 | 469 | |
451 | | - self.info('joined room %s' % room) |
| 470 | + self.connection.info( 'joined room %s' % self.jid.getStripped() ) |
452 | 471 | |
453 | 472 | return True |
454 | 473 | |