def fetch_mails(self, filter): result, data = yield self.call("uid", "SEARCH", None, filter) if not data or not data[0]: return for uid in data[0].split(): collected = [] yield self.walk_mail(uid) | idiokit.map(collected.append) parts = list() for path, headers in collected: parts.append((headers, self.fetcher(uid, path))) if parts: top_header = parts[0][0][0] subject = get_header(top_header, "Subject", "<no subject>") sender = get_header(top_header, "From", "<unknown sender>") self.log.info("Handling mail {0!r} from {1!r}".format(subject, sender)) yield self.handle(parts) self.log.info("Done with mail {0!r} from {1!r}".format(subject, sender)) # UID STORE command flags have to be in parentheses, otherwise # imaplib quotes them, which is not allowed. yield self.call("uid", "STORE", uid, "+FLAGS", "(\\Seen)")
def _stats(self, name, interval=60.0): def counter(event): counter.count += 1 return (event,) counter.count = 0 @idiokit.stream def logger(): while True: try: yield idiokit.sleep(interval) finally: if counter.count > 0: self.log.info( "Sent {0} events to room {1!r}".format(counter.count, name), event=events.Event({ "type": "room", "service": self.bot_name, "sent events": unicode(counter.count), "room": name})) counter.count = 0 result = idiokit.map(counter) idiokit.pipe(logger(), result) return result
def _stats(self, name, interval=60.0): def counter(event): counter.count += 1 return (event, ) counter.count = 0 @idiokit.stream def logger(): while True: try: yield idiokit.sleep(interval) finally: if counter.count > 0: self.log.info("Sent {0} events to room {1!r}".format( counter.count, name), event=events.Event({ "type": "room", "service": self.bot_name, "sent events": unicode(counter.count), "room": name })) counter.count = 0 result = idiokit.map(counter) idiokit.pipe(logger(), result) return result
class BruteForceBlockerBot(bot.PollingBot): # Ignore the last column ("id"). COLUMNS = ["ip", "time", "count", None] use_cymru_whois = bot.BoolParam() def poll(self): if self.use_cymru_whois: return self._poll() | cymruwhois.augment("ip") return self._poll() @idiokit.stream def _poll( self, url="http://danger.rulez.sk/projects/bruteforceblocker/blist.php"): self.log.info("Downloading %s" % url) try: info, fileobj = yield utils.fetch_url(url) except utils.FetchUrlFailed, fuf: self.log.error("Download failed: %r", fuf) idiokit.stop(False) self.log.info("Downloaded") filtered = (x for x in fileobj if x.strip() and not x.startswith("#")) lines = (re.sub("\t+", "\t", x) for x in filtered) yield (utils.csv_to_events(lines, delimiter="\t", columns=self.COLUMNS, charset=info.get_param("charset")) | idiokit.map(self._normalize, url))
def fetch_mails(self, filter): result, data = yield self.call("uid", "SEARCH", None, filter) if not data or not data[0]: return for uid in data[0].split(): collected = [] yield self.walk_mail(uid) | idiokit.map(collected.append) parts = list() for path, headers in collected: parts.append((headers, self.fetcher(uid, path))) if parts: top_header = parts[0][0][0] subject = get_header(top_header, "Subject", "<no subject>") sender = get_header(top_header, "From", "<unknown sender>") self.log.info("Handling mail {0!r} from {1!r}".format( subject, sender)) yield self.handle(parts) self.log.info("Done with mail {0!r} from {1!r}".format( subject, sender)) # UID STORE command flags have to be in parentheses, otherwise # imaplib quotes them, which is not allowed. yield self.call("uid", "STORE", uid, "+FLAGS", "(\\Seen)")
def _handle_room(self, room_name): room = yield self.xmpp.muc.join(room_name, self.bot_name) distributor = yield self._ready.fork() yield idiokit.pipe( room, idiokit.map(self._map, room_name), distributor.fork(), idiokit.Event() )
def poll(self): self.log.info("Downloading updates from {0!r}".format(self.url)) try: info, fileobj = yield utils.fetch_url(self.url) except utils.FetchUrlFailed as fuf: raise bot.PollSkipped("Downloading {0!r} failed ({1})".format(self.url, fuf)) self.log.info("Updates downloaded from {0!r}".format(self.url)) yield idiokit.pipe( utils.csv_to_events(fileobj, columns=self._columns), idiokit.map(self._normalize))
def session(self, state, src_room, **keys): keys["src_room"] = src_room def _alert(_): yield self.REPORT_NOW @idiokit.stream def _collect(): while True: item = yield idiokit.next() self.queue(0.0, item, **keys) collector = idiokit.pipe(self.collect(state, **keys), _collect()) idiokit.pipe(self.alert(**keys), idiokit.map(_alert), collector) result = yield idiokit.pipe(self._rooms.inc(src_room), collector) idiokit.stop(result)
def _handle_room(self, room_name): room = yield self.xmpp.muc.join(room_name, self.bot_name) distributor = yield self._ready.fork() yield idiokit.pipe(room, idiokit.map(self._map, room_name), distributor.fork(), idiokit.Event())
def augment(self): return idiokit.map(lambda x: (x,))
def events_to_elements(): return idiokit.map(lambda x: (x.to_elements(),))
def stanzas_to_events(): return idiokit.map(Event.from_elements)
def augment(self): return idiokit.map(lambda x: (x, ))