-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·302 lines (270 loc) · 11.4 KB
/
main.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import config
import telepot
import time
import smtplib
import threading
import sys
import email
import tempfile
import uuid
from email.mime.text import MIMEText
from email.mime.audio import MIMEAudio
from email.mime.application import MIMEApplication
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from xmlrpc.server import SimpleXMLRPCServer
__version__ = "0.1.0"
lock = threading.Lock()
msgs = 0
import imghdr
# backport webp support from python 3.5, see https://hg.python.org/cpython/annotate/4fd17e28d4bf/Lib/imghdr.py
def test_webp(h, f):
if h.startswith(b'RIFF') and h[8:12] == b'WEBP':
return 'webp'
imghdr.tests.append(test_webp)
def sprint(*args, **kwargs):
with lock:
print(*args, **kwargs)
def get_alias(sender):
the_alias = sender["first_name"]
if int(sender["id"]) in config.ALIASES:
the_alias = config.ALIASES[int(sender["id"])]
elif "nickname" in sender:
the_alias = sender["nickname"]
return the_alias
def send_mail(msg, telegram=True):
if config.OFFLINE_MODE or config.TEST_MODE:
sprint("Not sending email in offline or test mode")
return
if telegram:
the_sender = get_alias(msg["from"])
else:
the_sender = msg["from"]
mails_to_send = []
for forward_email in config.EMAILS:
mail = MIMEMultipart()
if 'text' in msg:
mail.attach(MIMEText('{} sagt: {}'.format(the_sender, msg['text'])))
for i in ['photo', 'sticker']:
if i in msg:
with open(msg[i], 'rb') as fp:
mail.attach(MIMEImage(fp.read()))
for i in ['audio', 'voice']:
if i in msg:
with open(msg[i], 'rb') as fp:
subtype = 'mpeg' if 'audio' else 'opus'
mail.attach(MIMEAudio(fp.read(), _subtype=subtype))
for i in ['document', 'video']:
if i in msg:
with open(msg[i], 'rb') as fp:
mail.attach(MIMEApplication(fp.read()))
if 'special' in msg:
for i in msg['special']:
major_type = i[1][:i[1].find('/')]
sub_type = i[1][i[1].find('/')+1:]
with open(i[0], 'rb') as fp:
if major_type == 'image':
mail.attach(MIMEImage(fp.read(), _subtype=sub_type))
elif major_type == 'audio':
mail.attach(MIMEAudio(fp.read(), _subtype=sub_type))
elif major_type == 'video':
mail.attach(MIMEApplication(fp.read()))
elif major_type == 'text':
mail.attach(MIMEText(fp.read(), _subtype=sub_type))
elif major_type == 'application':
mail.attach(MIMEApplication(fp.read(), _subtype=sub_type))
else:
mail.attach(MIMEApplication(fp.read()))
mail["Subject"] = "Telegram Message from {}".format(the_sender)
mail["From"] = config.BOT_EMAIL_FROM
mail["To"] = forward_email
mail["X-Telegram-Bot"] = config.BOT_NAME
if telegram:
mail["X-Telegram-Original-User"] = str(msg["from"]["id"])
mails_to_send.append(mail)
s = smtplib.SMTP("localhost")
for mail in mails_to_send:
s.send_message(mail)
s.quit()
def handle_bot_message(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
user = msg["from"]
uid = msg["from"]["id"]
if chat_type == 'private':
sprint('Private message from {}({})'.format(user, uid))
return
if content_type == "text":
text = msg["text"]
sprint("Group message from {} ({}): {}".format(user, uid, text))
send_mail(msg)
elif content_type in ['audio', 'document', 'photo', 'sticker', 'video', 'voice']:
with tempfile.TemporaryDirectory() as tmpdir:
if content_type == 'photo':
file_id = msg['photo'][-1]['file_id']
sprint("Got Photo from {} ({}): {}".format(user, uid, file_id))
bot.download_file(file_id, tmpdir+'/photo')
msg["photo"] = tmpdir+'/photo'
send_mail(msg)
else:
file_id = msg[content_type]['file_id']
sprint("Got some content ({}) from {} ({}): {}".format(content_type, user, uid, file_id))
bot.download_file(msg[content_type]['file_id'], tmpdir+'/file')
msg[content_type] = tmpdir+'/file'
send_mail(msg)
elif content_type in ['new_chat_member', 'left_chat_member']:
new_msg = {"from": msg[content_type]}
new_msg["text"] = "*tschüss*" if content_type == 'left_chat_member' else "*hallo*"
send_mail(new_msg)
def handle_reply_mail(mail):
global msgs
msgs += 1
the_mail = email.message_from_string(mail)
the_sender = the_mail["from"]
sender_parts = the_sender.split(" ")
if len(sender_parts) != 2:
sender_parts = sender_parts[0].split("@")
the_sender = sender_parts[0].strip("<")
else:
the_sender = sender_parts[0]
new_mail = {'from': the_sender}
with tempfile.TemporaryDirectory() as tmpdir:
for part in the_mail.walk():
part_type = part.get_content_type(decode=True)
if part_type.startswith('message/') or part_type.startswith('multipart/'):
sprint('Received mail, skipping current part:', part_type)
continue
sprint('Received mail, current part is:', part_type)
content = part.get_payload(decode=True)
if part_type == 'text/plain':
text = content.decode()
bot_send_message(the_sender, text)
if 'text' in new_mail:
new_mail['text'] += '\n' + text
else:
new_mail['text'] = text
continue
major_type = part_type[:part_type.find('/')]
filename = part.get_filename('file.bin')
if filename == 'file.bin':
filename = uuid.uuid4() + '.bin'
filename = tmpdir+'/'+filename
if major_type == 'image':
with open(filename, 'wb') as fp:
fp.write(content)
bot_send_image(the_sender, filename)
elif major_type == 'audio':
sub_type = part_type[part_type.find('/')+1:]
if sub_type in ['mpeg'] and not filename.endswith('.mp3'):
filename = filename[:filename.find('.')] + '.mp3'
with open(filename, 'wb') as fp:
fp.write(content)
bot_send_audio(the_sender, filename)
elif major_type == 'video':
sub_type = part_type[part_type.find('/')+1:]
if sub_type in ['mp4', 'mpeg4-generic'] and not filename.endswith('.mp4'):
filename = filename[:filename.find('.')] + '.mp4'
with open(filename, 'wb') as fp:
fp.write(content)
bot_send_video(the_sender, filename)
else:
with open(filename, 'wb') as fp:
fp.write(content)
bot_send_document(the_sender, filename)
if 'special' in new_mail:
new_mail['special'].append((filename, part_type))
else:
new_mail['special'] = [(filename, part_type)]
send_mail(new_mail, telegram=False)
return True
def bot_send_document(sender, filename):
if config.OFFLINE_MODE or config.TEST_MODE:
sprint('Not sending document via bot due to offline or test mode')
return
with open(filename, 'rb') as fp:
bot.sendDocument(config.GROUP_ID, (filename, fp),
caption="Gesendet von: {}".format(sender))
def bot_send_video(sender, filename):
if config.OFFLINE_MODE or config.TEST_MODE:
sprint('Not sending video via bot due to offline or test mode')
return
with open(filename, 'rb') as fp:
if filename.endswith('.mp4'):
bot.sendVideo(config.GROUP_ID, (filename, fp),
caption="Gesendet von: {}".format(sender))
else:
bot.sendDocument(config.GROUP_ID, (filename, fp),
caption="Gesendet von: {}".format(sender))
def bot_send_audio(sender, filename):
if config.OFFLINE_MODE or config.TEST_MODE:
sprint('Not sending audio via bot due to offline or test mode')
return
what = None
with open(filename, 'rb') as fp:
if fp.read(8) == 'OpusHead'.encode():
what = 'opus'
with open(filename, 'rb') as fp:
if what == 'opus':
if not filename.endswith('.ogg'):
filename = filename[:filename.find('.')] + '.ogg'
bot.sendVoice(config.GROUP_ID, (filename, fp),
caption="Gesendet von: {}".format(sender))
elif filename.endswith('.mp3'):
bot.sendAudio(config.GROUP_ID, (filename, fp),
caption="Gesendet von: {}".format(sender))
else:
bot.sendDocument(config.GROUP_ID, (filename, fp),
caption="Gesendet von: {}".format(sender))
def bot_send_image(sender, filename):
if config.OFFLINE_MODE or config.TEST_MODE:
sprint('Not sending image via bot due to offline or test mode')
return
what = imghdr.what(filename)
if what is None:
sprint('Not sending image because of unknown file type')
return
with open(filename, 'rb') as fp:
if what in ['gif', 'jpeg', 'png']:
if not (filename[-3:] == what or (what == 'jpeg' and filename[-4:] in ['.jpg', 'jpeg'])):
if what == 'jpeg':
what = 'jpg'
filename = filename[:filename.find('.')] + '.' + what
bot.sendPhoto(config.GROUP_ID, (filename, fp),
caption="Gesendet von: {}".format(sender))
elif what == 'webp':
if not filename.endswith('.webp'):
filename = filename[:filename.find('.')] + '.webp'
bot.sendSticker(config.GROUP_ID, (filename, fp))
else:
bot.sendDocument(config.GROUP_ID, (filename, fp),
caption="Gesendet von: {}".format(sender))
def bot_send_message(sender, msg):
if config.OFFLINE_MODE or config.TEST_MODE:
sprint('Not sending message via bot due to offline or test mode')
return
bot.sendMessage(config.GROUP_ID, '{} sagt: {}'.format(sender, msg))
def xmlrpc_worker():
server.serve_forever()
if __name__ == "__main__":
server = SimpleXMLRPCServer(("localhost", 4711), logRequests=False)
server.register_function(handle_reply_mail)
if config.OFFLINE_MODE:
sprint("Starting in offline mode")
else:
bot = telepot.Bot(config.BOT_TOKEN)
bot.message_loop(handle_bot_message)
sprint("Starting", config.BOT_NAME)
# print(bot.getMe())
sprint("Starting email worker thread")
worker = threading.Thread(target=xmlrpc_worker)
worker.start()
sprint("Waiting for messages...")
while True:
try:
time.sleep(10)
except KeyboardInterrupt as ex:
sprint("Shutting down...")
server.shutdown()
worker.join()
sys.exit(0)