-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmucbot.py
More file actions
107 lines (92 loc) · 3.3 KB
/
mucbot.py
File metadata and controls
107 lines (92 loc) · 3.3 KB
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
#!/usr/bin/python2
# -*- coding: utf-8 -*-
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import xmpp
import time,os
from configobj import ConfigObj
from xml.sax.saxutils import escape
def messageCB(session,message):
"""
handle messages
"""
if not message.getBody():
return # abort on empty message
frm = message.getFrom()
if frm.getNode()+"@"+frm.getDomain() == chatroom:
if not frm.getResource() == nickname:
# Message from chatroom
#msg = xmpp.Message(
# to=tojid,
# body=escape(frm.getResource()+": "+message.getBody()),
# typ="chat",
# frm=username
#) # Jolla XMPP would receive this
msg = u"""<message from='{frm}' to='{to}' type='chat' id='e7e65551-2215-4a8d-8711-917980dda53f'>
<body>{body} </body>
<active xmlns='http://jabber.org/protocol/chatstates'/>
</message> """.format(
to=tojid,
body=escape(frm.getResource()+": "+message.getBody()),
frm=username
)
client.send(msg)
else:
msg = xmpp.Message(
to=chatroom,
body=escape(message.getBody()),
typ="groupchat",
frm=username
)
client.send(msg)
# load config
conf = ConfigObj("config.ini")
credentials = conf["credentials"]
username = credentials["username"]
password = credentials["password"]
chat = conf["chat"]
nickname = chat["nickname"]
chatroom = chat["chatroom"]
botjid = conf["bot"]
tojid = botjid["tojid"]
if "password" in chat:
roompasswd = chat["password"]
else:
roompasswd = ''
# Create XMPP client and connect
client = xmpp.Client(xmpp.JID(username).getDomain())
connection = client.connect()
if not connection:
print("Unable to connect to server {}, aborting!".format(server))
sys.exit(1)
if connection != "tls":
print("Warning: unable to estabilish secure connection - TLS failed!")
auth = client.auth(xmpp.JID(username).getNode(), password)
if not auth:
print("Unable to authorize on {} - check login/password. Aborting!".format(server))
sys.exit(1)
if auth != 'sasl':
print("Warning: unable to perform SASL auth os {}. Old authentication method used!".format(server))
# register message handler
client.RegisterHandler('message',messageCB)
#client.RegisterHandler('presence',presenceCB)
# init
client.sendInitPresence()
# connect to muc
muc = xmpp.Presence(to='{}/{}'.format(chatroom,nickname))
muc.setTag('x', namespace=xmpp.NS_MUC).setTagData('password',roompasswd)
muc.getTag('x').addChild('history',{'maxchars':'0','maxstanzas':'0'})
client.send(muc)
print("Bot started.")
while 1:
client.Process(1)