def _getTime(self, lat, lon): currentTime = timestamp(now()) params = { "format": "json", "by": "position", "key": self.apiKey, "lat": lat, "lng": lon } result = self.bot.moduleHandler.runActionUntilValue( "fetch-url", self.timeBaseURL, params) if not result: return "No time for this location could be found at this moment. Try again later." timeJSON = result.json() if timeJSON["status"] != "OK": if "message" in timeJSON: return timeJSON["message"] else: return "An unknown error occurred while requesting the time." resultDate = datetime.fromtimestamp(currentTime + int(timeJSON["gmtOffset"])) properDay = self._getProperDay(resultDate.day) formattedTime = resultDate.strftime("%H:%M (%I:%M %p) on %A, " + properDay + " of %B, %Y") return "Timezone: {} ({}) | Local time is {} | Daylight Savings Time: {}".format( timeJSON["zoneName"], timeJSON["abbreviation"], formattedTime, "Yes" if timeJSON["dst"] == "1" else "No")
def _processTells(self, message: IRCMessage): chanTells = [] pmTells = [] for tell in [ i for i in self.storage["tells"] ]: # Iterate over a copy so we don'rlt modify the list we're iterating over if not any( fnmatch(message.user.nick.lower(), r) for r in tell["to"].split("/")): continue if now().isoformat() < tell["datetoreceive"]: continue if tell["source"][0] in self.bot.supportHelper.chanTypes and len( chanTells) < 3: if tell["source"] == message.replyTo: chanTells.append(tell) self.storage["tells"].remove(tell) elif tell["source"][0] not in self.bot.supportHelper.chanTypes: pmTells.append(tell) self.storage["tells"].remove(tell) responses = [] for tell in chanTells: responses.append( IRCResponse(ResponseType.Say, _parseTell(message.user.nick, tell), message.replyTo)) for tell in pmTells: responses.append( IRCResponse(ResponseType.Say, _parseTell(message.user.nick, tell), message.user.nick)) return responses
def __init__(self, configFile): self.config = Config(configFile) self.connectionFactory = DesertBotFactory(self) self.log = None self.moduleHandler = ModuleHandler(self) self.servers = {} self.storage = None self.storageSync = None self.startTime = now()
def _handleTOPIC(self, nick, ident, host, params): if params[0] not in self.connection.channels: self._logWarning( "Received TOPIC message for unknown channel {}.".format( params[0])) return channel = self.connection.channels[params[0]] if nick not in self.connection.users: # A user that's not in the channel can change the topic so let's make a temporary user. user = IRCUser(nick, ident, host) else: user = self.connection.users[nick] oldTopic = channel.topic channel.topic = params[1] channel.topicSetter = user.fullUserPrefix() channel.topicTimestamp = timeutils.timestamp(timeutils.now()) self.moduleHandler.runGenericAction("changetopic", self.connection.name, channel, user, oldTopic, params[1])
def _getTime(self, lat, lon): currentTime = timestamp(now()) params = { "format": "json", "by": "position", "key": self.apiKey, "lat": lat, "lng": lon } result = self.bot.moduleHandler.runActionUntilValue("fetch-url", self.timeBaseURL, params) if not result: return "No time for this location could be found at this moment. Try again later." timeJSON = result.json() if timeJSON["status"] != "OK": if "message" in timeJSON: return timeJSON["message"] else: return "An unknown error occurred while requesting the time." resultDate = datetime.fromtimestamp(currentTime + int(timeJSON["gmtOffset"])) properDay = self._getProperDay(resultDate.day) formattedTime = resultDate.strftime("%H:%M (%I:%M %p) on %A, " + properDay + " of %B, %Y") return "Timezone: {} ({}) | Local time is {} | Daylight Savings Time: {}".format( timeJSON["zoneName"], timeJSON["abbreviation"], formattedTime, "Yes" if timeJSON["dst"] == "1" else "No")
def _processTells(self, message: IRCMessage): chanTells = [] pmTells = [] for tell in [i for i in self.tells]: if not any(fnmatch(message.user.nick.lower(), r) for r in tell["to"].split("/")): continue if now().isoformat() < tell["datetoreceive"]: continue if tell["source"][0] in self.bot.supportHelper.chanTypes and len(chanTells) < 3: if tell["source"] == message.replyTo: chanTells.append(tell) self.tells.remove(tell) elif tell["source"][0] not in self.bot.supportHelper.chanTypes: pmTells.append(tell) self.tells.remove(tell) responses = [] for tell in chanTells: responses.append(IRCResponse(ResponseType.Say, _parseTell(message.user.nick, tell), message.replyTo)) for tell in pmTells: responses.append(IRCResponse(ResponseType.Say, _parseTell(message.user.nick, tell), message.user.nick)) if len(chanTells) > 0 or len(pmTells) > 0: self.bot.storage["tells"] = self.tells return responses
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 _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 _parseTell(nick, tell): return "{}: {} < From {} ({} ago).".format(nick, b64ToStr(tell["body"]), tell["from"], timeDeltaString(now(), parse(tell["date"])))