diff --git a/modules/nntp.py b/modules/nntp.py index ace343c..8f9175b 100644 --- a/modules/nntp.py +++ b/modules/nntp.py @@ -18,6 +18,13 @@ from nemubot.hooks import hook from more import Response +# LOADING ############################################################# + +def load(context): + for wn in context.data.getNodes("watched_newsgroup"): + watch(**wn) + + # MODULE CORE ######################################################### def list_groups(group_pattern="*", **server): @@ -64,22 +71,50 @@ def format_article(art, **response_args): title=title.format(adler32(art["Newsgroups"].encode()) & 0xf, adler32(art["X-FromEmail"].encode()) & 0xf, **{h: decode_header(i) for h,i in art.items()}), **response_args) -def watch(to_server, to_channel, group="*", **server): - def newevt(arg): - context.add_event(ModuleEvent(call=fini, call_data=arg, interval=42)) - def fini(cnow): - newevt(datetime.now()) - n = 0 - for art in whatsnew(cnow, group, **server): - n += 1 - if n > 10: - continue - context.send_response(to_server, format_article(art, channel=to_channel)) +watches = dict() + +def _indexServer(**kwargs): + return "{user}:{password}@{host}:{port}".format(**kwargs) + +def _newevt(*args): + context.add_event(ModuleEvent(call=_fini, call_data=args, interval=42)) + +def _fini(lastcheck, server): + _newevt(datetime.now(), server) + n = 0 + for art in whatsnew(lastcheck, group, **server): + n += 1 if n > 10: - context.send_response(to_server, Response("... and %s others news" % (n - 10), channel=to_channel)) + continue + context.send_response(to_server, format_article(art, channel=to_channel)) + if n > 10: + context.send_response(to_server, Response("... and %s others news" % (n - 10), channel=to_channel)) - newevt(datetime.now()) +def watch(to_server, to_channel, group="*", lastcheck=None, **server): + idsrv = _indexServer(**server) + if lastcheck is None: + lastcheck = datetime.now() + + if idsrv not in watches: + wnnode = ModuleState("watched_newsgroup") + wnnode.setIndex("group") + wnnode["id"] = idsrv + wnnode.update(server) + context.data.addChild(wnnode) + _newevt(lastcheck, server) + else: + wnnode = context.data.index[idsrv] + + if group not in wnnode: + ngnode = ModuleState("notify_group") + ngnode["group"] = group + wnnode.addChild(ngnode) + else: + ngnode = wnnode.index[group] + + # Ensure this watch is not already registered + watches[idsrv][group].append((to_server, to_channel)) # MODULE INTERFACE #################################################### @@ -167,6 +202,8 @@ def cmd_watch(msg): if not msg.frm_owner: raise IMException("sorry, this command is currently limited to the owner") + wnnode = ModuleState("watched_newsgroup") + context.data.addChild(wnnode) watch(msg.server, msg.channel, msg.args[0] if len(msg.args) > 0 else "*", **msg.kwargs) return Response("Ok ok, I watch this newsgroup!", channel=msg.channel)