Add misc/sensitivefilter.py

master
informatic 2018-03-14 20:18:55 +01:00
parent 924cd87ed6
commit 4a6ba7a009
2 changed files with 137 additions and 1 deletions

11
README
View File

@ -1,8 +1,17 @@
Bash scripts for Hackerspace.pl LDAP administration.
====================================================
To activate, type bin/activate.
To get help, type help.
The effects of issued commands end up in a buffer file, by default ~/.ldap-admin.ldif . To make them persistent, issue the commit command. If successful, it will clear the buffer. Otherwise the buffer will remain unchanged.
The effects of issued commands end up in a buffer file, by default
~/.ldap-admin.ldif . To make them persistent, issue the commit command. If
successful, it will clear the buffer. Otherwise the buffer will remain
unchanged.
Misc
----
misc/ directory contains random tools used for internal management (eg. mailing
lists sync) that don't really need a separate repository.

127
misc/sensitivefilter.py Normal file
View File

@ -0,0 +1,127 @@
#!/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 urllib2
SPECIAL = {
# People who prefer non-@hackerspace.pl emails
'seb': "s@informa.pl",
'enki': "enki@fsck.pl",
'pixel': "kkocel20@gmail.com",
'ar': 'arachnist@i.am-a.cat',
}
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('{}@hackerspace.pl'.format(uid).lower())
return set(emails)
def get_current_subscriptions():
p = subprocess.Popen(('/var/lib/mailman/bin/list_members', 'waw-sensitive'),
stdout=subprocess.PIPE)
out, err = p.communicate()
current = [m.strip() for m in out.split('\n') if '@' in m]
return set(current)
def add_users(users):
p = subprocess.Popen(('/var/lib/mailman/bin/add_members', '-r', '-', '-w', 'y', 'waw-sensitive'),
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.communicate('\n'.join(users))
def remove_users(users):
p = subprocess.Popen(('/var/lib/mailman/bin/remove_members', '-f', '-', 'waw-sensitive'),
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.communicate('\n'.join(users))
def notify(add, remove, address):
if len(add) == 0 and len(remove) == 0:
return
message = u"Cześć! \n\n"
if add:
message += u"Na listę zostały zasubskrybowane następujące adresy email:\n"
message += '\n'.join(' - {}'.format(m) for m in add)
message += '\n'
if remove:
message += u"Z listy zostały usunięte następujące adresy email:\n"
message += '\n'.join(' - {}'.format(m) for m in remove)
message += '\n'
message += u'\n\n--\nPozdro 600,\nAutomat do Subskrybowania Adresów po Listach'
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_string())
if __name__ == '__main__':
c = ldap_connect()
target = get_target_subscriptions(c)
target.add('bofh@hackerspace.pl')
current = get_current_subscriptions()
to_add = target - current
to_remove = current - target
print "Will add", to_add
if not strtobool(raw_input("Proceed? yes/no").lower()):
to_add = False
print "Will remove", to_remove
if not strtobool(raw_input("Proceed? yes/no").lower()):
to_remove = False
if to_add:
add_users(to_add)
print "Added", to_add
else:
print "Not adding any addresses"
if to_remove:
remove_users(to_remove)
print "Removed", to_remove
else:
print "Not removing any addresses"
if to_add or to_remove:
notify(to_add, to_remove, 'waw-sensitive@lists.hackerspace.pl')