Пример #1
0
    def readMailertable(self):
        self.domains = []
        self.mtdom = {}

        if self.sendmailFree:
            self.domains = [self.fixdomain]
            me = socket.getfqdn()
            if me:
                self.domains.append(me)
            db = agn.DBase()
            if not db is None:
                c = db.cursor()
                if not c is None:
                    for r in c.query(
                            'SELECT mailloop_domain FROM company_tbl'):
                        if r[0] and not r[0] in self.domains:
                            self.domains.append(r[0])
                    c.close()
                db.close()
            return
        try:
            for line in fileReader(mailBase + '/mailertable'):
                parts = line.split()
                if len(parts) > 0 and parts[0][0] != '.':
                    self.domains.append(parts[0])
                    self.mtdom[parts[0]] = 0
        except IOError, e:
            agn.log(agn.LV_ERROR, 'data',
                    'Unable to read mailertable %s' % ` e.args `)
Пример #2
0
 def scanForData(self):
     self.data = []
     files = [_f for _f in os.listdir(self.incoming) if _f[:7] == 'AgnMail']
     if len(files) > 0:
         data = []
         finals = []
         db = agn.DBase()
         if db is None:
             agn.log(agn.LV_ERROR, 'scan',
                     'Unable to get database instance')
             return 0
         inst = db.cursor()
         if inst is None:
             db.close()
             agn.log(agn.LV_ERROR, 'scan', 'Unable to get database cursor')
             return 0
         deleted = {}
         for fname in files:
             block = Block(self.incoming + os.sep + fname)
             if not deleted.has_key(block.mailingID):
                 r = inst.querys(
                     'SELECT deleted FROM mailing_tbl WHERE mailing_id = %d'
                     % block.mailingID)
                 if not r is None:
                     deleted[block.mailingID] = r[0]
             if deleted.has_key(block.mailingID):
                 if deleted[block.mailingID] != 0:
                     block.moveTo(agn.mkArchiveDirectory(self.deleted))
                 else:
                     if block.isdata and block.stamp in files:
                         data.append(block)
                     elif block.isfinal:
                         finals.append(block)
         inst.close()
         db.close()
         now = time.localtime()
         ts = 'D%04d%02d%02d%02d%02d%02d' % now[:6]
         for block in data:
             for final in finals:
                 if final.isFinalFor(block):
                     final.finalcount += 1
                     if block.readyToSend(ts):
                         self.data.append(block)
                     break
         for final in finals:
             if final.finalcount == 0:
                 final.moveTo(agn.mkArchiveDirectory(self.archive))
         self.data.sort()
     return len(self.data)
Пример #3
0
 def __scanMID(self, scan, mid, where):
     if mid and not scan.minfo:
         mt = Rule.NMidRE.search(mid)
         if mt:
             grps = mt.groups()
             try:
                 uid = agn.UID()
                 uid.parseUID(grps[0])
                 uid.password = None
                 if self.passwords.has_key(uid.companyID):
                     uid.password = self.passwords[uid.companyID]
                 else:
                     db = agn.DBase()
                     if db:
                         inst = db.cursor()
                         if inst:
                             for r in inst.query(
                                     'SELECT xor_key FROM company_tbl WHERE company_id = %d'
                                     % uid.companyID):
                                 if not r[0] is None:
                                     if type(r[0]) in types.StringTypes:
                                         uid.password = r[0]
                                     else:
                                         uid.password = str(r[0])
                                     self.passwords[
                                         uid.companyID] = uid.password
                         else:
                             agn.log(agn.LV_ERROR, 'mid',
                                     'Unable to get databse cursor')
                         db.close()
                     else:
                         agn.log(agn.LV_ERROR, 'mid',
                                 'Unable to create database')
             except agn.error, e:
                 agn.log(agn.LV_ERROR, 'mid', 'Failed: ' + e.msg)
             if not uid.password is None:
                 if uid.validateUID():
                     scan.minfo = (uid.mailingID, uid.customerID)
                     agn.log(
                         agn.LV_INFO, 'mid',
                         'Found new message id in %s: %s' % (where, mid))
                 else:
                     agn.log(
                         agn.LV_WARNING, 'mid',
                         'Found invalid new message id in %s: %s' %
                         (where, mid))
Пример #4
0
 def isInBlacklist(self, company_id):
     accept = True
     db = agn.DBase()
     if db:
         i = db.cursor()
         if i:
             for state in [0, 1]:
                 table = None
                 if state == 0:
                     table = 'cust_ban_tbl'
                 if not table:
                     continue
                 for r in i.queryc('SELECT email FROM %s' % table):
                     if not r[0] is None:
                         eMail = r[0]
                         if '_' in eMail[0] or '%' in eMail[0]:
                             pattern = eMail[0].replace('%', '*').replace(
                                 '_', '?')
                             if fnmatch.fnmatch(self.sender, pattern):
                                 accept = False
                         elif eMail[0] == self.sender:
                             accept = False
                         if not accept:
                             agn.log(
                                 agn.LV_INFO, 'blist',
                                 'Autoresponder disabled due to blacklist entry "%s" on %s'
                                 % (eMail[0], table))
                             break
                     if not accept:
                         break
                 else:
                     agn.log(agn.LV_WARNING, 'blist',
                             'Unable to read data from ' + table)
             i.close()
         else:
             agn.log(agn.LV_ERROR, 'blist',
                     'Unable to get cursor for blacklisting')
         db.close()
     else:
         agn.log(agn.LV_ERROR, 'blist',
                 'Unable to setup database interface for blacklisting')
     return not accept
Пример #5
0
    def readDatabase(self, auto):
        rc = ''
        db = agn.DBase()
        if not db:
            agn.log(agn.LV_ERROR, 'data',
                    'Unable to create database connection')
            raise agn.error('readDatabase.open')
        try:
            i = db.cursor()
            if not i:
                agn.log(agn.LV_ERROR, 'data', 'Unable to get database cursor')
                raise agn.error('readDatabase.cursor')
            try:
                ctab = {}
                query = 'SELECT company_id, mailloop_domain FROM company_tbl WHERE status = \'active\''
                missing = []
                for record in i.query(query):
                    if record[1]:
                        ctab[record[0]] = record[1]
                    else:
                        missing.append(record[0])
                if missing:
                    missing.sort()
                    agn.log(
                        agn.LV_VERBOSE, 'data',
                        'Missing mailloop_domain for %s' %
                        ', '.join([str(m) for m in missing]))

                query = 'SELECT rid, shortname, company_id, forward_enable, forward, ar_enable, ar_sender, ar_subject, ar_text, ar_html, subscribe_enable, mailinglist_id, form_id, date_format(change_date,\'%Y%m%d%H%i%S\') FROM mailloop_tbl'
                for record in i.query(query):
                    subscribe_enable = None
                    mailinglist_id = None
                    form_id = None

                    (rid, shortname, company_id, forward_enable, forward,
                     ar_enable, ar_sender, ar_subject, ar_text, ar_html,
                     subscribe_enable, mailinglist_id, form_id,
                     timestamp) = record
                    if not rid is None:
                        rid = str(rid)
                        domains = None

                        if timestamp is None:
                            timestamp = time.time()
                        else:
                            timestamp = int(timestamp)
                        if ar_enable and not ar_text:
                            ar_enable = False
                        if ar_enable:

                            def nvl(s):
                                if s is not None:
                                    return str(s)
                                return s

                            auto.append(
                                Autoresponder(rid, timestamp, ar_sender,
                                              ar_subject, nvl(ar_text),
                                              nvl(ar_html)))
                        if domains is None:
                            try:
                                cdomain = ctab[company_id]
                                if self.domains and cdomain != self.domains[0]:
                                    domains = [self.domains[0]]
                                else:
                                    domains = []

                                if agn.iswin and not cdomain in self.domains:
                                    self.domains.append(cdomain)
                                if not self.domains or cdomain in self.domains:
                                    domains.append(cdomain)

                                else:

                                    agn.log(agn.LV_WARNING, 'data',
                                            'Domain "%s" not known' % cdomain)
                            except KeyError:
                                agn.log(
                                    agn.LV_DEBUG, 'data',
                                    'No domain for company found, further processing'
                                )
                        if domains is None:
                            domains = self.domains
                        elif not self.domains[0] in domains:
                            domains.insert(0, self.domains[0])
                        extra = 'rid=%s' % rid
                        if company_id:
                            extra += ',cid=%d' % company_id
                        if forward_enable and forward:
                            extra += ',fwd=%s' % forward
                        if ar_enable:
                            extra += ',ar=%s' % rid
                        if subscribe_enable and mailinglist_id and form_id:
                            extra += ',sub=%d:%d' % (mailinglist_id, form_id)
                        for domain in domains:
                            line = '%s%s@%s\taccept:%s' % (self.prefix, rid,
                                                           domain, extra)
                            agn.log(agn.LV_VERBOSE, 'data',
                                    'Add line: ' + line)
                            rc += line + '\n'
            finally:
                i.close()
        finally:
            db.close()
        return rc
Пример #6
0
			error ('Unable to find %s.sql (or %s-%s.sql)' % (prefix, prefix,curversion2))
		show ('setup:\n')
		show ('Setup database, please enter the super user password defined during MySQL installation:\n')
		if os.system ('mysqladmin -u root -p create openemm'):
			error ('Failed to create database')
		show ('Database created, now setting up initial data, please enter again your databae super user password:\n')
		if os.system ('mysql -u root -p -e "source %s" openemm' % findSQL ('openemm')):
			error ('Failed to setup database')
		show ('Setup CMS database, please enter the super user password defined during MySQL installation:\n')
		if os.system ('mysqladmin -u root -p create openemm_cms'):
			error ('Failed to create CMS database')
		show ('CMS Database created, now setting up initial data, please enter again your databae super user password:\n')
		if os.system ('mysql -u root -p -e "source %s" openemm_cms' % findSQL ('openemm_cms')):
			error ('Failed to setup CMS database')
		show ('Database setup completed.\n')
		db = agn.DBase ()
		if not db is None:
			cursor = db.cursor ()
			if not cursor is None:
				cursor.execute ('CREATE TABLE %s (version varchar(50))' % versionTable)
				cursor.execute ('INSERT INTO %s VALUES (:version)' % versionTable, {'version': curversion})
				cursor.sync ()
				cursor.close ()
			db.close ()
	if sys.argv[1] in ('setup', 'config'):
		db = agn.DBase ()
		if not db:
			error ('Failed to setup database connection')
		i = db.cursor ()
		if not i:
			error ('Failed to connect to database')
Пример #7
0
    def iniciaServico(self):
        try:
            homedrive = os.environ['HOMEDRIVE']
        except KeyError:
            homedrive = 'C:'
        self.home = homedrive + os.path.sep + 'OpenEMM'
        if not os.path.isdir(self.home):
            guess = None
            for disk in 'CDEFGHIJKLMNOPQRSTUVWXYZ':
                temp = disk + ':' + os.path.sep + 'OpenEMM'
                if os.path.isdir(temp):
                    guess = temp
                    break
            if guess is None:
                self.error(listaErros["semPasta"][1] % self.home,
                           listaErros["semPasta"][0])
            self.home = guess
        self.show('Pasta do OpenEMM: %s.' % self.home)
        checkprop(self, self.home)
        checkpaths(self, self.home)
        checkserverxml(self.home)

        os.environ['HOME'] = self.home
        binhome = self.home + os.path.sep + 'bin'
        addpath(binhome)
        schome = binhome + os.path.sep + 'scripts'
        os.environ['PYTHONPATH'] = schome
        if not schome in sys.path:
            sys.path.append(schome)
        os.environ['LC_ALL'] = 'C'
        os.environ['LANG'] = 'en_US.ISO8859_1'
        os.environ['NLS_LANG'] = 'american_america.UTF8'

        try:
            import agn
            agn.require('2.0.0')
        except:
            self.error(listaErros["semCodAgn"][1], listaErros["semCodAgn"][0])

        #
        # Check for working database
        if not 'DBase' in dir(agn):
            self.error(listaErros["semBD"][1], listaErros["semBD"][0])
        #
        # add python to path
        addpath(agn.pythonpath)
        #
        # find jdk
        jdkkey = r'SOFTWARE\JavaSoft\Java Development Kit'
        version = agn.winregFind(jdkkey, 'CurrentVersion')
        if version is None:
            self.error(listaErros["semJDK"][1], listaErros["semJDK"][0])
        javahome = agn.winregFind(jdkkey + '\\' + version, 'JavaHome')
        addpath(javahome + os.path.sep + 'bin')
        os.environ['JAVA_HOME'] = javahome
        os.environ[
            'JAVA_OPTIONS'] = '-Xms256m -Xmx512m -XX:MaxPermSize=256m -Xss256k'

        #
        # find tomcat
        if self.tomcathome is None:
            for fname in sorted([
                    _f for _f in os.listdir(homedrive + os.path.sep)
                    if _f.startswith('apache-tomcat-')
            ]):
                self.tomcathome = os.path.sep.join([homedrive, fname])
            if self.tomcathome is None:
                self.error(listaErros["semTomcat"][1],
                           listaErros["semTomcat"][0])
        addpath(self.tomcathome + os.path.sep + 'bin')
        os.environ['CATALINA_HOME'] = self.tomcathome
        os.environ['CATALINA_BASE'] = self.home

        #
        # find mysql
        mysqlhome = None
        for version in ['5.0', '5.1']:
            mskey = r'SOFTWARE\MySQL AB\MySQL Server %s' % version
            mysqlhome = agn.winregFind(mskey, 'Location')
            if not mysqlhome is None:
                break
        if mysqlhome is None:
            bkey = r'SOFTWARE\MySQL AB'
            try:
                for mskey in sorted([
                        _r for _r in agn.winregList(bkey)
                        if type(_r) in types.StringTypes
                ]):
                    if 'server' in mskey.lower():
                        mysqlhome = agn.winregFind('%s\\%s' % (bkey, mskey),
                                                   'Location')
            except TypeError:
                self.warning(
                    'Servidor MySQL nao encontrado usando reg.key %s. Isto so e um problema, se voce NAO utiliza banco de dados remoto.'
                    % bkey)
        if not mysqlhome is None:
            addpath(mysqlhome + os.path.sep + 'bin')

        #
        #
        db = agn.DBase()
        if not db:
            self.error(listaErros["erroConfBD"][1],
                       listaErros["erroConfBD"][0])
        i = db.cursor()
        if not i:
            self.error(listaErros["erroConexaoBD"][1],
                       listaErros["erroConexaoBD"][0])
        i.close()
        db.close()
        #
        # remove potential stale files
        sessions = os.path.sep.join(
            [self.home, 'webapps', 'openemm', 'WEB-INF', 'sessions'])
        fnames = [agn.winstopfile]
        if os.path.isdir(sessions):
            for fname in os.listdir(sessions):
                fnames.append(sessions + os.path.sep + fname)
        for fname in fnames:
            try:
                os.unlink(fname)
        #		show ('Removed stale file %s.\n' % fname)
            except (WindowsError, OSError):
                pass
        #
        # change to home directory
        os.chdir(self.home)

        #
        # Iniciando
        p_upd = pystart(schome + os.path.sep + 'update.py', 'account bounce')
        if p_upd == -1:
            self.error(listaErros["erroIniUpdate"][1],
                       listaErros["erroIniUpdate"][0])
        p_dst = pystart(schome + os.path.sep + 'pickdist.py')
        if p_dst == -1:
            self.error(listaErros["erroIniPickdist"][1],
                       listaErros["erroIniPickdist"][0])
        p_bav = pystart(schome + os.path.sep + 'bav-update.py')
        if p_bav == -1:
            self.error(listaErros["erroIniBavupdate"][1],
                       listaErros["erroIniBavupdate"][0])
        p_sem = pystart(schome + os.path.sep + 'semu.py')
        if p_sem == -1:
            self.error(listaErros["erroIniSemu"][1],
                       listaErros["erroIniSemu"][0])
        p_con = tomcatstart(self, 'openemm')
        if p_con == -1:
            self.error(listaErros["erroIniTomcat"][1],
                       listaErros["erroIniTomcat"][0])

        return agn.winstopfile
Пример #8
0
    def subscribe(self, address, fullname, company_id, mailinglist_id,
                  formular_id):
        db = agn.DBase()
        if not db is None:
            curs = db.cursor()
            if not curs is None:
                agn.log(
                    agn.LV_REPORT, 'sub',
                    'Try to subscribe %s (%s) for %d to %d using %d' %
                    (address, fullname, company_id, mailinglist_id,
                     formular_id))
                customer_id = None
                newBinding = True
                sendMail = True
                userRemark = 'Subscribe via mailloop #%s' % self.rid
                custids = []
                for rec in curs.query(
                        'SELECT customer_id FROM customer_%d_tbl WHERE email = :email'
                        % company_id, {'email': address}):
                    custids.append(rec[0])
                if custids:
                    agn.log(
                        agn.LV_REPORT, 'sub',
                        'Found these customer_ids %s for the email %s' %
                        ( ` custids `, address))
                    query = 'SELECT customer_id, user_status FROM customer_%d_binding_tbl WHERE customer_id ' % company_id
                    if len(custids) > 1:
                        query += 'IN ('
                        sep = ''
                        for custid in custids:
                            query += '%s%d' % (sep, custid)
                            sep = ', '
                        query += ')'
                    else:
                        query += '= %d' % custids[0]
                    query += ' AND mailinglist_id = %d AND mediatype = 0' % mailinglist_id
                    use = None
                    for rec in curs.query(query):
                        agn.log(agn.LV_REPORT, 'sub',
                                'Found binding [cid, status] %s' % ` rec `)
                        if rec[1] == agn.UserStatus.ACTIVE:
                            if use is None or use[
                                    1] != agn.UserStatus.ACTIVE or rec[
                                        0] > use[0]:
                                use = rec
                        elif use is None or (use[1] != agn.UserStatus.ACTIVE
                                             and rec[0] > use[0]):
                            use = rec
                    if not use is None:
                        agn.log(
                            agn.LV_REPORT, 'sub',
                            'Use customer_id %d with user_status %d' %
                            (use[0], use[1]))
                        customer_id = use[0]
                        newBinding = False
                        if use[1] in (agn.UserStatus.ACTIVE,
                                      agn.UserStatus.WAITCONFIRM):
                            agn.log(
                                agn.LV_REPORT, 'sub',
                                'User status is %d, stop processing here' %
                                use[1])
                            sendMail = False
                        else:
                            agn.log(agn.LV_REPORT, 'sub',
                                    'Set user status to 5')

                            query = 'UPDATE customer_%d_binding_tbl SET change_date = current_timestamp, user_status = %d, user_remark = :remark WHERE customer_id = %d AND mailinglist_id = %d AND mediatype = 0' % (
                                company_id, agn.UserStatus.WAITCONFIRM,
                                customer_id, mailinglist_id)
                            curs.update(query, {'remark': userRemark},
                                        commit=True)
                    else:
                        customer_id = max(custids)
                        agn.log(
                            agn.LV_REPORT, 'sub',
                            'No matching binding found, use cutomer_id %d' %
                            customer_id)
                else:
                    datasrcdesc = 'Mailloop #%s' % self.rid
                    dsid = agn.Datasource()
                    datasrcid = dsid.getID(datasrcdesc, company_id, 4)

                    query = 'INSERT INTO customer_%d_tbl (email, gender, mailtype, change_date, creation_date, datasource_id) ' % company_id + \
                     'VALUES (:email, 2, 1, current_timestamp, current_timestamp, %d)' % datasrcid
                    data = {'email': address}
                    curs.update(query, data, commit=True)
                    for rec in curs.query(
                            'SELECT customer_id FROM customer_%d_tbl WHERE email = :email'
                            % company_id, data):
                        customer_id = rec[0]
                if not customer_id is None:
                    if newBinding:

                        query = 'INSERT INTO customer_%d_binding_tbl (customer_id, mailinglist_id, user_type, user_status, user_remark, change_date, creation_date, mediatype) VALUES (%d, %d, \'W\', %d, :remark, current_timestamp, current_timestamp, 0)' % (
                            company_id, customer_id, mailinglist_id,
                            agn.UserStatus.WAITCONFIRM)
                        agn.log(agn.LV_REPORT, 'sub',
                                'Create new binding using "%s"' % query)
                        curs.update(query, {'remark': userRemark}, commit=True)
                    if sendMail:
                        formname = None
                        rdir = None
                        password = None
                        for rec in curs.query(
                                'SELECT formname FROM userform_tbl WHERE form_id = %d AND company_id = %d'
                                % (formular_id, company_id)):
                            if rec[0]:
                                formname = rec[0]
                        for rec in curs.query(
                                'SELECT rdir_domain, xor_key FROM company_tbl WHERE company_id = %d'
                                % company_id):
                            if rdir is None:
                                rdir = rec[0]
                            password = rec[1]
                        if not formname is None and not rdir is None:
                            uid = agn.UID()
                            uid.companyID = company_id
                            uid.customerID = customer_id
                            if password is None:
                                uid.password = ''
                            else:
                                uid.password = str(password)
                            url = '%s/form.do?agnCI=%d&agnFN=%s&agnUID=%s' % (
                                rdir, company_id, urllib.quote(formname),
                                uid.createUID())
                            agn.log(agn.LV_REPORT, 'sub',
                                    'Trigger mail using "%s"' % url)
                            try:
                                uh = urllib2.urlopen(url)
                                resp = uh.read()
                                uh.close()
                                agn.log(
                                    agn.LV_REPORT, 'sub',
                                    'Subscription request returns "%s"' % resp)
                                if len(resp) < 2 or resp[:2].lower() != 'ok':
                                    agn.log(
                                        agn.LV_ERROR, 'sub',
                                        'Subscribe formular "%s" returns error "%s"'
                                        % (url, resp))
                            except urllib2.HTTPError, e:
                                agn.log(
                                    agn.LV_ERROR, 'sub',
                                    'Failed to trigger [http] forumlar using "%s": %s'
                                    % (url, str(e)))
                            except urllib2.URLError, e:
                                agn.log(
                                    agn.LV_ERROR, 'sub',
                                    'Failed to trigger [prot] forumlar using "%s": %s'
                                    % (url, ` e.reason `))
                        else:
                            if not formname:
                                agn.log(
                                    agn.LV_ERROR, 'sub',
                                    'No formular with id #%d found' %
                                    formular_id)
                            if not rdir:
                                agn.log(
                                    agn.LV_ERROR, 'sub',
                                    'No rdir domain for company #%d/mailinglist #%d found'
                                    % (company_id, mailinglist_id))
                curs.close()
                agn.log(agn.LV_REPORT, 'sub', 'Subscribe finished')