-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc.py
93 lines (69 loc) · 3.08 KB
/
irc.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
from module import XMPPModule
import pydle, threading, halutils
class IrcClient(pydle.Client):
module = None
def on_connect(self):
super().on_connect()
self.join(self.module.xmpp.config['irc']['channel'])
def on_channel_message(self, target, by, msg):
if msg.startswith("!list"):
self.message(self.module.xmpp.config['irc']['channel'], ", ".join(self.module.xmpp.mucusers[self.module.xmpp.config['irc']['muc']].keys()))
return
self.module.ircRecv(by, msg)
def on_xmpp_msg(self, user, msg):
self.message(self.module.xmpp.config['irc']['channel'], "<xmpp-{}>: {}".format(user,msg))
def on_ctcp(self, by, target, what, contents):
if what == 'ACTION':
self.module.ircRecv(by, "/me " + contents)
def on_xmpp_presence(self, name, typ):
self.message(self.module.xmpp.config['irc']['channel'], "<xmpp>: {} has {}".format(name, typ))
def on_join(self, channel, user):
self.module.xmpp.sendGroupMsg(self.module.xmpp.config['irc']['muc'], "<irc>: {} has joined".format(user))
def on_part(self, channel, nick, reason):
self.module.xmpp.sendGroupMsg(self.module.xmpp.config['irc']['muc'], "<irc>: {} has parted: {}".format(nick, reason))
def on_quit(self, channel, nick, reason):
self.module.xmpp.sendGroupMsg(self.module.xmpp.config['irc']['muc'], "<irc>: {} has quit".format(nick, reason))
class Irc(XMPPModule):
bot = None
thread = None
priority = 100
def init(self):
self.bot = IrcClient(
nickname = self.xmpp.config['irc']['nick'],
tls_certificate_file = self.xmpp.config['irc'].get('tls_certificate_file'),
tls_certificate_keyfile = self.xmpp.config['irc'].get('tls_certificate_keyfile'),
tls_certificate_password = self.xmpp.config['irc'].get('tls_certificate_password')
)
self.bot.connect(
hostname = self.xmpp.config['irc']['server'],
port = self.xmpp.config['irc']['port'],
tls = self.xmpp.config['irc'].get('tls'),
tls_verify = self.xmpp.config['irc'].get('tls_verify')
)
self.bot.module = self
self._create_thread()
def deinit(self):
self.bot.disconnect()
self.bot.eventloop.stop()
def _create_thread(self):
self.thread = threading.Thread(target=self.bot.handle_forever)
self.thread.start()
def ircRecv(self, by, msg):
self.xmpp.sendGroupMsg(self.xmpp.config['irc']['muc'],"<irc-{}>: {}".format(by,msg))
def recvGroupMsg(self, msg):
string = msg['body']
name = msg['mucnick']
if string.startswith("!irc"):
foo, args = halutils.splitArgList(msg)
if args[0] == "list":
self.xmpp.reply(msg, "\n" + "\n".join(self.bot.channels[self.xmpp.config['irc']['channel']]['users']))
return
self.bot.on_xmpp_msg(name, string)
def handleMucPresence(self, presence):
m = {"available":"entered the room", "unavailable":"left the room"}.get(presence['type'])
if m:
self.bot.on_xmpp_presence(presence['muc']['nick'], m)
def help(self, feature):
return '''Irc: Unifies an IRC channel and an XMPP server. All messages sent to either is relayed to the other.
Usage: Just talk, or use !irc to interact with the irc side, sorta.
!irc list - get a list of the nicknames on the IRC channel'''