comparison frontends/src/jp/jp @ 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 2a072735e459
children 2c4016921403
comparison
equal deleted inserted replaced
492:9248f2c5e03e 493:b7c4bb2c0668
58 import gobject 58 import gobject
59 from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService 59 from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService
60 import tarfile 60 import tarfile
61 import tempfile 61 import tempfile
62 import shutil 62 import shutil
63 import unicodedata
63 try: 64 try:
64 from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed 65 from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed
65 except ImportError, e: 66 except ImportError, e:
66 info (_('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar')) 67 info (_('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar'))
67 info (_('Progress bar deactivated\n--\n')) 68 info (_('Progress bar deactivated\n--\n'))
153 154
154 if self.options.connect: #if connection is asked, we connect the profile 155 if self.options.connect: #if connection is asked, we connect the profile
155 self.bridge.asyncConnect(self.profile, self.connected, cantConnect) 156 self.bridge.asyncConnect(self.profile, self.connected, cantConnect)
156 return 157 return
157 elif not self.bridge.isConnected(self.profile): 158 elif not self.bridge.isConnected(self.profile):
158 error(_(u"SàT is not conneted, please connect before using jp")) 159 error(_(u"Profile [%(profile)s] is not connected, please connect it before using jp, or use --connect option") % { "profile": self.profile })
159 exit(1) 160 exit(1)
160 161
161 self.connected() 162 self.connected()
162 163
163 def check_jids(self): 164 def check_jids(self):
164 """Check jids validity, transform roster name to corresponding jids""" 165 """Check jids validity, transform roster name to corresponding jids"""
165 names2jid = {} 166 names2jid = {}
167 nodes2jid = {}
166 168
167 for contact in self.bridge.getContacts(self.options.profile): 169 for contact in self.bridge.getContacts(self.options.profile):
168 _jid, attr, groups = contact 170 _jid, attr, groups = contact
169 if attr.has_key("name"): 171 if attr.has_key("name"):
170 names2jid[attr["name"].lower()] = _jid 172 names2jid[attr["name"].lower()] = _jid
173 nodes2jid[JID(_jid).node.lower()] = _jid
171 174
172 def expandJid(jid): 175 def expandJid(jid):
173 _jid = jid.lower() 176 _jid = jid.lower()
174 return unicode(names2jid[_jid] if _jid in names2jid else jid) 177 if _jid in names2jid:
178 expanded = names2jid[_jid]
179 elif _jid in nodes2jid:
180 expanded = nodes2jid[_jid]
181 else:
182 expanded = jid
183 return unicode(expanded)
175 184
176 def check(jid): 185 def check(jid):
177 if not jid.is_valid: 186 if not jid.is_valid:
178 error (_("%s is not a valid JID !"), self.dest_jid) 187 error (_("%s is not a valid JID !"), self.dest_jid)
179 exit(1) 188 exit(1)
188 self.dest_jids[i] = expandJid(self.dest_jids[i]) 197 self.dest_jids[i] = expandJid(self.dest_jids[i])
189 check(self.dest_jids[i]) 198 check(self.dest_jids[i])
190 except AttributeError: 199 except AttributeError:
191 pass 200 pass
192 201
202 def clean_ustr(self, ustr):
203 """Clean unicode string
204 remove special characters from unicode string"""
205 def valid_chars(unicode_source):
206 for char in unicode_source:
207 if unicodedata.category(char) == 'Cc' and char!='\n':
208 continue
209 yield char
210 return ''.join(valid_chars(ustr))
211
212
193 def send_stdin(self): 213 def send_stdin(self):
194 """Send incomming data on stdin to jabber contact""" 214 """Send incomming data on stdin to jabber contact"""
195 header = "\n" if self.options.new_line else "" 215 header = "\n" if self.options.new_line else ""
196 216
197 if self.options.separate: #we send stdin in several messages 217 if self.options.separate: #we send stdin in several messages
198 if header: 218 if header:
199 self.bridge.sendMessage(self.dest_jid, header, profile_key=self.profile) 219 self.bridge.sendMessage(self.dest_jid, header, profile_key=self.profile)
200 while (True): 220 while (True):
201 line = sys.stdin.readline() 221 line = self.clean_ustr(sys.stdin.readline().decode('utf-8','ignore'))
202 if not line: 222 if not line:
203 break 223 break
204 self.bridge.sendMessage(self.dest_jid, line.replace("\n",""), profile_key=self.profile) 224 self.bridge.sendMessage(self.dest_jid, line.replace("\n",""), profile_key=self.profile)
205 else: 225 else:
206 self.bridge.sendMessage(self.dest_jid, header + "".join(sys.stdin.readlines()), profile_key=self.profile) 226 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)
207 227
208 228
209 def pipe_out(self): 229 def pipe_out(self):
210 """Create named pipe, and send stdin to it""" 230 """Create named pipe, and send stdin to it"""
211 tmp_dir = tempfile.mkdtemp() 231 tmp_dir = tempfile.mkdtemp()