From 0f6fc60b5e952298513317a8ef769451516ccb87 Mon Sep 17 00:00:00 2001 From: b3yond Date: Thu, 4 Jan 2018 11:02:42 +0100 Subject: [PATCH] crash reports are now sent via mail. documented config.toml.example --- config.toml.example | 33 ++++++++++++++---------- retootbot.py | 63 ++++++++++++++++++++++++++++++++++++--------- retweetbot.py | 27 ++++++++++--------- ticketfrei.py | 5 ++-- 4 files changed, 88 insertions(+), 40 deletions(-) diff --git a/config.toml.example b/config.toml.example index bb39b2c..1549e6c 100644 --- a/config.toml.example +++ b/config.toml.example @@ -1,28 +1,32 @@ [mapp] -name = 'yourcity_ticketfrei' # :todo: where do you receive the app name? +# The bot registers a mastodon app automatically to acquire OAuth keys. +name = 'yourcity_ticketfrei' # What you want the app to be called [muser] email = 'youremail@server.tld' # E-mail address of your Mastodon account password = 'yourpassword' # Password of your Mastodon account server = 'yourmastodoninstance' # Instance where you have your Mastodon account -[tapp] +[tapp] +# You get those keys when you follow these steps: +# https://developer.twitter.com/en/docs/basics/authentication/guides/access-tokens consumer_key = "OD0CLn6twBxHjN2DqMkKuSvli" -consumer_secret = "XkvbViwjBWoWoJzIlseJLXmg2fqluq4HYqvwOwoSHGwxdTNi4l" - -shutdown_contact_userid = 801098086005243904 -shutdown_contact_screen_name = 'links_tech' - -[tuser] -access_token_key = "876046057721008128-J35moxFXUvLb24MnaMVbVpqiEtxBlcc" +consumer_secret = "XkvbViwjBWoWoJzIlseJLXmg2fqluq4HYqvwOwoSHGwxdTNi4l" + +[tuser] +# You get those keys when you follow these steps: +# https://developer.twitter.com/en/docs/basics/authentication/guides/access-tokens +access_token_key = "876046057721008128-J35moxFXUvLb24MnaMVbVpqiEtxBlcc" access_token_secret = "I7PQZMHuJDS5WslgUhqEeZbEWGhwLhmOetvwFoTn8YDKW" [mail] +# This is the mail the bot uses to send emails. mailserver = "smtp.riseup.net" -port = 587 user = "ticketfrei" passphrase = "sup3rs3cur3" - +# If you want to receive crash reports (so you can restart the bot +# when it breaks down), you should specify a contact email address: +#contact = "your_mail@riseup.net" # [trigger] @@ -30,6 +34,7 @@ passphrase = "sup3rs3cur3" # badlists are one badword per line. # a message musst match at least one regex in goodlist and contain none of the badwords. # the variables mention the directory where the lists are located, not the filenames. - -# goodlist_path = 'goodlists' -# blacklist_path = 'blacklists' +# These are the default folders. If you want to specify differents folders, uncomment +# those lines and enter relative paths. +#goodlist_path = 'goodlists' +#blacklist_path = 'blacklists' diff --git a/retootbot.py b/retootbot.py index 05a4503..1df59ae 100755 --- a/retootbot.py +++ b/retootbot.py @@ -8,14 +8,17 @@ import re import time import trigger import logger +import sendmail +import traceback +import sys class RetootBot(object): def __init__(self, config, filter, logger): self.config = config self.filter = filter - self.register() - self.login() + self.client_id = self.register() + self.m = self.login() # load state try: @@ -24,31 +27,40 @@ class RetootBot(object): except IOError: self.seen_toots = set() + # intialize shutdown contact + try: + self.no_shutdown_contact = False + self.contact = self.config['mail']['contact'] + except KeyError: + self.no_shutdown_contact = True + self.logger = logger def register(self): - self.client_id = os.path.join( + client_id = os.path.join( 'appkeys', self.config['mapp']['name'] + '@' + self.config['muser']['server'] ) - if not os.path.isfile(self.client_id): + if not os.path.isfile(client_id): mastodon.Mastodon.create_app( self.config['mapp']['name'], api_base_url=self.config['muser']['server'], - to_file=self.client_id + to_file=client_id ) + return self.client_id def login(self): - self.m = mastodon.Mastodon( + m = mastodon.Mastodon( client_id=self.client_id, api_base_url=self.config['muser']['server'] ) - self.m.log_in( + m.log_in( self.config['muser']['email'], self.config['muser']['password'] ) + return m def retoot(self, toots=()): # toot external provided messages @@ -83,17 +95,44 @@ class RetootBot(object): # return mentions for mirroring return retoots + def shutdown(self, tb): + """ If something breaks, it shuts down the bot and messages the owner. + """ + logmessage = "Shit went wrong, closing down.\n" + tb + "\n\n" + if self.no_shutdown_contact: + self.logger.log(logmessage) + return + logmessage = logmessage + " Sending message to " + self.contact + self.logger.log(logmessage) + # feature yet only in RetweetBot: + # self.save_last_mention() + try: + mailer = sendmail.Mailer(self.config) + mailer.send(tb, self.contact, "Ticketfrei Crash Report") + except: + # traceback.print_exc() + self.logger.log(traceback.extract_tb(sys.exc_info()[2])) + print() + if __name__ == '__main__': # read config in TOML format (https://github.com/toml-lang/toml#toml) with open('config.toml') as configfile: config = toml.load(configfile) - filter = trigger.Trigger(config) + trigger = trigger.Trigger(config) logger = logger.Logger() - bot = RetootBot(config, filter, logger) + bot = RetootBot(config, trigger, logger) - while True: - bot.retoot() - time.sleep(1) + try: + while True: + bot.retoot() + time.sleep(1) + except KeyboardInterrupt: + print("Good bye! Remember to restart the bot.") + except: + tb = traceback.extract_tb(sys.exc_info()[2]) + bot.logger.log(tb) + print() + bot.shutdown(tb) diff --git a/retweetbot.py b/retweetbot.py index ba20ed1..8fd50b4 100755 --- a/retweetbot.py +++ b/retweetbot.py @@ -8,6 +8,7 @@ import trigger from time import sleep import traceback import logger +import sendmail class RetweetBot(object): @@ -46,8 +47,7 @@ class RetweetBot(object): # intialize shutdown contact try: self.no_shutdown_contact = False - self.screen_name = \ - self.config['tapp']['shutdown_contact_screen_name'] + self.contact = self.config['mail']['contact'] except KeyError: self.no_shutdown_contact = True @@ -55,9 +55,8 @@ class RetweetBot(object): self.last_mention = self.get_history(self.historypath) self.trigger = trigger self.waitcounter = 0 - - self.logger = logger + self.logger = logger def get_api_keys(self): """ @@ -217,18 +216,21 @@ class RetweetBot(object): # Return Retweets for tooting on mastodon return mastodon - def shutdown(self): + def shutdown(self, tb): """ If something breaks, it shuts down the bot and messages the owner. + + :param tb: (string) traceback """ - logmessage = "Shit went wrong, closing down." - if self.screen_name: - logmessage = logmessage + " Sending message to " + self.screen_name - self.logger.log(logmessage) + logmessage = "Shit went wrong, closing down.\n" + tb + "\n\n" if self.no_shutdown_contact: + self.logger.log(logmessage) return + logmessage = logmessage + " Sending message to " + self.contact + self.logger.log(logmessage) self.save_last_mention() try: - self.api.send_direct_message(self.screen_name, "Help! I broke down. restart me pls :$") + mailer = sendmail.Mailer(self.config) + mailer.send(tb, self.contact, "Ticketfrei Crash Report") except: # traceback.print_exc() self.logger.log(traceback.extract_tb(sys.exc_info()[2])) @@ -251,6 +253,7 @@ if __name__ == "__main__": except KeyboardInterrupt: print("Good bye! Remember to restart the bot.") except: - bot.logger.log(traceback.extract_tb(sys.exc_info()[2])) + tb = traceback.extract_tb(sys.exc_info()[2]) + bot.logger.log(tb) print() - bot.shutdown() \ No newline at end of file + bot.shutdown(tb) diff --git a/ticketfrei.py b/ticketfrei.py index 4283fb9..9176383 100755 --- a/ticketfrei.py +++ b/ticketfrei.py @@ -35,5 +35,6 @@ if __name__ == '__main__': except KeyboardInterrupt: print("Good bye. Remember to restart the bot!") except: - tbot.logger(traceback.extract_tb(sys.exc_info()[2])) - tbot.shutdown() + tb = traceback.extract_tb(sys.exc_info()[2]) + tbot.logger.log(tb) + tbot.shutdown(tb)