Package emma :: Package module :: Package irc_moderator
[hide private]
[frames] | no frames]

Source Code for Package emma.module.irc_moderator

  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 
22 23 24 -class irc_moderator(Module):
25 - def run(self):
26 # self._ = {channel: {'talking': nick, 'words': [nicks], 27 # 'session': str}} 28 self._ = {} 29 30 help_event = Event(event="help", interface="irc", \ 31 identifier=self.conf['irc_id']) 32 subscribe(help_event, self.help_handler) 33 cmd_event = Event(event="command", interface="irc", \ 34 identifier=self.conf['irc_id']) 35 subscribe(cmd_event, self.cmd_handler) 36 rcv_event = Event(event="receive", interface="irc", \ 37 identifier=self.conf['irc_id']) 38 subscribe(rcv_event, self.rcv_handler)
39
40 - def help_handler(self, event, data):
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
64 - def cmd_handler(self, event, data):
65 channel = data[1]['To'] 66 if channel[0] != '#': 67 return # not in channel 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
87 - def rcv_handler(self, event, data):
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
116 - def add_word(self, nick, channel):
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
124 - def give_turn(self, channel):
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
129 - def send_ctcp(self, txt, channel):
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
143 - def wikistore(self, channel):
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