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
This commit is contained in:
Goffi 2012-08-17 03:17:44 +02:00
commit 15c05811fa

View file

@ -60,6 +60,7 @@ from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoServic
import tarfile
import tempfile
import shutil
import unicodedata
try:
from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed
except ImportError, e:
@ -155,7 +156,7 @@ class JP():
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 @@ class JP():
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 @@ class JP():
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 @@ class JP():
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):