def execute(self, message: IRCMessage): params = message.parameterList responses = [] if message.command == "tell" or message.command == "tellafter": if len(params) == 0 or len(params) == 1: return IRCResponse(ResponseType.Say, "Tell who?", message.replyTo) elif len(params) == 1 and message.command == "tellafter": return IRCResponse(ResponseType.Say, "Tell it when?", message.replyTo) elif len(params) == 1 or len( params) == 2 and message.command == "tellafter": return IRCResponse(ResponseType.Say, "Tell {} what?".format(params[0]), message.replyTo) sentTells = [] if message.command == "tellafter": try: date = now() + timedelta(seconds=timeparse(params[1])) except TypeError: return IRCResponse(ResponseType.Say, "The given duration is invalid.", message.replyTo) else: date = now() for recep in params[0].split("&"): if recep.lower() == self.bot.nick.lower(): responses.append( IRCResponse( ResponseType.Say, "Thanks for telling me that, {}.".format( message.user.nick), message.replyTo)) continue msg = { "to": recep.lower(), "body": strToB64(" ".join(params[1:]) if message.command == "tell" else " ".join(params[2:])), "date": now().isoformat(), "datetoreceive": date.isoformat(), "from": message.user.nick, "source": message.replyTo if message.replyTo[0] in self.bot.supportHelper.chanTypes else "PM" } self.storage["tells"].append(msg) sentTells.append(recep.replace("/", " or ")) if len(sentTells) > 0: if message.command == "tellafter": m = "Okay, I'll tell {} that when they speak after {}.".format( " and ".join(sentTells), strftimeWithTimezone(date)) else: m = "Okay, I'll tell {} that next time they speak.".format( " and ".join(sentTells)) responses.append( IRCResponse(ResponseType.Say, m, message.replyTo)) elif message.command == "stells": for tell in self.storage["tells"]: if tell["from"].lower() == message.user.nick.lower(): responses.append( IRCResponse(ResponseType.Notice, _parseSentTell(tell), message.user.nick)) if len(responses) == 0: return IRCResponse( ResponseType.Notice, "No undelivered messages sent by you were found.", message.user.nick) elif message.command == "rtell": if len(params) == 0: return IRCResponse(ResponseType.Say, "Remove what?", message.replyTo) tells = [ x for x in self.storage["tells"] if x["from"].lower() == message.user.nick.lower() ] for tell in tells: if re2.search(" ".join(params), b64ToStr(tell["body"]), re2.IGNORECASE): self.storage["tells"].remove(tell) m = "Message {!r} was removed from the message database.".format( _parseSentTell(tell)) return IRCResponse(ResponseType.Notice, m, message.user.nick) else: return IRCResponse( ResponseType.Notice, "No tells matching {!r} were found.".format( " ".join(params)), message.user.nick) return responses
def _parseSentTell(tell): return "{} < Sent to {} on {}, to be received on {} in {}.".format( b64ToStr(tell["body"]), tell["to"], strftimeWithTimezone(tell["date"]), strftimeWithTimezone(tell["datetoreceive"]), tell["source"])
def _parseTell(nick, tell): return "{}: {} < From {} ({} ago).".format( nick, b64ToStr(tell["body"]), tell["from"], timeDeltaString(now(), parse(tell["date"])))
def execute(self, message: IRCMessage): params = message.parameterList responses = [] if message.command == "tell" or message.command == "tellafter": if len(params) == 0 or len(params) == 1: return IRCResponse(ResponseType.Say, "Tell who?", message.replyTo) elif len(params) == 1 and message.command == "tellafter": return IRCResponse(ResponseType.Say, "Tell it when?", message.replyTo) elif len(params) == 1 or len(params) == 2 and message.command == "tellafter": return IRCResponse(ResponseType.Say, "Tell {} what?".format(params[0]), message.replyTo) sentTells = [] if message.command == "tellafter": try: date = now() + timedelta(seconds=timeparse(params[1])) except TypeError: return IRCResponse(ResponseType.Say, "The given duration is invalid.", message.replyTo) else: date = now() for recep in params[0].split("&"): if recep.lower() == self.bot.nick.lower(): responses.append(IRCResponse(ResponseType.Say, "Thanks for telling me that, {}.".format(message.user.nick), message.replyTo)) continue msg = { "to": recep.lower(), "body": strToB64(" ".join(params[1:]) if message.command == "tell" else " ".join(params[2:])), "date": now().isoformat(), "datetoreceive": date.isoformat(), "from": message.user.nick, "source": message.replyTo if message.replyTo[0] in self.bot.supportHelper.chanTypes else "PM" } self.tells.append(msg) sentTells.append(recep.replace("/", " or ")) if len(sentTells) > 0: self.bot.storage["tells"] = self.tells if message.command == "tellafter": m = "Okay, I'll tell {} that when they speak after {}.".format("&".join(sentTells), strftimeWithTimezone(date)) else: m = "Okay, I'll tell {} that next time they speak.".format("&".join(sentTells)) responses.append(IRCResponse(ResponseType.Say, m, message.replyTo)) elif message.command == "stells": for tell in self.tells: if tell["from"].lower() == message.user.nick.lower(): responses.append(IRCResponse(ResponseType.Notice, _parseSentTell(tell), message.user.nick)) if len(responses) == 0: return IRCResponse(ResponseType.Notice, "No undelivered messages sent by you were found.", message.user.nick) elif message.command == "rtell": if len(params) == 0: return IRCResponse(ResponseType.Say, "Remove what?", message.replyTo) tells = [x for x in self.tells if x["from"].lower() == message.user.nick.lower()] for tell in tells: if re2.search(" ".join(params), b64ToStr(tell["body"]), re2.IGNORECASE): self.tells.remove(tell) self.bot.storage["tells"] = self.tells m = "Message {!r} was removed from the message database.".format(_parseSentTell(tell)) return IRCResponse(ResponseType.Notice, m, message.user.nick) else: return IRCResponse(ResponseType.Notice, "No tells matching {!r} were found.".format(" ".join(params)), message.user.nick) return responses
def _parseSentTell(tell): return "{} < Sent to {} on {}, to be received on {} in {}.".format(b64ToStr(tell["body"]), tell["to"], strftimeWithTimezone(tell["date"]), strftimeWithTimezone(tell["datetoreceive"]), tell["source"])
def _parseTell(nick, tell): return "{}: {} < From {} ({} ago).".format(nick, b64ToStr(tell["body"]), tell["from"], timeDeltaString(now(), parse(tell["date"])))