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,137 +1,169 @@
#/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
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):
return (self._hlogin + "@" + self._host)
def setHost(self,hostName): def getDesc(self):
self._host=hostName return (self._hlogin + "@" + self._host)
def setLogin(self,userName):
self._login=userName
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):
return self._proto2transport[protocolName]
else:
return None
def getProtocol(self,hostName): def setHost(self, hostName):
if self._transport2proto.has_key(hostName): self._host = hostName
return self._transport2proto[hostName]
else:
return None def setLogin(self, userName):
self._login = userName
def connect(self):
# se connecte et choppe le roster
## connexion au serveur def setPassword(self, passWord):
self._cnx=jabber.Client(host=self._host) self._password = passWord
self._cnx.setDisconnectHandler(_gdisconnectHD)
if self._cnx:
try: def setProtocol(self, protocolName, transportName):
self._cnx.connect() self._proto2transport[protocolName] = transportName
sys.stderr.write("* Connected to "+self._host+"\n")
## authentification
if self._login and self._password \ def setTransport(self, transportName, protocolName):
and self._cnx.auth(self._login, self._password, 'default'): self._transport2proto[transportName] = protocolName
sys.stderr.write(" Authenticated\n")
else:
sys.stderr.write(" Not Athenticated. \n") def getTransport(self, protocolName):
sys.stderr.write(" Disconnecting from "+self._host+"\n") if self._proto2transport.has_key(protocolName):
self._cnx=None return self._proto2transport[protocolName]
except IOError, e:
print e else:
self._cnx=None return None
def disconnect(self):
if self._cnx: def getProtocol(self,hostName):
self._cnx.disconnect() if self._transport2proto.has_key(hostName):
sys.stderr.write(" Disconnected from"+self._host+"\n") return self._transport2proto[hostName]
else:
return None
def connect(self):
# se connecte et choppe le roster
## connexion au serveur
self._cnx = jabber.Client(host=self._host)
self._cnx.setDisconnectHandler(_gdisconnectHD)
if self._cnx:
try:
self._cnx.connect()
sys.stderr.write("* Connected to "+self._host+"\n")
## authentification
if self._login and self._password \
and self._cnx.auth(self._login, self._password, 'default'):
sys.stderr.write(" Authenticated\n")
else:
sys.stderr.write(" Not Athenticated. \n")
sys.stderr.write(" Disconnecting from " + self._host+"\n")
self._cnx = None
except IOError, e:
print e
self._cnx = None
def disconnect(self):
if self._cnx:
self._cnx.disconnect()
sys.stderr.write(" Disconnected from"+self._host+"\n")
def getAgents(self):
if self._cnx:
agentsList=self._cnx.requestAgents()
sys.stderr.write(" Agents list downloaded:\n")
print agentsList
for agent in agentsList.keys():
if agent:
self.setProtocol(agentsList[agent]["service"],agent)
self.setTransport(agent,agentsList[agent]["service"])
print " - "+agent+" @ "+agentsList[agent]["service"]
return self._transport2proto
else:
return None
def getAgents(self):
if self._cnx: def getRoster(self):
agentsList=self._cnx.requestAgents() if self._cnx:
sys.stderr.write(" Agents list downloaded:\n") roster=self._cnx.requestRoster()
print agentsList sys.stderr.write(" Roster downloaded\n")
for agent in agentsList.keys(): jid_list=roster.getJIDs()
if agent: for jid in jid_list:
self.setProtocol(agentsList[agent]["service"],agent) transport=jid.getDomain()
self.setTransport(agent,agentsList[agent]["service"]) node=jid.getNode()
print " - "+agent+" @ "+agentsList[agent]["service"] if not self._rosterHash.has_key(transport):
return self._transport2proto self._rosterHash[transport]={}
else:
return None if not self._rosterHash[transport].has_key(node):
self._rosterHash[transport][node]=1
def getRoster(self): return roster
if self._cnx:
roster=self._cnx.requestRoster() else:
sys.stderr.write(" Roster downloaded\n") return None
jid_list=roster.getJIDs()
for jid in jid_list: def setRoster(self,rosterHash):
transport=jid.getDomain() # on se connecte
node=jid.getNode() if self._cnx:
if not self._rosterHash.has_key(transport): for trans_proto in rosterHash.keys():
self._rosterHash[transport]={} trans_host = self.getTransport(trans_proto)
if not self._rosterHash[transport].has_key(node): if not trans_host:
self._rosterHash[transport][node]=1 trans_host = trans_proto
return roster
else: # pour chaque id connue dans ce proto
return None for proto_id in rosterHash[trans_proto]:
if proto_id:
def setRoster(self,rosterHash): jid = jabber.JID()
# on se connecte jid.setNode(proto_id)
if self._cnx: jid.setDomain(trans_host)
for trans_proto in rosterHash.keys(): jid.setResource("")
trans_host=self.getTransport(trans_proto) jid_name, jid_groups = rosterHash[trans_proto][proto_id]
if not trans_host: trans_host=trans_proto sys.stdout.write(" -> " + proto_id)
# pour chaque id connue dans ce proto sys.stdout.write(" @ " + trans_host)
for proto_id in rosterHash[trans_proto]: sys.stdout.write(" = " + \
if proto_id: jid_name.encode('iso-8859-1','replace') + \
jid=jabber.JID() "...")
jid.setNode(proto_id)
jid.setDomain(trans_host) sys.stdout.flush()
jid.setResource("") if not(self._rosterHash.has_key(trans_host) \
jid_name,jid_groups=rosterHash[trans_proto][proto_id] and self._rosterHash[trans_host].has_key(proto_id)):
sys.stdout.write(" -> "+proto_id) self._cnx.addRosterItem(jid)
sys.stdout.write(" @ "+trans_host)
sys.stdout.write(" = "+jid_name.encode('iso-8859-1','replace')+"...") sys.stdout.write(" [done]\n")
sys.stdout.flush()
if not(self._rosterHash.has_key(trans_host) \ else:
and self._rosterHash[trans_host].has_key(proto_id)): #ne pas faire la mise a jour, le contact existe deja la-bas
self._cnx.addRosterItem(jid) sys.stdout.write(" [exists]\n")
sys.stdout.write(" [done]\n") # on met d'abord a jour le nom
else:
#ne pas faire la mise a jour, le contact existe deja la-bas self._cnx.updateRosterItem(jid, name = jid_name)
sys.stdout.write(" [exists]\n") # puis le groupe
# on met d'abord a jour le nom self._cnx.updateRosterItem(jid, \
self._cnx.updateRosterItem(jid,name=jid_name) name = jid_name, \
# puis le groupe groups=jid_groups)
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 *