This repository was archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathirccat.py
More file actions
109 lines (84 loc) · 2.92 KB
/
irccat.py
File metadata and controls
109 lines (84 loc) · 2.92 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
108
109
# coding=utf8
"""
irccat.py - Network socket listener
Copyright (c) 2014 Mikael Sörlin <iamtew@asylunatic.se>
Licensed under the MIT License.
https://github.com/iamtew/willie-irccat
"""
import os
import re
import socket
from datetime import datetime
from willie.config import ConfigurationError
from willie import module
def irccat_config(bot):
"""Return our configuration or False"""
if not bot.config.has_option('irccat', 'address') or \
not bot.config.has_option('irccat', 'port'):
return False
else:
return [bot.config.irccat.address, bot.config.irccat.port]
def irccat_logger(bot, src, body, level=False):
"""
Write messages to our log with timestamp and source IP.
Also write to debug log if we set a level
"""
message = str(datetime.now()) + ': From: ' + src + ': ' + body
logfile = open(os.path.join(bot.config.logdir, 'irccat.log'), 'a')
logfile.write(message)
logfile.close()
if level is not False:
bot.debug('irccat', message, level)
def irccat_targets(bot, targets):
"""
Go through our potential targets and place them in an array so we can
easily loop through them when sending messages.
"""
result = []
for s in targets.split(','):
if re.search('^@', s):
result.append(re.sub('^@', '', s))
elif re.search('^#', s) and s in bot.config.core.channels:
result.append(s)
elif re.search('^#\*$', s):
for c in bot.config.core.channels:
result.append(c)
return result
def configure(config):
"""
| [irccat] | example | purpose |
| -------- | ------- | -------------- |
| address | 0.0.0.0 | Listen address |
| port | 5234 | Listen port |
"""
if config.option('Listening address and port for irccat', False):
config.interactive_add('irccat', 'address', 'Listen address', '0.0.0.0')
config.interactive_add('irccat', 'port', 'Listen port', '5234')
def setup(bot):
if not irccat_config(bot):
raise ConfigurationError('irccat module not configured')
@module.event('001')
@module.rule('.*')
def netpipe(bot, trigger):
"""
Open our listening socket upon successfull server connection when the bot
receives RPL_WELCOME (001)
"""
netcfg = irccat_config(bot)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((netcfg[0], int(netcfg[1])))
sock.listen(5)
while True:
conn, addr = sock.accept()
data = conn.recv(1024)
if not len(data.split()) >= 2:
errmsg = 'Too short message received'
irccat_logger(bot, addr[0], errmsg + ': ' + data, 'warning')
conn.close()
continue
irccat_logger(bot, addr[0], 'Received: ' + data)
target, message = data.split(' ', 1)
for chat in irccat_targets(bot, target):
bot.msg(chat, message)
conn.close()
sock.close()