fcn-report 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/python3
  2. # Style Guide: https://www.python.org/dev/peps/pep-0008/
  3. import argparse
  4. import configparser
  5. import pprint
  6. import psycopg2
  7. # fcntoolbox package
  8. import fcntoolbox.dolibarr as dolibarr
  9. import fcntoolbox.mail as mail
  10. import fcntoolbox.sympa as sympa
  11. # network imports
  12. import smtplib
  13. from email.mime.text import MIMEText
  14. parser = argparse.ArgumentParser()
  15. parser.add_argument("-c", "--config", type=str,
  16. default="/etc/fcntoolbox/config.ini",
  17. help="specify a configuration file")
  18. args = parser.parse_args()
  19. config = configparser.RawConfigParser()
  20. config.sections()
  21. config.read(args.config)
  22. confdoli = config['dolibarr']
  23. confsympa = config['sympa']
  24. conn = psycopg2.connect(host=confdoli['host'], database=confdoli['database'],
  25. user=confdoli['user'], password=confdoli['password'])
  26. doli = dolibarr.Instance(conn)
  27. fields = ["rowid", "firstname", "lastname", "email"]
  28. fieldsextra = ['email_second']
  29. # Filter out missing extra fields
  30. #cur.execute("SELECT * FROM llx_adherent_extrafields LIMIT 1")
  31. #columns_extra = map(lambda col : col.name, cur.description)
  32. #rows_extra = filter(lambda r : r in columns_extra, fieldsextra)
  33. cur = doli.get_adherent(fields, fieldsextra)
  34. aliases = mail.Aliases('/etc/aliases', 'franciliens.net')
  35. raliases = dict((val, key) for key, val in aliases.items())
  36. adherents = set() # set(email)
  37. adherents_alt = dict() # dict(email => set(email))
  38. radherents_alt = dict() # dict(email => email)
  39. for record in cur:
  40. columns = map(lambda col : col.name, cur.description)
  41. adh = dict(zip(columns, record))
  42. adh_email = adh['email'].lower()
  43. adherents.add(adh_email)
  44. adh_alt = set()
  45. if 'email_second' in adh and adh['email_second'] != None:
  46. adh_alt.add(adh['email_second'])
  47. radherents_alt[adh['email_second']] = adh_email
  48. if adh_email in aliases:
  49. adh_alt.add(aliases[adh_email])
  50. radherents_alt[aliases[adh_email]] = adh_email
  51. if adh_email in raliases:
  52. adh_alt.add(raliases[adh_email])
  53. radherents_alt[raliases[adh_email]] = adh_email
  54. adherents_alt[adh_email] = adh_alt
  55. sympaconn = psycopg2.connect(host=confsympa['host'], database=confsympa['database'],
  56. user=confsympa['user'], password=confsympa['password'])
  57. sympaIns = sympa.Instance(sympaconn)
  58. list = 'adherents'
  59. francisubs = set(sympaIns.get_subscribers(list, confsympa['robot']))
  60. list2 = 'admin'
  61. adminsubs = set(sympaIns.get_subscribers(list2, confsympa['robot']))
  62. tosubscribe = set()
  63. for adh in adherents:
  64. adh_alt = adherents_alt[adh]
  65. if adh in francisubs or len(adh_alt.intersection(francisubs)) > 0:
  66. pass
  67. else:
  68. tosubscribe.add(adh)
  69. tounsubscribe = set()
  70. for sub in francisubs:
  71. if sub in adherents \
  72. or (sub in radherents_alt and radherents_alt[sub] in adherents):
  73. pass
  74. else:
  75. tounsubscribe.add(sub)
  76. tounsubscribe2 = set()
  77. for sub in adminsubs:
  78. if sub in adherents \
  79. or (sub in radherents_alt and radherents_alt[sub] in adherents):
  80. pass
  81. else:
  82. tounsubscribe2.add(sub)
  83. reportFormat = open('/etc/fcntoolbox/fcn-report.format', 'r').read()
  84. s = smtplib.SMTP('localhost')
  85. pp = pprint.PrettyPrinter()
  86. def sendReport(listname, tosubscribe, tounsubscribe):
  87. body = reportFormat.format(
  88. list = listname,
  89. tosubscribe = pp.pformat(tosubscribe),
  90. tounsubscribe = pp.pformat(tounsubscribe))
  91. msg = MIMEText(body)
  92. msg['Subject'] = "Rapport de la liste [%s]" % listname
  93. msg['From'] = confsympa['mail_from']
  94. if 'mail_cc' in confsympa:
  95. msg['Cc'] = confsympa['mail_cc']
  96. msg['To'] = "%s-request@%s" % (listname, confsympa['robot'])
  97. s.send_message(msg)
  98. if len(adherents) > 0 and (len(tosubscribe) > 0 or len(tounsubscribe) > 0) :
  99. sendReport(list, tosubscribe, tounsubscribe)
  100. if len(adherents) > 0 and len(tounsubscribe2) > 0 :
  101. sendReport(list2, {}, tounsubscribe2)
  102. s.quit()