def display_quote(self, request): """ Select a quote from database and post it to a channel. Usage: .quote [<text>|<id>] @param request: A runtime request of an InteractiveModule command. @return InteractiveModuleResponse """ response = InteractiveModuleResponse() try: text = request.parameter[0] if (len(text) > 0): try: id = int(text) quote = self.component.get_quote_by_id(id) except ValueError: quote = self.component.get_random_quote_by_string(request.parameter[0]) else: quote = self.component.get_random_quote() response.add_line("{0} ({1})".format(quote.text,quote.id)) except NoQuoteAvailable: response.add_line("No quotes in database.") return response
def roll(self, request): response = InteractiveModuleResponse() if request.parameter[1]: min_value = int(request.parameter[0]) max_value = int(request.parameter[1]) else: min_value = 1 max_value = int(request.parameter[0]) if max_value <= 0: response.add_line('Zahl muss groesser als 0 sein') return response if max_value <= 1: response.add_line('Zahl muss groesser als 1 sein') return response if max_value == min_value: response.add_line('Erste Zahl ({0}) == zweite Zahl ({0}), wie soll das denn gehen du Trottel?'.format(min_value, min_value)) return response if max_value < min_value: max_value, min_value = min_value, max_value result = random.randint(min_value, max_value) response.add_line('{0} has rolled: {1} ({2}-{3})'.format( request.source.nickname, result, min_value, min_value )) return response
def who(self, request): response = InteractiveModuleResponse() channel_name = request.target channel = self.usermgmt.chanlist.get(channel_name) user = random.choice(channel.get_users().keys()) response.add_line('Meine Wahl fällt auf {0}!'.format(user)) return response
def who(self, request): response = InteractiveModuleResponse() channel_name = request.target channel = self.usermgmt.chanlist.get(channel_name) user = random.choice(channel.get_users().keys()) response.add_line('Meine Wahl fällt auf {0}!'.format(user)) return response
def del_quote(self, request): """ Delete a quote with the given id from database. Usage: .delquote <id> @param request: A runtime request of an InteractiveModule command. @return InteractiveModuleResponse """ response = InteractiveModuleResponse() try: id = int(request.parameter[0]) self.component.delete_quote_by_id(id) response.add_line("Delete was successful.") except ValueError: response.add_line( "If you like to delete a quote, you'll have to declare an ID instead of a string." ) except NoAffectedRows: response.add_line( "Delete didn't work. Maybe, there isn't a quote with the given ID." ) return response
def add_new_addition(self, request): """ Insert a new addition to database Usage: .addtopic <addtion(text)> @param request: A runtime request of an InteractiveModule command. @return InteractiveModuleResponse """ response = InteractiveModuleResponse() self.component.insert_addition(request.parameter[0],request.source.nickname) response.add_line("The process was successful.") return response
def insert_user(self, request): """ .adduser [password] """ """password = request.parameter[0] try: user = self.getAuth(request.source) return "User '???' wurde als Admin hinzugefügt." if not user: raise UserNotAuthenticated if self.isPrivateUser(user): raise UserExists # TODO impl #password_hash = hashlib.sha1(password).hexdigest() except UserNotAuthenticated: return "Du bist nicht geauthed!" except UserExists: return "Du hast bereits einen Account!" return "Dein Account wurde erstellt!""" response = InteractiveModuleResponse('not implemented') return response
def add_new_addition(self, request): """ Insert a new addition to database Usage: .addtopic <addtion(text)> @param request: A runtime request of an InteractiveModule command. @return InteractiveModuleResponse """ response = InteractiveModuleResponse() self.component.insert_addition(request.parameter[0], request.source.nickname) response.add_line("The process was successful.") return response
def search_contact(self, request): """ """ response = InteractiveModuleResponse("not implemented") return response
def display_calendar_address(self, request): """ Display the web calendar's address. """ response = InteractiveModuleResponse(self.config.get('calendarUrl')) return response
def display_current_topic(self, request): """ Display the current topic. Usage: .topic @return InteractiveModuleResponse """ response = InteractiveModuleResponse() try: topic = self.component.get_last_topic() topic_string = self.component.create_topic_string(topic.text,topic.addition.text,topic.year) response.add_line('{0} set by {1}'.format(topic_string,topic.user)) except TopicNotFound: response.add_line("No topic available.") return response
def display_topic_additions(self, request): """ Send a link to a list with all additions. Usage: .listtopic @return: InteractiveModuleResponse """ return InteractiveModuleResponse("www.derlinkfehltnoch.de")
def sort(self,request): """ Reorder the given items. @return: InteractiveModuleResponse() """ response = InteractiveModuleResponse() items_unsorted = request.parameter[0].split(',') items_sorted = [] while len(items_unsorted) > 0: num = random.randint(0,len(items_unsorted)-1) items_sorted.append(items_unsorted[num].strip()) del items_unsorted[num] response.add_line(", ".join(items_sorted)) return response
def sort(self, request): """ Reorder the given items. @return: InteractiveModuleResponse() """ response = InteractiveModuleResponse() items_unsorted = request.parameter[0].split(',') items_sorted = [] while len(items_unsorted) > 0: num = random.randint(0, len(items_unsorted) - 1) items_sorted.append(items_unsorted[num].strip()) del items_unsorted[num] response.add_line(", ".join(items_sorted)) return response
def set_new_topic(self, request): """ Change the topic of a channel. Usage: .settopic <text|'reset'> If the module receive the string 'reset', it will set the default topic @param request: A runtime request of an InteractiveModule command. @return InteractiveModuleResponse """ response = InteractiveModuleResponse() #Normally, here I would check if there was set the mode +t in channel modes #because if it was set, I won't need to check the userMode. #But get_modes() is not implemnted, yet :( #channelModes = channelObject.get_modes() channel_object = self.usermgmt.chanlist.get(request.target) userMode = channel_object.get_user_mode(self.me.source.nickname) try: if (userMode == BOT_IS_OPERATOR): text = request.parameter[0] if (text == 'reset'): #default topic text = DEFAULT_TOPIC addition = self.component.get_random_addition().text year = random.randint(RANDOM_YEAR_START,RANDOM_YEAR_END) topic_cmd = self.client.get_command('Topic').get_sender() topic_cmd.channel = request.target topic_cmd.topic = self.create_topic_string(text,addition,year) topic_cmd.send() self.component.insert_topic(text,addition,year,request.source.nickname) else: response.add_line("Bot needs to be an operator to do this.") except NoAdditionAvailable: response.add_line("There are no topic additions available at the moment.") return response
def search_quotes(self,request): """ Search in database for quote with the given string. It will display all IDs of the founded quotes Usage: .searchquote <text> @param request: A runtime request of an InteractiveModule command. @return InteractiveModuleResponse """ response = InteractiveModuleResponse() ids = self.component.get_ids_by_string(request.parameter[0]) if len(ids) > 0: response.add_line("Found the string in following quotes: {0}".format(" ".join(str(ids)))) else: response.add_line("There isn't a quote with the given id.") return response
def display_quote(self, request): """ Select a quote from database and post it to a channel. Usage: .quote [<text>|<id>] @param request: A runtime request of an InteractiveModule command. @return InteractiveModuleResponse """ response = InteractiveModuleResponse() try: text = request.parameter[0] if (len(text) > 0): try: id = int(text) quote = self.component.get_quote_by_id(id) except ValueError: quote = self.component.get_random_quote_by_string( request.parameter[0]) else: quote = self.component.get_random_quote() response.add_line("{0} ({1})".format(quote.text, quote.id)) except NoQuoteAvailable: response.add_line("No quotes in database.") return response
def add_new_quote(self, request): """ Add a new quote to database. Max length of the quote is 375 letters. Usage: .addquote <text> @param request: A runtime request of an InteractiveModule command. @return InteractiveModuleResponse """ response = InteractiveModuleResponse() text = request.parameter[0] if len(text) <= MAX_LENGTH_QUOTE: self.component.insert_quote(text,request.source.nickname) response.add_line("The process was successful.") else: response.add_line("Max length of a quote is {0} letters. Yours is {1}." .format(MAX_LENGTH_QUOTE,str(len(text)))) return response
def search_event(self, request): """ Search for an event .searchevent <text> """ response = InteractiveModuleResponse() query = request.parameter[0] try: events = self.component.find_events_by_substring(query) [ response.add_line("(ID={0}) {1}".format(event.id, event.title)) for event in events ] except NoEventsFound: response.add_line("Keine Einträge mit diesem Inhalt gefunden.") return response
def change_event(self, event, location, command, parameter): """ Change an existing event. The event is identified by its ID. Syntax: <id> <start|ende|titel|beschreibung|ort> <wert> """ response = InteractiveModuleResponse() eventId = parameter[0] attribute = parameter[1] value = parameter[2] try: map = { 'start': 'start', 'ende': 'end', 'titel': 'title', 'beschreibung': 'description', 'ort': 'location' } if attribute in ['start', 'ende']: value = self._get_date(value) event = self.component.find_event_by_id(eventId) setattr(event, map[attribute], value) self.component.update_object(event) response.add_line( '{0} fuer Eintrag {1} wurde auf {2} gesetzt'.format( attribute.capitalize(), event.id, value)) except DateFormatInvalid: response.add_line( "Datum muss im Format [d]d.[m]m.yyyy sein. Bsp: 12.5.2010") except EventNotFound: response.add_line('Kein Event mit ID {0} gefunden'.format(eventId)) return response
def insert_event(self, request): """ Create a new event. .addevent <startdate>[-<enddate>] title """ response = InteractiveModuleResponse() date = request.parameter[0] title = request.parameter[1] try: if '-' not in date: dateFrom = self._get_date(date) dateTo = dateFrom else: dates = date.split('-') dateFrom = self._get_date(dates[0]) dateTo = self._get_date(dates[1]) calendar = self.component.find_default_calendar() event = Event(calendar=calendar, start=dateFrom, end=dateTo, title=title) event = self.component.insert_object(event) response.add_line("Eintrag erfolgreich eingefügt! ID: {0}".format( event.id)) except DateFormatInvalid: response.add_line( "Datum muss im Format [d]d.[m]m.yyyy sein. Bsp: 12.5.2010") except DateRangeInvalid as e: response.add_line('Ungültiges Datum: {0}.'.format(e)) return response
def del_addition(self, request): """ Delete a addition with the given id. .deltopic <id> @param request: A runtime request of an InteractiveModule command. @return: InteractiveModuleResponse """ response = InteractiveModuleResponse() try: id = int(request.parameter[0]) self.component.delete_addition_by_id(id) response.add_line("Delete was successful.") except NoAffectedRows: response.add_line("No entry was deleted.") except ValueError: response.add_line("Please enter a valid ID!") return response
def del_quote(self,request): """ Delete a quote with the given id from database. Usage: .delquote <id> @param request: A runtime request of an InteractiveModule command. @return InteractiveModuleResponse """ response = InteractiveModuleResponse() try: id = int(request.parameter[0]) self.component.delete_quote_by_id(id) response.add_line("Delete was successful.") except ValueError: response.add_line("If you like to delete a quote, you'll have to declare an ID instead of a string.") except NoAffectedRows: response.add_line("Delete didn't work. Maybe, there isn't a quote with the given ID.") return response
def set_new_topic(self, request): """ Change the topic of a channel. Usage: .settopic <text|'reset'> If the module receive the string 'reset', it will set the default topic @param request: A runtime request of an InteractiveModule command. @return InteractiveModuleResponse """ response = InteractiveModuleResponse() #Normally, here I would check if there was set the mode +t in channel modes #because if it was set, I won't need to check the userMode. #But get_modes() is not implemnted, yet :( #channelModes = channelObject.get_modes() channel_object = self.usermgmt.chanlist.get(request.target) userMode = channel_object.get_user_mode(self.me.source.nickname) try: if (userMode == BOT_IS_OPERATOR): text = request.parameter[0] if (text == 'reset'): #default topic text = DEFAULT_TOPIC addition = self.component.get_random_addition().text year = random.randint(RANDOM_YEAR_START, RANDOM_YEAR_END) topic_cmd = self.client.get_command('Topic').get_sender() topic_cmd.channel = request.target topic_cmd.topic = self.create_topic_string( text, addition, year) topic_cmd.send() self.component.insert_topic(text, addition, year, request.source.nickname) else: response.add_line("Bot needs to be an operator to do this.") except NoAdditionAvailable: response.add_line( "There are no topic additions available at the moment.") return response
def del_addition(self, request): """ Delete a addition with the given id. .deltopic <id> @param request: A runtime request of an InteractiveModule command. @return: InteractiveModuleResponse """ response = InteractiveModuleResponse() try: id = int(request.parameter[0]) self.component.delete_addition_by_id(id) response.add_line("Delete was successful.") except NoAffectedRows: response.add_line("No entry was deleted.") except ValueError: response.add_line("Please enter a valid ID!") return response
def display_current_topic(self, request): """ Display the current topic. Usage: .topic @return InteractiveModuleResponse """ response = InteractiveModuleResponse() try: topic = self.component.get_last_topic() topic_string = self.component.create_topic_string( topic.text, topic.addition.text, topic.year) response.add_line('{0} set by {1}'.format(topic_string, topic.user)) except TopicNotFound: response.add_line("No topic available.") return response
def search_quotes(self, request): """ Search in database for quote with the given string. It will display all IDs of the founded quotes Usage: .searchquote <text> @param request: A runtime request of an InteractiveModule command. @return InteractiveModuleResponse """ response = InteractiveModuleResponse() ids = self.component.get_ids_by_string(request.parameter[0]) if len(ids) > 0: response.add_line( "Found the string in following quotes: {0}".format(" ".join( str(ids)))) else: response.add_line("There isn't a quote with the given id.") return response
def add_new_quote(self, request): """ Add a new quote to database. Max length of the quote is 375 letters. Usage: .addquote <text> @param request: A runtime request of an InteractiveModule command. @return InteractiveModuleResponse """ response = InteractiveModuleResponse() text = request.parameter[0] if len(text) <= MAX_LENGTH_QUOTE: self.component.insert_quote(text, request.source.nickname) response.add_line("The process was successful.") else: response.add_line( "Max length of a quote is {0} letters. Yours is {1}.".format( MAX_LENGTH_QUOTE, str(len(text)))) return response
def display_events_by_date(self, request): """ Display events occuring at the given date. """ response = InteractiveModuleResponse() date = request.parameter[0] try: calendar_events = self.component.find_events_by_date(date) if len(calendar_events) == 0: raise NoEventsFound for calendar_event in calendar_events: response.add_line("ID {0}: {1}".format(calendar_event.id, calendar_event.title)) except NoEventsFound: response.add_line('Keine Termine gefunden.') return response
def topic_event(self, request): """ Post the given event to the current topic. .topicevent TODO: implement """ response = InteractiveModuleResponse() eventId = int(request.parameter[0]) try: result = self.component.find_event_by_id(eventId) if result is None: raise EventNotFound response.add_line('currently not implemented') except EventNotFound: response.add_line('Kein Event mit ID {0} gefunden'.format(eventId)) return response
def delete_event(self, request): """ .delevent [<id>|<datum>] """ response = InteractiveModuleResponse() # when only one parameter is defined a string is returned id_or_date = request.parameter[0] try: try: date = self._get_date(id_or_date) #--------------------------------------------------------------- # delete by date #--------------------------------------------------------------- events = self.component.find_events_by_date(date) count = len(events) if count == 0: raise NoEventsFound if count > 1: raise AmbiguousEventsFound(events) event = events[0] except DateFormatInvalid: #--------------------------------------------------------------- # delete by id #--------------------------------------------------------------- eventId = int(id_or_date) event = self.component.find_event_by_id(eventId) if not event: raise NoEventsFound self.component.delete_object(event) response.add_line( "Eintrag {0} wurde geloescht.".format(id_or_date)) except InvalidObjectId: response.add_line("Kein Eintrag zu dieser ID gefunden.") except NoEventsFound: response.add_line("Kein Eintrag zu diesem Datum gefunden.") except AmbiguousEventsFound as error: response.add_line('Mehrere Einträge gefunden:') [ response.add_line("(ID={0}) {1}".format(event.id, event.title)) for event in error.message ] return response
def quit(self, request): response = InteractiveModuleResponse('not implemented') return response
def restore_deleted_object(self, request): """ """ return InteractiveModuleResponse('not implemented')
def decide(self, request): response = InteractiveModuleResponse() issue = request.parameter[0] if len(issue) == 0: if random.randint(1, 1000) % 2 == 0: response.add_line('Du solltst dich dafür entscheiden!') else: response.add_line('Du solltst dich dagegen entscheiden!') else: choices = re.sub('( oder | or )', ',', issue).split(',') if len(choices) == 1: pick = choices[0] if random.randint(1, 1000) % 2 == 0: response.add_line( 'Du solltst dich für \'{0}\' entscheiden!'.format( pick.strip())) else: response.add_line( 'Du solltst dich gegen \'{0}\' entscheiden'.format( pick.strip())) else: pick = random.choice(choices) response.add_line( 'Du solltst dich für \'{0}\' entscheiden'.format( pick.strip())) return response
def decide(self, request): response = InteractiveModuleResponse() issue = request.parameter[0] if len(issue) == 0: if random.randint(1,1000) % 2 == 0: response.add_line('Du solltst dich dafür entscheiden!') else: response.add_line('Du solltst dich dagegen entscheiden!') else: choices = re.sub('( oder | or )', ',', issue).split(',') if len(choices) == 1: pick = choices[0] if random.randint(1,1000) % 2 == 0: response.add_line('Du solltst dich für \'{0}\' entscheiden!'.format(pick.strip())) else: response.add_line('Du solltst dich gegen \'{0}\' entscheiden'.format(pick.strip())) else: pick = random.choice(choices) response.add_line('Du solltst dich für \'{0}\' entscheiden'.format(pick.strip())) return response
def delete_user(self, request): response = InteractiveModuleResponse('not implemented') return response
def change_nickname(self, request): response = InteractiveModuleResponse('not implemented') return response
def part_channel(self, request): response = InteractiveModuleResponse('not implemented') return response
def display_deleted_objects(self, event, location, command, parameter): """ """ return InteractiveModuleResponse('not implemented')