Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mailblaster re-write #16

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions .github/workflows/make-lint.yml

This file was deleted.

14 changes: 0 additions & 14 deletions .github/workflows/make-test.yml

This file was deleted.

58 changes: 50 additions & 8 deletions .github/workflows/release-container.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,65 @@
# Workflow built using instructions from
# https://docs.github.com/en/actions/guides/publishing-docker-images

name: Create and publish a Docker image
name: Mailblaster CI

on:
push:
branches:
- main
- "main"
tags:
- "*"
pull_request:
branches:
- main

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
test:
name: Pytest the Bot
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
with:
fetch-depth: 1
- name: Run Makefile
run: make test

formatblack:
name: Check Black Formatting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Check files using the black formatter
uses: rickstaa/action-black@v1
id: action_black
with:
black_args: "."

- name: Annotate diff changes using reviewdog
if: steps.action_black.outputs.is_formatted == 'true'
uses: reviewdog/action-suggester@v1
with:
tool_name: blackfmt

- name: Fail if actions taken
if: steps.action_black.outputs.is_formatted == 'true'
run: exit 1

- name: Discord notification
if: ${{ failure() }}
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
uses: Ilshidur/action-discord@master
with:
args: "Black formatter reported errors in {{ EVENT_PAYLOAD.pull_request.html_url }} !"

build-and-publish-image:
# Only publish on tags
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')

name: Build and Publish Registry Image
runs-on: ubuntu-latest

permissions:
contents: read
packages: write
Expand Down
48 changes: 25 additions & 23 deletions email_blaster/cogs/check_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ def cog_unload(self):

@tasks.loop(minutes=3)
async def check_email(self):
alert_chennel_id = self.bot.config['alertschannel']
alert_chennel_id = self.bot.config["alertschannel"]
alert_chanel = self.bot.get_channel(int(alert_chennel_id))

logging.debug(f"Checking for new emails, sending info to {alert_chennel_id}")

email_content = self.get_new_emails()

msg_chunk = self.split_into_chunks(email_content, 1900)
logging.debug('Split into chunks')
logging.debug("Split into chunks")
logging.debug(msg_chunk)

if len(msg_chunk) > 0:
Expand All @@ -50,18 +50,20 @@ async def check_email(self):

await alert_chanel.send(payload)
else:
logging.debug('Nothing to send.')
logging.debug("Nothing to send.")

@check_email.before_loop
async def before_email(self):
# Wait till the bot is ready
logging.info('Email Checking Cog is loaded and ready, waiting for bot to start..')
logging.info(
"Email Checking Cog is loaded and ready, waiting for bot to start.."
)
await self.bot.wait_until_ready()

# Start the imap connector
self.email_address = self.bot.config['email']
self.email_password = self.bot.config['emailpassword']
self.email_server = self.bot.config['emailserver']
self.email_address = self.bot.config["email"]
self.email_password = self.bot.config["emailpassword"]
self.email_server = self.bot.config["emailserver"]

# login to mailserver.
self.login()
Expand All @@ -78,11 +80,11 @@ def get_new_emails(self):

# Select mailbox
try:
self.mail.select('inbox')
self.mail.select("inbox")
except socket.error as e:
logging.error(f'Doing a socket reconnect, got error: {e}')
logging.error(f"Doing a socket reconnect, got error: {e}")
self.login()
self.mail.select('inbox')
self.mail.select("inbox")

logging.debug("Checking for new emails.")
# From here https://humberto.io/blog/sending-and-receiving-emails-with-python/
Expand All @@ -95,14 +97,14 @@ def get_new_emails(self):

# Fetch the mail using each ID
for i in mail_ids:
status, data = self.mail.fetch(i, '(RFC822)')
status, data = self.mail.fetch(i, "(RFC822)")
for response_part in data:
if isinstance(response_part, tuple):
message = email.message_from_bytes(response_part[1])
mail_from = message['from']
mail_subject = message['subject']
mail_from = message["from"]
mail_subject = message["subject"]
if message.is_multipart():
mail_content = ''
mail_content = ""

# on multipart we have the text message and
# another things like annex, and html version
Expand All @@ -111,37 +113,37 @@ def get_new_emails(self):
for part in message.get_payload():
# if the content type is text/plain
# we extract it
if part.get_content_type() == 'text/plain':
if part.get_content_type() == "text/plain":
mail_content += part.get_payload()
else:
# if the message isn't multipart, just extract it
mail_content = message.get_payload()

# and then let's show its result
logging.info(f'From: {mail_from}')
logging.info(f'Subject: {mail_subject}')
logging.info(f'Content: {mail_content}')
logging.info(f"From: {mail_from}")
logging.info(f"Subject: {mail_subject}")
logging.info(f"Content: {mail_content}")

return mail_content
return "" # Return empty string otherwise

@check_email.error
async def exception_catching_callback(self, e):
logging.error(f'caught error: {e}')
logging.error(f"caught error: {e}")
quit()

def split_into_chunks(self, string, max_len):
chunks = []

i = 0
while i < len(string):
if i+max_len < len(string):
chunks.append(string[i:i+max_len])
if i + max_len < len(string):
chunks.append(string[i : i + max_len])
else:
chunks.append(string[i:len(string)])
chunks.append(string[i : len(string)])
i += max_len

return(chunks)
return chunks


def setup(bot):
Expand Down
10 changes: 6 additions & 4 deletions email_blaster/cogs/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ def __init__(self, bot):
async def on_member_join(self, member):
channel = member.guild.system_channel
if channel is not None:
await channel.send('Welcome {0.mention}.'.format(member))
await channel.send("Welcome {0.mention}.".format(member))

@commands.Cog.listener()
async def on_ready(self):
status_channel = self.bot.get_channel(int('590312300695650305'))
status_channel = self.bot.get_channel(int("590312300695650305"))

await status_channel.send(f'Email Blaster version {self.bot.version} just restarted.')
await status_channel.send(
f"Email Blaster version {self.bot.version} just restarted."
)

@commands.command()
async def version(self, ctx, *, member: discord.Member = None):
await ctx.send(f'I am running version {self.bot.version}.')
await ctx.send(f"I am running version {self.bot.version}.")

# This wont work till discord 2.0
# @commands.command()
Expand Down
31 changes: 19 additions & 12 deletions email_blaster/keyValueTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@


class KeyValueTable(dict):
'''
"""
A dict-like object inheriting dict and allowing manipulation of a database
via dict-like interface.
'''
"""

def __init__(self, filename=None):
self.conn = sqlite3.connect(filename)
self.conn.execute("CREATE TABLE IF NOT EXISTS config (key text unique, value text)")
self.conn.execute(
"CREATE TABLE IF NOT EXISTS config (key text unique, value text)"
)

def commit(self):
self.conn.commit()
Expand All @@ -23,22 +26,22 @@ def close(self):
self.conn.close()

def __len__(self):
rows = self.conn.execute('SELECT COUNT(*) FROM config').fetchone()[0]
rows = self.conn.execute("SELECT COUNT(*) FROM config").fetchone()[0]
return rows if rows is not None else 0

def iterkeys(self):
self.conn.cursor()
for row in self.conn.execute('SELECT key FROM config'):
for row in self.conn.execute("SELECT key FROM config"):
yield row[0]

def itervalues(self):
c = self.conn.cursor()
for row in c.execute('SELECT value FROM config'):
for row in c.execute("SELECT value FROM config"):
yield row[0]

def iteritems(self):
c = self.conn.cursor()
for row in c.execute('SELECT key, value FROM config'):
for row in c.execute("SELECT key, value FROM config"):
yield row[0], row[1]

def keys(self):
Expand All @@ -51,22 +54,26 @@ def items(self):
return list(self.iteritems())

def __contains__(self, key):
return self.conn.execute('SELECT 1 FROM config WHERE key = ?',
(key,)).fetchone() is not None
return (
self.conn.execute("SELECT 1 FROM config WHERE key = ?", (key,)).fetchone()
is not None
)

def __getitem__(self, key):
item = self.conn.execute('SELECT value FROM config WHERE key = ?', (key,)).fetchone()
item = self.conn.execute(
"SELECT value FROM config WHERE key = ?", (key,)
).fetchone()
if item is None:
raise KeyError(key)
return item[0]

def __setitem__(self, key, value):
self.conn.execute('REPLACE INTO config (key, value) VALUES (?,?)', (key, value))
self.conn.execute("REPLACE INTO config (key, value) VALUES (?,?)", (key, value))

def __delitem__(self, key):
if key not in self:
raise KeyError(key)
self.conn.execute('DELETE FROM config WHERE key = ?', (key,))
self.conn.execute("DELETE FROM config WHERE key = ?", (key,))

def __iter__(self):
return self.iterkeys()
Loading