simple (hackish) sending of messages, needs improvement

removed some debug messages
This commit is contained in:
Stefan Bethge 2006-06-27 22:28:49 +00:00
parent 908a4782d4
commit 9921ce24f4
3 changed files with 119 additions and 62 deletions

View File

@ -73,8 +73,8 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
#self.last_sent = [] #self.last_sent = []
#self.last_history_line = {} #self.last_history_line = {}
#we don't need a password #we don't need a password, but must be non-empty
self.password = 'dummy' # gajim.config.get_per('accounts', name, 'password') self.password = 'dummy'
self.privacy_rules_supported = False self.privacy_rules_supported = False
# Do we continue connection when we get roster (send presence,get vcard...) # Do we continue connection when we get roster (send presence,get vcard...)
@ -109,17 +109,6 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
signed = self.get_signed_msg(self.status) signed = self.get_signed_msg(self.status)
# We are doing disconnect at so many places, better use one function in all
def disconnect(self, on_purpose = False):
self.on_purpose = on_purpose
self.connected = 0
self.time_to_reconnect = None
if self.connection:
# make sure previous connection is completely closed
self.last_connection = None
self.connection = None
self.call_resolve_timeout = False
self.zeroconf.disconnect()
def quit(self, kill_core): def quit(self, kill_core):
@ -157,24 +146,25 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.zeroconf.resolve_all() self.zeroconf.resolve_all()
diffs = self.roster.getDiffs() diffs = self.roster.getDiffs()
for key in diffs: for key in diffs:
# display_key = self.zeroconf.check_jid(key) self.roster.setItem(key)
self.dispatch('NOTIFY', (key, diffs[key], '', 'Gajim', 0, None, 0)) display_key = self.zeroconf.check_jid(key)
self.dispatch('NOTIFY', (display_key, self.roster.getStatus(key), self.roster.getMessage(key), 'local', 0, None, 0))
return self.call_resolve_timeout return self.call_resolve_timeout
# callbacks called from zeroconf # callbacks called from zeroconf
def _on_new_service(self,jid): def _on_new_service(self,jid):
self.roster.setItem(jid) self.roster.setItem(jid)
# display_jid = self.zeroconf.check_jid(jid) display_jid = self.zeroconf.check_jid(jid)
self.dispatch('ROSTER_INFO', (jid, jid, 'both', 'no', self.roster.getGroups(jid))) self.dispatch('ROSTER_INFO', (display_jid, display_jid, 'both', 'no', self.roster.getGroups(jid)))
self.dispatch('NOTIFY', (jid, self.roster.getStatus(jid), '', 'Gajim', 0, None, 0)) self.dispatch('NOTIFY', (display_jid, self.roster.getStatus(jid), self.roster.getMessage(jid), 'local', 0, None, 0))
def _on_remove_service(self,jid): def _on_remove_service(self,jid):
self.roster.delItem(jid) self.roster.delItem(jid)
# 'NOTIFY' (account, (jid, status, status message, resource, priority, # 'NOTIFY' (account, (jid, status, status message, resource, priority,
# keyID, timestamp)) # keyID, timestamp))
# jid = self.zeroconf.check_jid(jid) jid = self.zeroconf.check_jid(jid)
self.dispatch('NOTIFY', (jid, 'offline', '', 'Gajim', 0, None, 0)) self.dispatch('NOTIFY', (jid, 'offline', '', 'local', 0, None, 0))
def connect(self, data = None, show = 'online'): def connect(self, data = None, show = 'online'):
@ -185,6 +175,13 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.connection = client_zeroconf.ClientZeroconf(self.zeroconf) self.connection = client_zeroconf.ClientZeroconf(self.zeroconf)
self.roster = self.connection.getRoster() self.roster = self.connection.getRoster()
self.dispatch('ROSTER', self.roster) self.dispatch('ROSTER', self.roster)
#display contacts already detected and resolved
for jid in self.roster.keys():
display_jid = self.zeroconf.check_jid(jid)
self.dispatch('ROSTER_INFO', (display_jid, display_jid, 'both', 'no', self.roster.getGroups(jid)))
self.dispatch('NOTIFY', (display_jid, self.roster.getStatus(jid), self.roster.getMessage(jid), 'local', 0, None, 0))
self.connected = STATUS_LIST.index(show) self.connected = STATUS_LIST.index(show)
# refresh all contacts data all 10 seconds # refresh all contacts data all 10 seconds
@ -198,14 +195,25 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.zeroconf.txt['msg'] = msg self.zeroconf.txt['msg'] = msg
self.connect('',show) self.connect('',show)
def disconnect(self, on_purpose = False):
self.on_purpose = on_purpose
self.connected = 0
self.time_to_reconnect = None
if self.connection:
# make sure previous connection is completely closed
self.last_connection = None
self.connection = None
# stop calling the timeout
self.call_resolve_timeout = False
self.zeroconf.disconnect()
def change_status(self, show, msg, sync = False, auto = False): def change_status(self, show, msg, sync = False, auto = False):
print "connection_zeroconf.py: show: %s msg: %s in change_status" % (show, msg)
if not show in STATUS_LIST: if not show in STATUS_LIST:
return -1 return -1
# 'connect' # 'connect'
if show != 'offline' and not self.connected: if show != 'offline' and not self.connected:
print "connection_zeroconf.py: connect in change_status"
self.on_purpose = False self.on_purpose = False
self.connect_and_init(show, msg, '') self.connect_and_init(show, msg, '')
if show != 'invisible': if show != 'invisible':
@ -215,21 +223,18 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
# 'disconnect' # 'disconnect'
elif show == 'offline' and self.connected: elif show == 'offline' and self.connected:
print "connection_zeroconf.py: disconnect in change_status"
self.connected = 0 self.connected = 0
self.dispatch('STATUS', 'offline') self.dispatch('STATUS', 'offline')
self.disconnect() self.disconnect()
# update status # update status
elif show != 'offline' and self.connected: elif show != 'offline' and self.connected:
print "connection_zeroconf.py: update in change_status"
was_invisible = self.connected == STATUS_LIST.index('invisible') was_invisible = self.connected == STATUS_LIST.index('invisible')
self.connected = STATUS_LIST.index(show) self.connected = STATUS_LIST.index(show)
if show == 'invisible': if show == 'invisible':
self.zeroconf.remove_announce() self.zeroconf.remove_announce()
return return
if was_invisible: if was_invisible:
print "connection_zeroconf.py: reannounce after invisible in change_status"
self.zeroconf.announce() self.zeroconf.announce()
if self.connection: if self.connection:
txt = {} txt = {}
@ -243,14 +248,15 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
def send_message(self, jid, msg, keyID, type = 'chat', subject='', def send_message(self, jid, msg, keyID, type = 'chat', subject='',
chatstate = None, msg_id = None, composing_jep = None, resource = None): chatstate = None, msg_id = None, composing_jep = None, resource = None):
''' print 'connection_zeroconf.py: send_message'
fjid = jid
if not self.connection: if not self.connection:
return return
if not msg and chatstate is None: if not msg and chatstate is None:
return return
fjid = jid
if resource:
fjid += '/' + resource
msgtxt = msg msgtxt = msg
msgenc = '' msgenc = ''
if keyID and USE_GPG: if keyID and USE_GPG:
@ -262,8 +268,11 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
if lang is not None or lang != 'en': # we're not english if lang is not None or lang != 'en': # we're not english
msgtxt = _('[This message is encrypted]') +\ msgtxt = _('[This message is encrypted]') +\
' ([This message is encrypted])' # one in locale and one en ' ([This message is encrypted])' # one in locale and one en
if type == 'chat': if type == 'chat':
msg_iq = common.xmpp.Message(to = fjid, body = msgtxt, typ = type) msg_iq = common.xmpp.Message(to = fjid, body = msgtxt, typ = type)
else: else:
if subject: if subject:
msg_iq = common.xmpp.Message(to = fjid, body = msgtxt, msg_iq = common.xmpp.Message(to = fjid, body = msgtxt,
@ -271,6 +280,8 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
else: else:
msg_iq = common.xmpp.Message(to = fjid, body = msgtxt, msg_iq = common.xmpp.Message(to = fjid, body = msgtxt,
typ = 'normal') typ = 'normal')
if msgenc: if msgenc:
msg_iq.setTag(common.xmpp.NS_ENCRYPTED + ' x').setData(msgenc) msg_iq.setTag(common.xmpp.NS_ENCRYPTED + ' x').setData(msgenc)
@ -292,7 +303,6 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
if chatstate is 'composing' or msgtxt: if chatstate is 'composing' or msgtxt:
chatstate_node.addChild(name = 'composing') chatstate_node.addChild(name = 'composing')
self.connection.send(msg_iq)
no_log_for = gajim.config.get_per('accounts', self.name, 'no_log_for') no_log_for = gajim.config.get_per('accounts', self.name, 'no_log_for')
ji = gajim.get_jid_without_resource(jid) ji = gajim.get_jid_without_resource(jid)
if self.name not in no_log_for and ji not in no_log_for: if self.name not in no_log_for and ji not in no_log_for:
@ -305,17 +315,17 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
else: else:
kind = 'single_msg_sent' kind = 'single_msg_sent'
gajim.logger.write(kind, jid, log_msg) gajim.logger.write(kind, jid, log_msg)
self.zeroconf.send_message(jid, msgtxt)
self.dispatch('MSGSENT', (jid, msg, keyID)) self.dispatch('MSGSENT', (jid, msg, keyID))
'''
def send_stanza(self, stanza): def send_stanza(self, stanza):
# send a stanza untouched # send a stanza untouched
''' print 'connection_zeroconf.py: send_stanza'
if not self.connection: if not self.connection:
return return
self.connection.send(stanza) #self.connection.send(stanza)
'''
pass pass
def ack_subscribed(self, jid): def ack_subscribed(self, jid):

View File

@ -10,7 +10,7 @@ class Roster:
self.setItem(jid) self.setItem(jid)
def getRoster(self): def getRoster(self):
print 'roster_zeroconf.py: getRoster' #print 'roster_zeroconf.py: getRoster'
self.update_roster() self.update_roster()
return self return self
@ -25,8 +25,7 @@ class Roster:
if self._data.has_key(key): if self._data.has_key(key):
if old_data[key] != self._data[key]: if old_data[key] != self._data[key]:
diffs[key] = self._data[key]['status'] diffs[key] = self._data[key]['status']
print 'roster_zeroconf.py: diffs:', #print 'roster_zeroconf.py: diffs:' + diffs
print diffs
return diffs return diffs
def setItem(self, jid, name = '', groups = ''): def setItem(self, jid, name = '', groups = ''):
@ -50,57 +49,64 @@ class Roster:
status = '' status = ''
if status == 'avail': status = 'online' if status == 'avail': status = 'online'
self._data[jid]['txt_dict'] = txt_dict self._data[jid]['txt_dict'] = txt_dict
if not self._data[jid]['txt_dict'].has_key('msg'):
self._data[jid]['txt_dict']['msg'] = ''
self._data[jid]['status'] = status self._data[jid]['status'] = status
self._data[jid]['show'] = status self._data[jid]['show'] = status
# print self._data[jid] # print self._data[jid]
def delItem(self, jid): def delItem(self, jid):
print 'roster_zeroconf.py: delItem %s' % jid #print 'roster_zeroconf.py: delItem %s' % jid
if self._data.has_key(jid): if self._data.has_key(jid):
del self._data[jid] del self._data[jid]
def getItem(self, jid): def getItem(self, jid):
print 'roster_zeroconf.py: getItem: %s' % jid #print 'roster_zeroconf.py: getItem: %s' % jid
if self._data.has_key(jid): if self._data.has_key(jid):
return self._data[jid] return self._data[jid]
def __getitem__(self,jid): def __getitem__(self,jid):
print 'roster_zeroconf.py: __getitem__' #print 'roster_zeroconf.py: __getitem__'
return self._data[jid] return self._data[jid]
def getItems(self): def getItems(self):
print 'roster_zeroconf.py: getItems' #print 'roster_zeroconf.py: getItems'
# Return list of all [bare] JIDs that the roster currently tracks. # Return list of all [bare] JIDs that the roster currently tracks.
return self._data.keys() return self._data.keys()
def keys(self): def keys(self):
print 'roster_zeroconf.py: keys' #print 'roster_zeroconf.py: keys'
return self._data.keys() return self._data.keys()
def getRaw(self): def getRaw(self):
print 'roster_zeroconf.py: getRaw' #print 'roster_zeroconf.py: getRaw'
return self._data return self._data
def getResources(self, jid): def getResources(self, jid):
print 'roster_zeroconf.py: getResources(%s)' % jid #print 'roster_zeroconf.py: getResources(%s)' % jid
return {} return {}
def getGroups(self, jid): def getGroups(self, jid):
return self._data[jid]['groups'] return self._data[jid]['groups']
def getStatus(self, jid): def getStatus(self, jid):
return self._data[jid]['status'] if self._data.has_key(jid):
return self._data[jid]['status']
def getMessage(self, jid):
if self._data.has_key(jid):
return self._data[jid]['txt_dict']['msg']
def getShow(self, jid): def getShow(self, jid):
print 'roster_zeroconf.py: getShow' #print 'roster_zeroconf.py: getShow'
return getStatus(jid) return getStatus(jid)
def getPriority(jid): def getPriority(jid):
return 5 return 5
def getSubscription(self,jid): def getSubscription(self,jid):
print 'roster_zeroconf.py: getSubscription' #print 'roster_zeroconf.py: getSubscription'
return 'both' return 'both'
def Subscribe(self,jid): def Subscribe(self,jid):

View File

@ -70,9 +70,16 @@ class Zeroconf:
self.browse_domain(interface, protocol, domain) self.browse_domain(interface, protocol, domain)
def check_jid(self, jid): def check_jid(self, jid):
# miranda uses bad service names, so change them... # TODO: at least miranda uses bad service names(only host name), so change them - probabaly not so nice... need to find a better solution
if jid.find('@') == -1: if jid.find('@') == -1:
return 'miranda@' + jid return 'bad-client@' + jid
else:
return jid
def recreate_bad_jid(self,jid):
at = jid.find('@')
if jid[:at] == 'bad-client':
return jid[at+1:]
else: else:
return jid return jid
@ -91,8 +98,8 @@ class Zeroconf:
# different handler when resolving all contacts # different handler when resolving all contacts
def service_resolved_all_callback(self, interface, protocol, name, stype, domain, host, aprotocol, address, port, txt, flags): def service_resolved_all_callback(self, interface, protocol, name, stype, domain, host, aprotocol, address, port, txt, flags):
print "Service data for service '%s' in domain '%s' on %i.%i:" % (name, domain, interface, protocol) #print "Service data for service '%s' in domain '%s' on %i.%i:" % (name, domain, interface, protocol)
print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(avahi.txt_array_to_string_array(txt))) #print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(avahi.txt_array_to_string_array(txt)))
self.contacts[name] = (name, domain, interface, protocol, host, address, port, txt) self.contacts[name] = (name, domain, interface, protocol, host, address, port, txt)
@ -163,9 +170,10 @@ class Zeroconf:
self.create_service() self.create_service()
def remove_announce(self): def remove_announce(self):
self.entrygroup.Reset() if self.entrygroup:
self.entrygroup.Free() self.entrygroup.Reset()
self.entrygroup = None self.entrygroup.Free()
self.entrygroup = None
def browse_domain(self, interface, protocol, domain): def browse_domain(self, interface, protocol, domain):
self.new_service_type(interface, protocol, self.stype, domain, '') self.new_service_type(interface, protocol, self.stype, domain, '')
@ -190,7 +198,6 @@ class Zeroconf:
dbus.UInt32(0))), avahi.DBUS_INTERFACE_DOMAIN_BROWSER) dbus.UInt32(0))), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
db.connect_to_signal('ItemNew', self.new_domain_callback) db.connect_to_signal('ItemNew', self.new_domain_callback)
else: else:
# Just browse the domain the user wants us to browse
self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain) self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
def disconnect(self): def disconnect(self):
@ -224,10 +231,44 @@ class Zeroconf:
self.entrygroup.UpdateServiceTxt(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0), self.name, self.stype,'', txt, reply_handler=self.service_updated_callback, error_handler=self.print_error_callback) self.entrygroup.UpdateServiceTxt(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0), self.name, self.stype,'', txt, reply_handler=self.service_updated_callback, error_handler=self.print_error_callback)
# self.entrygroup.Commit() # TODO: necessary? # self.entrygroup.Commit() # TODO: necessary?
def send (self, msg, sock):
print 'send:'+msg
totalsent = 0
while totalsent < len(msg):
sent = sock.send(msg[totalsent:])
if sent == 0:
raise RuntimeError, "socket connection broken"
totalsent = totalsent + sent
def send_message(self, jid, msg):
print 'zeroconf.py: send_message:'+ msg
jid = self.recreate_bad_jid(jid)
sock = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
#sock.setblocking(False)
sock.connect ( ( self.contacts[jid][4], self.contacts[jid][6] ) )
try : recvd = sock.recv(16384)
except: recvd = ''
print 'receive:' + recvd
self.send("<?xml version='1.0' encoding='UTF-8'?><stream:stream to="+ self.contacts[jid][5] +" xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>", sock)
self.send("<message to=" + jid + " type='chat'><body>" + msg + "</body><html xmlns='html://www.w3.org/1999/xhtml'><body ichatballoncolor='#111111' ichattextcolor='#000000'><font face='Courier' ABSZ='3'>" + msg +"</font></body></html><x xmlns='jabber:x:event'><delivered /></x></message>", sock)
try: recvd = sock.recv(16384)
except: recvd = ''
print 'receive:' + recvd
self.send('</stream>', sock)
sock.close()
# END Zeroconf # END Zeroconf
''' '''
# how to use... # how to use
zeroconf = Zeroconf() zeroconf = Zeroconf()
zeroconf.connect() zeroconf.connect()