Skip to content

Commit

Permalink
Merge 922990c into 9fb1090
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya274 committed Oct 15, 2019
2 parents 9fb1090 + 922990c commit 91c94d2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pastepwn/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .savejsonaction import SaveJSONAction
from .twitteraction import TwitterAction
from .discordaction import DiscordAction
from .emailaction import EmailAction

__all__ = (
"BasicAction",
Expand All @@ -20,4 +21,5 @@
"SaveJSONAction",
"TwitterAction",
"DiscordAction",
"EmailAction"
)
55 changes: 55 additions & 0 deletions pastepwn/actions/emailaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
import re
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

from .basicaction import BasicAction


class EmailAction(BasicAction):
"""This action takes a username, password, receiver mail address,
hostname, port and when executed sends out an e-mail to the receiver
containing the paste"""
name = "EmailAction"

def __init__(self, username, password, receiver, hostname, port=465, template=None):
super().__init__()
mail_regex = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
if username is None or not re.match(mail_regex, username):
raise ValueError('Invalid username !')
else:
self.username = username
if receiver is None or not re.match(mail_regex, receiver):
raise ValueError('Invalid reciever address !')
else:
self.receiver = receiver
self.password = password
self.hostname = hostname
self.port = port
self.template = template

def perform(self, paste, analyzer_name=None):
"""
Sends an email to the specified receiver with the paste's content
:param paste: The paste passed by the ActionHandler
:param analyzer_name: The name of the analyzer which matched the paste
:return: None
"""
if self.template is None:
text = "New paste matched by analyzer '{0}' - Link: {1}".format(analyzer_name, paste.full_url)
else:
text = paste.body
# text = TemplatingEngine.fill_template(paste, analyzer_name, template_string=self.template)

email = MIMEMultipart()
email['From'] = self.username
email['To'] = self.receiver
email['Subject'] = 'Paste matched by pastepwn via analyzer "{}"'.format(analyzer_name)
email.attach(MIMEText(text, 'plain'))

# TODO there should be a way to use starttls - check https://realpython.com/python-send-email/
with smtplib.SMTP_SSL(self.hostname, self.port) as smtp:
smtp.login(self.username, self.password)
text = email.as_string()
smtp.sendmail(self.username, self.receiver, text)

0 comments on commit 91c94d2

Please sign in to comment.