-
Notifications
You must be signed in to change notification settings - Fork 0
/
w_xmpp.py
75 lines (63 loc) · 2.74 KB
/
w_xmpp.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
########################################################################
# Copyright 2012 wst, wstcode@gmail.com
#
# This file is part of wstbot.
#
# wstbot 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.
#
# wstbot 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 wstbot. If not, see <http://www.gnu.org/licenses/>.
########################################################################
import sys
import logging
import getpass
import sleekxmpp
from util import escape
from optparse import OptionParser
class WstXMPP(sleekxmpp.ClientXMPP):
def __init__(self, jid, password, room, nick):
super().__init__(jid, password)
self.room = room
self.nick = nick
self.add_event_handler("session_start", self.start)
self.add_event_handler("groupchat_message", self.muc_message)
self.add_event_handler("muc::{0}::got_online".format(self.room), self.muc_online)
self.register_plugin("xep_0030") # Service Discovery
self.register_plugin("xep_0045") # Multi-User Chat
self.MUC = self.plugin["xep_0045"]
self.register_plugin("xep_0199") # XMPP Ping
def start(self, event):
self.get_roster()
self.send_presence()
self.MUC.joinMUC(self.room, self.nick, wait=True)
def muc_message(self, msg):
pass
def muc_online(self, presence):
pass
def send_room_message(self, message, formatted=False):
"""formatted means that it is an html message"""
if message is None:
return
try:
if formatted:
html_message = "<span>" + message + "</span>"
self.send_message(mto=self.room, mbody=message, mhtml=html_message, mtype="groupchat")
else:
# send every line seperately
lines = message.split("\n")
for line in lines:
self.send_message(mto=self.room, mbody=line, mtype="groupchat")
except Exception as e:
print("ERROR sending message: " + message)
message = "Error. Tried to send message: <br/>\n" + escape(message)
message += "<br/>\n<br/>\nException was: <br/>\n" + escape(str(e))
html_message = "<span>" + message + "</span>"
self.send_message(mto=self.room, mbody=message, mhtml=html_message, mtype="groupchat")