-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
163 lines (126 loc) · 4.96 KB
/
core.py
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import logging
from contextlib import contextmanager
from django.conf import settings
if settings.HAS_XMPP:
from sleekxmpp import ClientXMPP
from sleekxmpp.xmlstream import ET
SERVER = settings.XMPP_SERVER
ADMIN_USER = settings.XMPP_ADMIN_USER
ADMIN_PASS = settings.XMPP_ADMIN_PASSWORD
ADMIN_JID = '{}@{}'.format(ADMIN_USER, SERVER)
ROOM_DOMAIN = 'conference.' + SERVER
if settings.HAS_XMPP:
C = ClientXMPP(ADMIN_JID, ADMIN_PASS)
C.connected = False
C.register_plugin('xep_0133')
C.register_plugin('old_0004')
C.register_plugin('xep_0045')
DROP_CONNECTIONS = False
@contextmanager
def xmpp():
if settings.HAS_XMPP:
try:
if not C.connected or DROP_CONNECTIONS:
C.connect()
C.process()
C.connected = True
yield C
finally:
if DROP_CONNECTIONS:
C.disconnect()
C.connected = False
def create_user(username, password, name='', email=''):
'''
Connect to a server.
'''
if settings.HAS_XMPP:
with xmpp() as client:
iq = client.makeIqSet(
ET.Element('{http://jabber.org/protocol/commands}item', {
'action': 'execute',
'node': 'http://jabber.org/protocol/admin#add-user'
})
)
iq['from'] = ADMIN_JID
iq['to'] = SERVER
iq['xml:lang'] = 'en'
command = iq.send()['command']
jid = '{}@{}'.format(username, SERVER)
form = command['form']
form.field['accountjid'].setValue(jid)
form.field['password'].setValue(password)
form.field['password-verify'].setValue(password)
form.field['email'].setValue(email)
form.field['given_name'].setValue(name)
form.field['surname'].setValue('')
del command['status']
iq = client.makeIqSet(command)
iq['from'] = ADMIN_JID
iq['to'] = SERVER
iq['xml:lang'] = 'en'
result = iq.send()
if 'Operation finished successfully' in str(result):
return jid
def create_room(room, title):
room = '{}@{}'.format(room, ROOM_DOMAIN)
if settings.HAS_XMPP:
with xmpp() as client:
muc = client.plugin['xep_0045']
muc.joinMUC(room, ADMIN_USER, wait=True, pfrom=ADMIN_JID)
form = muc.getRoomForm(room)
if form:
form.field['muc#roomconfig_roomname'].setValue(title)
form.field['muc#roomconfig_roomdesc'].setValue(title)
form.field['muc#roomconfig_persistentroom'].setValue('1')
form.field['muc#roomconfig_membersonly'].setValue('1')
form.field['muc#roomconfig_publicroom'].setValue('0')
form.field['muc#roomconfig_maxusers'].setValue('0')
muc.configureRoom(room, form=form, ifrom=ADMIN_USER)
muc.leaveMUC(room, ADMIN_USER)
return True
muc.leaveMUC(room, ADMIN_USER)
def delete_room(room):
room = '{}@{}'.format(room, ROOM_DOMAIN)
if settings.HAS_XMPP:
with xmpp() as client:
muc = client.plugin['xep_0045']
muc.joinMUC(room, ADMIN_USER, wait=True, pfrom=ADMIN_JID)
query = ET.Element('{http://jabber.org/protocol/muc#owner}query')
item = ET.Element('{http://jabber.org/protocol/muc#owner}destroy')
query.append(item)
iq = client.makeIqSet(query)
iq['to'] = room
iq['from'] = client.boundjid
iq.send()
muc.leaveMUC(room, ADMIN_USER)
return True
def set_affiliation(room, jid, affiliation):
room = '{}@{}'.format(room, ROOM_DOMAIN)
if settings.HAS_XMPP:
with xmpp() as client:
muc = client.plugin['xep_0045']
muc.joinMUC(room, ADMIN_USER, wait=True, pfrom=ADMIN_JID)
query = ET.Element('{http://jabber.org/protocol/muc#admin}query')
item = ET.Element('{http://jabber.org/protocol/muc#admin}item', {
'affiliation': affiliation,
'jid': jid,
'nick': jid.split('@')[0]
})
query.append(item)
iq = client.makeIqSet(query)
iq['to'] = room
iq['from'] = client.boundjid
iq.send()
muc.leaveMUC(room, ADMIN_USER)
return True
def add_member(room, jid):
return set_affiliation(room, jid, 'member')
def remove_member(room, jid):
return set_affiliation(room, jid, 'none')
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG,
format='%(levelname)-8s %(message)s')
assert create_room('test-room', 'Test')
assert add_member('test-room', 'rohan@localhost')
assert remove_member('test-room', 'rohan@localhost')
assert delete_room('test-room')