This commit is contained in:
glenux 2006-02-13 22:44:29 +00:00
parent 623d584382
commit 3eff1b83b9
2 changed files with 156 additions and 123 deletions

View file

@ -1,4 +1,5 @@
#/usr/bin/env python #/usr/bin/env python
# vim: set tw=72 ts=4 sw=4 st et sts si at:
import jabber import jabber
import sys import sys
@ -6,52 +7,65 @@ import sys
def _gdisconnectHD(param=None): def _gdisconnectHD(param=None):
if param: if param:
print " Disconnecting : "+param print " Disconnecting : "+param
return None return None
class Account: class Account:
def __init__(self): def __init__(self):
self._host=None self._host = None
self._login=None self._login = None
self._password=None self._password = None
self._rosterHash={} self._rosterHash = {}
self._proto2transport={} self._proto2transport = {}
self._transport2proto={} self._transport2proto = {}
self._cnx=None self._cnx = None
def getDesc(self): def getDesc(self):
return (self._hlogin + "@" + self._host) return (self._hlogin + "@" + self._host)
def setHost(self,hostName):
self._host=hostName
def setLogin(self,userName): def setHost(self, hostName):
self._login=userName self._host = hostName
def setPassword(self,passWord):
self._password=passWord
def setProtocol(self,protocolName,transportName): def setLogin(self, userName):
self._proto2transport[protocolName]=transportName self._login = userName
def setTransport(self,transportName,protocolName):
self._transport2proto[transportName]=protocolName
def getTransport(self,protocolName): def setPassword(self, passWord):
self._password = passWord
def setProtocol(self, protocolName, transportName):
self._proto2transport[protocolName] = transportName
def setTransport(self, transportName, protocolName):
self._transport2proto[transportName] = protocolName
def getTransport(self, protocolName):
if self._proto2transport.has_key(protocolName): if self._proto2transport.has_key(protocolName):
return self._proto2transport[protocolName] return self._proto2transport[protocolName]
else: else:
return None return None
def getProtocol(self,hostName): def getProtocol(self,hostName):
if self._transport2proto.has_key(hostName): if self._transport2proto.has_key(hostName):
return self._transport2proto[hostName] return self._transport2proto[hostName]
else: else:
return None return None
def connect(self): def connect(self):
# se connecte et choppe le roster # se connecte et choppe le roster
## connexion au serveur ## connexion au serveur
self._cnx=jabber.Client(host=self._host) self._cnx = jabber.Client(host=self._host)
self._cnx.setDisconnectHandler(_gdisconnectHD) self._cnx.setDisconnectHandler(_gdisconnectHD)
if self._cnx: if self._cnx:
try: try:
@ -61,13 +75,16 @@ class Account:
if self._login and self._password \ if self._login and self._password \
and self._cnx.auth(self._login, self._password, 'default'): and self._cnx.auth(self._login, self._password, 'default'):
sys.stderr.write(" Authenticated\n") sys.stderr.write(" Authenticated\n")
else: else:
sys.stderr.write(" Not Athenticated. \n") sys.stderr.write(" Not Athenticated. \n")
sys.stderr.write(" Disconnecting from "+self._host+"\n") sys.stderr.write(" Disconnecting from " + self._host+"\n")
self._cnx=None self._cnx = None
except IOError, e: except IOError, e:
print e print e
self._cnx=None self._cnx = None
def disconnect(self): def disconnect(self):
if self._cnx: if self._cnx:
@ -86,9 +103,12 @@ class Account:
self.setTransport(agent,agentsList[agent]["service"]) self.setTransport(agent,agentsList[agent]["service"])
print " - "+agent+" @ "+agentsList[agent]["service"] print " - "+agent+" @ "+agentsList[agent]["service"]
return self._transport2proto return self._transport2proto
else: else:
return None return None
def getRoster(self): def getRoster(self):
if self._cnx: if self._cnx:
roster=self._cnx.requestRoster() roster=self._cnx.requestRoster()
@ -99,9 +119,11 @@ class Account:
node=jid.getNode() node=jid.getNode()
if not self._rosterHash.has_key(transport): if not self._rosterHash.has_key(transport):
self._rosterHash[transport]={} self._rosterHash[transport]={}
if not self._rosterHash[transport].has_key(node): if not self._rosterHash[transport].has_key(node):
self._rosterHash[transport][node]=1 self._rosterHash[transport][node]=1
return roster return roster
else: else:
return None return None
@ -109,29 +131,39 @@ class Account:
# on se connecte # on se connecte
if self._cnx: if self._cnx:
for trans_proto in rosterHash.keys(): for trans_proto in rosterHash.keys():
trans_host=self.getTransport(trans_proto) trans_host = self.getTransport(trans_proto)
if not trans_host: trans_host=trans_proto if not trans_host:
trans_host = trans_proto
# pour chaque id connue dans ce proto # pour chaque id connue dans ce proto
for proto_id in rosterHash[trans_proto]: for proto_id in rosterHash[trans_proto]:
if proto_id: if proto_id:
jid=jabber.JID() jid = jabber.JID()
jid.setNode(proto_id) jid.setNode(proto_id)
jid.setDomain(trans_host) jid.setDomain(trans_host)
jid.setResource("") jid.setResource("")
jid_name,jid_groups=rosterHash[trans_proto][proto_id] jid_name, jid_groups = rosterHash[trans_proto][proto_id]
sys.stdout.write(" -> "+proto_id) sys.stdout.write(" -> " + proto_id)
sys.stdout.write(" @ "+trans_host) sys.stdout.write(" @ " + trans_host)
sys.stdout.write(" = "+jid_name.encode('iso-8859-1','replace')+"...") sys.stdout.write(" = " + \
jid_name.encode('iso-8859-1','replace') + \
"...")
sys.stdout.flush() sys.stdout.flush()
if not(self._rosterHash.has_key(trans_host) \ if not(self._rosterHash.has_key(trans_host) \
and self._rosterHash[trans_host].has_key(proto_id)): and self._rosterHash[trans_host].has_key(proto_id)):
self._cnx.addRosterItem(jid) self._cnx.addRosterItem(jid)
sys.stdout.write(" [done]\n") sys.stdout.write(" [done]\n")
else: else:
#ne pas faire la mise a jour, le contact existe deja la-bas #ne pas faire la mise a jour, le contact existe deja la-bas
sys.stdout.write(" [exists]\n") sys.stdout.write(" [exists]\n")
# on met d'abord a jour le nom # on met d'abord a jour le nom
self._cnx.updateRosterItem(jid,name=jid_name)
# puis le groupe self._cnx.updateRosterItem(jid, name = jid_name)
self._cnx.updateRosterItem(jid,name=jid_name,groups=jid_groups) # puis le groupe
self._cnx.updateRosterItem(jid, \
name = jid_name, \
groups=jid_groups)

View file

@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
# vim: set sw=4 ts=4 si et:
from xml.dom.ext import * from xml.dom.ext import *
from xml.dom.ext.reader.Sax import * from xml.dom.ext.reader.Sax import *