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,9 +73,9 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
#self.last_sent = []
#self.last_history_line = {}
#we don't need a password
self.password = 'dummy' # gajim.config.get_per('accounts', name, 'password')
#we don't need a password, but must be non-empty
self.password = 'dummy'
self.privacy_rules_supported = False
# Do we continue connection when we get roster (send presence,get vcard...)
self.continue_connect_info = None
@ -92,7 +92,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
gajim.config.set_per('accounts', name, 'name', self.zeroconf.username)
gajim.config.set_per('accounts', name, 'hostname', self.zeroconf.host)
# END __init__
def put_event(self, ev):
if gajim.handlers.has_key(ev[0]):
@ -109,17 +109,6 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
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):
@ -157,24 +146,25 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.zeroconf.resolve_all()
diffs = self.roster.getDiffs()
for key in diffs:
# display_key = self.zeroconf.check_jid(key)
self.dispatch('NOTIFY', (key, diffs[key], '', 'Gajim', 0, None, 0))
self.roster.setItem(key)
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
# callbacks called from zeroconf
def _on_new_service(self,jid):
self.roster.setItem(jid)
# display_jid = self.zeroconf.check_jid(jid)
self.dispatch('ROSTER_INFO', (jid, jid, 'both', 'no', self.roster.getGroups(jid)))
self.dispatch('NOTIFY', (jid, self.roster.getStatus(jid), '', 'Gajim', 0, None, 0))
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))
def _on_remove_service(self,jid):
self.roster.delItem(jid)
# 'NOTIFY' (account, (jid, status, status message, resource, priority,
# keyID, timestamp))
# jid = self.zeroconf.check_jid(jid)
self.dispatch('NOTIFY', (jid, 'offline', '', 'Gajim', 0, None, 0))
jid = self.zeroconf.check_jid(jid)
self.dispatch('NOTIFY', (jid, 'offline', '', 'local', 0, None, 0))
def connect(self, data = None, show = 'online'):
@ -185,6 +175,13 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.connection = client_zeroconf.ClientZeroconf(self.zeroconf)
self.roster = self.connection.getRoster()
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)
# refresh all contacts data all 10 seconds
@ -198,14 +195,25 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.zeroconf.txt['msg'] = msg
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):
print "connection_zeroconf.py: show: %s msg: %s in change_status" % (show, msg)
if not show in STATUS_LIST:
return -1
# 'connect'
if show != 'offline' and not self.connected:
print "connection_zeroconf.py: connect in change_status"
self.on_purpose = False
self.connect_and_init(show, msg, '')
if show != 'invisible':
@ -215,21 +223,18 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
# 'disconnect'
elif show == 'offline' and self.connected:
print "connection_zeroconf.py: disconnect in change_status"
self.connected = 0
self.dispatch('STATUS', 'offline')
self.disconnect()
# update status
elif show != 'offline' and self.connected:
print "connection_zeroconf.py: update in change_status"
was_invisible = self.connected == STATUS_LIST.index('invisible')
self.connected = STATUS_LIST.index(show)
if show == 'invisible':
self.zeroconf.remove_announce()
return
if was_invisible:
print "connection_zeroconf.py: reannounce after invisible in change_status"
self.zeroconf.announce()
if self.connection:
txt = {}
@ -243,14 +248,15 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
def send_message(self, jid, msg, keyID, type = 'chat', subject='',
chatstate = None, msg_id = None, composing_jep = None, resource = None):
'''
print 'connection_zeroconf.py: send_message'
fjid = jid
if not self.connection:
return
if not msg and chatstate is None:
return
fjid = jid
if resource:
fjid += '/' + resource
msgtxt = msg
msgenc = ''
if keyID and USE_GPG:
@ -262,8 +268,11 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
if lang is not None or lang != 'en': # we're not english
msgtxt = _('[This message is encrypted]') +\
' ([This message is encrypted])' # one in locale and one en
if type == 'chat':
msg_iq = common.xmpp.Message(to = fjid, body = msgtxt, typ = type)
else:
if subject:
msg_iq = common.xmpp.Message(to = fjid, body = msgtxt,
@ -271,9 +280,11 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
else:
msg_iq = common.xmpp.Message(to = fjid, body = msgtxt,
typ = 'normal')
if msgenc:
msg_iq.setTag(common.xmpp.NS_ENCRYPTED + ' x').setData(msgenc)
# chatstates - if peer supports jep85 or jep22, send chatstates
# please note that the only valid tag inside a message containing a <body>
# tag is the active event
@ -292,7 +303,6 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
if chatstate is 'composing' or msgtxt:
chatstate_node.addChild(name = 'composing')
self.connection.send(msg_iq)
no_log_for = gajim.config.get_per('accounts', self.name, 'no_log_for')
ji = gajim.get_jid_without_resource(jid)
if self.name not in no_log_for and ji not in no_log_for:
@ -305,17 +315,17 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
else:
kind = 'single_msg_sent'
gajim.logger.write(kind, jid, log_msg)
self.zeroconf.send_message(jid, msgtxt)
self.dispatch('MSGSENT', (jid, msg, keyID))
'''
def send_stanza(self, stanza):
# send a stanza untouched
'''
print 'connection_zeroconf.py: send_stanza'
if not self.connection:
return
self.connection.send(stanza)
'''
#self.connection.send(stanza)
pass
def ack_subscribed(self, jid):

View File

@ -10,7 +10,7 @@ class Roster:
self.setItem(jid)
def getRoster(self):
print 'roster_zeroconf.py: getRoster'
#print 'roster_zeroconf.py: getRoster'
self.update_roster()
return self
@ -25,8 +25,7 @@ class Roster:
if self._data.has_key(key):
if old_data[key] != self._data[key]:
diffs[key] = self._data[key]['status']
print 'roster_zeroconf.py: diffs:',
print diffs
#print 'roster_zeroconf.py: diffs:' + diffs
return diffs
def setItem(self, jid, name = '', groups = ''):
@ -50,57 +49,64 @@ class Roster:
status = ''
if status == 'avail': status = 'online'
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]['show'] = status
# print self._data[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):
del self._data[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):
return self._data[jid]
def __getitem__(self,jid):
print 'roster_zeroconf.py: __getitem__'
#print 'roster_zeroconf.py: __getitem__'
return self._data[jid]
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 self._data.keys()
def keys(self):
print 'roster_zeroconf.py: keys'
#print 'roster_zeroconf.py: keys'
return self._data.keys()
def getRaw(self):
print 'roster_zeroconf.py: getRaw'
#print 'roster_zeroconf.py: getRaw'
return self._data
def getResources(self, jid):
print 'roster_zeroconf.py: getResources(%s)' % jid
#print 'roster_zeroconf.py: getResources(%s)' % jid
return {}
def getGroups(self, jid):
return self._data[jid]['groups']
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):
print 'roster_zeroconf.py: getShow'
#print 'roster_zeroconf.py: getShow'
return getStatus(jid)
def getPriority(jid):
return 5
def getSubscription(self,jid):
print 'roster_zeroconf.py: getSubscription'
#print 'roster_zeroconf.py: getSubscription'
return 'both'
def Subscribe(self,jid):

View File

@ -70,12 +70,19 @@ class Zeroconf:
self.browse_domain(interface, protocol, domain)
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:
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:
return jid
def txt_array_to_dict(self,t):
l = {}
@ -91,8 +98,8 @@ class Zeroconf:
# different handler when resolving all contacts
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 "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(avahi.txt_array_to_string_array(txt)))
#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)))
self.contacts[name] = (name, domain, interface, protocol, host, address, port, txt)
@ -163,9 +170,10 @@ class Zeroconf:
self.create_service()
def remove_announce(self):
self.entrygroup.Reset()
self.entrygroup.Free()
self.entrygroup = None
if self.entrygroup:
self.entrygroup.Reset()
self.entrygroup.Free()
self.entrygroup = None
def browse_domain(self, interface, protocol, domain):
self.new_service_type(interface, protocol, self.stype, domain, '')
@ -190,7 +198,6 @@ class Zeroconf:
dbus.UInt32(0))), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
db.connect_to_signal('ItemNew', self.new_domain_callback)
else:
# Just browse the domain the user wants us to browse
self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
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.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
'''
# how to use...
# how to use
zeroconf = Zeroconf()
zeroconf.connect()