示例#1
0
def register_growl(growl_server, growl_password):
    """ Register this app with Growl """
    error = None
    host, port = split_host(growl_server or '')

    sys_name = hostname(host)

    # Clean up persistent data in GNTP to make re-registration work
    GNTPRegister.notifications = []
    GNTPRegister.headers = {}

    growler = GrowlNotifier(
        applicationName='SABnzbd%s' % sys_name,
        applicationIcon=get_icon(),
        notifications=[Tx(NOTIFICATION[key]) for key in NOTIFY_KEYS],
        hostname=host or 'localhost',
        port=port or 23053,
        password=growl_password or None
    )

    try:
        ret = growler.register()
        if ret is None or isinstance(ret, bool):
            logging.info('Registered with Growl')
            ret = growler
        else:
            error = 'Cannot register with Growl %s' % str(ret)
            logging.debug(error)
            del growler
            ret = None
    except socket.error, err:
        error = 'Cannot register with Growl %s' % str(err)
        logging.debug(error)
        del growler
        ret = None
示例#2
0
 def test_split_host(self):
     assert (None, None) == misc.split_host(None)
     assert (None, None) == misc.split_host("")
     assert ("sabnzbd.org", 123) == misc.split_host("sabnzbd.org:123")
     assert ("sabnzbd.org", None) == misc.split_host("sabnzbd.org")
     assert ("127.0.0.1", 566) == misc.split_host("127.0.0.1:566")
     assert ("[::1]", 1234) == misc.split_host("[::1]:1234")
     assert ("[2001:db8::8080]", None) == misc.split_host("[2001:db8::8080]")
示例#3
0
def register_growl(growl_server, growl_password):
    """ Register this app with Growl """
    error = None
    host, port = split_host(growl_server or '')

    sys_name = hostname(host)

    # Reduce logging of Growl in Debug/Info mode
    logging.getLogger('gntp').setLevel(logging.WARNING)

    # Clean up persistent data in GNTP to make re-registration work
    GNTPRegister.notifications = []
    GNTPRegister.headers = {}

    growler = GrowlNotifier(
        applicationName='SABnzbd%s' % sys_name,
        applicationIcon=get_icon(),
        notifications=[Tx(NOTIFICATION[key]) for key in NOTIFY_KEYS],
        hostname=host or 'localhost',
        port=port or 23053,
        password=growl_password or None
    )

    try:
        ret = growler.register()
        if ret is None or isinstance(ret, bool):
            logging.info('Registered with Growl')
            ret = growler
        else:
            error = 'Cannot register with Growl %s' % str(ret)
            logging.debug(error)
            del growler
            ret = None
    except (gntp.errors.NetworkError, gntp.errors.AuthError) as err:
        error = 'Cannot register with Growl %s' % str(err)
        logging.debug(error)
        del growler
        ret = None
    except:
        error = 'Unknown Growl registration error'
        logging.debug(error)
        logging.info("Traceback: ", exc_info=True)
        del growler
        ret = None

    return ret, error
示例#4
0
def register_growl(growl_server, growl_password):
    """ Register this app with Growl """
    error = None
    host, port = split_host(growl_server or '')

    sys_name = hostname(host)

    # Reduce logging of Growl in Debug/Info mode
    logging.getLogger('gntp').setLevel(logging.WARNING)

    # Clean up persistent data in GNTP to make re-registration work
    GNTPRegister.notifications = []
    GNTPRegister.headers = {}

    growler = GrowlNotifier(
        applicationName='SABnzbd%s' % sys_name,
        applicationIcon=get_icon(),
        notifications=[Tx(NOTIFICATION[key]) for key in NOTIFY_KEYS],
        hostname=host or 'localhost',
        port=port or 23053,
        password=growl_password or None
    )

    try:
        ret = growler.register()
        if ret is None or isinstance(ret, bool):
            logging.info('Registered with Growl')
            ret = growler
        else:
            error = 'Cannot register with Growl %s' % str(ret)
            logging.debug(error)
            del growler
            ret = None
    except (gntp.errors.NetworkError, gntp.errors.AuthError) as err:
        error = 'Cannot register with Growl %s' % str(err)
        logging.debug(error)
        del growler
        ret = None
    except:
        error = 'Unknown Growl registration error'
        logging.debug(error)
        logging.info("Traceback: ", exc_info=True)
        del growler
        ret = None

    return ret, error
示例#5
0
def send_email(message, email_to, test=None):
    """ Send message if message non-empty and email-parms are set """
    # we should not use CFG if we are testing. we should use values
    # from UI instead.
    # email_to is replaced at send_with_template, since it can be an array
    if test:
        email_server = test.get("email_server")
        email_from = test.get("email_from")
        email_account = test.get("email_account")
        email_pwd = test.get("email_pwd")
        if email_pwd and not email_pwd.replace("*", ""):
            # If all stars, get stored password instead
            email_pwd = cfg.email_pwd()
    else:
        email_server = cfg.email_server()
        email_from = cfg.email_from()
        email_account = cfg.email_account()
        email_pwd = cfg.email_pwd()

    if not message.strip("\n\r\t "):
        return "Skipped empty message"

    # Prepare the email
    email_message = _prepare_message(message)

    if email_server and email_to and email_from:
        server, port = split_host(email_server)
        if not port:
            port = 25
        logging.debug("Connecting to server %s:%s", server, port)

        try:
            mailconn = smtplib.SMTP_SSL(server, port)
            mailconn.ehlo()
            logging.debug("Connected to server %s:%s", server, port)
        except:
            # Non SSL mail server
            logging.debug("Non-SSL mail server detected reconnecting to server %s:%s", server, port)

            try:
                mailconn = smtplib.SMTP(server, port)
                mailconn.ehlo()
            except:
                logging.info("Traceback: ", exc_info=True)
                return errormsg(T("Failed to connect to mail server"))

        # TLS support
        if mailconn.ehlo_resp:
            m = re.search(b"STARTTLS", mailconn.ehlo_resp, re.IGNORECASE)
            if m:
                logging.debug("TLS mail server detected")
                try:
                    mailconn.starttls()
                    mailconn.ehlo()
                except:
                    logging.info("Traceback: ", exc_info=True)
                    return errormsg(T("Failed to initiate TLS connection"))

        # Authentication
        if (email_account != "") and (email_pwd != ""):
            try:
                mailconn.login(email_account, email_pwd)
            except smtplib.SMTPHeloError:
                return errormsg(T("The server didn't reply properly to the helo greeting"))
            except smtplib.SMTPAuthenticationError:
                return errormsg(T("Failed to authenticate to mail server"))
            except smtplib.SMTPException:
                return errormsg(T("No suitable authentication method was found"))
            except:
                logging.info("Traceback: ", exc_info=True)
                return errormsg(T("Unknown authentication failure in mail server"))

        try:
            mailconn.sendmail(email_from, email_to, email_message)
            msg = None
        except smtplib.SMTPHeloError:
            msg = errormsg("The server didn't reply properly to the helo greeting.")
        except smtplib.SMTPRecipientsRefused:
            msg = errormsg("The server rejected ALL recipients (no mail was sent).")
        except smtplib.SMTPSenderRefused:
            msg = errormsg("The server didn't accept the from_addr.")
        except smtplib.SMTPDataError:
            msg = errormsg("The server replied with an unexpected error code (other than a refusal of a recipient).")
        except:
            logging.info("Traceback: ", exc_info=True)
            msg = errormsg(T("Failed to send e-mail"))

        try:
            mailconn.close()
        except:
            logging.info("Traceback: ", exc_info=True)
            errormsg(T("Failed to close mail connection"))

        if msg:
            return msg
        else:
            logging.info("Notification e-mail successfully sent")
            return T("Email succeeded")
    else:
        return T("Cannot send, missing required data")
示例#6
0
def send(message, email_to, test=None):
    """ Send message if message non-empty and email-parms are set """

    # we should not use CFG if we are testing. we should use values
    # from UI instead.

    if test:
        email_server  = test.get('email_server')
        email_from    = test.get('email_from')
        email_account = test.get('email_account')
        email_pwd     = test.get('email_pwd')
        if email_pwd and not email_pwd.replace('*', ''):
            # If all stars, get stored password instead
            email_pwd = cfg.email_pwd()
    else:
        email_server  = cfg.email_server()
        email_from    = cfg.email_from()
        email_account = cfg.email_account()
        email_pwd     = cfg.email_pwd()

    # email_to is replaced at send_with_template, since it can be an array

    if not message.strip('\n\r\t '):
        return "Skipped empty message"

    if email_server and email_to and email_from:

        message = _prepare_message(message)

        server, port = split_host(email_server)
        if not port:
            port = 25

        logging.debug("Connecting to server %s:%s", server, port)

        try:
            mailconn = ssmtplib.SMTP_SSL(server, port)
            mailconn.ehlo()

            logging.debug("Connected to server %s:%s", server, port)

        except Exception, errorcode:
            if errorcode[0]:

                # Non SSL mail server
                logging.debug("Non-SSL mail server detected " \
                             "reconnecting to server %s:%s", server, port)

                try:
                    mailconn = smtplib.SMTP(server, port)
                    mailconn.ehlo()
                except:
                    return errormsg(T('Failed to connect to mail server'))
            else:
                return errormsg(T('Failed to connect to mail server'))

        # TLS support
        if mailconn.ehlo_resp:
            m = re.search('STARTTLS', mailconn.ehlo_resp, re.IGNORECASE)
            if m:
                logging.debug("TLS mail server detected")

                try:
                    mailconn.starttls()
                    mailconn.ehlo()
                except:
                    return errormsg(T('Failed to initiate TLS connection'))

        # Authentication
        if (email_account != "") and (email_pwd != ""):
            try:
                mailconn.login(email_account, email_pwd)
            except smtplib.SMTPHeloError:
                return errormsg(T("The server didn't reply properly to the helo greeting"))
            except smtplib.SMTPAuthenticationError:
                return errormsg(T("Failed to authenticate to mail server"))
            except smtplib.SMTPException:
                return errormsg(T("No suitable authentication method was found"))
            except:
                return errormsg(T("Unknown authentication failure in mail server"))

        try:
            mailconn.sendmail(email_from, email_to, message)
            msg = None
        except smtplib.SMTPHeloError:
            msg = errormsg('The server didn\'t reply properly to the helo greeting.')
        except smtplib.SMTPRecipientsRefused:
            msg = errormsg('The server rejected ALL recipients (no mail was sent).')
        except smtplib.SMTPSenderRefused:
            msg = errormsg('The server didn\'t accept the from_addr.')
        except smtplib.SMTPDataError:
            msg = errormsg('The server replied with an unexpected error code (other than a refusal of a recipient).')
        except:
            msg = errormsg(T('Failed to send e-mail'))

        try:
            mailconn.close()
        except:
            errormsg(T('Failed to close mail connection'))

        if msg:
            return msg
        else:
            logging.info("Notification e-mail succesfully sent")
            return T('Email succeeded')
示例#7
0
def send(message, recipient):
    """ Send message if message non-empty and email-parms are set """

    if not message.strip('\n\r\t '):
        return "Skipped empty message"

    if cfg.email_server() and recipient and cfg.email_from():

        message = _prepare_message(message)

        server, port = split_host(cfg.email_server())
        if not port:
            port = 25

        logging.debug("Connecting to server %s:%s", server, port)

        try:
            mailconn = ssmtplib.SMTP_SSL(server, port)
            mailconn.ehlo()

            logging.debug("Connected to server %s:%s", server, port)

        except Exception, errorcode:
            if errorcode[0]:

                # Non SSL mail server
                logging.debug("Non-SSL mail server detected " \
                             "reconnecting to server %s:%s", server, port)

                try:
                    mailconn = smtplib.SMTP(server, port)
                    mailconn.ehlo()
                except:
                    return errormsg(T('Failed to connect to mail server'))
            else:
                return errormsg(T('Failed to connect to mail server'))

        # TLS support
        if mailconn.ehlo_resp:
            m = re.search('STARTTLS', mailconn.ehlo_resp, re.IGNORECASE)
            if m:
                logging.debug("TLS mail server detected")

                try:
                    mailconn.starttls()
                    mailconn.ehlo()
                except:
                    return errormsg(T('Failed to initiate TLS connection'))

        # Authentication
        if (cfg.email_account() != "") and (cfg.email_pwd() != ""):
            try:
                mailconn.login(cfg.email_account(), cfg.email_pwd())
            except:
                return errormsg(T('Failed to authenticate to mail server'))

        try:
            mailconn.sendmail(cfg.email_from(), recipient, message)
            msg = None
        except smtplib.SMTPHeloError:
            msg = errormsg('The server didn\'t reply properly to the helo greeting.')
        except smtplib.SMTPRecipientsRefused:
            msg = errormsg('The server rejected ALL recipients (no mail was sent).')
        except smtplib.SMTPSenderRefused:
            msg = errormsg('The server didn\'t accept the from_addr.')
        except smtplib.SMTPDataError:
            msg = errormsg('The server replied with an unexpected error code (other than a refusal of a recipient).')
        except:
            msg = errormsg(T('Failed to send e-mail'))

        try:
            mailconn.close()
        except:
            errormsg(T('Failed to close mail connection'))

        if msg:
            return msg
        else:
            logging.info("Notification e-mail succesfully sent")
            return T('Email succeeded')
示例#8
0
def send(message, recipient):
    """ Send message if message non-empty and email-parms are set """

    if not message.strip('\n\r\t '):
        return "Skipped empty message"

    if cfg.email_server() and recipient and cfg.email_from():

        message = _prepare_message(message)

        server, port = split_host(cfg.email_server())
        if not port:
            port = 25

        logging.debug("Connecting to server %s:%s", server, port)

        try:
            mailconn = ssmtplib.SMTP_SSL(server, port)
            mailconn.ehlo()

            logging.debug("Connected to server %s:%s", server, port)

        except Exception, errorcode:
            if errorcode[0]:

                # Non SSL mail server
                logging.debug("Non-SSL mail server detected " \
                             "reconnecting to server %s:%s", server, port)

                try:
                    mailconn = smtplib.SMTP(server, port)
                    mailconn.ehlo()
                except:
                    return errormsg(T('Failed to connect to mail server'))
            else:
                return errormsg(T('Failed to connect to mail server'))

        # TLS support
        if mailconn.ehlo_resp:
            m = re.search('STARTTLS', mailconn.ehlo_resp, re.IGNORECASE)
            if m:
                logging.debug("TLS mail server detected")

                try:
                    mailconn.starttls()
                    mailconn.ehlo()
                except:
                    return errormsg(T('Failed to initiate TLS connection'))

        # Authentication
        if (cfg.email_account() != "") and (cfg.email_pwd() != ""):
            try:
                mailconn.login(cfg.email_account(), cfg.email_pwd())
            except:
                return errormsg(T('Failed to authenticate to mail server'))

        try:
            mailconn.sendmail(cfg.email_from(), recipient, message)
            msg = None
        except smtplib.SMTPHeloError:
            msg = errormsg(
                'The server didn\'t reply properly to the helo greeting.')
        except smtplib.SMTPRecipientsRefused:
            msg = errormsg(
                'The server rejected ALL recipients (no mail was sent).')
        except smtplib.SMTPSenderRefused:
            msg = errormsg('The server didn\'t accept the from_addr.')
        except smtplib.SMTPDataError:
            msg = errormsg(
                'The server replied with an unexpected error code (other than a refusal of a recipient).'
            )
        except:
            msg = errormsg(T('Failed to send e-mail'))

        try:
            mailconn.close()
        except:
            errormsg(T('Failed to close mail connection'))

        if msg:
            return msg
        else:
            logging.info("Notification e-mail succesfully sent")
            return T('Email succeeded')
示例#9
0
def send(message, email_to, test=None):
    """ Send message if message non-empty and email-parms are set """

    def utf8(p):
        return p.encode('utf8', 'ignore')

    # we should not use CFG if we are testing. we should use values
    # from UI instead.

    if test:
        email_server = utf8(test.get('email_server'))
        email_from = utf8(test.get('email_from'))
        email_account = utf8(test.get('email_account'))
        email_pwd = utf8(test.get('email_pwd'))
        if email_pwd and not email_pwd.replace('*', ''):
            # If all stars, get stored password instead
            email_pwd = utf8(cfg.email_pwd())
    else:
        email_server = utf8(cfg.email_server())
        email_from = utf8(cfg.email_from())
        email_account = utf8(cfg.email_account())
        email_pwd = utf8(cfg.email_pwd())

    # email_to is replaced at send_with_template, since it can be an array

    if not message.strip('\n\r\t '):
        return "Skipped empty message"

    if email_server and email_to and email_from:

        message = _prepare_message(message)

        server, port = split_host(email_server)
        if not port:
            port = 25

        logging.debug("Connecting to server %s:%s", server, port)

        try:
            mailconn = smtplib.SMTP_SSL(server, port)
            mailconn.ehlo()

            logging.debug("Connected to server %s:%s", server, port)

        except Exception, errorcode:
            if errorcode[0]:

                # Non SSL mail server
                logging.debug("Non-SSL mail server detected "
                             "reconnecting to server %s:%s", server, port)

                try:
                    mailconn = smtplib.SMTP(server, port)
                    mailconn.ehlo()
                except:
                    return errormsg(T('Failed to connect to mail server'))
            else:
                return errormsg(T('Failed to connect to mail server'))

        # TLS support
        if mailconn.ehlo_resp:
            m = re.search('STARTTLS', mailconn.ehlo_resp, re.IGNORECASE)
            if m:
                logging.debug("TLS mail server detected")

                try:
                    mailconn.starttls()
                    mailconn.ehlo()
                except:
                    return errormsg(T('Failed to initiate TLS connection'))

        # Authentication
        if (email_account != "") and (email_pwd != ""):
            try:
                mailconn.login(email_account, email_pwd)
            except smtplib.SMTPHeloError:
                return errormsg(T("The server didn't reply properly to the helo greeting"))
            except smtplib.SMTPAuthenticationError:
                return errormsg(T("Failed to authenticate to mail server"))
            except smtplib.SMTPException:
                return errormsg(T("No suitable authentication method was found"))
            except:
                return errormsg(T("Unknown authentication failure in mail server"))

        try:
            mailconn.sendmail(email_from, email_to, message)
            msg = None
        except smtplib.SMTPHeloError:
            msg = errormsg('The server didn\'t reply properly to the helo greeting.')
        except smtplib.SMTPRecipientsRefused:
            msg = errormsg('The server rejected ALL recipients (no mail was sent).')
        except smtplib.SMTPSenderRefused:
            msg = errormsg('The server didn\'t accept the from_addr.')
        except smtplib.SMTPDataError:
            msg = errormsg('The server replied with an unexpected error code (other than a refusal of a recipient).')
        except:
            msg = errormsg(T('Failed to send e-mail'))

        try:
            mailconn.close()
        except:
            errormsg(T('Failed to close mail connection'))

        if msg:
            return msg
        else:
            logging.info("Notification e-mail successfully sent")
            return T('Email succeeded')