142 lines
3.9 KiB
Python
142 lines
3.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
|
|
import random
|
|
import smtplib
|
|
|
|
DEBUG = False
|
|
|
|
def sendmail(sender, receiver, msg):
|
|
smtp = None
|
|
try:
|
|
smtp = smtplib.SMTP("localhost")
|
|
smtp.sendmail("nobody@hottis.de", (receiver), msg)
|
|
except smtplib.SMTPException as e:
|
|
print("Fehler beim Senden der Mail an {0}, {1}, {2}".format(msg, receiver, e))
|
|
raise Exception("Nochmal von vorne")
|
|
finally:
|
|
if smtp:
|
|
smtp.close()
|
|
|
|
|
|
|
|
class Wichtelee(object):
|
|
def __init__(self, name, email, forbiddenNames):
|
|
self.name = name
|
|
self.email = email
|
|
self.forbiddenNames = forbiddenNames
|
|
self.receiverName = None
|
|
self.giverName = False
|
|
|
|
def check(self, receiverName):
|
|
return (receiverName != self.name) and (receiverName not in self.forbiddenNames)
|
|
|
|
def storeOther(self, receiver):
|
|
self.receiverName = receiver.name
|
|
receiver.giverName = self.name
|
|
|
|
def isReceiver(self):
|
|
return self.giverName
|
|
|
|
def isGiver(self):
|
|
return self.receiverName
|
|
|
|
def inform(self):
|
|
infoLine = "{0} -> {1}".format(self.name, self.receiverName)
|
|
global notarMsg
|
|
notarMsg = notarMsg + infoLine + "\n"
|
|
if DEBUG:
|
|
print(infoLine)
|
|
|
|
msg = """From: Der Geist der Weihnacht <nobody@hottis.de>
|
|
To: {0} <{1}>
|
|
Subject: Wichteln
|
|
|
|
Du, {0}, darfst ein Geschenk fuer {2} besorgen.
|
|
|
|
Der Geist der Weihnacht
|
|
""".format(self.name, self.email, self.receiverName)
|
|
|
|
if DEBUG:
|
|
receiver = "woho@hottis.de"
|
|
else:
|
|
receiver = self.email
|
|
|
|
sendmail("nobody@hottis.de", receiver, msg)
|
|
|
|
|
|
wichtelees = [
|
|
Wichtelee("Wolfgang", "wolfgang.hottgenroth@icloud.com", ("Patty", "Matthias", "Anna")),
|
|
Wichtelee("Patty", "patricia.hottgenroth@icloud.com", ("Wolfgang", "Matthias", "Anna")),
|
|
Wichtelee("Matthias", "matthias.hottgenroth@icloud.com", ("Wolfgang", "Patty", "Anna", "Robert")),
|
|
Wichtelee("Anna", "anna.hottgenroth@icloud.com", ("Wolfgang", "Patty", "Matthias", "Lucia")),
|
|
Wichtelee("Thomas", "thomas@nober.de", ("Sandra")),
|
|
Wichtelee("Sandra", "sandra-nober@web.de", ("Thomas")),
|
|
Wichtelee("Philipp", "philipp@nicolai.eu", ("Barbara")),
|
|
Wichtelee("Barbara", "barbara@nicolai.eu", ("Philipp")),
|
|
Wichtelee("Robert", "robert@nober.de", ("Matthias")),
|
|
Wichtelee("Raphael", "raphael@nober.de", ("Nina")),
|
|
Wichtelee("Nina", "nina.massolle@gmx.de", ("Raphael")),
|
|
Wichtelee("Lucia", "luci@erbeck.net", ("Viktor", "Anna")),
|
|
Wichtelee("Viktor", "vik@erbeck.net", ("Lucia")),
|
|
Wichtelee("Gregor", "gregor@nober.de", ())
|
|
]
|
|
|
|
|
|
byName = { x.name : x for x in wichtelees }
|
|
|
|
|
|
|
|
for first in wichtelees:
|
|
if DEBUG:
|
|
print("Handling {}".format(first.name))
|
|
if first.isGiver():
|
|
if DEBUG:
|
|
print("Is already giver, skip over")
|
|
continue
|
|
checked = []
|
|
while True:
|
|
n = random.randint(0, len(wichtelees)-1)
|
|
second = wichtelees[n]
|
|
if DEBUG:
|
|
print("First is {0}, Second is {1}".format(first.name, second.name))
|
|
if second.name not in checked:
|
|
checked.append(second.name)
|
|
if len(checked) == len(wichtelees):
|
|
raise Exception("no complete match possible, try again")
|
|
|
|
if second.isReceiver():
|
|
if DEBUG:
|
|
print(" Second receives already from {0}".format(second.giverName))
|
|
continue
|
|
|
|
if first.check(second.name):
|
|
if DEBUG:
|
|
print(" Match")
|
|
first.storeOther(second)
|
|
break
|
|
else:
|
|
if DEBUG:
|
|
print(" Forbidden")
|
|
|
|
|
|
|
|
notarMsg = """Subject: Wichtelergebnis
|
|
To: Papa <heribert-ludger@nober.de>
|
|
From: Wolfgang <woho@hottis.de>
|
|
|
|
Bitte gut aufheben und keinem zeigen!
|
|
|
|
"""
|
|
|
|
for first in wichtelees:
|
|
first.inform()
|
|
|
|
|
|
if DEBUG:
|
|
notar = "woho@hottis.de"
|
|
else:
|
|
notar = "heribert-ludger@nober.de"
|
|
|
|
sendmail("woho@hottis.de", notar, notarMsg)
|