jabsync/start.py

162 lines
5.3 KiB
Python
Raw Normal View History

2005-08-16 10:12:25 +00:00
#!/usr/bin/env python
2006-02-13 22:07:08 +00:00
# vim: set sw=4 ts=4 si et:
2005-08-16 10:12:25 +00:00
import sys
import os
import string
#from qt import *
from jabber import *
import StringIO
#import time
from account import Account
from config import Config
#from re import *
from optparse import OptionParser
#from threading import *
from xml.sax import saxutils
from xml.sax import make_parser
# for prettyprint
from xml.dom.ext import *
from xml.dom.ext.reader.Sax import *
class Answer:
2006-02-13 22:07:08 +00:00
def __init__(self, question, answerlist):
self._answer = None
while not self._answer:
sys.stderr.write(question)
tmp_ans = raw_input()
if tmp_ans in (answerlist):
self._answer = tmp_ans
2005-08-16 10:12:25 +00:00
def get(self):
return self._answer;
class JabSync:
def __init__(self):
# init des var
2006-02-13 22:07:08 +00:00
self._commonJIDs = {}
self._commonTransports = {}
self._configFile = None
self._config = None
self._accountList = None
self._jabservers = {}
2005-08-16 10:12:25 +00:00
2006-02-13 22:07:08 +00:00
def parseCmdLine(self):
parser = OptionParser()
parser.add_option(\
"-c","--config",\
action="store", \
type="string", \
dest="filename",\
help="Use file a configuration file",\
metavar="config")
(options,args)=parser.parse_args()
if options.filename:
self._configFile=options.filename
else:
self._configFile=os.path.join(os.path.expanduser("~"),".jabsync")
print "Configuration file: " + self._configFile
def init(self):
self._config = Config()
self._config.loadFromFile(self._configFile)
self._accountList = self._config.getAccounts()
for account in self._accountList:
account.connect()
agents = account.getAgents()
self._commonTransports.update(agents)
for transport in self._commonTransports.keys():
print "Transport: "+transport
2005-08-16 10:12:25 +00:00
2006-02-13 22:07:08 +00:00
def giveTransport(self, hostName):
if self._jabservers.has_key(hostName):
trans_proto = hostName
return trans_proto
2005-08-16 10:12:25 +00:00
else:
2006-02-13 22:07:08 +00:00
if self._commonTransports.has_key(hostName):
trans_proto = self._commonTransports[hostName]
return trans_proto
else:
# TODO: ask user input
is_jabber = Answer(\
"# ERR : is "+hostName+" a jabber server ? [y/n]\n",\
["y","n"]).get()
if is_jabber == "y":
self._jabservers[hostName] = 1
else:
what_tp_ans = {\
"a" : "aim",\
"i" : "icq",\
"r" : "irc",\
"g" : "gadu",\
"m" : "msn",\
"p" : "public",\
"v" : "private",\
"y" : "yahoo",\
"-" : "forget"}
what_transport = Answer(\
" Select transport type :\n"+\
" [a]im"+\
" [i]cq"+\
" i[r]c):"+\
" [g]adu"+\
" [m]sn"+\
" [p]ublic:"+\
" pri[v]ate:"+\
" [y]ahoo"+\
" [-] Forget JIDs using this host",\
what_tp_ans.keys())
trans_proto = what_tp_ans[what_transport.get()]
print "Selected : " + trans_proto
if trans_proto == "forget":
# TODO: ajouter regles d'exclusion
zero = 0
else:
# TODO: ajouter transport a la config XML
self._commonTransports[hostName] = trans_proto
zero = 0
return self.giveTransport(hostName)
def get(self):
print "jabsync::get\n"
#get Roster
for account in self._accountList:
roster = account.getRoster()
# fusionner les roster
jid_list = roster.getJIDs()
for jid in jid_list:
print "Getting... "
print jid
transport = jid.getDomain()
trans_proto = self.giveTransport(transport)
if not self._commonJIDs.has_key(trans_proto):
self._commonJIDs[trans_proto]={}
jid_name = roster.getName(jid)
jid_groups = roster.getGroups(jid)
if not jid_name: jid_name = jid.getNode()
if not self._commonJIDs[trans_proto].has_key(jid.getNode()):
self._commonJIDs[trans_proto][jid.getNode()] = \
(jid_name,jid_groups)
print "\x1B[G <- "+jid.getNode()+" @ "+trans_proto+" = "+jid_name
2005-08-16 10:12:25 +00:00
#upload rosters
2006-02-13 22:07:08 +00:00
def set(self):
for account in self._accountList:
account.setRoster(self._commonJIDs)
2005-08-16 10:12:25 +00:00
2006-02-13 22:07:08 +00:00
def stop(self):
for account in self._accountList:
account.disconnect()
2005-08-16 10:12:25 +00:00
if __name__ == "__main__":
2006-02-13 22:07:08 +00:00
jabsync=JabSync()
jabsync.parseCmdLine()
jabsync.init()
jabsync.get()
jabsync.set()
jabsync.stop()
2005-08-16 10:12:25 +00:00