1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
#!/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
from logging import logger
from handler import Handler
from common import exception_handler
import threading
import thread
class Connection(threading.Thread):
"""
Receives everything from Jabber and emits the
appropriate signals
"""
def __init__(self, server, resource):
threading.Thread.__init__(self)
self.handler = Handler()
self.daemon = True # exit the program when this exits
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)
self.client = xmpp.Client(self.server, debug=[])
def run(self):
"""
run in a thread
connect to server
"""
sys.excepthook = exception_handler
if not self.connect_to_server(self.server, self.port):
logger.error('Could not connect to server')
sys.exit(-1)
if not self.authenticate():
logger.error('Could not authenticate to server')
sys.exit(-1)
self.client.sendInitPresence(requestRoster=0)
self.online = 1 # 2 when confirmation of auth is received
self.register_handlers()
while 1:
self.process()
def connect_to_server(self, server, port):
if config.get('use_proxy','false') == 'true':
return self.client.connect((server, port),
{'host': config.get("proxy_server", ""),
'port': config.get("proxy_port", 1080),
'user': config.get("proxy_user", ""),
'password': config.get("proxy_password", "")
})
else:
return self.client.connect((server, port))
def authenticate(self, anon=True):
if anon:
return self.client.auth(None, "", self.resource)
else:
log.error('Non-anonymous connections not handled currently')
return None
def register_handlers(self):
"""
register handlers from xmpppy signals
"""
self.client.RegisterHandler('iq', self.on_get_time, typ='get', ns="urn:xmpp:time")
self.client.RegisterHandler('iq', self.on_get_version, typ='get', ns=xmpp.NS_VERSION)
self.client.RegisterHandler('presence', self.handler_presence)
self.client.RegisterHandler('message', self.handler_message)
# self.client.RegisterHandler('message', self.handler_delayed_message, ns=xmpp.NS_DELAY)
def handler_presence(self, connection, presence):
fro = presence.getFrom()
to = presence.getAttr('to')
if fro == to: # own presence
self.online = 2
self.jid = to
self.handler.emit('on-connected', jid=fro)
return
self.handler.emit('room-presence', stanza=presence)
raise xmpp.protocol.NodeProcessed
def handler_delayed_message(self, connection, message):
self.handler.emit('room-delayed-message', stanza=message)
raise xmpp.protocol.NodeProcessed
def handler_message(self, connection, message):
self.handler.emit('room-message', stanza=message)
raise xmpp.protocol.NodeProcessed
def handler_error(self, connection, error):
pass
def process(self, timeout=10):
if self.online:
self.client.Process(timeout)
else:
log.warning('disconnecting...')
sys.exit()
def on_get_version(self, connection, iq):
self.handler.emit('send-version', iq_obj=iq)
def on_get_time(self, connection, iq):
self.handler.emit('send-time', iq_obj=iq)
|