view src/plugins/plugin_misc_smtp.py @ 1963:a2bc5089c2eb

backend, frontends: message refactoring (huge commit): /!\ several features are temporarily disabled, like notifications in frontends next step in refactoring, with the following changes: - jp: updated jp message to follow changes in backend/bridge - jp: added --lang, --subject, --subject_lang, and --type options to jp message + fixed unicode handling for jid - quick_frontend (QuickApp, QuickChat): - follow backend changes - refactored chat, message are now handled in OrderedDict and uid are kept so they can be updated - Message and Occupant classes handle metadata, so frontend just have to display them - Primitivus (Chat): - follow backend/QuickFrontend changes - info & standard messages are handled in the same MessageWidget class - improved/simplified handling of messages, removed update() method - user joined/left messages are merged when next to each other - a separator is shown when message is received while widget is out of focus, so user can quickly see the new messages - affiliation/role are shown (in a basic way for now) in occupants panel - removed "/me" messages handling, as it will be done by a backend plugin - message language is displayed when available (only one language per message for now) - fixed :history and :search commands - core (constants): new constants for messages type, XML namespace, entity type - core: *Message methods renamed to follow new code sytle (e.g. sendMessageToBridge => messageSendToBridge) - core (messages handling): fixed handling of language - core (messages handling): mes_data['from'] and ['to'] are now jid.JID - core (core.xmpp): reorganised message methods, added getNick() method to client.roster - plugin text commands: fixed plugin and adapted to new messages behaviour. client is now used in arguments instead of profile - plugins: added information for cancellation reason in CancelError calls - plugin XEP-0045: various improvments, but this plugin still need work: - trigger is used to avoid message already handled by the plugin to be handled a second time - changed the way to handle history, the last message from DB is checked and we request only messages since this one, in seconds (thanks Poezio folks :)) - subject reception is waited before sending the roomJoined signal, this way we are sure that everything including history is ready - cmd_* method now follow the new convention with client instead of profile - roomUserJoined and roomUserLeft messages are removed, the events are now handled with info message with a "ROOM_USER_JOINED" info subtype - probably other forgotten stuffs :p
author Goffi <goffi@goffi.org>
date Mon, 20 Jun 2016 18:41:53 +0200
parents 633b5c21aefd
children 1d3f73e065e1
line wrap: on
line source

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

# SàT plugin for managing smtp server
# Copyright (C) 2011  Jérôme Poisson (goffi@goffi.org)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from sat.core.i18n import _
from sat.core.log import getLogger
log = getLogger(__name__)
from twisted.internet import defer
from twisted.cred import portal, checkers, credentials
from twisted.cred import error as cred_error
from twisted.mail import smtp
from twisted.python import failure
from email.parser import Parser
from email.utils import parseaddr
from twisted.mail.imap4 import LOGINCredentials, PLAINCredentials
from twisted.internet import reactor
import sys

from zope.interface import implements

PLUGIN_INFO = {
    "name": "SMTP server Plugin",
    "import_name": "SMTP",
    "type": "Misc",
    "protocols": [],
    "dependencies": ["Maildir"],
    "main": "SMTP_server",
    "handler": "no",
    "description": _("""Create a SMTP server that you can use to send your "normal" type messages""")
}


class SMTP_server(object):

    params = """
    <params>
    <general>
    <category name="Mail Server">
        <param name="SMTP Port" value="10125" type="int" constraint="1;65535" />
    </category>
    </general>
    </params>
    """

    def __init__(self, host):
        log.info(_("Plugin SMTP Server initialization"))
        self.host = host

        #parameters
        host.memory.updateParams(self.params)

        port = int(self.host.memory.getParamA("SMTP Port", "Mail Server"))
        log.info(_("Launching SMTP server on port %d") % port)

        self.server_factory = SmtpServerFactory(self.host)
        reactor.listenTCP(port, self.server_factory)


class SatSmtpMessage(object):
    implements(smtp.IMessage)

    def __init__(self, host, profile):
        self.host = host
        self.profile = profile
        self.message = []

    def lineReceived(self, line):
        """handle another line"""
        self.message.append(line)

    def eomReceived(self):
        """handle end of message"""
        mail = Parser().parsestr("\n".join(self.message))
        try:
            self.host._messageSend(parseaddr(mail['to'].decode('utf-8', 'replace'))[1], mail.get_payload().decode('utf-8', 'replace'),  # TODO: manage other charsets
                                  subject=mail['subject'].decode('utf-8', 'replace'), mess_type='normal', profile_key=self.profile)
        except:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            log.error(_(u"Can't send message: %s") % exc_value)  # The email is invalid or incorreclty parsed
            return defer.fail()
        self.message = None
        return defer.succeed(None)

    def connectionLost(self):
        """handle message truncated"""
        raise smtp.SMTPError


class SatSmtpDelivery(object):
    implements(smtp.IMessageDelivery)

    def __init__(self, host, profile):
        self.host = host
        self.profile = profile

    def receivedHeader(self, helo, origin, recipients):
        """
        Generate the Received header for a message
        @param helo: The argument to the HELO command and the client's IP
        address.
        @param origin: The address the message is from
        @param recipients: A list of the addresses for which this message
        is bound.
        @return: The full \"Received\" header string.
        """
        return "Received:"

    def validateTo(self, user):
        """
        Validate the address for which the message is destined.
        @param user: The address to validate.
        @return: A Deferred which becomes, or a callable which
        takes no arguments and returns an object implementing IMessage.
        This will be called and the returned object used to deliver the
        message when it arrives.
        """
        return lambda: SatSmtpMessage(self.host, self.profile)

    def validateFrom(self, helo, origin):
        """
        Validate the address from which the message originates.
        @param helo: The argument to the HELO command and the client's IP
        address.
        @param origin: The address the message is from
        @return: origin or a Deferred whose callback will be
        passed origin.
        """
        return origin


class SmtpRealm(object):
    implements(portal.IRealm)

    def __init__(self, host):
        self.host = host

    def requestAvatar(self, avatarID, mind, *interfaces):
        log.debug('requestAvatar')
        profile = avatarID.decode('utf-8')
        if smtp.IMessageDelivery not in interfaces:
            raise NotImplementedError
        return smtp.IMessageDelivery, SatSmtpDelivery(self.host, profile), lambda: None


class SatProfileCredentialChecker(object):
    """
    This credential checker check against SàT's profile and associated jabber's password
    Check if the profile exists, and if the password is OK
    Return the profile as avatarId
    """
    implements(checkers.ICredentialsChecker)
    credentialInterfaces = (credentials.IUsernamePassword,
                            credentials.IUsernameHashedPassword)

    def __init__(self, host):
        self.host = host

    def _cbPasswordMatch(self, matched, profile):
        if matched:
            return profile.encode('utf-8')
        else:
            return failure.Failure(cred_error.UnauthorizedLogin())

    def requestAvatarId(self, credentials):
        profiles = self.host.memory.getProfilesList()
        if not credentials.username in profiles:
            return defer.fail(cred_error.UnauthorizedLogin())
        d = self.host.memory.asyncGetParamA("Password", "Connection", profile_key=credentials.username)
        d.addCallback(credentials.checkPassword)
        d.addCallback(self._cbPasswordMatch, credentials.username)
        return d


class SmtpServerFactory(smtp.SMTPFactory):

    def __init__(self, host):
        self.protocol = smtp.ESMTP
        self.host = host
        _portal = portal.Portal(SmtpRealm(self.host))
        _portal.registerChecker(SatProfileCredentialChecker(self.host))
        smtp.SMTPFactory.__init__(self, _portal)

    def startedConnecting(self, connector):
        log.debug(_("SMTP server connection started"))
        smtp.SMTPFactory.startedConnecting(self, connector)

    def clientConnectionLost(self, connector, reason):
        log.debug(_(u"SMTP server connection lost (reason: %s)"), reason)
        smtp.SMTPFactory.clientConnectionLost(self, connector, reason)

    def buildProtocol(self, addr):
        p = smtp.SMTPFactory.buildProtocol(self, addr)
        # add the challengers from imap4, more secure and complicated challengers are available
        p.challengers = {"LOGIN": LOGINCredentials, "PLAIN": PLAINCredentials}
        return p