r70836 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r70835‎ | r70836 | r70837 >
Date:19:09, 10 August 2010
Author:daniel
Status:deferred
Tags:
Comment:
fixed several sloppy bugs. seems to work for basic operation. more testing later
Modified paths:
  • /trunk/extensions/XMLRC/bridge/udp2xmpp.py (modified) (history)

Diff [purge]

Index: trunk/extensions/XMLRC/bridge/udp2xmpp.py
@@ -44,15 +44,16 @@
4545 self.config = config
4646
4747 def get_wikis( self ):
48 - self.sections()
 48+ return self.config.sections()
4949
5050 def get_wiki_property( self, wiki, prop ):
51 - opt = self.config.options( wiki )
 51+ if not self.config.has_section( wiki ):
 52+ return None
5253
53 - if opt is None:
54 - return False
 54+ if not self.config.has_option( wiki, prop ):
 55+ return None
5556
56 - return opt.get( prop )
 57+ return self.config.get( wiki, prop )
5758
5859 def get_wiki_page_url( self, wiki ):
5960 return self.get_wiki_property( wiki, 'page-url' )
@@ -70,6 +71,8 @@
7172 self.loglevel = LOG_VERBOSE
7273 self.wiki_info = wiki_info;
7374
 75+ #FIXME: test unicode!
 76+
7477 def warn(self, message):
7578 if self.loglevel >= LOG_QUIET:
7679 sys.stderr.write( "WARNING: %s\n" % ( message.encode( self.console_encoding ) ) )
@@ -83,7 +86,7 @@
8487 sys.stderr.write( "DEBUG: %s\n" % ( message.encode( self.console_encoding ) ) )
8588
8689 def get_all_channels(self):
87 - return self.targets.values()
 90+ return self.channels.values()
8891
8992 def get_channel( self, name ):
9093 return self.channels.get( name )
@@ -94,15 +97,15 @@
9598
9699 def create_channels( self, names, factories ):
97100 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 )
100103
101104 f = factories[ t ]
102105 channel = f( x )
103106
104107 channel.join() #FIXME: error detection / recovery!
105108
106 - self.add_channel( name, channel )
 109+ self.add_channel( wiki, channel )
107110
108111 def broadcast_message( self, message, xml = None ):
109112 targets = self.get_all_channels()
@@ -112,9 +115,11 @@
113116
114117 def process_command(self, line):
115118 args = line.split()
116 - command = args[0]
 119+ command = args[0][1:]
117120 args = args[1:]
118121
 122+ self.debug( "processing command: %s" % command )
 123+
119124 if ( command == 'quit' ):
120125 self.online = False
121126 #elif ( command.startswith( '/' ) ):
@@ -127,6 +132,8 @@
128133 self.loglevel = LOG_VERBOSE
129134 elif ( command == 'quiet' ):
130135 self.loglevel = LOG_QUIET
 136+ else:
 137+ self.warn( "unknwon command: %s" % command )
131138
132139 def get_rc_text( self, rc ):
133140 for k, v in rc.items():
@@ -182,7 +189,7 @@
183190 u = self.wiki_info.get_wiki_page_url( wikiid )
184191 if not u: return False
185192
186 - return u.sub( '$1', urllib.quote( page ) )
 193+ return u.replace( '$1', urllib.quote( page ) )
187194
188195 def relay_rc_message( self, rc ):
189196 w = rc['wikiid']
@@ -242,6 +249,7 @@
243250 def __init__( self, relay, message_encoding = 'utf-8' ):
244251 super( XmppConnection, self ).__init__( relay )
245252 self.message_encoding = message_encoding
 253+ self.jid = None
246254
247255 def process( self ):
248256 self.jabber.Process(1)
@@ -260,20 +268,22 @@
261269 return JabberChannel( self, jid )
262270
263271 def make_muc_channel( self, room_jid, room_nick = None ):
 272+ if type(room_jid) != object:
 273+ room_jid = xmpp.protocol.JID( room_jid )
264274
265275 if not room_nick:
266276 room_nick = room_jid.getResource()
267277
268278 if not room_nick:
269 - room_nick = connection.jid.getNode()
 279+ room_nick = self.jid.getNode()
270280
271281 return MucChannel( self, room_jid, room_nick )
272282
273283 def process_message(self, con, message):
274284 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() ))
276286 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() ))
278288
279289 def register_handlers(self):
280290 self.jabber.RegisterHandler( 'message', self.process_message )
@@ -334,7 +344,7 @@
335345 self.socket.close()
336346
337347 def process(self):
338 - msg = self.socket.readline().sub('^\\s+|\\s+$', '')
 348+ msg = self.socket.readline().strip()
339349
340350 if (msg.startswith('/')):
341351 self.process_command( msg )
@@ -373,10 +383,12 @@
374384 def connect( self, port, interface = '0.0.0.0' ):
375385 self.socket = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
376386 self.socket.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
 387+ self.socket.setblocking( 0 )
377388
378389 self.debug( "binding to UDP %s:%d" % (interface, port) )
 390+ self.socket.bind( (interface, port) )
379391
380 - if not self.socket.bind( (interface, port) ):
 392+ if not self.socket.fileno():
381393 self.warn( "failed to bind to UDP %s:%d" % (interface, port) )
382394 return False
383395
@@ -398,6 +410,10 @@
399411 class JabberChannel (Channel):
400412 def __init__( self, connection, jid ):
401413 super( JabberChannel, self ).__init__( connection )
 414+
 415+ if type( jid ) != object:
 416+ jid = xmpp.protocol.JID( jid )
 417+
402418 self.connection = connection
403419 self.jid = jid
404420 self.message_type = 'chat'
@@ -410,7 +426,7 @@
411427 if mtype is None:
412428 mtype = self.message_type
413429
414 - message = xmpp.protocol.Message( jid, body= message, type= mtype )
 430+ message = xmpp.protocol.Message( self.jid, body= message, typ= mtype )
415431
416432 if xml:
417433 message.addChild( node = xml )
@@ -430,6 +446,9 @@
431447
432448 class MucChannel (JabberChannel):
433449 def __init__( self, connection, room_jid, room_nick ):
 450+ if type( room_jid ) != object:
 451+ room_jid = xmpp.protocol.JID( room_jid )
 452+
434453 super( MucChannel, self ).__init__( connection, room_jid.getStripped() )
435454
436455 self.nick = room_nick
@@ -437,7 +456,7 @@
438457
439458 def join(self):
440459 # 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;
442461
443462 #create presence stanza
444463 join = xmpp.Presence( to= jid )
@@ -447,7 +466,7 @@
448467
449468 self.connection.jabber.send( join )
450469
451 - self.info('joined room %s' % room)
 470+ self.connection.info( 'joined room %s' % self.jid.getStripped() )
452471
453472 return True
454473

Status & tagging log