forked from getmail6/getmail6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetmail_maildir
executable file
·85 lines (67 loc) · 2.91 KB
/
getmail_maildir
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# docs/COPYING 2a + DRY: https://github.com/getmail6/getmail6
# Please refer to the git history regarding who changed what and when in this file.
'''getmail_maildir
Reads a message from stdin and delivers it to a maildir specified as
a commandline argument. Expects the envelope sender address to be in the
environment variable SENDER.
Copyright (C) 2001-2024 Charles Cazabon and others
This program is free software; you can redistribute it and/or modify it under
the terms of version 2 (only) of the GNU General Public License as published by
the Free Software Foundation. A copy of this license should be included in the
file COPYING.
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.
'''
import sys
import os
from getmailcore.message import Message
from getmailcore.utilities import *
from getmailcore.exceptions import *
def main():
hostname = localhostname()
verbose = False
path = None
for arg in sys.argv[1:]:
if arg in ('-h', '--help'):
sys.stdout.write('Usage: %s maildirpath\n' % sys.argv[0])
raise SystemExit
elif arg in ('-v', '--verbose'):
verbose = True
elif not path:
path = arg
else:
raise SystemExit('Error: maildir path specified twice (was %s, now %s)'
% (path, arg))
if os.name == 'posix' and (os.geteuid() == 0 or os.getegid() == 0):
raise SystemExit('Error: do not run this program as user root')
if not (path and (path.startswith('.') or path.startswith('/'))
and path.endswith('/')):
raise SystemExit('Error: maildir must start with . or / and end with /')
if not is_maildir(path):
raise SystemExit('Error: %s is not a maildir' % path)
try:
msg = Message(fromfile=sys.stdin.buffer)
except AttributeError: #py2
msg = Message(fromfile=sys.stdin)
if 'SENDER' in os.environ:
msg.sender = os.environ['SENDER']
if 'RECIPIENT' in os.environ:
msg.recipient = os.environ['RECIPIENT']
try:
d = deliver_maildir(path, msg.flatten(True, False), hostname)
except getmailDeliveryError as o:
raise SystemExit('Error: delivery error delivering to maildir %s (%s)'
% (path, o))
except Exception as o:
raise SystemExit('Error: other error delivering to maildir %s (%s)'
% (path, o))
if verbose:
sys.stdout.write('Delivered to maildir %s\n' % path)
if __name__ == "__main__":
main()