def run(self): event_send = Event(event='send', interface='irc', identifier=self.identifier) subscribe(event_send, self.send_handler) event_history = Event(event='history', interface='irc', identifier=self.identifier) subscribe(event_history, self.history_handler) event_rcv = Event(event='receive', interface='irc', identifier=self.identifier) subscribe(event_rcv, self.rcv_handler) self.store = '' self.update_db() server = self.conf['server'] port = int(self.conf['port']) nick = self.conf['nick'] channels = [i.strip() for i in self.conf['channels'].split(',')] self.log(_("Connect to %(server)s:%(port)s nick:%(nick)s " \ "channels:%(channels)s") % self.conf) try: self.irc = IrcClient(self.identifier, channels, nick, server, port) self.irc.start() except irclib.ServerConnectionError, x: self.log(_("error conecting to server: %s") % x)
def run(self): self.todo_list = [] help_event = Event(event="help", identifier=self.conf['id']) subscribe(help_event, self.help_handler) cmd_event = Event(event="command", identifier=self.conf['id']) subscribe(cmd_event, self.cmd_handler)
def run(self): self.search = {} """ {channel:[email]} """ help_event = Event(event="help", identifier=self.conf['im_id']) subscribe(help_event, self.help_handler) cmd_event = Event(event="command", identifier=self.conf['im_id']) subscribe(cmd_event, self.cmd_handler)
def run(self): # self._ = {channel: {'talking': nick, 'words': [nicks], # 'session': str}} self._ = {} help_event = Event(event="help", interface="irc", \ identifier=self.conf['irc_id']) subscribe(help_event, self.help_handler) cmd_event = Event(event="command", interface="irc", \ identifier=self.conf['irc_id']) subscribe(cmd_event, self.cmd_handler) rcv_event = Event(event="receive", interface="irc", \ identifier=self.conf['irc_id']) subscribe(rcv_event, self.rcv_handler)
def fetch(self): """ Fetch email Will be run periodically fetching the email from a POP3 server. """ if 'pop_user' in self.conf: user = self.conf['pop_user'] passwd = self.conf['pop_pass'] elif 'user' in self.conf: user = self.conf['user'] passwd = self.conf['pass'] else: self.log(_("No user defined for pop"), logging.ERROR) return self.log( _("fetching email %(user)s@%(host)s") % { 'user': user, 'host': self.conf['pop_host'] }) try: if 'pop_ssl' in self.conf and self.conf['pop_ssl'] == "yes": pop = poplib.POP3_SSL(self.conf['pop_host']) else: pop = poplib.POP3(self.conf['pop_host']) pop.user(user) pop.pass_(passwd) except: self.log(_(" error connecting by POP3"), logging.ERROR) return recv_event = Event(event='receive', interface='email', \ identifier=self.identifier) cmd_event = Event(event='command', interface='email', \ identifier=self.identifier) numMessages = len(pop.list()[1]) for i in range(numMessages): message = Message(pop.retr(i + 1)[1]) trigger(recv_event, message) for command in message.commands(): trigger(cmd_event, (command, message)) if self.conf['store'] == "yes": self.store(message) pop.dele(i + 1) pop.quit() self.log(_(" %s found") % numMessages)
def wikistore(self, channel): if not 'session' in self._[channel]: return if not 'wiki_id' in self.conf: return session = self._[channel]['session'] self.trigger_history('stop') self.log(_("Store irc log on the wiki page %s") % session) history = self.trigger_history('get', session) today = time.gmtime() text = _("Assembly log from %(day)s/%(month)s/%(year)s\n\n") \ % {'day': today.tm_mday, 'month': today.tm_mon, 'year': today.tm_year} text += _("Present: ") text += ', '.join(set([msg['From'] for msg in history])) text += "\n\n<pre>\n" for msg in history: if msg['To'] != channel: continue if msg['Type'] == "ctcp": text += "[%s] * %s %s\n" % (msg['Date'].strftime("%H:%M"), msg['From'], msg['Body']) else: text += "[%s] < %s> %s\n" % (msg['Date'].strftime("%H:%M"), msg['From'], msg['Body']) text += "</pre>" event = Event(event="write", interface="mediawiki", identifier=self.conf['wiki_id']) trigger(event, (session, text))
def cmd_handler(self, event, data): interface = event.interface if not interface in ["irc", "xmpp"]: return cmd, args = data[0] to = data[1]['From'] if interface == 'irc' and data[1]['To'][0] == '#': to = data[1]['To'] if cmd in ("find", _("find")): event = Event("db", "email", self.conf['email_id']) search = self.parse_args(args) self.log("Find: " + str(search)) res = run_event(event, search) if not res or not res[0]: self.say(_("Not found any email"), to, interface) else: self.add_search(res[0], to) self.show_list(to, interface) elif cmd in ("display", _("display")) and to in self.search: emails = self.search[to] try: email_index = int(args) except ValueError: self.say(_("Not valid index: %s") % args, to, interface) return if len(emails) > email_index: self.show_email(emails[email_index], to, interface) else: err_str = (_("Index not in range(0-%(number)d): %(args)s") % { 'number': len(emails) - 1, 'args': args }) self.say(err_str, to, interface)
def trigger_history(self, cmd, param=''): event = Event(event="history", interface="irc", identifier=self.conf['irc_id']) if cmd in ('start', 'stop'): trigger(event, (cmd, param)) else: return run_event(event, (cmd, param))[0]
def test_delay_store(monkeypatch): myThread = MyThread(monkeypatch) myEvents = MyEvents(monkeypatch, emma.sched) myDB = MyDB(monkeypatch, emma.sched) delay(Event(), None, 0) data = myDB.deleted[0] assert data['element'] == "sched" assert data['type'] == "at" assert data['data'] == dumps(None)
def run(self): """ Initialize email interface """ self.update_db() event = Event(event='send', interface='email', \ identifier=self.identifier) subscribe(event, self.send_handle) sleep(1) # allow the rest of the interfaces to boot before parse email period = self.conf['pop_period'] periodic(self.fetch, period)
def run(self): """ Initialize mediawiki interface """ host = self.conf['host'] path = self.conf['path'] self.wiki = mwclient.Site(host, path) user = self.conf['user'] password = self.conf['password'] self.wiki.login(user, password) self.log(_("Login to %s wiki") % (host)) event_read = Event(event='read', interface='mediawiki', identifier=self.identifier) subscribe(event_read, self.read) event_write = Event(event='write', interface='mediawiki', identifier=self.identifier) subscribe(event_write, self.write)
def _trigger_cmd(self, args, msg): s = args.split(" ", 1) if len(s) == 2: cmd, args = s else: cmd = s[0] args = "" logging.info(_("[irc %(identifier)s] command received: %(cmd)s: " \ "%(args)s") % {'identifier': self.identifier, 'cmd': cmd, 'args': args}) cmd_event = Event(event='command', interface='irc', \ identifier=self.identifier) trigger(cmd_event, ((cmd, args), msg))
def run(self): event = Event(event='send', interface='xmpp', identifier=self.identifier) subscribe(event, self.handler) jid = self.conf['jid'] password = self.conf['password'] server = self.conf['server'] port = int(self.conf['port']) self.log(_("Connect to jid: %(jid)s") % self.conf) self.xmpp = XMPPClient(self.identifier, jid, password) self.xmpp.register_plugin('xep_0030') # Service Discovery self.xmpp.register_plugin('xep_0199') # XMPP Ping if self.xmpp.connect((server, port)): self.xmpp.process() else: self.log(_("error conecting to xmpp: %s") % jid)
def issue(self, event, data): cmd, args = data[0] if event.interface == "email": group, text = args.split("\n", 1) else: group, text = args.split(" ", 1) self.db.insert({"group": group, "text": text}) # Reply ack to = data[1]['From'] if event.interface == 'irc' and data[1]['To'][0] == '#': to = data[1]['To'] msg = Message(_("issue accepted"), to) event = Event(event="send", interface=event.interface, identifier=event.identifier) trigger(event, msg) self.log(_("Issue accepted (%s, %s)") % (group, text))
def cmd_handler(self, event, data): cmd, args = data[0] if cmd not in ('remind', _('remind')): return s = args.split(';') if len(s) == 4: date, to, subject, body = s elif len(s) == 3: date, to, body = s subject = _("reminder") else: self.log(_("args not well formed")) return msg = Message(body, to) msg['Subject'] = subject send_event = Event('send', self.conf['interface'], self.conf['identifier']) at(send_event, msg, date)
def test_delay(monkeypatch): myThread = MyThread(monkeypatch) myEvents = MyEvents(monkeypatch, emma.sched) myDB = MyDB(monkeypatch, emma.sched) delay(Event(), None, 0) assert myEvents.event == Event()
def send_ctcp(self, txt, channel): msg = Message(txt, channel, tpe="ctcp") event = Event(event="send", interface="irc", identifier=self.conf['irc_id']) trigger(event, msg)
def run(self): cmd_event = Event(event="command") subscribe(cmd_event, self.cmd_handler)
def _trigger_rcv(self, msg): recv_event = Event(event='receive', interface='irc', \ identifier=self.identifier) trigger(recv_event, msg)
def run(self): help_event = Event(event="help") subscribe(help_event, self.help_handler) cmd_event = Event(event="command") subscribe(cmd_event, self.cmd_handler)
def say(self, msg, channel, interface): event = Event(event="send", interface=interface, \ identifier=self.conf['im_id']) message = Message(msg, channel) run_event(event, message)
def __init__(self, *args): Complement.__init__(self, *args) name = self.__module__.split(".")[-1] event = Event(event="db", interface=name, identifier=self.identifier) subscribe(event, self.db_handler)