started an IMAP listener to implement a 3rd bot: the Mailbot. #11

This commit is contained in:
b3yond 2018-01-05 14:16:24 +01:00
parent 01ad0e1c40
commit 5c98aa7677
4 changed files with 105 additions and 2 deletions

View file

@ -27,6 +27,8 @@ passphrase = "sup3rs3cur3"
# If you want to receive crash reports (so you can restart the bot # If you want to receive crash reports (so you can restart the bot
# when it breaks down), you should specify a contact email address: # when it breaks down), you should specify a contact email address:
#contact = "your_mail@riseup.net" #contact = "your_mail@riseup.net"
# Mailing list where you want to send warnings to
#list = "nbg_ticketfrei@lists.links-tech.org"
[logging] [logging]
# The directory where logs should be stored. # The directory where logs should be stored.

99
mailbot.py Normal file
View file

@ -0,0 +1,99 @@
#!/usr/bin/env python3
import sendmail
import datetime
import email
import logger
import pytoml as toml
import imaplib
import sys
class Mailbot(object):
"""
Bot which sends Mails if mentioned via twitter/mastodon, and tells
other bots that it received mails.
"""
def __init__(self, config, logger):
"""
Creates a Bot who listens to mails and forwards them to other
bots.
:param config: (dictionary) config.toml as a dictionary of dictionaries
"""
self.config = config
self.logger = logger
try:
self.mailinglist = self.config["mail"]["list"]
except KeyError:
self.mailinglist = None
self.mailbox = imaplib.IMAP4_SSL(self.config["mail"]["imapserver"])
# context = ssl.create_default_context()
# print(self.mailbox.starttls(ssl_context=context)) # print is a debug
try:
rv, data = self.mailbox.login(self.config["mail"]["user"],
self.config["mail"]["passphrase"])
except imaplib.IMAP4.error:
logmsg = "Login to mail server failed."
logmsg = logmsg + logger.generate_tb(sys.exc_info())
logger.log(logmsg)
logger.shutdown(logmsg)
def listen(self):
"""
listen for mails which contain goodwords but no badwords.
:return:
"""
rv, data = self.mailbox.select("Inbox")
if rv == 'OK':
rv, data = self.mailbox.search(None, "ALL")
print(data)
rv, data = self.mailbox.search(None, "ALL")
if rv != 'OK':
print("No messages found!")
return
for num in data[0].split():
rv, data = self.mailbox.fetch(num, '(RFC822)')
if rv != 'OK':
print("ERROR getting message", num)
return
msg = email.message_from_bytes(data[0][1])
hdr = email.header.make_header(email.header.decode_header(msg['Subject']))
subject = str(hdr)
print('Message %s: %s' % (num, subject))
print('Raw Date:', msg['Date'])
# Now convert to local date-time
date_tuple = email.utils.parsedate_tz(msg['Date'])
if date_tuple:
local_date = datetime.datetime.fromtimestamp(
email.utils.mktime_tz(date_tuple))
print ("Local Date:", local_date.strftime("%a, %d %b %Y %H:%M:%S"))
#print(msg.as_string())
author = msg.get("From") # get mail author from email header
text = msg.get_payload()
print(author)
print(text)
# :todo check if they match trigger
# :todo return a nice list of warning messages
def send_warning(self, statuses):
"""
sends warnings by twitter & mastodon to a mailing list.
"""
for status in statuses:
mailer = sendmail.Mailer(self.config)
mailer.send(status, self.mailinglist, "Warnung: Kontrolleure gesehen")
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)
logger = logger.Logger(config)
m = Mailbot(config, logger)
m.listen()

View file

@ -27,7 +27,7 @@ class RetweetBot(object):
Initializes the bot and loads all the necessary data. Initializes the bot and loads all the necessary data.
:param trigger: object of the trigger :param trigger: object of the trigger
:param config: object of the config :param config: (dictionary) config.toml as a dictionary of dictionaries
:param logger: object of the logger :param logger: object of the logger
:param historypath: Path to the file with ID of the last retweeted :param historypath: Path to the file with ID of the last retweeted
Tweet Tweet

View file

@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import smtplib import smtplib
import ssl
import pytoml as toml import pytoml as toml
from email.mime.text import MIMEText from email.mime.text import MIMEText
from email.mime.application import MIMEApplication from email.mime.application import MIMEApplication
@ -27,7 +28,8 @@ class Mailer(object):
# starts a client session with the SMTP server # starts a client session with the SMTP server
self.s = smtplib.SMTP(config["mail"]["mailserver"]) self.s = smtplib.SMTP(config["mail"]["mailserver"])
self.s.starttls() context = ssl.create_default_context()
self.s.starttls(context=context)
self.s.login(config["mail"]["user"], config["mail"]["passphrase"]) self.s.login(config["mail"]["user"], config["mail"]["passphrase"])
def send(self, text, recipient, subject, attachment=None): def send(self, text, recipient, subject, attachment=None):