gajim-plural/core/core.py

175 lines
4.8 KiB
Python
Raw Normal View History

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>
## - David Ferlier <david@yazzy.org>
2003-10-22 22:23:45 +02:00
##
## 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 Queue
import socket
import sys
import time
import logging
import plugins
import common.hub
import common.jabber
import common.optparser
log = logging.getLogger('core.core')
log.setLevel(logging.DEBUG)
CONFPATH = "~/.gajimrc"
2003-10-22 22:23:45 +02:00
class GajimCore:
def __init__(self):
self.connected = 0
self.cfgParser = common.optparser.OptionsParser(CONFPATH)
# print '%s\n' % self.cfgParser.Server_hostname;
2003-10-22 22:23:45 +02:00
self.hub = common.hub.GajimHub()
self.cfgParser.parseCfgFile()
# END __init__
def messageCB(self, con, msg):
self.hub.sendPlugin('MSG', (msg.getFrom().getBasic(), \
msg.getBody()))
# END messageCB
def presenceCB(self, con, prs):
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'
self.hub.sendPlugin('NOTIFY', \
(prs.getFrom().getBasic(), show, prs.getStatus()))
2003-10-27 17:30:37 +01:00
elif type == 'unavailable':
self.hub.sendPlugin('NOTIFY', \
(prs.getFrom().getBasic(), 'offline', prs.getStatus()))
2003-10-27 17:30:37 +01:00
elif type == 'subscribe':
log.debug("subscribe request from %s" % who)
self.con.send(common.jabber.Presence(who, 'subscribed'))
2003-10-27 17:30:37 +01:00
elif type == 'subscribed':
#plein de trucs a faire
jid = prs.getFrom()
self.hub.sendPlugin('SUBSCRIBED', {'jid':jid.getBasic(), \
'nom':jid.getNode()})
self.con.updateRosterItem(jid=jid.getBasic(), name=jid.getNode())
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)
2003-10-22 22:23:45 +02:00
# END presenceCB
def disconnectedCB(self, con):
log.debug("disconnectedCB")
# END disconenctedCB
2003-10-22 22:23:45 +02:00
def connect(self):
self.con = common.jabber.Client(host = \
self.cfgParser.Server_hostname, \
debug = False, log = sys.stderr)
try:
self.con.connect()
except IOError, e:
log.debug("Couldn't connect to %s" % e)
sys.exit(0)
else:
log.debug("Connected to server")
self.con.setMessageHandler(self.messageCB)
self.con.setPresenceHandler(self.presenceCB)
self.con.setDisconnectHandler(self.disconnectedCB)
if self.con.auth(self.cfgParser.Profile_name,
self.cfgParser.Profile_password,
self.cfgParser.Profile_ressource):
self.con.requestRoster()
roster = self.con.getRoster().getRaw()
2003-11-02 22:39:40 +01:00
if not roster :
roster = {}
self.hub.sendPlugin('ROSTER', roster)
2003-10-22 22:23:45 +02:00
self.con.sendInitPresence()
self.connected = 1
else:
sys.exit(1)
# END connect
def mainLoop(self):
while 1:
if not self.hub.queueIn.empty():
ev = self.hub.queueIn.get()
if ev[0] == 'QUIT':
if self.connected == 1:
self.con.disconnect()
return
2003-10-27 17:30:37 +01:00
#('STATUS', status)
2003-10-22 22:23:45 +02:00
elif ev[0] == 'STATUS':
2003-11-02 22:39:40 +01:00
2003-10-22 22:23:45 +02:00
if (ev[1] != 'offline') and (self.connected == 0):
self.connect()
elif (ev[1] == 'offline') and (self.connected == 1):
self.con.disconnect()
self.connected = 0
2003-11-02 22:39:40 +01:00
return 1
p = common.jabber.Presence()
p.setShow(ev[1])
self.con.send(p)
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-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-10-27 17:30:37 +01:00
#('UNSUB', jid)
elif ev[0] == 'UNSUB':
delauth = self.cfgParser.Core_delauth
if not delauth: delauth = 1
delroster = self.cfgParser.Core_delroster
if not delroster: delroster = 1
if delauth:
self.con.send(common.jabber.Presence(ev[1], 'unsubscribe'))
if delroster:
self.con.removeRosterItem(ev[1])
2003-10-27 17:30:37 +01:00
else:
log.debug("Unknown Command")
2003-10-22 22:23:45 +02:00
elif self.connected == 1:
self.con.process(1)
time.sleep(0.1)
# END main
# END GajimCore
def start():
gc = GajimCore()
guiPl = gc.hub.newPlugin ('gtkgui')
gc.hub.register('gtkgui', 'ROSTER')
gc.hub.register('gtkgui', 'NOTIFY')
gc.hub.register('gtkgui', 'MSG')
gc.hub.register('gtkgui', 'SUBSCRIBED')
2003-10-22 22:23:45 +02:00
guiPl.load ()
gc.mainLoop()