changeset 493:b7c4bb2c0668

jp: - better expandJid: roster's jids' nodes are used after names to expand jid - small error message fix - stdin stream cleaning when sending it as a normal message
author Goffi <goffi@goffi.org>
date Fri, 17 Aug 2012 03:17:44 +0200
parents 9248f2c5e03e
children 385cd2169eb5
files frontends/src/jp/jp
diffstat 1 files changed, 24 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/jp/jp	Fri Aug 17 03:14:20 2012 +0200
+++ b/frontends/src/jp/jp	Fri Aug 17 03:17:44 2012 +0200
@@ -60,6 +60,7 @@
 import tarfile
 import tempfile
 import shutil
+import unicodedata
 try:
     from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed
 except ImportError, e:
@@ -155,7 +156,7 @@
             self.bridge.asyncConnect(self.profile, self.connected, cantConnect)
             return
         elif not self.bridge.isConnected(self.profile):
-            error(_(u"SàT is not conneted, please connect before using jp"))
+            error(_(u"Profile [%(profile)s] is not connected, please connect it before using jp, or use --connect option") % { "profile": self.profile })
             exit(1)
 
         self.connected()
@@ -163,15 +164,23 @@
     def check_jids(self):
         """Check jids validity, transform roster name to corresponding jids"""
         names2jid = {}
+        nodes2jid = {}
 
         for contact in self.bridge.getContacts(self.options.profile):
             _jid, attr, groups = contact
             if attr.has_key("name"):
                 names2jid[attr["name"].lower()] = _jid
+            nodes2jid[JID(_jid).node.lower()] = _jid
 
         def expandJid(jid):
             _jid = jid.lower()
-            return unicode(names2jid[_jid] if _jid in names2jid else jid)
+            if _jid in names2jid:
+                expanded = names2jid[_jid]
+            elif _jid in nodes2jid:
+                expanded = nodes2jid[_jid]
+            else:
+                expanded = jid
+            return unicode(expanded)
 
         def check(jid):
             if not jid.is_valid:
@@ -190,6 +199,17 @@
         except AttributeError:
             pass
 
+    def clean_ustr(self, ustr):
+        """Clean unicode string
+        remove special characters from unicode string"""
+        def valid_chars(unicode_source):
+            for char in unicode_source:
+                if unicodedata.category(char) == 'Cc' and char!='\n':
+                    continue
+                yield char
+        return ''.join(valid_chars(ustr))
+
+
     def send_stdin(self):
         """Send incomming data on stdin to jabber contact"""
         header = "\n" if self.options.new_line else ""
@@ -198,12 +218,12 @@
             if header:
                 self.bridge.sendMessage(self.dest_jid, header, profile_key=self.profile)
             while (True):
-                line = sys.stdin.readline()
+                line = self.clean_ustr(sys.stdin.readline().decode('utf-8','ignore'))
                 if not line:
                     break
                 self.bridge.sendMessage(self.dest_jid, line.replace("\n",""), profile_key=self.profile)
         else:
-            self.bridge.sendMessage(self.dest_jid, header + "".join(sys.stdin.readlines()), profile_key=self.profile)
+            self.bridge.sendMessage(self.dest_jid, header + self.clean_ustr(u"".join([stream.decode('utf-8','ignore') for stream in sys.stdin.readlines()])), profile_key=self.profile)
 
 
     def pipe_out(self):