forked from Abdur-rahmaanJ/honeybot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.py
116 lines (101 loc) 路 4.63 KB
/
mail.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
# -*- coding: utf-8 -*-
"""
[mail.py]
Email plugin
[Author]
Tanner Fry
[About]
Will allow the user to send an email while in the chat
with the bot. The text send will be interpreted as html
so you can use formatting such as <br> to make new lines,
and any other html formatting commands in the text of the
body of the email. No additional libraries need to be installed.
Make sure that you configure the email_config.conf file with your
email, your email password, SMTP server, and SMTP server port.
See that document for more information.
[Commands]
.mail <To email address> .body <Text for the body of your email> .subject <Text of subject for email>
EX:// ".mail test@email.com .body testing body for honeybot email .subject testing subject"
"""
import smtplib
import string
from email.mime.text import MIMEText
#open config file for user credentials to email
f = open("email_config.conf")
str = f.read()
f.close()
#split the read in words into a list, and pop them into their respective
#variables
test = str.rsplit()
USER = test[test.index("Email:")+1]
PASS = test[test.index("Password:")+1]
HOST = test[test.index("Server:")+1]
PORT = test[test.index("Port:")+1]
#used to tie into the body of what the user sends, if this is not at the
#front of the message then the message will not appear in the email
text = "\n"
class Plugin:
def __init__(self):
pass
#__email will take 6 parameters to setup the email reconnection
#and send the message.
def __email(HOST, PORT, USER, PASS, TO, MSG):
server = smtplib.SMTP(HOST, int(PORT))
server.connect(HOST, int(PORT))
server.starttls()
server.login(USER, PASS)
server.sendmail(USER, TO, MSG.as_string())
server.quit()
#body is used to loop through the msgs list from the chat
#and grab everything that was said after .body and before
#.subject.
def body(BODY_INDEX,SUBJECT_INDEX, msgs):
i = BODY_INDEX+1
while i < SUBJECT_INDEX:
if i == BODY_INDEX + 1:
if msgs[i] == "\n":
Body = msgs[i]
BODY = msgs[i] + " "
elif i == SUBJECT_INDEX - 1:
BODY = BODY + msgs[i]
else:
if msgs[i] == "\n":
Body = BODY + msgs[i]
BODY = BODY + msgs[i] + " "
i = i+1
return (BODY)
#subject is used to loop through the msgs list from the chat
#and grab everythin after .subject and before the end of the
#message
def subject(SUBJECT_INDEX, MAX_INDEX, msgs):
i = SUBJECT_INDEX + 1
while i < MAX_INDEX:
if i == SUBJECT_INDEX + 1:
SUBJECT = msgs[i] + " "
elif i == MAX_INDEX - 1:
SUBJECT = SUBJECT + msgs[i]
else:
SUBJECT = SUBJECT + msgs[i] + " "
i = i+1
return (SUBJECT)
def run(self, incoming, methods, info):
try:
# if '!~' in info['prefix']:
# print(info)
msgs = info['args'][1:][0].split()
if info['command'] == 'PRIVMSG':
#makes sure the first parameter is ".mail"
if msgs[0] == '.mail':
TO = msgs[1] #grabs the TO email provided
BODY_INDEX = msgs.index(".body") #gets index for ".body", used in body function
SUBJECT_INDEX = msgs.index(".subject") #gets index for ".subject" used in body, and subject functions
MAX_INDEX = len(msgs) #gets max index to know the max indexing value
BODY = Plugin.body(BODY_INDEX,SUBJECT_INDEX, msgs) #fill the BODY string using the body function to extraxt the text
SUBJECT = Plugin.subject(SUBJECT_INDEX, MAX_INDEX, msgs) #fill the SUBJECT string using the subject function to extract the text
MSG = MIMEText(text + BODY, 'html') #sets MSG format to html, and fills in the BODY portion
MSG["Subject"] = SUBJECT #Fills the MSG's subject with SUBJECT
MSG["From"] = USER #Fills the From field with USER string
MSG["To"] = TO #Fills the TO field with the TO string
Plugin.__email(HOST, PORT, USER, PASS, TO, MSG) #Passes paramters needed to __email to be able to start up connection to SMTP server and send message
except Exception as e:
print('\n*error*\nwoops plugin', __file__, e, '\n')