def gen_response(res, msg, srv): if res is None: raise IMException("bad URL : %s" % srv) else: return Text("URL for %s: %s" % (srv, res), server=None, to=msg.to_response)
def next_response(self, maxlen=440): if self.nick: return DirectAsk(self.nick, self.get_message(maxlen - len(self.nick) - 2), server=None, to=self.to) else: return Text(self.get_message(maxlen), server=None, to=self.to)
def visit_OwnerCommand(self, msg): res = Text("`%s%s%s" % (msg.cmd, " " if len(msg.args) else "", " ".join(msg.args)), server=msg.server, date=msg.date, to=msg.to, frm=msg.frm) res.accept(self)
def visit_DirectAsk(self, msg): others = [to for to in msg.to if to != msg.designated] # Avoid nick starting message when discussing on user channel if len(others) == 0 or len(others) != len(msg.to): res = Text(msg.message, server=msg.server, date=msg.date, to=msg.to, frm=msg.frm) res.accept(self) if len(others): res = Text("%s: %s" % (msg.designated, msg.message), server=msg.server, date=msg.date, to=others, frm=msg.frm) res.accept(self)
def setUp(self): self.msgs = [ # Texts (Text(message="TEXT", ), "TEXT"), (Text(message="TEXT TEXT2", ), "TEXT TEXT2"), (Text(message="TEXT @ARG=1 TEXT2", ), "TEXT @ARG=1 TEXT2"), # DirectAsk (DirectAsk(message="TEXT", designated="someone", to=["#somechannel"]), "someone: TEXT"), ( # Private message to someone DirectAsk(message="TEXT", designated="someone", to=["someone"]), "TEXT"), # Commands (Command(cmd="COMMAND", ), "!COMMAND"), (Command( cmd="COMMAND", args=["TEXT"], ), "!COMMAND TEXT"), (Command( cmd="COMMAND", kwargs={"KEY1": "VALUE"}, ), "!COMMAND @KEY1=VALUE"), (Command( cmd="COMMAND", args=["TEXT"], kwargs={"KEY1": "VALUE"}, ), "!COMMAND @KEY1=VALUE TEXT"), (Command( cmd="COMMAND", kwargs={"KEY2": None}, ), "!COMMAND @KEY2"), (Command( cmd="COMMAND", args=["TEXT"], kwargs={"KEY2": None}, ), "!COMMAND @KEY2 TEXT"), ]
def fill_response(self, msg): if self.personnal: from nemubot.message import DirectAsk return DirectAsk(msg.frm, *self.args, server=msg.server, to=msg.to_response) else: from nemubot.message import Text return Text(*self.args, server=msg.server, to=msg.to_response)
def visit_Command(self, msg): res = Text( "!%s%s%s%s%s" % (msg.cmd, " " if len(msg.kwargs) else "", " ".join([ "@%s=%s" % (k, msg.kwargs[k]) if msg.kwargs[k] is not None else "@%s" % k for k in msg.kwargs ]), " " if len(msg.args) else "", " ".join(msg.args)), server=msg.server, date=msg.date, to=msg.to, frm=msg.frm) res.accept(self)
def treat_msg(self, msg): """Treat a given message Arguments: msg -- the message to treat """ try: handled = False # Run pre-treatment: from Message to [ Message ] msg_gen = self._pre_treat(msg) m = next(msg_gen, None) # Run in-treatment: from Message to [ Response ] while m is not None: hook_gen = self._in_hooks(m) hook = next(hook_gen, None) if hook is not None: handled = True for response in self._in_treat(m, hook, hook_gen): # Run post-treatment: from Response to [ Response ] yield from self._post_treat(response) m = next(msg_gen, None) if not handled: for m in self._in_miss(msg): yield from self._post_treat(m) except BaseException as e: logger.exception( "Error occurred during the processing of the %s: " "%s", type(msg).__name__, msg) from nemubot.message import Text yield from self._post_treat( Text( "Sorry, an error occured (%s). Feel free to open a new issue at https://github.com/nemunaire/nemubot/issues/new" % type(e).__name__, to=msg.to_response))
def in_echo(msg): from nemubot.message import Text return Text(msg.frm + ": " + " ".join(msg.args), to=msg.to_response)