2014-03-10 01:18:05 -04:00
|
|
|
# Authors:
|
|
|
|
# Derek Battams <derek@battams.ca>
|
|
|
|
# Pedro Jose Pereira Vieito (@pvieito) <pvieito@gmail.com>
|
|
|
|
#
|
2014-05-05 05:06:15 -04:00
|
|
|
# URL: https://github.com/echel0n/SickRage
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# This file is part of SickRage.
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is free software: you can redistribute it and/or modify
|
2014-03-10 01:18:05 -04:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is distributed in the hope that it will be useful,
|
2014-03-10 01:18:05 -04:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2014-07-03 17:04:26 -04:00
|
|
|
# GNU General Public License for more details.
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2014-05-23 08:37:22 -04:00
|
|
|
# along with SickRage. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
import smtplib
|
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
from email.mime.text import MIMEText
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
import sickbeard
|
|
|
|
|
2014-07-03 02:43:48 -04:00
|
|
|
from sickbeard import logger, common
|
2014-03-10 01:18:05 -04:00
|
|
|
from sickbeard import db
|
|
|
|
from sickbeard.exceptions import ex
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
class EmailNotifier:
|
2014-03-20 14:03:22 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.last_err = None
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-20 14:03:22 -04:00
|
|
|
def test_notify(self, host, port, smtp_from, use_tls, user, pwd, to):
|
2014-05-23 08:37:22 -04:00
|
|
|
msg = MIMEText('This is a test message from SickRage. If you\'re reading this, the test succeeded.')
|
|
|
|
msg['Subject'] = 'SickRage: Test Message'
|
2014-03-20 14:03:22 -04:00
|
|
|
msg['From'] = smtp_from
|
|
|
|
msg['To'] = to
|
|
|
|
return self._sendmail(host, port, smtp_from, use_tls, user, pwd, [to], msg, True)
|
|
|
|
|
|
|
|
def notify_snatch(self, ep_name, title="Snatched:"):
|
|
|
|
"""
|
|
|
|
Send a notification that an episode was snatched
|
|
|
|
|
|
|
|
ep_name: The name of the episode that was snatched
|
|
|
|
title: The title of the notification (optional)
|
|
|
|
"""
|
|
|
|
if sickbeard.EMAIL_NOTIFY_ONSNATCH:
|
|
|
|
show = self._parseEp(ep_name)
|
|
|
|
to = self._generate_recepients(show)
|
|
|
|
if len(to) == 0:
|
|
|
|
logger.log('Skipping email notify because there are no configured recepients', logger.WARNING)
|
|
|
|
else:
|
2014-03-25 01:57:24 -04:00
|
|
|
try:
|
|
|
|
msg = MIMEMultipart('alternative')
|
|
|
|
msg.attach(MIMEText(
|
2014-05-23 08:37:22 -04:00
|
|
|
"<body style='font-family:Helvetica, Arial, sans-serif;'><h3>SickRage Notification - Snatched</h3>\n<p>Show: <b>" + re.search(
|
2014-03-25 01:57:24 -04:00
|
|
|
"(.+?) -.+", ep_name).group(1) + "</b></p>\n<p>Episode: <b>" + re.search(
|
|
|
|
".+ - (.+?-.+) -.+", ep_name).group(
|
2014-05-23 08:37:22 -04:00
|
|
|
1) + "</b></p>\n\n<footer style='margin-top: 2.5em; padding: .7em 0; color: #777; border-top: #BBB solid 1px;'>Powered by SickRage.</footer></body>",
|
2014-03-25 01:57:24 -04:00
|
|
|
'html'))
|
|
|
|
except:
|
|
|
|
msg = MIMEText(ep_name)
|
|
|
|
|
2014-03-20 14:03:22 -04:00
|
|
|
msg['Subject'] = 'Snatched: ' + ep_name
|
|
|
|
msg['From'] = sickbeard.EMAIL_FROM
|
|
|
|
msg['To'] = ','.join(to)
|
2014-03-25 01:57:24 -04:00
|
|
|
if self._sendmail(sickbeard.EMAIL_HOST, sickbeard.EMAIL_PORT, sickbeard.EMAIL_FROM, sickbeard.EMAIL_TLS,
|
|
|
|
sickbeard.EMAIL_USER, sickbeard.EMAIL_PASSWORD, to, msg):
|
2014-03-20 14:03:22 -04:00
|
|
|
logger.log("Snatch notification sent to [%s] for '%s'" % (to, ep_name), logger.DEBUG)
|
|
|
|
else:
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log("Snatch notification ERROR: %s" % self.last_err, logger.ERROR)
|
2014-03-20 14:03:22 -04:00
|
|
|
|
|
|
|
def notify_download(self, ep_name, title="Completed:"):
|
|
|
|
"""
|
|
|
|
Send a notification that an episode was downloaded
|
|
|
|
|
|
|
|
ep_name: The name of the episode that was downloaded
|
|
|
|
title: The title of the notification (optional)
|
|
|
|
"""
|
|
|
|
if sickbeard.EMAIL_NOTIFY_ONDOWNLOAD:
|
|
|
|
show = self._parseEp(ep_name)
|
|
|
|
to = self._generate_recepients(show)
|
|
|
|
if len(to) == 0:
|
|
|
|
logger.log('Skipping email notify because there are no configured recepients', logger.WARNING)
|
|
|
|
else:
|
2014-03-25 01:57:24 -04:00
|
|
|
try:
|
|
|
|
msg = MIMEMultipart('alternative')
|
|
|
|
msg.attach(MIMEText(
|
2014-05-23 08:37:22 -04:00
|
|
|
"<body style='font-family:Helvetica, Arial, sans-serif;'><h3>SickRage Notification - Downloaded</h3>\n<p>Show: <b>" + re.search(
|
2014-03-25 01:57:24 -04:00
|
|
|
"(.+?) -.+", ep_name).group(1) + "</b></p>\n<p>Episode: <b>" + re.search(
|
|
|
|
".+ - (.+?-.+) -.+", ep_name).group(
|
2014-05-23 08:37:22 -04:00
|
|
|
1) + "</b></p>\n\n<footer style='margin-top: 2.5em; padding: .7em 0; color: #777; border-top: #BBB solid 1px;'>Powered by SickRage.</footer></body>",
|
2014-03-25 01:57:24 -04:00
|
|
|
'html'))
|
|
|
|
except:
|
|
|
|
msg = MIMEText(ep_name)
|
|
|
|
|
2014-03-20 14:03:22 -04:00
|
|
|
msg['Subject'] = 'Downloaded: ' + ep_name
|
|
|
|
msg['From'] = sickbeard.EMAIL_FROM
|
|
|
|
msg['To'] = ','.join(to)
|
2014-03-25 01:57:24 -04:00
|
|
|
if self._sendmail(sickbeard.EMAIL_HOST, sickbeard.EMAIL_PORT, sickbeard.EMAIL_FROM, sickbeard.EMAIL_TLS,
|
|
|
|
sickbeard.EMAIL_USER, sickbeard.EMAIL_PASSWORD, to, msg):
|
2014-03-20 14:03:22 -04:00
|
|
|
logger.log("Download notification sent to [%s] for '%s'" % (to, ep_name), logger.DEBUG)
|
|
|
|
else:
|
|
|
|
logger.log("Download notification ERROR: %s" % self.last_err, logger.ERROR)
|
|
|
|
|
|
|
|
def notify_subtitle_download(self, ep_name, lang, title="Downloaded subtitle:"):
|
|
|
|
"""
|
|
|
|
Send a notification that an subtitle was downloaded
|
|
|
|
|
|
|
|
ep_name: The name of the episode that was downloaded
|
|
|
|
lang: Subtitle language wanted
|
|
|
|
"""
|
|
|
|
if sickbeard.EMAIL_NOTIFY_ONSUBTITLEDOWNLOAD:
|
|
|
|
show = self._parseEp(ep_name)
|
|
|
|
to = self._generate_recepients(show)
|
|
|
|
if len(to) == 0:
|
|
|
|
logger.log('Skipping email notify because there are no configured recepients', logger.WARNING)
|
|
|
|
else:
|
2014-03-25 01:57:24 -04:00
|
|
|
try:
|
|
|
|
msg = MIMEMultipart('alternative')
|
|
|
|
msg.attach(MIMEText(
|
2014-05-23 08:37:22 -04:00
|
|
|
"<body style='font-family:Helvetica, Arial, sans-serif;'><h3>SickRage Notification - Subtitle Downloaded</h3>\n<p>Show: <b>" + re.search(
|
2014-03-25 01:57:24 -04:00
|
|
|
"(.+?) -.+", ep_name).group(1) + "</b></p>\n<p>Episode: <b>" + re.search(
|
|
|
|
".+ - (.+?-.+) -.+", ep_name).group(
|
2014-05-23 08:37:22 -04:00
|
|
|
1) + "</b></p>\n<p>Language: <b>" + lang + "</b></p>\n\n<footer style='margin-top: 2.5em; padding: .7em 0; color: #777; border-top: #BBB solid 1px;'>Powered by SickRage.</footer></body>",
|
2014-03-25 01:57:24 -04:00
|
|
|
'html'))
|
|
|
|
except:
|
|
|
|
msg = MIMEText(ep_name + ": " + lang)
|
|
|
|
|
2014-03-20 14:03:22 -04:00
|
|
|
msg['Subject'] = lang + ' Subtitle Downloaded: ' + ep_name
|
|
|
|
msg['From'] = sickbeard.EMAIL_FROM
|
|
|
|
msg['To'] = ','.join(to)
|
2014-03-25 01:57:24 -04:00
|
|
|
if self._sendmail(sickbeard.EMAIL_HOST, sickbeard.EMAIL_PORT, sickbeard.EMAIL_FROM, sickbeard.EMAIL_TLS,
|
|
|
|
sickbeard.EMAIL_USER, sickbeard.EMAIL_PASSWORD, to, msg):
|
2014-03-20 14:03:22 -04:00
|
|
|
logger.log("Download notification sent to [%s] for '%s'" % (to, ep_name), logger.DEBUG)
|
|
|
|
else:
|
|
|
|
logger.log("Download notification ERROR: %s" % self.last_err, logger.ERROR)
|
2014-07-03 02:43:48 -04:00
|
|
|
|
2014-07-03 17:04:26 -04:00
|
|
|
|
|
|
|
def notify_git_update(self, new_version="??"):
|
|
|
|
pass
|
2014-03-20 14:03:22 -04:00
|
|
|
|
|
|
|
def _generate_recepients(self, show):
|
|
|
|
addrs = []
|
|
|
|
|
|
|
|
# Grab the global recipients
|
|
|
|
for addr in sickbeard.EMAIL_LIST.split(','):
|
2014-03-25 01:57:24 -04:00
|
|
|
if (len(addr.strip()) > 0):
|
2014-03-20 14:03:22 -04:00
|
|
|
addrs.append(addr)
|
|
|
|
|
|
|
|
# Grab the recipients for the show
|
2014-06-21 18:46:59 -04:00
|
|
|
myDB = db.DBConnection()
|
|
|
|
for s in show:
|
|
|
|
for subs in myDB.select("SELECT notify_list FROM tv_shows WHERE show_name = ?", (s,)):
|
|
|
|
if subs['notify_list']:
|
|
|
|
for addr in subs['notify_list'].split(','):
|
|
|
|
if (len(addr.strip()) > 0):
|
|
|
|
addrs.append(addr)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-20 14:03:22 -04:00
|
|
|
addrs = set(addrs)
|
|
|
|
logger.log('Notification recepients: %s' % addrs, logger.DEBUG)
|
|
|
|
return addrs
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-20 14:03:22 -04:00
|
|
|
def _sendmail(self, host, port, smtp_from, use_tls, user, pwd, to, msg, smtpDebug=False):
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log('HOST: %s; PORT: %s; FROM: %s, TLS: %s, USER: %s, PWD: %s, TO: %s' % (
|
2014-07-03 17:04:26 -04:00
|
|
|
host, port, smtp_from, use_tls, user, pwd, to), logger.DEBUG)
|
2014-03-20 14:03:22 -04:00
|
|
|
srv = smtplib.SMTP(host, int(port))
|
|
|
|
if smtpDebug:
|
|
|
|
srv.set_debuglevel(1)
|
|
|
|
try:
|
|
|
|
if (use_tls == '1' or use_tls == True) or (len(user) > 0 and len(pwd) > 0):
|
|
|
|
srv.ehlo()
|
|
|
|
logger.log('Sent initial EHLO command!', logger.DEBUG)
|
|
|
|
if use_tls == '1' or use_tls == True:
|
|
|
|
srv.starttls()
|
|
|
|
logger.log('Sent STARTTLS command!', logger.DEBUG)
|
|
|
|
if len(user) > 0 and len(pwd) > 0:
|
|
|
|
srv.login(user, pwd)
|
|
|
|
logger.log('Sent LOGIN command!', logger.DEBUG)
|
|
|
|
srv.sendmail(smtp_from, to, msg.as_string())
|
|
|
|
srv.quit()
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
|
|
self.last_err = '%s' % e
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _parseEp(self, ep_name):
|
|
|
|
sep = " - "
|
|
|
|
titles = ep_name.split(sep)
|
|
|
|
titles.sort(key=len, reverse=True)
|
|
|
|
logger.log("TITLES: %s" % titles, logger.DEBUG)
|
|
|
|
return titles
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
notifier = EmailNotifier
|