forked from glatzor/mail2rm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail2rm
executable file
·88 lines (76 loc) · 3.2 KB
/
mail2rm
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
#!/usr/bin/python3
"""mail2rm - send PDF documents via email to your reMarkable cloud"""
# Copyright (C) 2020 Sebastian Heinlein <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
__author__ = "Sebastian Heinlein <[email protected]>"
__state__ = "stable"
import email
import os
import os.path
import sys
import subprocess
import tempfile
from optparse import OptionParser
from pathlib import Path
class Mail2reMarkable:
def run(self, sender, options):
if options.allowed_senders and sender.lower() not in options.allowed_senders:
print("Unallowed sender: %s" % sender)
sys.exit(1)
mail = email.message_from_string(sys.stdin.read())
if not mail.is_multipart():
print("Could not find any attachments")
sys.exit(1)
with tempfile.TemporaryDirectory() as tempdir:
found_pdf = False
for part in mail.get_payload():
if part.get_content_type() == "application/pdf":
found_pdf = True
payload = part.get_payload(decode=True)
filename = os.path.join(tempdir, part.get_filename())
with open(filename, "wb") as f:
f.write(payload)
env = os.environ.copy()
env["HOME"] = Path.home()
proc = subprocess.run(
[options.path_rmapi, 'put', filename],
timeout=20, env=env,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
print(proc.stdout)
proc.check_returncode()
if found_pdf == False:
print("Could not find any PDF attachments")
sys.exit(1)
if __name__ == "__main__":
usage = "usage: %prog [options] SENDER"
parser = OptionParser(usage=usage)
parser.add_option('-a', '--allow-sender',
type='string', action='append', metavar='ALLOWED_SENDERS',
default=[], dest='allowed_senders',
help='Limit senders to the given address only. Can be used multiple times')
parser.add_option('-r', '--rmapi',
type='string', action='store',
default="~/go/bin/rmapi", dest='path_rmapi',
help='Path to the rmapi binary. Defaults to ~/go/bin/rmapi')
options, args = parser.parse_args()
if len(args) == 0:
parser.error("You have to specify a sender address")
mailer = Mail2reMarkable()
mailer.run(args[0], options)
# vim:ts=4:sw=4:et