forked from TheRealBanana/Mailmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailmon2.py
190 lines (141 loc) · 5.78 KB
/
mailmon2.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
#!/bin/env python2.7
# Mailmon v2.0 by Kyle Claisse
# This script reads in an email in text form and executes certain functions based on the content of the message.
#
# Current requires a working MTA and the mail command on the box this bot is running (I use Heirloom Mail v12.4)
# Probably should rewrite to use the smtplib python library but I never needed to. Would be trivial to rewrite to
# allow using third-party SMTP servers like gmail. Something for version 2.1 I guess.
import email
import re
from time import strftime
import os
from subprocess import check_output as sp_co
from difflib import SequenceMatcher as sMatcher
#######################
#### OPTIONS START ####
#######################
#This is the email address the autoresponder bot will use for the From: address in its return email.
FROM_ADDRESS = "[email protected]"
#This is the path where the mail.txt is being output to. See readme for details.
path = "/home/mailmon/data"
#Approved email addresses. Only approved addresses can use this bot. Just add your approved emails to this list like in the example.
#If you want to allow anyone to use this bot, just add ALLOW_ALL.
authorized_emails = []
authorized_emails.append("[email protected]")
authorized_emails.append("[email protected]")
#This is the list of commands. If you don't add your command to this list it won't appear in any help messages.
cmdlist = []
cmdlist.append("uptime")
cmdlist.append("whos-logged-in")
#When an unknown command is received it will try to match the unknown command against the command list.
#This controls the sensitivity of the matcher, a higher number is more restrictive matching.
#0.75 is a good value to keep it at.
_MASTER_RATIO_ = 0.75
#These you don't need to change (unless you know what you are doing)
emf = path + os.sep + "mail.txt"
outfile = path + os.sep + "out.txt"
#####################
#### OPTIONS END ####
#####################
def check_address(addy):
for x in authorized_emails:
if x == "ALLOW_ALL":
return True
if x == addy:
return True
return False
def main():
return_message = "UNKNOWN ERROR, RETURN EMAIL NOT SET"
email_file = open(emf, 'r')
msgobj = email.message_from_file(email_file)
email_file.close()
if msgobj.is_multipart() is False:
message = msgobj.get_payload()
return_addy = msgobj.__getitem__("from")
#We lower the received message to make matching easier
nmsg = message.lower().strip()
#FOR DEBUGGING!
pq = open(path + os.sep + "mailmon_use.log", 'a')
log_message = str(strftime("%A %B %d, %Y - %r %Z")) + " :: " + return_addy + " :: " + nmsg
pq.write(log_message)
pq.write("\n")
pq.close()
if check_address(return_addy) is not True:
return_message = "You are not authorized to use this service. Your previous and future messages will be logged."
#############################
#### COMMANDS START HERE ####
#############################
elif "hi" in nmsg or "hello" in nmsg or "hey" in nmsg:
return_message = "Hello there! The current time here is: " + str(strftime("%A %B %d, %Y - %r %Z"))
elif nmsg == "uptime":
uptime = sp_co('''uptime | egrep "([0-9][0-9]:?){3}.up.[0-9]{1,4}.days?" -o''', shell=True)
return_message = "Current system uptime is: " + uptime.strip()
elif nmsg == "whos-logged-in":
try:
last_raw = sp_co("last -w -F | grep logged", shell=True).strip()
except:
last_raw = ""
last_list = re.split("\n", last_raw)
return_message = "There are currently %s users logged in%s"
num = 0
for entry in last_list:
entry = entry.strip()
line_match = re.match("^(.*?)\s+(.*?)\s+(.*?)\s+.*$", entry)
if line_match is not None:
num += 1
return_message += "\n%s from %s" % (line_match.group(1), line_match.group(3))
if num == 0:
return_message = return_message % (str(num), "")
else:
return_message = return_message % (str(num), ":")
#############################################
#### ADD MORE COMMANDS BELOW USING ELIFS ####
#############################################
elif "help" in nmsg:
cmdstr = "%s" % (cmdlist[3],)
for cmd in cmdlist[4:]:
cmdstr += ", " + cmd
return_message = "List of commands: %s" % cmdstr
else:
return_message = "Unknown command, %s." % str(nmsg)
#Try and match the failed command to a known command
for cmd in cmdlist:
if sMatcher(None, nmsg.lower(), cmd).ratio() > _MASTER_RATIO_:
return_message += " Did you mean %s? " % cmd
break
return_message += " Reply with 'help' for a list of commands."
send_email(return_message, return_addy)
def send_email(message, to_address):
if len(message) < 1:
return
#Remove file first
try:
os.remove(outfile)
except:
pass
ofile = open(outfile, 'w')
ofile.write(message)
ofile.close()
#Check if we have a good return email
if "ERROR" not in to_address:
#Send email out
cmd = "mail -r %s %s < %s" % (FROM_ADDRESS, to_address, outfile)
os.system(cmd)
else:
#Write the error to the log
pq = open(path + os.sep + "mailmon_use.log", 'a')
log_message = str(strftime("%A %B %d, %Y - %r %Z")) + " :: " + to_address
pq.write(log_message)
pq.write("\n")
pq.close()
#Remove both email files
try:
os.remove(outfile)
except:
pass
try:
os.remove(emf)
except:
pass
if __name__ == "__main__":
main()