| ... |
... |
@@ -2,17 +2,17 @@ |
|
2
|
2
|
|
|
3
|
3
|
import sys
|
|
4
|
4
|
import os
|
|
5
|
|
-import nbxmpp
|
|
6
|
|
-import time
|
|
7
|
5
|
import logging
|
|
8
|
|
-try:
|
|
9
|
|
- from gi.repository import GObject as gobject
|
|
10
|
|
-except Exception:
|
|
11
|
|
- import gobject
|
|
|
6
|
+
|
|
|
7
|
+import nbxmpp
|
|
|
8
|
+from gi.repository import GLib
|
|
|
9
|
+
|
|
|
10
|
+if sys.platform in ('win32', 'darwin'):
|
|
|
11
|
+ import certifi
|
|
12
|
12
|
|
|
13
|
13
|
consoleloghandler = logging.StreamHandler()
|
|
14
|
14
|
root_log = logging.getLogger('nbxmpp')
|
|
15
|
|
-#root_log.setLevel('DEBUG')
|
|
|
15
|
+root_log.setLevel('DEBUG')
|
|
16
|
16
|
root_log.addHandler(consoleloghandler)
|
|
17
|
17
|
|
|
18
|
18
|
if len(sys.argv) < 2:
|
| ... |
... |
@@ -38,19 +38,29 @@ class Connection: |
|
38
|
38
|
def __init__(self):
|
|
39
|
39
|
self.jid = nbxmpp.protocol.JID(jidparams['jid'])
|
|
40
|
40
|
self.password = jidparams['password']
|
|
41
|
|
- self.sm = nbxmpp.Smacks(self) # Stream Management
|
|
42
|
41
|
self.client_cert = None
|
|
|
42
|
+ self.sm = nbxmpp.Smacks(self)
|
|
|
43
|
+ self.idle_queue = nbxmpp.idlequeue.get_idlequeue()
|
|
|
44
|
+ self.client = None
|
|
43
|
45
|
|
|
44
|
46
|
def on_auth(self, con, auth):
|
|
45
|
47
|
if not auth:
|
|
46
|
48
|
print('could not authenticate!')
|
|
47
|
49
|
sys.exit()
|
|
48
|
50
|
print('authenticated using ' + auth)
|
|
49
|
|
- self.send_message(to_jid, text)
|
|
|
51
|
+ con.RegisterHandler('message', self._on_message)
|
|
|
52
|
+ self.send_presence()
|
|
|
53
|
+
|
|
|
54
|
+ def _on_message(self, con, stanza):
|
|
|
55
|
+ print('message received')
|
|
|
56
|
+ print(stanza.getBody())
|
|
50
|
57
|
|
|
51
|
58
|
def on_connected(self, con, con_type):
|
|
52
|
59
|
print('connected with ' + con_type)
|
|
53
|
|
- auth = self.client.auth(self.jid.getNode(), self.password, resource=self.jid.getResource(), sasl=1, on_auth=self.on_auth)
|
|
|
60
|
+ self.client.auth(self.jid.getNode(),
|
|
|
61
|
+ self.password,
|
|
|
62
|
+ resource=self.jid.getResource(),
|
|
|
63
|
+ on_auth=self.on_auth)
|
|
54
|
64
|
|
|
55
|
65
|
def get_password(self, cb, mech):
|
|
56
|
66
|
cb(self.password)
|
| ... |
... |
@@ -62,14 +72,36 @@ class Connection: |
|
62
|
72
|
pass
|
|
63
|
73
|
|
|
64
|
74
|
def connect(self):
|
|
65
|
|
- idle_queue = nbxmpp.idlequeue.get_idlequeue()
|
|
66
|
|
- self.client = nbxmpp.NonBlockingClient(self.jid.getDomain(), idle_queue, caller=self)
|
|
67
|
|
- self.con = self.client.connect(self.on_connected, self.on_connection_failed, secure_tuple=('tls', '', '', None, None))
|
|
|
75
|
+ cacerts = ''
|
|
|
76
|
+ if sys.platform in ('win32', 'darwin'):
|
|
|
77
|
+ cacerts = certifi.where()
|
|
|
78
|
+
|
|
|
79
|
+ self.client = nbxmpp.NonBlockingClient(self.jid.getDomain(),
|
|
|
80
|
+ self.idle_queue,
|
|
|
81
|
+ caller=self)
|
|
|
82
|
+
|
|
|
83
|
+ self.client.connect(self.on_connected,
|
|
|
84
|
+ self.on_connection_failed,
|
|
|
85
|
+ secure_tuple=('tls', cacerts, '', None, None, False))
|
|
|
86
|
+
|
|
|
87
|
+ if sys.platform == 'win32':
|
|
|
88
|
+ timeout, in_seconds = 20, None
|
|
|
89
|
+ else:
|
|
|
90
|
+ timeout, in_seconds = 100, False
|
|
|
91
|
+
|
|
|
92
|
+ if in_seconds:
|
|
|
93
|
+ GLib.timeout_add_seconds(timeout, self.process_connections)
|
|
|
94
|
+ else:
|
|
|
95
|
+ GLib.timeout_add(timeout, self.process_connections)
|
|
68
|
96
|
|
|
69
|
97
|
def send_message(self, to_jid, text):
|
|
70
|
98
|
id_ = self.client.send(nbxmpp.protocol.Message(to_jid, text, typ='chat'))
|
|
71
|
99
|
print('sent message with id ' + id_)
|
|
72
|
|
- gobject.timeout_add(1000, self.quit)
|
|
|
100
|
+ GLib.timeout_add(1000, self.quit)
|
|
|
101
|
+
|
|
|
102
|
+ def send_presence(self):
|
|
|
103
|
+ presence = nbxmpp.Presence()
|
|
|
104
|
+ self.client.send(presence)
|
|
73
|
105
|
|
|
74
|
106
|
def quit(self):
|
|
75
|
107
|
self.disconnect()
|
| ... |
... |
@@ -78,8 +110,28 @@ class Connection: |
|
78
|
110
|
def disconnect(self):
|
|
79
|
111
|
self.client.start_disconnect()
|
|
80
|
112
|
|
|
|
113
|
+ def process_connections(self):
|
|
|
114
|
+ try:
|
|
|
115
|
+ self.idle_queue.process()
|
|
|
116
|
+ except Exception:
|
|
|
117
|
+ # Otherwise, an exception will stop our loop
|
|
|
118
|
+
|
|
|
119
|
+ if sys.platform == 'win32':
|
|
|
120
|
+ # On Windows process() calls select.select(), so we need this
|
|
|
121
|
+ # executed as often as possible.
|
|
|
122
|
+ timeout, in_seconds = 1, None
|
|
|
123
|
+ else:
|
|
|
124
|
+ timeout, in_seconds = 100, False
|
|
|
125
|
+
|
|
|
126
|
+ if in_seconds:
|
|
|
127
|
+ GLib.timeout_add_seconds(timeout, self.process_connections)
|
|
|
128
|
+ else:
|
|
|
129
|
+ GLib.timeout_add(timeout, self.process_connections)
|
|
|
130
|
+ raise
|
|
|
131
|
+ return True # renew timeout (loop for ever)
|
|
|
132
|
+
|
|
81
|
133
|
|
|
82
|
134
|
con = Connection()
|
|
83
|
135
|
con.connect()
|
|
84
|
|
-ml = gobject.MainLoop()
|
|
|
136
|
+ml = GLib.MainLoop()
|
|
85
|
137
|
ml.run()
|