124 lines
3.9 KiB
Python
124 lines
3.9 KiB
Python
#!/usr/bin/env python2
|
|
# - * - encoding: utf-8 - * -
|
|
"""sensitivefilter.py
|
|
|
|
Simple tool to synchronize Warsaw Hackerspace internal mailman mailing list
|
|
(WAW-S) with LDAP group members.
|
|
"""
|
|
|
|
__author__ = "Sergiusz 'q3k' Bazanski <q3k@hackerspace.pl>"
|
|
|
|
|
|
import getpass
|
|
import json
|
|
import subprocess
|
|
from email.mime.text import MIMEText
|
|
from distutils.util import strtobool
|
|
|
|
import ldap
|
|
#import requests
|
|
import urllib.request, urllib.error, urllib.parse
|
|
|
|
SPECIAL = {
|
|
# People who prefer non-@hackerspace.pl emails
|
|
#b'seb': b"s@informa.pl", # bounce 2020/12/25 inf/q3k
|
|
b'enki': b"enki@fsck.pl",
|
|
b'pixel': b"kkocel20@gmail.com",
|
|
b'ar': b'arachnist@i.am-a.cat',
|
|
}
|
|
|
|
LIST_NAME = 'waw-sensitive@lists.hackerspace.pl'
|
|
DRY_RUN = False
|
|
|
|
def ldap_connect():
|
|
c = ldap.initialize('ldap://ldap.hackerspace.pl')
|
|
c.start_tls_s()
|
|
#c.simple_bind_s('uid=q3k,ou=People,dc=hackerspace,dc=pl', getpass.getpass('LDAP password for q3k: '))
|
|
return c
|
|
|
|
def get_target_subscriptions(c):
|
|
data = c.search_s('ou=People,dc=hackerspace,dc=pl', ldap.SCOPE_SUBTREE,
|
|
'(&(objectClass=hsMember)(|(memberOf=cn=fatty,ou=Group,dc=hackerspace,dc=pl)(memberOf=cn=starving,ou=Group,dc=hackerspace,dc=pl)(memberOf=cn=potato,ou=Group,dc=hackerspace,dc=pl)))', ('uid',))
|
|
emails = []
|
|
for dn, obj in data:
|
|
uid = obj['uid'][0]
|
|
#okay = is_paying(uid) or is_potato(uid)
|
|
okay = True
|
|
if not okay:
|
|
print("Skipping {}, because he's not a member.".format(uid))
|
|
else:
|
|
if uid in SPECIAL:
|
|
emails.append(SPECIAL[uid])
|
|
else:
|
|
emails.append((uid + b'@hackerspace.pl').lower())
|
|
return set(emails)
|
|
|
|
|
|
def get_current_subscriptions():
|
|
out = subprocess.check_output(['mailman', 'members', '-e', LIST_NAME])
|
|
current = [m.strip().lower() for m in out.split(b'\n') if b'@' in m]
|
|
return set(current)
|
|
|
|
def add_users(users):
|
|
if DRY_RUN:
|
|
return
|
|
p = subprocess.Popen(('mailman', 'addmembers', '-W', '-', LIST_NAME),
|
|
stdin=subprocess.PIPE)
|
|
p.communicate(b'\n'.join(users))
|
|
if p.returncode:
|
|
raise Exception('Users addition failed')
|
|
|
|
def remove_users(users):
|
|
if DRY_RUN:
|
|
return
|
|
p = subprocess.Popen(('mailman', 'delmembers', '-G', '-N', '-f', '-', '-l', LIST_NAME),
|
|
stdin=subprocess.PIPE)
|
|
p.communicate(b'\n'.join(users))
|
|
if p.returncode:
|
|
raise Exception('Users removal failed')
|
|
|
|
def notify(add, remove, address):
|
|
if len(add) == 0 and len(remove) == 0:
|
|
return
|
|
message = "Cześć! \n\n"
|
|
if add:
|
|
message += "Na listę zostały zasubskrybowane następujące adresy email:\n"
|
|
message += '\n'.join(' - {}'.format(m.decode('utf-8')) for m in add)
|
|
message += '\n'
|
|
if remove:
|
|
message += "Z listy zostały usunięte następujące adresy email:\n"
|
|
message += '\n'.join(' - {}'.format(m.decode('utf-8')) for m in remove)
|
|
message += '\n'
|
|
message += '\n\n--\nPozdro 600,\nAutomat do Subskrybowania Adresów po Listach'
|
|
|
|
if DRY_RUN:
|
|
print('Dry run - Notifying:', message)
|
|
return
|
|
|
|
msg = MIMEText(message, "plain", "utf-8")
|
|
msg['From'] = 'HS BOFH <bofh@hackerspace.pl>'
|
|
msg['To'] = address
|
|
msg['Subject'] = 'Zmiany na liście WAW-Sensitive'
|
|
p = subprocess.Popen(["/usr/sbin/sendmail", "-t"], stdin=subprocess.PIPE)
|
|
p.communicate(msg.as_bytes())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
c = ldap_connect()
|
|
target = get_target_subscriptions(c)
|
|
target.add(b'bofh@hackerspace.pl')
|
|
current = get_current_subscriptions()
|
|
to_add = target - current
|
|
to_remove = current - target
|
|
|
|
if to_remove:
|
|
print("Removing", to_remove)
|
|
remove_users(to_remove)
|
|
|
|
if to_add:
|
|
print("Adding", to_add)
|
|
add_users(to_add)
|
|
|
|
if to_add or to_remove:
|
|
notify(to_add, to_remove, 'waw-sensitive@lists.hackerspace.pl')
|