2014-08-06 09:43:40 +00:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
import redis
|
|
|
|
import ConfigParser
|
|
|
|
import pprint
|
|
|
|
import time
|
2014-08-11 07:27:50 +00:00
|
|
|
import dns.exception
|
2014-08-06 09:43:40 +00:00
|
|
|
from packages import Paste as P
|
|
|
|
from packages import lib_refine
|
|
|
|
from packages import ZMQ_PubSub
|
|
|
|
from pubsublogger import publisher
|
|
|
|
|
|
|
|
configfile = './packages/config.cfg'
|
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
|
2014-08-06 09:43:40 +00:00
|
|
|
def main():
|
|
|
|
"""Main Function"""
|
|
|
|
|
|
|
|
# CONFIG #
|
|
|
|
cfg = ConfigParser.ConfigParser()
|
|
|
|
cfg.read(configfile)
|
|
|
|
|
|
|
|
# REDIS #
|
|
|
|
r_serv = redis.StrictRedis(
|
2014-08-14 15:55:18 +00:00
|
|
|
host=cfg.get("Redis_Queues", "host"),
|
|
|
|
port=cfg.getint("Redis_Queues", "port"),
|
|
|
|
db=cfg.getint("Redis_Queues", "db"))
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
r_serv1 = redis.StrictRedis(
|
2014-08-14 15:55:18 +00:00
|
|
|
host=cfg.get("Redis_Data_Merging", "host"),
|
|
|
|
port=cfg.getint("Redis_Data_Merging", "port"),
|
|
|
|
db=cfg.getint("Redis_Data_Merging", "db"))
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
r_serv2 = redis.StrictRedis(
|
2014-08-14 15:55:18 +00:00
|
|
|
host=cfg.get("Redis_Cache", "host"),
|
|
|
|
port=cfg.getint("Redis_Cache", "port"),
|
|
|
|
db=cfg.getint("Redis_Cache", "db"))
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
# LOGGING #
|
|
|
|
publisher.channel = "Script"
|
|
|
|
|
|
|
|
# ZMQ #
|
2014-08-14 15:55:18 +00:00
|
|
|
sub = ZMQ_PubSub.ZMQSub(configfile, "PubSub_Categ", "mails_categ", "emails")
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
# FUNCTIONS #
|
|
|
|
publisher.info("Suscribed to channel mails_categ")
|
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
message = sub.get_msg_from_queue(r_serv)
|
2014-08-06 09:43:40 +00:00
|
|
|
prec_filename = None
|
|
|
|
|
|
|
|
email_regex = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}"
|
|
|
|
|
|
|
|
while True:
|
2014-08-11 07:18:55 +00:00
|
|
|
try:
|
2014-08-14 15:55:18 +00:00
|
|
|
if message is not None:
|
|
|
|
channel, filename, word, score = message.split()
|
2014-08-11 07:18:55 +00:00
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
if prec_filename is None or filename != prec_filename:
|
2014-08-11 07:18:55 +00:00
|
|
|
PST = P.Paste(filename)
|
|
|
|
MX_values = lib_refine.checking_MX_record(r_serv2, PST.get_regex(email_regex))
|
|
|
|
|
|
|
|
if MX_values[0] >= 1:
|
|
|
|
|
|
|
|
PST.__setattr__(channel, MX_values)
|
|
|
|
PST.save_attribute_redis(r_serv1, channel, (MX_values[0], list(MX_values[1])))
|
|
|
|
|
|
|
|
pprint.pprint(MX_values)
|
2014-08-14 15:55:18 +00:00
|
|
|
to_print = 'Mails;{};{};{};Checked {} e-mail(s)'.format(PST.p_source, PST.p_date, PST.p_name, MX_values[0])
|
2014-08-11 07:18:55 +00:00
|
|
|
if MX_values[0] > 10:
|
2014-08-14 15:55:18 +00:00
|
|
|
publisher.warning(to_print)
|
2014-08-11 07:18:55 +00:00
|
|
|
else:
|
2014-08-14 15:55:18 +00:00
|
|
|
publisher.info(to_print)
|
2014-08-11 07:18:55 +00:00
|
|
|
prec_filename = filename
|
|
|
|
|
|
|
|
else:
|
|
|
|
if r_serv.sismember("SHUTDOWN_FLAGS", "Mails"):
|
|
|
|
r_serv.srem("SHUTDOWN_FLAGS", "Mails")
|
|
|
|
print "Shutdown Flag Up: Terminating"
|
|
|
|
publisher.warning("Shutdown Flag Up: Terminating.")
|
|
|
|
break
|
|
|
|
publisher.debug("Script Mails is Idling 10s")
|
|
|
|
time.sleep(10)
|
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
message = sub.get_msg_from_queue(r_serv)
|
2014-08-11 07:18:55 +00:00
|
|
|
except dns.exception.Timeout:
|
|
|
|
print "dns.exception.Timeout"
|
|
|
|
pass
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|