forked from k4cg/Rezeptionistin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rezeptionistin.py
executable file
·209 lines (165 loc) · 5.57 KB
/
rezeptionistin.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import re
import json
import time
import random
import socket
import string
import codecs
import urllib2
import ConfigParser
import logging
from plugin import Plugin
from vendor import importdir
from bs4 import BeautifulSoup
from asyncirc.ircbot import IRCBot
class Rezeptionistin(object):
# Constructor
#
def __init__(self):
# load config
self.config = ConfigParser.ConfigParser()
self.translations = ConfigParser.ConfigParser()
if not self.config.read("config.ini"):
print "Error: your config.ini could not be read"
exit(1)
if not self.translations.read("language.ini"):
print "Error: your language.ini could not be read"
exit(1)
# load plugins
importdir.do("plugins", globals())
self.plugins = [module(config=self.config) for module in Plugin.__subclasses__()]
# load required config
self.server=self.config.get('IRC','server')
self.port=int(self.config.get('IRC', 'port'))
self.nick=self.config.get('IRC', 'nick')
self.ircchan=self.config.get('IRC', 'ircchan').split(",")
self.debugchan=self.config.get('IRC', 'debugchan')
self.useragent=self.config.get('HTTP', 'useragent')
self.language=self.config.get('Language','language')
self.spacestatus=self.config.get('SpaceStatus', 'url')
try:
self.ignore=self.config.get('IRC','ignore').split(',')
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
self.ignore = []
# load translation keys
# load optional config
try:
self.nickservpassword=self.config.get('IRC', 'nickservpassword')
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
pass
self.identified = False
self.httpregex = re.compile(r'https?://')
self.irc = IRCBot(self.server, self.port, self.nick)
# Helper Methods
#
def translate(self, language_key):
return self.translations.get(self.language, language_key)
def setlanguage(self, language):
if not self.translations.has_section(language):
return False
self.language = language
return True
def getlanguage(self):
return self.language
def nickserv_identify():
if not self.identified:
self.identified = True
if hasattr(self, "nickservpassword") and (self.ircchan == "#" + channel):
self.send_command("NickServ", "identify " + self.nickservpassword)
def netcat(self, hostname, port, content):
try:
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
s.connect((hostname, port))
s.shutdown(socket.SHUT_WR)
while 1:
data = s.recv(1024)
if data == "":
break
f = data
except:
f = ""
finally:
s.close()
return f
def debug(self, msg):
logging.debug(msg)
def geturlsfrommsg(self, message):
url = re.findall("(?P<url>https?://[^\s]+)", message)
return url
def getpage(self, url):
req = urllib2.Request(url, headers={ 'User-Agent': self.useragent })
soup = BeautifulSoup(urllib2.urlopen(req),"html.parser")
return soup
def get_spacestatus_data(self):
data = None
try:
data = self.getpage(self.spacestatus)
data = self.sanitize(data)
data = json.loads(data)
self.debug(data)
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
print "SpaceStatus was not properly configured in your config.ini"
return data
def geturltitle(self, url):
try:
page = self.getpage(url)
title = self.sanitize(page.title.string.lstrip())
except:
title = ""
return title
def split_by_nth(self, text, n, seperator=' '):
groups = text.split(seperator)
res_l = list(seperator.join(groups[:n]))
res_r = list(seperator.join(groups[n:]))
res_l.extend(res_r)
return res_l
def send_message(self, recipient, msg, srcuser=''):
if srcuser not in self.ignore:
self.irc.msg(recipient, "\x0F" + msg)
def send_command(self, recipient, cmd):
self.irc.msg(recipient, cmd)
def sanitize(self, s):
return str(s).translate(string.maketrans("\n\r",' '))
def get_plugin(self, name):
for plugin in self.plugins:
if type(plugin).__name__ == name:
return plugin
def check_user_authentication(self, user_nick, success_callback, failure_callback=None):
authentication = self.get_plugin("Authentication")
if authentication:
authentication.check_user_authentication(self, user_nick, success_callback, failure_callback)
# IRC Handlers
def on_msg(self, irc, user_nick, host, channel, message):
for plugin in self.plugins:
plugin.on_msg(self, user_nick, host, channel, message)
def on_privmsg(self, irc, user_nick, host, message):
for plugin in self.plugins:
plugin.on_privmsg(self, user_nick, host, message)
def on_join(self, irc, user_nick, host, channel):
for plugin in self.plugins:
plugin.on_join(self, user_nick, host, channel)
def on_notice(self, irc, user_nick, host, channel, message):
for plugin in self.plugins:
plugin.on_notice(self, user_nick, host, channel, message[1:])
# Operations
def run(self):
# Assign event handlers
self.irc.on_msg(self.on_msg)
self.irc.on_privmsg(self.on_privmsg)
self.irc.on_join(self.on_join)
self.irc.on_notice(self.on_notice)
# Start Bot
self.irc.start()
for channel in self.ircchan:
self.irc.join(channel)
self.irc.join(self.debugchan)
# Run Eventloop
try:
while self.irc.running:
time.sleep(1)
except KeyboardInterrupt:
print("Received exit command")
finally:
self.irc.stop()