poezio/src/connection.py

109 lines
3.5 KiB
Python
Raw Normal View History

2010-01-10 20:14:17 +00:00
#!/usr/bin/python
# -*- coding:utf-8 -*-
#
# Copyright 2010 Le Coz Florent <louizatakk@fedoraproject.org>
#
# This file is part of Poezio.
#
# Poezio 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 3 of the License.
#
# Poezio 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.
#
# You should have received a copy of the GNU General Public License
# along with Poezio. If not, see <http://www.gnu.org/licenses/>.
import sys
import xmpp
from config import config
2010-01-26 17:10:37 +00:00
from logging import logger
2010-01-10 20:14:17 +00:00
from threading import Thread
from handler import Handler
class Connection(Thread):
"""
Receives everything from Jabber and emits the
appropriate signals
2010-01-10 20:14:17 +00:00
"""
def __init__(self, server, resource):
Thread.__init__(self)
self.handler = Handler()
self.server = server
self.resource = resource
self.online = 0 # 1:connected, 2:auth confirmed
self.jid = '' # we don't know our jid yet (anon account)
self.port = config.get('port', 5222)
2010-01-26 17:10:37 +00:00
self.client = xmpp.Client(self.server, debug=[])
2010-01-10 20:14:17 +00:00
def run(self):
"""
run in a thread
2010-01-10 20:14:17 +00:00
connect to server
"""
if not self.connect_to_server(self.server, self.port):
2010-01-26 17:10:37 +00:00
logger.error('Could not connect to server')
2010-01-10 20:14:17 +00:00
sys.exit(-1)
if not self.authenticate():
2010-01-26 17:10:37 +00:00
logger.error('Could not authenticate to server')
2010-01-10 20:14:17 +00:00
sys.exit(-1)
self.client.sendInitPresence()
self.online = 1 # 2 when confirmation of auth is received
self.register_handlers()
2010-01-12 14:57:58 +00:00
while 1:
self.process()
2010-01-10 20:14:17 +00:00
def connect_to_server(self, server, port):
# TODO proxy stuff
return self.client.connect((server, port))
def authenticate(self, anon=True):
if anon:
return self.client.auth(None, "", self.resource)
2010-01-10 20:14:17 +00:00
else:
log.error('Non-anonymous connections not handled currently')
return None
def register_handlers(self):
"""
register handlers from xmpppy signals
"""
2010-01-10 20:14:17 +00:00
self.client.RegisterHandler('message', self.handler_message)
self.client.RegisterHandler('presence', self.handler_presence)
self.client.RegisterHandler('iq', self.handler_iq)
self.client.RegisterHandler('error', self.handler_error)
2010-01-10 20:14:17 +00:00
def handler_presence(self, connection, presence):
fro = presence.getFrom()
2010-01-10 20:14:17 +00:00
to = presence.getAttr('to')
if fro == to: # own presence
self.online = 2
self.jid = to
2010-01-29 16:24:44 +00:00
self.handler.emit('on-connected', jid=fro)
2010-01-10 20:14:17 +00:00
return
self.handler.emit('room-presence', stanza=presence)
2010-01-10 20:14:17 +00:00
def handler_message(self, connection, message):
self.handler.emit('room-message', stanza=message)
2010-01-12 14:57:58 +00:00
2010-01-10 20:14:17 +00:00
def handler_iq(self, connection, iq):
2010-01-29 16:24:44 +00:00
self.handler.emit('room-iq', stanza=iq)
2010-01-10 20:14:17 +00:00
def handler_error(self, connection, error):
2010-01-31 05:33:54 +00:00
pass
2010-01-10 20:14:17 +00:00
def process(self, timeout=10):
if self.online:
2010-02-10 16:47:43 +00:00
try:
self.client.Process(timeout)
2010-01-27 22:29:28 +00:00
except:
2010-01-31 05:33:54 +00:00
pass # FIXME
2010-01-10 20:14:17 +00:00
else:
log.warning('disconnecting...')
sys.exit()