1 """
2 irc moderator module
3
4 @copyright: (c) 2011 hackmeeting U{http://sindominio.net/hackmeeting}
5 @author: Ruben Pollan
6 @organization: hackmeeting U{http://sindominio.net/hackmeeting}
7 @contact: meskio@sindominio.net
8 @license:
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the Do What The Fuck You Want To
11 Public License, Version 2, as published by Sam Hocevar. See
12 U{http://sam.zoy.org/projects/COPYING.WTFPL} for more details.
13 """
14
15
16 import time
17
18 from emma.events import Event, subscribe, trigger, run_event
19 from emma.module import Module
20 from emma.complement import use_lock
21 from emma.interface.message import Message
39
41 if not data:
42 return _(" * moderate [session_name]\n" \
43 " Start moderating an assembly\n" \
44 " * word\n" \
45 " While moderating request word\n" \
46 " * stop\n" \
47 " Stop moderating\n")
48 elif data in ('moderate', _('moderate')):
49 return _("Start the moderation of an assembly.\n" \
50 "It will assign turns to talk as people request them" \
51 " with 'word'.\n" \
52 "If a session_name is given the session will be saved " \
53 "on the wiki.")
54 elif data in ('word', _('word')):
55 return _("While moderating request word.\n" \
56 "It can also be requestested with a /me containing " \
57 "the word 'word' in it.")
58 elif data in ('stop', _('stop')):
59 return _("Stop moderating the assembly started with 'moderate'")
60 else:
61 return ""
62
63 @use_lock
65 channel = data[1]['To']
66 if channel[0] != '#':
67 return
68
69 cmd, args = data[0]
70 if cmd in ("moderate", _("moderate")) and not channel in self._:
71 self.log(_("starts moderating"))
72 self._[channel] = {'talking': None, 'words': []}
73 if args:
74 self._[channel]['session'] = args
75 self.trigger_history('start', args)
76 self.send_ctcp(_("starts moderating"), channel)
77 elif cmd in ("stop", _("stop")) and channel in self._:
78 self.log(_("stops moderating"))
79 self.wikistore(channel)
80 del self._[channel]
81 self.send_ctcp(_("stops moderating"), channel)
82 elif cmd in ("word", _("word")):
83 nick = data[1]['From']
84 self.add_word(nick, channel)
85
86 @use_lock
88 if data['Type'] == 'nick':
89 old_nick = data['From']
90 new_nick = data['To']
91 for c in self._.keys():
92 if self._[c]['talking'] == old_nick:
93 self._[c]['talking'] = new_nick
94 if old_nick in self._[c]['words']:
95 f = lambda nick: (nick == old_nick) and new_nick or nick
96 self._[c]['words'] = map(f, self._[c]['words'])
97
98 else:
99 channel = data['To']
100 if channel[0] != '#' or not channel in self._:
101 return
102
103 if (data['Body'] == "." and
104 data['From'] == self._[channel]['talking']):
105 if self._[channel]['words']:
106 self._[channel]['talking'] = self._[channel]['words'][0]
107 self._[channel]['words']= self._[channel]['words'][1:]
108 self.give_turn(channel)
109 else:
110 self._[channel]['talking'] = None
111
112 if data['Type'] == "ctcp" and _("word") in data['Body']:
113 nick = data['From']
114 self.add_word(nick, channel)
115
117 self.log(_("Request word from: %s") % nick)
118 if self._[channel]['talking']:
119 self._[channel]['words'].append(nick)
120 else:
121 self._[channel]['talking'] = nick
122 self.give_turn(channel)
123
125 nick = self._[channel]['talking']
126 self.log(_("gives the word to %s") % nick)
127 self.send_ctcp(_("gives the word to %s") % nick, channel)
128
130 msg = Message(txt, channel, tpe="ctcp")
131 event = Event(event="send", interface="irc",
132 identifier=self.conf['irc_id'])
133 trigger(event, msg)
134
135 - def trigger_history(self, cmd, param=''):
136 event = Event(event="history", interface="irc",
137 identifier=self.conf['irc_id'])
138 if cmd in ('start', 'stop'):
139 trigger(event, (cmd, param))
140 else:
141 return run_event(event, (cmd, param))[0]
142
144 if not 'session' in self._[channel]:
145 return
146 if not 'wiki_id' in self.conf:
147 return
148
149 session = self._[channel]['session']
150 self.trigger_history('stop')
151 self.log(_("Store irc log on the wiki page %s") % session)
152
153 history = self.trigger_history('get', session)
154 today = time.gmtime()
155 text = _("Assembly log from %(day)s/%(month)s/%(year)s\n\n") \
156 % {'day': today.tm_mday, 'month': today.tm_mon,
157 'year': today.tm_year}
158 text += _("Present: ")
159 text += ', '.join(set([msg['From'] for msg in history]))
160
161 text += "\n\n<pre>\n"
162 for msg in history:
163 if msg['To'] != channel:
164 continue
165 if msg['Type'] == "ctcp":
166 text += "[%s] * %s %s\n" % (msg['Date'].strftime("%H:%M"),
167 msg['From'], msg['Body'])
168 else:
169 text += "[%s] < %s> %s\n" % (msg['Date'].strftime("%H:%M"),
170 msg['From'], msg['Body'])
171 text += "</pre>"
172
173 event = Event(event="write", interface="mediawiki",
174 identifier=self.conf['wiki_id'])
175 trigger(event, (session, text))
176