/
meetbot.py
158 lines (133 loc) · 4.25 KB
/
meetbot.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
# -*- coding: utf-8 -*-
import datetime
import os
from jinja2 import Environment, FileSystemLoader
from tasbot.plugin import IPlugin
from tasbot.decorators import AdminOnly, MinArgs, NotSelf
class VoteFailed(Exception):
def __init__(self, question, user, score):
self.question = question
self.user = user
self.score = score
def __str__(self):
return 'Your vote for %s with %s failed. Make sure you vote by saying "!vote [+,-] [1,0]'%(self.question,self.score)
class Message(object):
def __init__(self,u,m,i):
self._user = u
self._msg = m
self._i = i
def txt(self):
return '%s: %s'%(self._user, self._msg)
def html(self):
return '<span class="msg"><span class="user%d">%s</span>: %s</span>'%(self._i, self._user, self._msg)
class Top(object):
def __init__(self,top,num):
self.top = top
self._num = num
def html(self):
return '<a name="top_%d"><h2>%s</h2></a>'%(self._num,self.top)
def txt(self):
return '----------------- %s --------------------'%self.top
class Vote(object):
def __init__(self,question):
self._question = question
self._votes = {}
def add_vote(self,user,score):
try:
def signum(x):
return (x > 0) - (x < 0)
score = int(score)
self._votes[user] = signum(score)
except Exception, e:
self.logger.exception(e)
raise VoteFailed(self._question,user, score)
def result(self):
return sum([score for user,score in self._votes.iteritems()])
def html(self):
return '<span class="vote">%s - Result <span class="result">%d</span></span>'%(self._question,self.result())
def txt(self):
return '%s - Result %d\n'%(self._question,self.result())
class Main(IPlugin):
def __init__(self,name,tasc):
IPlugin.__init__(self,name,tasc)
self._msg = []
self._in_session = False
self._logdir = tasc.main.config.get('meetbot', "logdir")
try:
os.mkdir(self._logdir)
except:
pass
self._urlbase = tasc.main.config.get('meetbot', "urlbase")
self._channel = tasc.main.config.get('meetbot', "channel")
self._tops = []
self.nick = tasc.main.config.get('tasbot', "nick")
@AdminOnly
@MinArgs(2)
def cmd_saidprivate_goto(self,args,cmd):
"""pm "!goto CHANNEL" to switch the bot to another channel"""
self.say('bye')
self.tasclient.leave(self._channel)
self._channel = args[1]
self.tasclient.join(self._channel)
@NotSelf
@MinArgs(4)
def cmd_said_top(self,args,cmd):
"""start a new section in minutes with "!top title can have whitespace" """
top = Top(' '.join(args[3:]),len(self._tops) + 1)
self._tops.append(top)
self._msg.append(top)
@NotSelf
def cmd_said_meetingend(self,args,cmd):
"""write minutes to file and output url to it"""
self._in_session = False
self.say('meeting record ends')
dt = str(self._begin).replace(' ', '_')
fn = os.path.join(self._logdir, dt)
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template('html.jinja')
html_fn = fn + '.html'
with open(html_fn, 'wb') as outfile:
outfile.write( template.render(messages=self._msg,tops=self._tops,date=self._begin) )
url = '%s/%s'%(self._urlbase,html_fn)
self.say(url)
self._msg = []
self._votes = []
self._tops = []
@NotSelf
@MinArgs(3)
def cmd_said(self,args,cmd):
"""in an active session all SAID response not from myself are recorded"""
if self._in_session:
user = args[1]
if user != self.nick:
message = ' '.join(args[2:])
self._msg.append(Message(user, message, 1))
@NotSelf
def cmd_said_meetingbegin(self,args,cmd):
"""start a meeting session"""
self._begin = datetime.datetime.now()
self._in_session = True
self._msg = []
@NotSelf
@MinArgs(4)
def cmd_said_startvote(self,args,cmd):
"""start a +- 1|0 vote with "!startvote QUESTION_TITLE" """
vote = Vote(' '.join(args[3:]))
self._current_vote = vote
@NotSelf
@MinArgs(4)
def cmd_said_vote(self,args,cmd):
"""give your vote to current question with "!vote +1","!vote 0" or "!vote -1" """
user = args[1]
score = args[3]
try:
self._current_vote.add_vote(user, score)
except VoteFailed, fail:
self.tasclient.saypm(user, str(fail))
@NotSelf
def cmd_said_endvote(self,args,cmd):
"""end current voting and output result"""
self._msg.append(self._current_vote)
self.say(self._current_vote.txt())
def say(self,msg):
self.tasclient.say(self._channel, msg)