iredapd / libs / sql / modeler.py

# Author: Zhang Huangbin <zhb _at_ iredmail.org>

import logging
import settings
from libs import SMTP_ACTIONS, utils


class Modeler:
    def __init__(self):
        if settings.backend == 'mysql':
            import MySQLdb
            try:
                db = MySQLdb.connect(
                    host=settings.sql_server,
                    port=int(settings.sql_port),
                    db=settings.sql_db,
                    user=settings.sql_user,
                    passwd=settings.sql_password,
                )
                self.cursor = db.cursor()
            except Exception, e:
                logging.error("Error while creating database connection: %s" % str(e))
        elif settings.backend == 'pgsql':
            import psycopg2
            try:
                db = psycopg2.connect(
                    host=settings.sql_server,
                    port=int(settings.sql_port),
                    database=settings.sql_db,
                    user=settings.sql_user,
                    password=settings.sql_password,
                )
                self.cursor = db.cursor()
            except Exception, e:
                logging.error("Error while creating database connection: %s" % str(e))
        else:
            return SMTP_ACTIONS['default']

    def __del__(self):
        try:
            self.cursor.close()
            logging.debug('Closed SQL connection.')
        except Exception, e:
            logging.debug('Error while closing connection: %s' % str(e))

    def handle_data(self,
                    smtp_session_data,
                    plugins=[],
                    **kwargs
                   ):
        # No sender or recipient in smtp session.
        if not 'sender' in smtp_session_data or \
           not 'recipient' in smtp_session_data:
            return SMTP_ACTIONS['default']

        # Not a valid email address.
        if len(smtp_session_data['sender']) < 6:
            return 'DUNNO'

        # No plugins available.
        if not plugins:
            return 'DUNNO'

        plugin_kwargs = {'smtp_session_data': smtp_session_data,
                         'conn': self.cursor,
                         'sender': smtp_session_data['sender'],
                         'recipient': smtp_session_data['recipient'],
                         'sender_domain': smtp_session_data['sender'].split('@')[-1],
                         'recipient_domain': smtp_session_data['recipient'].split('@')[-1],
                        }

        # TODO Get SQL record of mail user or mail alias before applying plugins
        # TODO Query required sql columns instead of all

        for plugin in plugins:
            action = utils.apply_plugin(plugin, **plugin_kwargs)
            if not action.startswith('DUNNO'):
                return action

        return SMTP_ACTIONS['default']
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.