2003-10-22 22:23:45 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
## core/core.py
|
|
|
|
##
|
|
|
|
## Gajim Team:
|
|
|
|
## - Yann Le Boulanger <asterix@crans.org>
|
|
|
|
## - Vincent Hanquez <tab@tuxfamily.org>
|
|
|
|
##
|
|
|
|
## Copyright (C) 2003 Gajim Team
|
|
|
|
##
|
|
|
|
## This program is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published
|
|
|
|
## by the Free Software Foundation; version 2 only.
|
|
|
|
##
|
|
|
|
## This program is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
|
|
|
|
import sys
|
2003-11-30 16:50:23 +01:00
|
|
|
|
|
|
|
sys.path.append("..")
|
2003-10-22 22:23:45 +02:00
|
|
|
import time
|
2003-11-15 13:38:43 +01:00
|
|
|
import string
|
2003-10-22 22:23:45 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import common.hub
|
|
|
|
import common.jabber
|
|
|
|
import common.optparser
|
|
|
|
|
|
|
|
log = logging.getLogger('core.core')
|
|
|
|
log.setLevel(logging.DEBUG)
|
2003-11-30 16:50:23 +01:00
|
|
|
|
2004-01-18 23:56:28 +01:00
|
|
|
CONFPATH = "~/.gajim/config"
|
2003-10-22 22:23:45 +02:00
|
|
|
|
|
|
|
class GajimCore:
|
2004-01-17 16:38:29 +01:00
|
|
|
"""Core"""
|
2003-10-22 22:23:45 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.connected = 0
|
|
|
|
self.cfgParser = common.optparser.OptionsParser(CONFPATH)
|
|
|
|
self.hub = common.hub.GajimHub()
|
2004-01-24 21:32:51 +01:00
|
|
|
self.parse()
|
2003-10-22 22:23:45 +02:00
|
|
|
# END __init__
|
|
|
|
|
2004-01-24 21:32:51 +01:00
|
|
|
def parse(self):
|
|
|
|
self.cfgParser.parseCfgFile()
|
|
|
|
self.accounts = {}
|
|
|
|
accts = self.cfgParser.tab['Profile']['accounts']
|
|
|
|
for a in string.split(accts, ' '):
|
|
|
|
self.accounts[a] = self.cfgParser.tab[a]
|
|
|
|
|
2003-10-22 22:23:45 +02:00
|
|
|
def messageCB(self, con, msg):
|
2004-01-17 16:38:29 +01:00
|
|
|
"""Called when we recieve a message"""
|
2003-10-22 22:23:45 +02:00
|
|
|
self.hub.sendPlugin('MSG', (msg.getFrom().getBasic(), \
|
|
|
|
msg.getBody()))
|
|
|
|
# END messageCB
|
|
|
|
|
|
|
|
def presenceCB(self, con, prs):
|
2004-01-17 16:38:29 +01:00
|
|
|
"""Called when we recieve a presence"""
|
2003-10-22 22:23:45 +02:00
|
|
|
who = str(prs.getFrom())
|
|
|
|
type = prs.getType()
|
|
|
|
if type == None: type = 'available'
|
2003-10-27 17:30:37 +01:00
|
|
|
log.debug("PresenceCB : %s" % type)
|
2003-10-22 22:23:45 +02:00
|
|
|
if type == 'available':
|
|
|
|
if prs.getShow():
|
|
|
|
show = prs.getShow()
|
|
|
|
else:
|
|
|
|
show = 'online'
|
2004-01-24 21:32:51 +01:00
|
|
|
self.hub.sendPlugin('NOTIFY', (prs.getFrom().getBasic(), \
|
|
|
|
show, prs.getStatus(), prs.getFrom().getResource()))
|
2003-10-27 17:30:37 +01:00
|
|
|
elif type == 'unavailable':
|
2003-10-23 01:05:36 +02:00
|
|
|
self.hub.sendPlugin('NOTIFY', \
|
2004-01-24 21:32:51 +01:00
|
|
|
(prs.getFrom().getBasic(), 'offline', prs.getStatus(), \
|
|
|
|
prs.getFrom().getResource()))
|
2003-10-27 17:30:37 +01:00
|
|
|
elif type == 'subscribe':
|
|
|
|
log.debug("subscribe request from %s" % who)
|
2004-01-24 21:32:51 +01:00
|
|
|
if self.cfgParser.Core['alwaysauth'] == 1 or \
|
|
|
|
string.find(who, "@") <= 0:
|
2003-11-04 13:14:40 +01:00
|
|
|
self.con.send(common.jabber.Presence(who, 'subscribed'))
|
2003-12-20 14:22:37 +01:00
|
|
|
if string.find(who, "@") <= 0:
|
2004-01-24 21:32:51 +01:00
|
|
|
self.hub.sendPlugin('NOTIFY', (who, 'offline', 'offline', \
|
|
|
|
prs.getFrom().getResource()))
|
2003-11-04 13:14:40 +01:00
|
|
|
else:
|
|
|
|
self.hub.sendPlugin('SUBSCRIBE', who)
|
2003-10-27 17:30:37 +01:00
|
|
|
elif type == 'subscribed':
|
2003-10-28 20:37:40 +01:00
|
|
|
jid = prs.getFrom()
|
|
|
|
self.hub.sendPlugin('SUBSCRIBED', {'jid':jid.getBasic(), \
|
2004-01-14 02:06:27 +01:00
|
|
|
'nom':jid.getNode(), 'ressource':jid.getResource()})
|
|
|
|
self.hub.queueIn.put(('UPDUSER', (jid.getBasic(), \
|
|
|
|
jid.getNode(), ['general'])))
|
2004-01-24 21:32:51 +01:00
|
|
|
#BE CAREFUL : no self.con.updateRosterItem() in a callback
|
2003-10-27 17:30:37 +01:00
|
|
|
log.debug("we are now subscribed to %s" % who)
|
|
|
|
elif type == 'unsubscribe':
|
|
|
|
log.debug("unsubscribe request from %s" % who)
|
|
|
|
elif type == 'unsubscribed':
|
|
|
|
log.debug("we are now unsubscribed to %s" % who)
|
2004-01-22 00:09:03 +01:00
|
|
|
self.hub.sendPlugin('UNSUBSCRIBED', prs.getFrom().getBasic())
|
2003-12-27 03:17:28 +01:00
|
|
|
elif type == 'error':
|
2004-01-24 21:32:51 +01:00
|
|
|
print "\n\n******** ERROR *******"
|
|
|
|
#print "From : %s" % prs.getFrom()
|
|
|
|
#print "To : %s" % prs.getTo()
|
|
|
|
#print "Status : %s" % prs.getStatus()
|
|
|
|
#print "Show : %s" % prs.getShow()
|
|
|
|
#print "X : %s" % prs.getX()
|
|
|
|
#print "XNode : %s" % prs.getXNode()
|
|
|
|
#print "XPayload : %s" % prs.getXPayload()
|
|
|
|
#print "_node : %s" % prs._node.getData()
|
|
|
|
#print "kids : %s" % prs._node.kids[0].getData()
|
|
|
|
#print "\n\n"
|
2003-12-27 03:17:28 +01:00
|
|
|
errmsg = prs._node.kids[0].getData()
|
2003-10-22 22:23:45 +02:00
|
|
|
# END presenceCB
|
|
|
|
|
|
|
|
def disconnectedCB(self, con):
|
2004-01-17 16:38:29 +01:00
|
|
|
"""Called when we are disconnected"""
|
2003-10-22 22:23:45 +02:00
|
|
|
log.debug("disconnectedCB")
|
2004-01-14 02:06:27 +01:00
|
|
|
if self.connected == 1:
|
|
|
|
self.connected = 0
|
|
|
|
self.con.disconnect()
|
2004-01-20 13:46:27 +01:00
|
|
|
self.hub.sendPlugin('STATUS', 'offline')
|
2003-10-22 22:23:45 +02:00
|
|
|
# END disconenctedCB
|
2003-11-01 20:41:35 +01:00
|
|
|
|
2003-11-30 23:40:24 +01:00
|
|
|
def connect(self, account):
|
2004-01-17 16:38:29 +01:00
|
|
|
"""Connect and authentificate to the Jabber server"""
|
2004-01-24 21:32:51 +01:00
|
|
|
hostname = self.cfgParser.tab[account]["hostname"]
|
|
|
|
name = self.cfgParser.tab[account]["name"]
|
|
|
|
password = self.cfgParser.tab[account]["password"]
|
|
|
|
ressource = self.cfgParser.tab[account]["ressource"]
|
|
|
|
self.con = common.jabber.Client(host = hostname, \
|
|
|
|
debug = [common.jabber.DBG_ALWAYS], log = sys.stderr, \
|
|
|
|
connection=common.xmlstream.TCP, port=5222)
|
|
|
|
#debug = [common.jabber.DBG_ALWAYS], log = sys.stderr, \
|
|
|
|
#connection=common.xmlstream.TCP_SSL, port=5223)
|
2003-10-22 22:23:45 +02:00
|
|
|
try:
|
|
|
|
self.con.connect()
|
|
|
|
except IOError, e:
|
2003-12-02 13:39:28 +01:00
|
|
|
log.debug("Couldn't connect to %s %s" % (hostname, e))
|
2004-01-08 00:49:23 +01:00
|
|
|
self.hub.sendPlugin('STATUS', 'offline')
|
|
|
|
self.hub.sendPlugin('WARNING', "Couldn't connect to %s" % hostname)
|
2003-12-02 13:39:28 +01:00
|
|
|
return 0
|
2004-01-17 16:38:29 +01:00
|
|
|
except self.con.socket.gaierror, e:
|
|
|
|
log.debug("Couldn't connect to %s %s" % (hostname, e))
|
|
|
|
self.hub.sendPlugin('STATUS', 'offline')
|
|
|
|
self.hub.sendPlugin('WARNING', "Couldn't connect to %s" % hostname)
|
|
|
|
return 0
|
2003-10-22 22:23:45 +02:00
|
|
|
else:
|
|
|
|
log.debug("Connected to server")
|
|
|
|
|
2004-01-14 02:06:27 +01:00
|
|
|
self.con.registerHandler('message', self.messageCB)
|
|
|
|
self.con.registerHandler('presence', self.presenceCB)
|
2003-10-22 22:23:45 +02:00
|
|
|
self.con.setDisconnectHandler(self.disconnectedCB)
|
2003-12-02 13:39:28 +01:00
|
|
|
#BUG in jabberpy library : if hostname is wrong : "boucle"
|
2003-11-30 23:40:24 +01:00
|
|
|
if self.con.auth(name, password, ressource):
|
2003-10-22 22:23:45 +02:00
|
|
|
self.con.requestRoster()
|
2003-10-29 13:58:58 +01:00
|
|
|
roster = self.con.getRoster().getRaw()
|
2003-11-02 22:39:40 +01:00
|
|
|
if not roster :
|
|
|
|
roster = {}
|
2003-10-29 13:58:58 +01:00
|
|
|
self.hub.sendPlugin('ROSTER', roster)
|
2003-10-22 22:23:45 +02:00
|
|
|
self.con.sendInitPresence()
|
2004-01-08 00:49:23 +01:00
|
|
|
self.hub.sendPlugin('STATUS', 'online')
|
2003-10-22 22:23:45 +02:00
|
|
|
self.connected = 1
|
|
|
|
else:
|
2003-12-02 13:39:28 +01:00
|
|
|
log.debug("Couldn't authentificate to %s" % hostname)
|
2004-01-08 00:49:23 +01:00
|
|
|
self.hub.sendPlugin('STATUS', 'offline')
|
2004-01-24 21:32:51 +01:00
|
|
|
self.hub.sendPlugin('WARNING', \
|
|
|
|
'Authentification failed, check your login and password')
|
2003-12-02 13:39:28 +01:00
|
|
|
return 0
|
2003-10-22 22:23:45 +02:00
|
|
|
# END connect
|
|
|
|
|
|
|
|
def mainLoop(self):
|
2004-01-24 21:32:51 +01:00
|
|
|
"""Main Loop : Read the incomming queue to execute commands comming from
|
|
|
|
plugins and process Jabber"""
|
2003-10-22 22:23:45 +02:00
|
|
|
while 1:
|
|
|
|
if not self.hub.queueIn.empty():
|
|
|
|
ev = self.hub.queueIn.get()
|
|
|
|
if ev[0] == 'QUIT':
|
|
|
|
if self.connected == 1:
|
2004-01-14 02:06:27 +01:00
|
|
|
self.connected = 0
|
2003-10-22 22:23:45 +02:00
|
|
|
self.con.disconnect()
|
2003-11-26 01:02:43 +01:00
|
|
|
self.hub.sendPlugin('QUIT', ())
|
2003-10-22 22:23:45 +02:00
|
|
|
return
|
2004-01-24 21:32:51 +01:00
|
|
|
#('ASK_CONFIG', section)
|
|
|
|
elif ev[0] == 'ASK_CONFIG':
|
|
|
|
if ev[1] == 'accounts':
|
|
|
|
self.hub.sendPlugin('CONFIG', self.accounts)
|
|
|
|
else:
|
|
|
|
self.hub.sendPlugin('CONFIG', \
|
|
|
|
self.cfgParser.__getattr__(ev[1]))
|
|
|
|
#('CONFIG', (section, config))
|
|
|
|
elif ev[0] == 'CONFIG':
|
|
|
|
if ev[1][0] == 'accounts':
|
|
|
|
#Remove all old accounts
|
|
|
|
accts = string.split(self.cfgParser.tab\
|
|
|
|
['Profile']['accounts'], ' ')
|
|
|
|
for a in accts:
|
|
|
|
del self.cfgParser.tab[a]
|
|
|
|
#Write all new accounts
|
|
|
|
accts = ev[1][1].keys()
|
|
|
|
self.cfgParser.tab['Profile']['accounts'] = \
|
|
|
|
string.join(accts)
|
|
|
|
for a in accts:
|
|
|
|
self.cfgParser.tab[a] = ev[1][1][a]
|
|
|
|
else:
|
|
|
|
self.cfgParser.tab[ev[1][0]] = ev[1][1]
|
|
|
|
self.cfgParser.writeCfgFile()
|
|
|
|
#TODO: tell the changes to other plugins
|
2004-01-14 02:06:27 +01:00
|
|
|
#('STATUS', (status, msg, account))
|
2003-10-22 22:23:45 +02:00
|
|
|
elif ev[0] == 'STATUS':
|
2003-11-30 23:40:24 +01:00
|
|
|
if (ev[1][0] != 'offline') and (self.connected == 0):
|
2004-01-14 02:06:27 +01:00
|
|
|
self.connect(ev[1][2])
|
2003-11-30 23:40:24 +01:00
|
|
|
elif (ev[1][0] == 'offline') and (self.connected == 1):
|
2004-01-14 02:06:27 +01:00
|
|
|
self.connected = 0
|
2003-10-22 22:23:45 +02:00
|
|
|
self.con.disconnect()
|
2004-01-08 00:49:23 +01:00
|
|
|
self.hub.sendPlugin('STATUS', 'offline')
|
|
|
|
if ev[1][0] != 'offline' and self.connected == 1:
|
2003-11-09 23:29:41 +01:00
|
|
|
p = common.jabber.Presence()
|
2003-11-30 23:44:05 +01:00
|
|
|
p.setShow(ev[1][0])
|
2004-01-14 02:06:27 +01:00
|
|
|
p.setStatus(ev[1][1])
|
2003-11-09 23:29:41 +01:00
|
|
|
self.con.send(p)
|
2004-01-08 00:49:23 +01:00
|
|
|
self.hub.sendPlugin('STATUS', ev[1][0])
|
2003-10-27 17:30:37 +01:00
|
|
|
#('MSG', (jid, msg))
|
2003-10-22 22:23:45 +02:00
|
|
|
elif ev[0] == 'MSG':
|
|
|
|
msg = common.jabber.Message(ev[1][0], ev[1][1])
|
|
|
|
msg.setType('chat')
|
|
|
|
self.con.send(msg)
|
2003-11-26 01:02:43 +01:00
|
|
|
self.hub.sendPlugin('MSGSENT', ev[1])
|
2003-10-27 17:30:37 +01:00
|
|
|
#('SUB', (jid, txt))
|
|
|
|
elif ev[0] == 'SUB':
|
|
|
|
log.debug('subscription request for %s' % ev[1][0])
|
|
|
|
self.con.send(common.jabber.Presence(ev[1][0], 'subscribe'))
|
2003-11-02 22:39:40 +01:00
|
|
|
#('REQ', jid)
|
|
|
|
elif ev[0] == 'AUTH':
|
|
|
|
self.con.send(common.jabber.Presence(ev[1], 'subscribed'))
|
2003-11-04 13:14:40 +01:00
|
|
|
#('DENY', jid)
|
|
|
|
elif ev[0] == 'DENY':
|
|
|
|
self.con.send(common.jabber.Presence(ev[1], 'unsubscribed'))
|
2003-10-27 17:30:37 +01:00
|
|
|
#('UNSUB', jid)
|
|
|
|
elif ev[0] == 'UNSUB':
|
2004-01-24 21:32:51 +01:00
|
|
|
if self.cfgParser.Core.has_key('delauth'):
|
|
|
|
delauth = self.cfgParser.Core['delauth']
|
|
|
|
else:
|
|
|
|
delauth = 1
|
|
|
|
if self.cfgParser.Core.has_key('delroster'):
|
|
|
|
delroster = self.cfgParser.Core['delroster']
|
|
|
|
else:
|
|
|
|
delroster = 1
|
2003-10-28 20:37:40 +01:00
|
|
|
if delauth:
|
|
|
|
self.con.send(common.jabber.Presence(ev[1], 'unsubscribe'))
|
|
|
|
if delroster:
|
|
|
|
self.con.removeRosterItem(ev[1])
|
2003-11-03 00:35:38 +01:00
|
|
|
#('UPDUSER', (jid, name, groups))
|
|
|
|
elif ev[0] == 'UPDUSER':
|
2004-01-24 21:32:51 +01:00
|
|
|
self.con.updateRosterItem(jid=ev[1][0], name=ev[1][1], \
|
|
|
|
groups=ev[1][2])
|
2003-11-12 22:51:38 +01:00
|
|
|
elif ev[0] == 'REQ_AGENTS':
|
|
|
|
agents = self.con.requestAgents()
|
2003-11-15 13:38:43 +01:00
|
|
|
self.hub.sendPlugin('AGENTS', agents)
|
|
|
|
elif ev[0] == 'REQ_AGENT_INFO':
|
|
|
|
self.con.requestRegInfo(ev[1])
|
|
|
|
agent_info = self.con.getRegInfo()
|
|
|
|
self.hub.sendPlugin('AGENT_INFO', (ev[1], agent_info))
|
|
|
|
elif ev[0] == 'REG_AGENT':
|
|
|
|
self.con.sendRegInfo(ev[1])
|
2003-12-14 01:47:00 +01:00
|
|
|
#('NEW_ACC', (hostname, login, password, name, ressource))
|
|
|
|
elif ev[0] == 'NEW_ACC':
|
|
|
|
c = common.jabber.Client(host = \
|
|
|
|
ev[1][0], debug = False, log = sys.stderr)
|
|
|
|
try:
|
|
|
|
c.connect()
|
|
|
|
except IOError, e:
|
|
|
|
log.debug("Couldn't connect to %s %s" % (hostname, e))
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
log.debug("Connected to server")
|
|
|
|
c.requestRegInfo()
|
|
|
|
req = c.getRegInfo()
|
|
|
|
c.setRegInfo( 'username', ev[1][1])
|
|
|
|
c.setRegInfo( 'password', ev[1][2])
|
|
|
|
#FIXME: if users already exist, no error message :(
|
|
|
|
if not c.sendRegInfo():
|
|
|
|
print "error " + c.lastErr
|
|
|
|
else:
|
|
|
|
self.hub.sendPlugin('ACC_OK', ev[1])
|
2003-10-27 17:30:37 +01:00
|
|
|
else:
|
2004-01-24 21:32:51 +01:00
|
|
|
log.debug("Unknown Command %s" % ev[0])
|
2003-10-22 22:23:45 +02:00
|
|
|
elif self.connected == 1:
|
|
|
|
self.con.process(1)
|
|
|
|
time.sleep(0.1)
|
|
|
|
# END main
|
|
|
|
# END GajimCore
|
|
|
|
|
2003-11-30 16:50:23 +01:00
|
|
|
def loadPlugins(gc):
|
2004-01-24 21:32:51 +01:00
|
|
|
"""Load defaults plugins : plugins in 'modules' option of Core section
|
|
|
|
in ConfFile and register them to the hub"""
|
|
|
|
modStr = gc.cfgParser.Core['modules']
|
2004-01-17 16:38:29 +01:00
|
|
|
if modStr:
|
2003-12-20 14:22:37 +01:00
|
|
|
mods = string.split (modStr, ' ')
|
2003-11-30 16:50:23 +01:00
|
|
|
|
2003-12-20 14:22:37 +01:00
|
|
|
for mod in mods:
|
|
|
|
modObj = gc.hub.newPlugin(mod)
|
|
|
|
gc.hub.register(mod, 'ROSTER')
|
2004-01-08 00:49:23 +01:00
|
|
|
gc.hub.register(mod, 'WARNING')
|
|
|
|
gc.hub.register(mod, 'STATUS')
|
2003-12-20 14:22:37 +01:00
|
|
|
gc.hub.register(mod, 'NOTIFY')
|
|
|
|
gc.hub.register(mod, 'MSG')
|
2003-12-20 23:42:10 +01:00
|
|
|
gc.hub.register(mod, 'MSGSENT')
|
2003-12-20 14:22:37 +01:00
|
|
|
gc.hub.register(mod, 'SUBSCRIBED')
|
|
|
|
gc.hub.register(mod, 'SUBSCRIBE')
|
|
|
|
gc.hub.register(mod, 'AGENTS')
|
|
|
|
gc.hub.register(mod, 'AGENT_INFO')
|
|
|
|
gc.hub.register(mod, 'QUIT')
|
|
|
|
gc.hub.register(mod, 'ACC_OK')
|
2004-01-24 21:32:51 +01:00
|
|
|
gc.hub.register(mod, 'CONFIG')
|
2003-12-20 14:22:37 +01:00
|
|
|
modObj.load()
|
2003-11-30 16:50:23 +01:00
|
|
|
# END loadPLugins
|
|
|
|
|
2003-10-22 22:23:45 +02:00
|
|
|
def start():
|
2004-01-17 16:38:29 +01:00
|
|
|
"""Start the Core"""
|
2003-10-22 22:23:45 +02:00
|
|
|
gc = GajimCore()
|
2003-11-30 16:50:23 +01:00
|
|
|
loadPlugins(gc)
|
2004-01-24 21:32:51 +01:00
|
|
|
################ pr des tests ###########
|
|
|
|
gc.hub.sendPlugin('NOTIFY', ('aste@lagaule.org', 'online', 'online', 'oleron'))
|
|
|
|
# gc.hub.sendPlugin('MSG', ('ate@lagaule.org', 'msg'))
|
|
|
|
#########################################
|
2004-01-17 16:38:29 +01:00
|
|
|
try:
|
|
|
|
gc.mainLoop()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print "Keyboard Interrupt : Bye!"
|
2004-01-18 23:56:28 +01:00
|
|
|
if gc.connected:
|
|
|
|
gc.con.disconnect()
|
2004-01-17 16:38:29 +01:00
|
|
|
gc.hub.sendPlugin('QUIT', ())
|
|
|
|
return 0
|
2003-11-30 16:50:23 +01:00
|
|
|
# END start
|