-
Notifications
You must be signed in to change notification settings - Fork 0
/
dude.py
executable file
·224 lines (190 loc) · 9.32 KB
/
dude.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# possible input mail adresses:
# ${internal} => [email protected]
# ${[email protected]} => [email protected]
# ${[email protected] Some Name} => [email protected]
# ${Some Name [email protected]} => [email protected]
import argparse
import datetime
import subprocess
import sys
import ldap
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import getpass
# define common mail lists
list_users = [["fachschaft", "Liebe Fachschaft"]]
list_users.append(["flachschaft", "Liebe Fachschaft"])
list_users.append(["bernd", "Liebe Fachschaft"])
list_users.append(["fsinformatik", "Liebe Fachschaft"])
list_users.append(["fsphysik", "Liebe Fachschaft"])
list_users.append(["fsmathematik", "Liebe Fachschaft"])
list_users.append(["fsmathinf", "Liebe Fachschaft"])
list_users.append(["infostudkom", "Liebes Mitglied der Studienkommission Informatik"])
list_users.append(["tistudkom", "Liebes Mitglied der Studkom TI"])
list_users.append(["mathstudkom", "Liebe MathStudKomLerInnen"])
list_users.append(["mathestudkom", "Liebe MathStudKomLerInnen"])
list_users.append(["physstudkom", "Liebe Mitglied der Studkom Physik"])
list_users.append(["physikstudkom", "Liebe Mitglied der Studkom Physik"])
list_users.append(["studkomphysik", "Liebe Mitglied der Studkom Physik"])
list_users.append(["scstudkom", "Liebe Mitglied der Studkom SciCom"])
list_users.append(["mathfakrat", "Liebes Mitglied des MatheInfo-Fakrats"])
list_users.append(["fakratmathinf", "Liebes Mitglied des MatheInfo-Fakrats"])
list_users.append(["physfakrat", "Liebes Mitglied des Physik-Fakrats"])
list_users.append(["fakratphys", "Liebes Mitglied des Physik-Fakrats"])
list_users.append(["fakratphysik", "Liebes Mitglied des Physik-Fakrats"])
list_users.append(["akfest", "Liebes Mitglied der AK-Fest Liste"])
def check_path(path: str) -> bool:
"""checks the input file name for a valid date and type .txt"""
year = path[0:4].isnumeric()
month = path[5:7].isnumeric()
date = path[8:9].isnumeric()
name = year and month and date and path[4] is '-' and path[7] is '-'
if path.endswith('.txt'):
return True
else:
raise Exception('Der Dateipfad führt nicht zu einem Sitzungsprotokoll oder du schaust besser nochmal über den Filenamen!')
return False
class Protocol(object):
"""reads in the protocol and processes it"""
def __init__(self, path):
# validate filename as protocol (yyyy-mm-dd) and .txt
self.path = path
print('\nProtokoll "{}" wird bearbeitet .. \n \n'.format(self.path))
with open(self.path, 'r') as file:
self.protocol = file.read().splitlines()
self.tops = []
self.mails = False
def get_tops(self):
"""separate the given protocol in several TOPs from '===' to '==='"""
for i, line in enumerate(self.protocol):
# check for TOP title
if line.startswith("===") and self.protocol[i+2].startswith("==="):
end = self.protocol[i+3:].index("===")+i+2 if (line in self.protocol[i+3:]) else len(self.protocol)-1
top = TOP(len(self.tops)+1, i, end)
self.tops.append(top)
def rename_title(self):
"""Adjust TOP title type setting"""
for top in self.tops:
if not self.protocol[top.start+1].startswith("TOP: "): self.protocol[top.start+1] = "TOP " + str(top.number) + ": " + self.protocol[top.start+1]
else: self.protocol[top.start+1] = self.protocol[top.start+1][:3] + str(top.number) + " " + self.protocol[top.start+1][3:]
length = len(self.protocol[top.start+1])
self.protocol[top.start+2] = "="*length
self.protocol[top.start] = "="*length
def get_users(self):
for top in self.tops:
top.get_user(self.protocol)
top.get_mails()
def send_mails(self):
try:
server = smtplib.SMTP("mail.urz.uni-heidelberg.de", 587)
login = input('Uni ID für den Mailversand: ')
server.login(login, getpass.getpass(prompt='Passwort für deinen Uni Account: '))
for top in self.tops:
top.send_mail(server, self.protocol)
server.quit()
self.mails = True
print("\nAlle Mails wurden erfolgreich verschickt. \n")
except Exception as e:
print(e)
print("\nMails konnten nicht verschickt werden. Hast du die richtigen Anmeldedaten eingegeben?")
pass
def write_success(self):
if self.mails:
now = datetime.datetime.now()
self.protocol.insert(0, ":Protocoldude: Mails versandt @ {}".format(now.strftime("%H:%M %d.%m.%Y")))
self.protocol.insert(1, "\n")
with open(self.path, 'w') as file:
file.write('\n'.join(self.protocol) + '\n')
try:
subprocess.run(['svn', 'up'], check=True)
subprocess.run(['svn', 'add', '{}'.format(self.path)], check=True)
subprocess.run(['svn', 'commit', '-m', '"Protokoll der gemeinsamen Sitzung hinzugefügt"'], check=True)
print("Protokoll bearbeitet und in den Sumpf geschrieben.\n Für heute hast du's geschafft!")
except:
print("Konnte SVN Update nicht durchführen. \n Das musst Du irgendwie von Hand reparieren mit 'svn cleanup' oder so.")
print("Das Protokoll wurde trotzdem bearbeitet und gespeichert.")
pass
class TOP(Protocol):
"""Separates the several TOPs out of one protocol and provides different functions to further process the sections"""
def __init__(self, number: int, start: int, end: int):
self.number = number
self.start = start
self.end = end
self.users = []
self.mails = []
def get_user(self, protocol: list):
"""searches for all mentioned users in the TOP paragraph"""
# TODO: recognize multiple users in one line
for line in protocol[self.start:self.end]:
# check for mail address
if "${" in line and "}" in line:
start = line.index("${")
end = line.index("}")
user = line[start+2:end]
self.users.append(user)
self.users = list(set(self.users)) # remove duplicates
def get_mails(self):
if extract_mails(ldap_search(self.users)) is not None:
self.mails = extract_mails(ldap_search(self.users))
for user in self.users:
if user in list_users[:][0]:
self.mails.append(user + "@mathphys.stura.uni-heidelberg.de")
def send_mail(self, server, protocol):
for user,mail in zip(self.users,self.mails):
fromaddr = "[email protected]"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = mail
msg['Subject'] = "Gemeinsame Sitzung: {}".format(protocol[self.start+1])
if user in list_users[:][0]:
body = list_users[list_users[:][0].index(user)][1] + ",\n\n"
else:
body = "Hallo {},\n\n".format(user)
body += "Du sollst über irgendwas informiert werden. Im Sitzungsprotokoll steht dazu folgendes:\n\n{}\n\n\nViele Grüße, Dein SPAM-Skript.".format('\n'.join(protocol[self.start:self.end])+'\n')
# \n\nSollte der Text abgeschnitten sein, schaue bitte im Sitzungsprotokoll nach (Zeile #{tops[i]} – MathPhys Login notwendig).\n#{url}/#{file}\" | mail -a \"Reply-To: #{$replyto}\" -a \"Content-Type: text/plain; charset=UTF-8\" -s \"#{$subject}: #{title} (#{date})\" '#{mail}';", false) unless $debug
msg.attach(MIMEText(body, 'plain'))
text = msg.as_string()
server.sendmail(fromaddr, mail, text)
def ldap_search(users: list) -> list:
""" searches for a list of users in our ldap """
server = ldap.initialize('ldaps://' + MATHPHYS_LDAP_ADDRESS)
users = ['(uid={})'.format(user) for user in users]
query = '(|{})'.format("".join(users))
query_result = server.search_s(
MATHPHYS_LDAP_BASE_DN,
ldap.SCOPE_SUBTREE,
query
)
return query_result
def extract_mails(query: list) -> list:
""" extract mails from nonempty ldap queries """
if query:
mails = []
for result in query:
dn = result[0]
attributes = result[1]
mails.append(attributes["mail"][0].decode('utf-8'))
return mails
if __name__ == "__main__":
MATHPHYS_LDAP_ADDRESS = "ldap1.mathphys.stura.uni-heidelberg.de"
MATHPHYS_LDAP_BASE_DN = "ou=People,dc=mathphys,dc=stura,dc=uni-heidelberg,dc=de"
# disables error messages
sys.tracebacklimit = 0
# check system 'Fachschaftsserver'
# output = subprocess.check_output(["uname", "-n"]).decode("utf-8")
#
# if ('arcadia' not in output or 'blueberry' not in output):
# raise Exception("Im Moment funktioniert das Skript nur auf dem Fachschaftsserver. Versuch es da nochmal.")
parser = argparse.ArgumentParser()
parser.add_argument("infile", metavar="[path/to/file]", type=argparse.FileType('r'))
args = parser.parse_args()
check_path(path=args.infile.name)
protocol = Protocol(path=args.infile.name)
protocol.get_tops()
protocol.get_users()
protocol.rename_title()
protocol.send_mails()
protocol.write_success()