jabsync/account.py
2006-02-13 22:07:08 +00:00

137 lines
4 KiB
Python

#/usr/bin/env python
import jabber
import sys
def _gdisconnectHD(param=None):
if param:
print " Disconnecting : "+param
return None
class Account:
def __init__(self):
self._host=None
self._login=None
self._password=None
self._rosterHash={}
self._proto2transport={}
self._transport2proto={}
self._cnx=None
def getDesc(self):
return (self._hlogin + "@" + self._host)
def setHost(self,hostName):
self._host=hostName
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):
if self._transport2proto.has_key(hostName):
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 getRoster(self):
if self._cnx:
roster=self._cnx.requestRoster()
sys.stderr.write(" Roster downloaded\n")
jid_list=roster.getJIDs()
for jid in jid_list:
transport=jid.getDomain()
node=jid.getNode()
if not self._rosterHash.has_key(transport):
self._rosterHash[transport]={}
if not self._rosterHash[transport].has_key(node):
self._rosterHash[transport][node]=1
return roster
else:
return None
def setRoster(self,rosterHash):
# on se connecte
if self._cnx:
for trans_proto in rosterHash.keys():
trans_host=self.getTransport(trans_proto)
if not trans_host: trans_host=trans_proto
# pour chaque id connue dans ce proto
for proto_id in rosterHash[trans_proto]:
if proto_id:
jid=jabber.JID()
jid.setNode(proto_id)
jid.setDomain(trans_host)
jid.setResource("")
jid_name,jid_groups=rosterHash[trans_proto][proto_id]
sys.stdout.write(" -> "+proto_id)
sys.stdout.write(" @ "+trans_host)
sys.stdout.write(" = "+jid_name.encode('iso-8859-1','replace')+"...")
sys.stdout.flush()
if not(self._rosterHash.has_key(trans_host) \
and self._rosterHash[trans_host].has_key(proto_id)):
self._cnx.addRosterItem(jid)
sys.stdout.write(" [done]\n")
else:
#ne pas faire la mise a jour, le contact existe deja la-bas
sys.stdout.write(" [exists]\n")
# 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,groups=jid_groups)