-
Notifications
You must be signed in to change notification settings - Fork 0
/
imap_to_nextcloud.py
259 lines (220 loc) · 7.76 KB
/
imap_to_nextcloud.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
"""
This script will read messages from an IMAP mailbox,
and post the content of the messages to a Nextcloud Talk room.
We use this for posting messages from Wordpress Wordfence to a Talk room where the administrators are listening.
"""
import sqlite3
import os.path
import sys
from pathlib import Path
import re
import datetime
import requests
import json
import imaplib
import email
from email.header import decode_header
from webdav3.client import Client
import tempfile
# pdfkit requires wkhtmltopdf to be installed: apt-get install wkhtmltopdf
import pdfkit
import time
DEBUG = False
MAX_LENGTH_CHAT_MESSAGE = 1000
configparameter = "config.yml"
if len(sys.argv) > 1:
configparameter = sys.argv[1]
configfile=("%s/%s" % (os.path.dirname(os.path.realpath(__file__)),configparameter))
if os.path.isfile(configfile):
f = open(configfile, "r")
for line in f:
if line.strip().startswith("#"):
continue
if "nc_url" in line:
nc_url = re.search(r": (.*)", line).group(1)
if "nc_channel" in line:
nc_channel = re.search(r": (.*)", line).group(1)
if "nc_user" in line:
nc_user = re.search(r": (.*)", line).group(1)
if "nc_pwd" in line:
nc_pwd = re.search(r": (.*)", line).group(1)
if "imap_host" in line:
imap_host = re.search(r": (.*)", line).group(1)
if "imap_user" in line:
imap_user = re.search(r": (.*)", line).group(1)
if "imap_pwd" in line:
imap_pwd = re.search(r": (.*)", line).group(1)
if "nc_user_display_name" in line:
nc_user_display_name = re.search(r": (.*)", line).group(1)
if "sqlite_file" in line:
sqlite_file = re.search(r": (.*)", line).group(1)
# Connect to the sqlite database
sq3 = sqlite3.connect(sqlite_file)
sq3.execute("""
CREATE TABLE IF NOT EXISTS Notified (
id INTEGER PRIMARY KEY AUTOINCREMENT,
message_id VARCHAR(255),
t TIMESTAMP DEFAULT CURRENT_TIMESTAMP)""")
# verify if nextcloud has already been notified about this content
sqlCheckNotification = """
select *
from Notified
where message_id = ?"""
sqlAddNotification = """
insert into Notified(message_id)
values(?)"""
def alreadyNotified(messageId):
cursor = sq3.cursor()
cursor.execute(sqlCheckNotification, (messageId,))
row = cursor.fetchone()
if row is None:
return False
return True
def storeNotified(messageId):
sq3.execute(sqlAddNotification, (messageId,))
def storeAllNotified(messages):
for post in messages:
storeNotified(post['id'])
sq3.commit()
def sendNotification(msg):
S = requests.Session()
data = {
"token": nc_channel,
"message": msg,
"actorDisplayName": nc_user_display_name,
"actorType": "",
"actorId": "",
"timestamp": 0,
"messageParameters": []
}
# see https://nextcloud-talk.readthedocs.io/en/latest/chat/#sending-a-new-chat-message
url = "{}/ocs/v2.php/apps/spreed/api/v1/chat/{}".format(nc_url, nc_channel)
print(url)
payload = json.dumps(data)
headers = {'content-type': 'application/json', 'OCS-APIRequest': 'true'}
R = S.post(url, data=payload, headers=headers, auth=(nc_user, nc_pwd))
print(R)
if R.status_code < 200 or R.status_code >=300:
raise Exception("problem posting the message")
def shareAttachment(msg):
# upload the file
# see https://pypi.org/project/webdavclient3/
options = {
'webdav_hostname': f"https://cloud.iccm-europe.org/remote.php/dav/files/{nc_user}",
'webdav_login': nc_user,
'webdav_password': nc_pwd
}
filename = f"html_{time.time()}.pdf"
client = Client(options)
client.mkdir('/Talk')
fp = tempfile.NamedTemporaryFile(mode="w+", delete=False)
fp.close()
pdfkit.from_string(msg, fp.name)
client.upload_sync(remote_path=f"/Talk/{filename}", local_path=fp.name)
os.unlink(fp.name)
# if 404, is the app enabled? php occ app:enable files_sharing
S = requests.Session()
data = {
"shareType": 10,
"shareWith": nc_channel,
"path": f"/Talk/{filename}",
#"referenceId": "TODO",
#"talkMetaData": {"messageType": "comment"}
}
# see https://nextcloud-talk.readthedocs.io/en/latest/chat/#share-a-file-to-the-chat
url = f"{nc_url}/ocs/v2.php/apps/files_sharing/api/v1/shares"
print(url)
payload = json.dumps(data)
headers = {'content-type': 'application/json', 'OCS-APIRequest': 'true'}
R = S.post(url, data=payload, headers=headers, auth=(nc_user, nc_pwd))
print(R)
if R.status_code < 200 or R.status_code >=300:
raise Exception("problem sharing the file")
def sendNotifications(messages):
if (len(messages) == 0):
return
try:
for post in messages:
if alreadyNotified(post['id']):
continue
msg = ("Sender: %s\nDate: %s\nSubject: %s\n%s" %
(post['from'], post['date'], post['subject'], post['text']))
msg = msg.replace(""", '"').replace("'", "'")
if DEBUG:
print(msg)
else:
fullmsg = None
if len(msg) > MAX_LENGTH_CHAT_MESSAGE:
fullmsg = msg.replace("\n", "<br/>")
msg = msg[:MAX_LENGTH_CHAT_MESSAGE] + "\n[...]"
sendNotification(msg)
if fullmsg:
shareAttachment(fullmsg)
if post['html']:
shareAttachment(post['html'])
except Exception as e:
print(e)
if DEBUG:
# don't store in sqlite database
return False
return True
def main():
# get all new messages
messages = []
imap = imaplib.IMAP4_SSL(imap_host)
imap.login(imap_user, imap_pwd)
status, imapmessages = imap.select("INBOX")
total_number_messages = int(imapmessages[0])
max_messages = 10
if total_number_messages < max_messages:
max_messages = total_number_messages
for i in range(total_number_messages, total_number_messages - max_messages, -1):
# fetch the email message by ID
res, msg = imap.fetch(str(i), "(RFC822)")
for response in msg:
if isinstance(response, tuple):
post = {}
# parse a bytes email into a message object
msg = email.message_from_bytes(response[1])
# decode the email subject
subject, encoding = decode_header(msg["Subject"])[0]
if isinstance(subject, bytes):
# if it's a bytes, decode to str
subject = subject.decode(encoding)
# decode email sender
From, encoding = decode_header(msg.get("From"))[0]
if isinstance(From, bytes):
From = From.decode(encoding)
post['id'] = msg["Message-Id"]
if alreadyNotified(post['id']):
continue
post['date'] = msg["Date"]
post['subject'] = subject
post['from'] = From
post['text'] = ""
post['html'] = ""
if msg.is_multipart():
for part in msg.walk():
content_type = part.get_content_type()
content_disposition = str(part.get("Content-Disposition"))
try:
body = part.get_payload(decode=True).decode()
except:
pass
if content_type == "text/plain" and "attachment" not in content_disposition:
post['text'] = body
else:
# extract content type of email
content_type = msg.get_content_type()
# get the email body
body = msg.get_payload(decode=True).decode()
if content_type == "text/plain":
post['text'] = body
if content_type == "text/html":
post['html'] = body
messages.append(post)
imap.close()
imap.logout()
if sendNotifications(messages):
storeAllNotified(messages)
main()