-
Notifications
You must be signed in to change notification settings - Fork 0
/
resend.py
134 lines (100 loc) · 4.14 KB
/
resend.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
#!/usr/bin/python3
import os.path
import drawsanta
import sys
#a function that checks commandline arguments and displays help
def checkarg(arg):
version = "Secret Santa Resend 1.0"
helptxt = """Usage: resend [OPTION]... [SANTA]...
Resends Secret Santa e-mail to one or all santas.
-a. --all Resends mail to all stanas in last drawing
-v, --version Outputs version information and exits
--help Displays this help text
Example:
resend john Resends email to john based on entry in logfile
resend --all Resends email to all santas in last drawing"""
usage="""Usage: resend [Option]... [SANTA]...
Try 'resend --help' for more information."""
if len(arg)==2:
if str.startswith(str(arg[1]), "--") == True or str.startswith(str(arg[1]), "-") == True:
if str(arg[1]) =="--help":
print(helptxt)
return False
elif str(arg[1]) =="-v" or str(arg[1]) =="--version":
print(version)
return False
elif str(arg[1]) =="-a" or str(arg[1]) =="--all":
return "-a"
else:
print(usage)
return False
return True
else:
print(usage)
return False
def readlog(logfile):
if os.path.isfile(logfile):
with open(logfile, 'r' ) as f:
content = f.readlines()
return content
def main():
argument = checkarg(sys.argv)
if argument == True or argument == "-a":
configfile = "secretsanta.conf"
if drawsanta.checkConfig(configfile):
#fetching data from config file
config = drawsanta.configparser.ConfigParser()
config.read(configfile)
logfile = config["logging"]["logfile"]
mailserver = config["mailserver"]["server"]
promptForAuth = config["mailserver"]["promptForAuth"]
requireAuth = config["mailserver"]["requireAuth"]
logfile = readlog(logfile)
#getting date of last drawing for comparison
lastdrawing = logfile[-1].split(" ")
lastdrawing = lastdrawing[0]
username =""
password =""
#Requesting username and password for mailserver
if promptForAuth == "yes":
print("Your need to authenticate with " + mailserver)
username = input("Username: ")
password = drawsanta.getpass.getpass("Password: ")
if requireAuth == "yes" and promptForAuth == "no":
username = config["mailserver"]["username"]
password = config["mailserver"]["password"]
print("Sending mail...")
i=0
for line in reversed(logfile):
line = line.split(" ")
santaName = line[3]
santaEmail = line[4].strip("<")
santaEmail = santaEmail.strip(">")
child = line[6].strip()
if line[3].lower() == sys.argv[1].lower():
print(santaName + " <"+santaEmail + "> --> " + "**********")
if drawsanta.sendmail(santaName, santaEmail, child, username, password):
print("Done!")
#exits the loop as match was found
break
if sys.argv[1] == "--all":
logDate = line[0]
if logDate == lastdrawing:
i+=1
print(santaName + " <"+santaEmail + "> --> " + "**********")
if drawsanta.sendmail(santaName, santaEmail, child, username, password):
failed = False
else:
failed = True
break
else:
if not failed:
print("Done!")
print(str(i) + " mails sent")
break
# datetime = line[:19]
# print (datetime)
# print(line[santa:30])
#calling main function
if __name__ == "__main__":
main()