/
featuritis.py
217 lines (188 loc) · 6.41 KB
/
featuritis.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
210
211
212
213
214
215
#! /usr/bin/env python
# -*- coding: utf-8 -*
# licensed GPL3
import os, imp, sys, time, random, threading
import irc
import irc.bot
import irc.strings
from irc.client import ip_numstr_to_quad, ip_quad_to_numstr
from collections import defaultdict
import feature_backend as fb
from imp import reload
if len(sys.argv) != 2 or not os.path.isfile(sys.argv[1]):
print('featuritis.py config_file')
sys.exit(1)
conf = imp.load_source("conf", sys.argv[1])
# core data structures are not run-time changable
class Dummy(object):
pass
class User(object):
def __init__(self):
self.serial = None
self.nick = set() # case insensitive...
self.active = ''
self.mask = set()
self.votes = set()
self.source = ''
class Admin(User):
def __init__(self):
self.doing = None
#super().__init__()
User.__init__(self)
class Task(object):
def __init__(self):
self.number = 0
self.children = set()
self.parents = set()
self.votes = 0 # len(set(tree_users))
self.stars = 0
self.title = ''
self.desc = []
self.commit = None
self.tree_stars = 0
self.tree_users = []
self.devs = set()
class Stateful(object):
def __init__(self):
self.users = [] # list of User()
self.admins = [] # list of User()
self.tasks = {} # keyed by task number
self.logfile = None
# disable logfile during non-interactive use
# (also is used as a flag for interactive use)
self.tick = ''
self.notices = [] # (nick, message)
self.conf = conf
self.next_task = 100
self.next_user = 1
self.bot = None
self.userhosts = {}
self.seed_admin()
def seed_admin(self):
for nicks, hosts in conf.admin_array:
a1 = Admin()
a1.nick.update(nicks)
a1.mask.update(hosts)
a1.serial = self.next_user
self.next_user += 1
self.admins.append(a1)
# dumb methods to make objects available to backend
def new_task(self):
return Task()
def new_user(self):
return User()
stateful = Stateful()
bot = None
def reload_log(logfile):
if stateful.logfile:
stateful.logfile.close()
stateful.__init__()
if not os.path.isfile(logfile):
print('error:', logfile, 'not found')
return
for line in open(logfile):
if line.startswith('#'):
continue
source,_,message = line.strip().partition('\t')
e = Dummy()
e.source = source
e.arguments = [message]
fb.priv_action(stateful, None, e)
stateful.notices = []
stateful.logfile = open(conf.logfile, 'a')
class TestBot(irc.bot.SingleServerIRCBot):
def __init__(self):
irc.bot.SingleServerIRCBot.__init__(self, [(conf.server, conf.port)],
conf.nickname, conf.nickname)
self.channel = conf.main_channel
if conf.password:
self.server_list[0].password = conf.password
self.connection.add_global_handler(302, self.on_userhost)
def on_userhost(self, c, e):
user,_,host = e.arguments[0].partition('=+')
user = user.strip()
host = host.strip()
stateful.userhosts[user] = host
def on_nicknameinuse(self, c, e):
c.nick(c.get_nickname() + "_")
def on_welcome(self, c, e):
for priv,msg in conf.pre_join:
c.privmsg(priv, msg)
time.sleep(0.5)
if conf.channel_key:
c.join(self.channel, key=conf.channel_key)
else:
c.join(self.channel)
# why not just send messages directly or immediately?
def send_notices(self):
c = self.connection
for nick,message in stateful.notices:
# todo, split long messages
c.privmsg(nick, message)
stateful.notices = []
def on_privmsg(self, c, e):
self.check_reload(c, e)
status = fb.priv_action(stateful, c, e)
self.send_notices()
if status:
fb.render_html(conf.html_path, stateful)
fb.render_rss(conf.rss_path, stateful)
def on_pubmsg(self, c, e):
fb.pub_action(stateful, c, e)
self.send_notices()
def check_reload(self, c, e):
global conf
nick = e.source.nick
message = ' '.join(e.arguments)
if any(nick in nn for nn,hm in conf.admin_array) and message == 'reload-backend':
try:
tstart = time.time()
reload(fb)
tstop = time.time()
conf = imp.load_source("conf", sys.argv[1])
stateful.conf = conf
fb.recount_everything(stateful)
fb.render_html(conf.html_path, stateful)
fb.render_rss(conf.rss_path, stateful)
c.privmsg(nick, 'Reload successful, %f seconds' % (tstop-tstart))
except:
c.privmsg(nick, 'Reload failed.')
return
if any(nick in nn for nn,hm in conf.admin_array) and message == 'reload-log':
try:
tstart = time.time()
reload_log(conf.logfile)
tstop = time.time()
stateful.bot = bot
fb.recount_everything(stateful)
fb.render_html(conf.html_path, stateful)
fb.render_rss(conf.rss_path, stateful)
c.privmsg(nick, 'Reload successful, %f seconds' % (tstop-tstart))
except:
c.privmsg(nick, 'Reload failed.')
return
if any(nick in nn for nn,hm in conf.admin_array) and message == 'reload-html':
try:
tstart = time.time()
fb.recount_everything(stateful)
fb.render_html(conf.html_path, stateful)
fb.render_rss(conf.rss_path, stateful)
tstop = time.time()
c.privmsg(nick, 'Reload successful, %f seconds' % (tstop-tstart))
except:
c.privmsg(nick, 'Reload failed.')
return
def main():
global bot
reload_log(conf.logfile)
fb.log_tick(stateful)
fb.recount_everything(stateful)
fb.render_html(conf.html_path, stateful)
fb.render_rss(conf.rss_path, stateful)
print('Loading complete, connecting to IRC...')
bot = TestBot()
stateful.bot = bot
bot.start()
stateful.logfile.close()
if __name__ == "__main__":
main()