Alexander Shorin avatar Alexander Shorin committed c15dcd2

Force environ values to be unicode unless they are not strings.

Comments (0)

Files changed (1)

xmppflask/server.py

 """
 
 from collections import namedtuple
-import logging
-
-logging.basicConfig(level=logging.INFO)
+from pprint import pformat
 
 JID = namedtuple('JID', 'user,domain,resource')
 
         """
         environ = {}
         if isinstance(event, self.message_class):
-            event_type = 'message'
+            event_type = u'message'
         elif isinstance(event, self.presence_class):
-            event_type = 'presence'
+            event_type = u'presence'
         elif isinstance(event, self.iq_class):
-            event_type = 'iq'
+            event_type = u'iq'
         environ['XMPP_EVENT'] = event_type
         return environ
 
     def handle_message(self, message):
         """Handles message events."""
         environ = self.make_environ(message)
+        self.app.logger.debug(pformat(environ))
         if not environ['MESSAGE']:
             return
         notification_queue = []
     def handle_presence(self, presence):
         """Handles presence events."""
         environ = self.make_environ(presence)
+        self.app.logger.debug(pformat(environ))
         if environ['from'] == environ['to']: # we don't want infinity loop there
             return
         notification_queue = []
         :param event: XMPP event that would be base for environ.
         """
         environ = super(XmpppyServer, self).make_environ(event)
-        environ['id'] = event.getID()
-        environ['from'] = event.getFrom()
-        environ['to'] = event.getTo()
-        environ['type'] = event.getType()
-        if environ['from'] is not None:
-            environ['from'] = str(environ['from'])
-        if environ['to'] is not None:
-            environ['to'] = str(environ['to'])
+        environ['id'] = maybe_unicode(event.getID())
+        environ['from'] = maybe_unicode(event.getFrom())
+        environ['to'] = maybe_unicode(event.getTo())
+        environ['type'] = maybe_unicode(event.getType())
 
         xmpp_user = event.getFrom()
         jid = JID(
         environ['XMPP_JID'] = u'%s@%s' % (jid.user, jid.domain)
 
         if isinstance(event, self.message_class):
-            environ['body'] = environ['MESSAGE'] = event.getBody()
-            environ['subject'] = event.getSubject()
+            environ['body'] = environ['MESSAGE'] = maybe_unicode(event.getBody())
+            environ['subject'] = maybe_unicode(event.getSubject())
             if environ['type'] == 'groupchat':
                 environ['mucroom'] = u'%s@%s' % (jid.user, jid.domain)
-                environ['mucnick'] = jid.resource
+                environ['mucnick'] = maybe_unicode(jid.resource)
             else:
                 environ['mucroom'] = None
                 environ['mucnick'] = None
         elif isinstance(event, self.presence_class):
-            environ['priority'] = event.getPriority()
-            environ['status'] = event.getStatus()
-            environ['type'] = environ['type'] or event.getShow()
+            environ['priority'] = maybe_unicode(event.getPriority())
+            environ['status'] = maybe_unicode(event.getStatus())
+            environ['type'] = maybe_unicode(environ['type'] or event.getShow())
 
         return environ
 
         :param event: XMPP event that would be base for environ.
         """
         environ = super(SleekXmppServer, self).make_environ(event)
-        environ['id'] = event['id']
-        environ['from'] = event['from']
-        environ['to'] = event['to']
-        environ['type'] = event['type']
-        if environ['from'] is not None:
-            environ['from'] = str(environ['from'])
-        if environ['to'] is not None:
-            environ['to'] = str(environ['to'])
+        environ['id'] = maybe_unicode(event['id'])
+        environ['from'] = maybe_unicode(event['from'])
+        environ['to'] = maybe_unicode(event['to'])
+        environ['type'] = maybe_unicode(event['type'])
 
         if isinstance(event, self.message_class):
             jid = JID(
                 event['from'].resource
             )
             environ['XMPP_JID'] = u'%s@%s' % (jid.user, jid.domain)
-            environ['body'] = environ['MESSAGE'] = event['body']
-            environ['subject'] = event['subject'] or None
-            environ['mucroom'] = event['mucroom'] or None
-            environ['mucnick'] = event['mucnick'] or None
+            environ['body'] = environ['MESSAGE'] = maybe_unicode(event['body'])
+            environ['subject'] = maybe_unicode(event['subject'])
+            environ['mucroom'] = maybe_unicode(event['mucroom'])
+            environ['mucnick'] = maybe_unicode(event['mucnick'])
 
         return environ
 
             self.client.send_presence(pto=to_jid,
                                       pstatus=payload.get('status', ''),
                                       pshow=payload['type'])
+
+def maybe_unicode(value):
+    if value is None:
+        return value
+    if isinstance(value, str):
+        if not value:
+            return None
+        return unicode(value)
+    return value
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.