def __db_sanity(self, r): with eagn.DB() as db: key = 'mask-envelope-from' rq = db.querys( 'SELECT count(*) AS cnt ' 'FROM company_info_tbl ' 'WHERE company_id = 0 AND cname = :cname', {'cname': key}) if rq is None or not rq.cnt: count = db.execute( 'INSERT INTO company_info_tbl (' ' company_id, cname, cvalue, description, creation_date, timestamp' ') VALUES (' ' 0, :cname, :cvalue, NULL, current_timestamp, current_timestamp' ')', { 'cname': key, 'cvalue': 'false' }, commit=True) if count == 1: agn.log(agn.LV_INFO, 'sanity', 'Added configuration for envelope address') else: agn.log( agn.LV_ERROR, 'sanity', 'Failed to set configuration for envelope address: %s' % db.lastError())
def get_ready_to_run(self): (ready, stamps, finals) = self.scan_ready_to_run() if ready: for info in ready: info.stamp = stamps[info.basename] with eagn.DB() as db: invalids = set() for mailing in (agn.Stream(ready).map( lambda i: i.mailing).distinct().sorted()): rq = db.querys( 'SELECT deleted ' 'FROM mailing_tbl ' 'WHERE mailing_id = :mailingID', {'mailingID': mailing}) if rq is None: agn.log(agn.LV_INFO, 'ready', 'Mailing %d no more existing' % mailing) invalids.add(mailing) elif rq.deleted: agn.log(agn.LV_INFO, 'ready', 'Mailing %d is marked as deleted' % mailing) invalids.add(mailing) if invalids: for info in (agn.Stream(ready).filter( lambda i: i.mailing in invalids)): self.move(info.path, self.deleted) self.move(info.stamp.path, self.deleted) ready = (agn.Stream(ready).filter( lambda i: i.mailing not in invalids).list()) if ready: agn.log(agn.LV_INFO, 'ready', '%s files are ready to send' % agn.numfmt(len(ready))) return ready
def readConfiguration(self): config = None db = eagn.DB() try: if db.isopen(): config = (db.cursor.stream( 'SELECT name, value FROM config_tbl WHERE class = :class', { 'class': 'generate' }).filter(lambda nv: nv[0] is not None).dict()) else: raise agn.error('access to database: %s' % db.lastError()) except Exception as e: agn.log(agn.LV_ERROR, 'sched', 'Failed to read configuration: %s' % e) finally: db.done() if config is not None: self.config = config self.companies = self.configuration( 'companies', convert=lambda v: agn.Stream.ifelse(agn.Range(v))) if self.companies: agn.log( agn.LV_INFO, 'sched', 'Limit operations on these companies: %s' % self.companies)
def open (self): for state in 1, 2: if self.db is None: self.db = eagn.DB () if not self.db.isopen (): self.log (agn.LV_ERROR, 'Failed to open database: %s' % self.db.lastError ()) self.close () else: break return self.db is not None
def main(): exitCode = 0 db = eagn.DB() if db.isopen(): if not updateDatabase(db): exitCode = 1 db.commit(exitCode == 0) db.close() else: agn.log(agn.LV_ERROR, 'db', 'Failed to setup database') exitCode = 1 db.done() if exitCode: sys.exit(exitCode)
def triggerMessage (self, cinfo, parm, dryrun): try: mailingID = int (parm['armid']) companyID = int (parm['cid']) if cinfo is None: raise agn.error ('failed to determinate origin of mail') if not cinfo.valid: raise agn.error ('uid from foreign instance %d (expected from %d)' % (cinfo.licenceID, agn.licence)) customerID = cinfo.customerID rdirDomain = None with eagn.DB () as db: rq = db.cursor.querys ( 'SELECT mailinglist_id, company_id, deleted ' 'FROM mailing_tbl ' 'WHERE mailing_id = :mailingID', {'mailingID': mailingID} ) if rq is None: raise agn.error ('mailing %d not found' % mailingID) if rq.company_id != companyID: raise agn.error ('mailing %d belongs to company %d, but mailloop belongs to company %d' % (mailingID, rq[1], companyID)) if rq.deleted: agn.log (agn.LV_INFO, 'ar', 'mailing %d is marked as deleted' % mailingID) mailinglistID = rq.mailinglist_id # rq = db.cursor.querys ( 'SELECT user_type ' 'FROM customer_%d_binding_tbl ' 'WHERE customer_id = :customerID AND mailinglist_id = :mailinglistID AND mediatype = 0' % companyID, { 'customerID': customerID, 'mailinglistID': mailinglistID } ) if rq is None or rq.user_type not in ('A', 'T', 't'): if not self.allow (parm, dryrun): raise agn.error ('recipient is not allowed to received (again) an autoresponder') else: agn.log (agn.LV_INFO, 'ar', 'recipient %d on %d for %d is admin/test recipient and not blocked' % (customerID, mailinglistID, mailingID)) # rq = db.cursor.querys ( 'SELECT rdir_domain ' 'FROM mailinglist_tbl ' 'WHERE mailinglist_id = :mailinglistID', {'mailinglistID': mailinglistID} ) if rq is not None and rq.rdir_domain is not None: rdirDomain = rq.rdir_domain else: rq = db.cursor.querys ( 'SELECT rdir_domain ' 'FROM company_tbl ' 'WHERE company_id = :companyID', {'companyID': companyID} ) if rq is not None: rdirDomain = rq.rdir_domain if rdirDomain is None: raise agn.error ('failed to determinate rdir-domain') # rq = db.cursor.querys ( 'SELECT security_token ' 'FROM mailloop_tbl ' 'WHERE rid = :rid', {'rid': int (self.aid)} ) if rq is None: raise agn.error ('no entry in mailloop_tbl for %s found' % self.aid) securityToken = rq.security_token if rq.security_token else '' url = ('%s/sendMailloopAutoresponder.do?' 'mailloopID=%s&companyID=%d&customerID=%d&securityToken=%s' % (rdirDomain, self.aid, companyID, customerID, urllib.quote (securityToken))) if dryrun: print ('Would trigger mail using %s' % url) else: try: uh = urllib2.urlopen (url) response = uh.read () uh.close () agn.log (agn.LV_INFO, 'ar', 'Autoresponder mailing %d for customer %d triggered: %s' % (mailingID, customerID, response)) except (urllib2.URLError, urllib2.HTTPError), e: agn.log (agn.LV_ERROR, 'ar', 'Failed to trigger %s: %s' % (url, str (e))) except agn.error, e: agn.log (agn.LV_INFO, 'ar', 'Failed to send autoresponder: %s' % str (e))