-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutt_flagged_vfolder_jump.py
executable file
·122 lines (104 loc) · 4.26 KB
/
mutt_flagged_vfolder_jump.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
#!/usr/bin/env python3
"""Generates mutt command file to jump to the source of a symlinked mail"""
#
# mutt_flagged_vfolder_jump.py
#
# Generates mutt command file to jump to the source of a symlinked mail
#
# Copyright (C) 2009-2018 Georg Lutz <georg AT NOSPAM georglutz DOT de>
#
# 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
# (at your option) 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, see <http://www.gnu.org/licenses/>.
import argparse
import email
import os
import re
import sys
def parse_message_id(file_):
'''Returns the message id for a given file.
It is assumed that file represents a valid RFC822 message'''
msg = email.message_from_binary_file(file_)
msg_id = ""
if "Message-ID" in msg:
msg_id = msg["Message-ID"].strip("<>")
return msg_id
def parse_maildir(filename):
'''Returns the maildir folder for a given file in a maildir'''
(head, _) = os.path.split(os.path.dirname(filename))
return head
def write_cmd_file(filename, maildir, msg_id):
'''Writes a file which can be directly sourced by mutt. The file causes
mutt to change to the given maildir and search there for the given
message id.'''
file_ = open(filename, "w")
cmd = "push \"<change-folder> " + maildir + "<enter>/~i "
# Helps if matching something like 123@[1.2.3.4]
regex = re.escape(msg_id)
# Replace dollar sign "$" with ".+" as mutt has problems with push
# commands and a dollar sign followed with a non numeric value e.g. like "$u".
# This seems to reference a variable and cannot be escaped apparently.
# Something like "$1" does not oppose problems when escaped.
regex = regex.replace("\\$", ".+")
# According to mutt manual "4.1 Regular Expressions" backslashes must
# be quoted for a regular expression in initialization command
regex = regex.replace("\\", "\\\\\\\\")
# For some unknown reason "=" must not be escaped twice
regex = regex.replace("\\\\=", "=")
cmd += regex + "<enter>\""
file_.write(cmd)
file_.close()
def main():
'''main function, called when script file is executed directly'''
parser = argparse.ArgumentParser()
parser.add_argument('vfolder',
help='The folder with the virtual messages')
parser.add_argument('cmdfile',
help='The command file to generate')
args = parser.parse_args()
opt_vfolder = os.path.expanduser(args.vfolder)
opt_cmd_file = os.path.expanduser(args.cmdfile)
if not os.path.isdir(opt_vfolder):
print("Could not find given vfolder")
sys.exit(1)
if os.path.exists(opt_cmd_file):
os.unlink(opt_cmd_file)
sys.stdin = sys.stdin.detach()
msg_id = parse_message_id(sys.stdin)
if msg_id:
found = False
cmd_file_written = False
for entry in os.listdir(os.path.join(opt_vfolder, "cur")):
entry = os.path.join(opt_vfolder, "cur", entry)
if os.path.islink(entry):
file_ = open(entry, "rb")
msg_id2 = parse_message_id(file_)
file_.close()
if msg_id == msg_id2:
found = True
sourcefile = os.path.realpath(entry)
maildir = parse_maildir(sourcefile)
write_cmd_file(opt_cmd_file, maildir, msg_id)
break
if found and cmd_file_written:
sys.exit(0)
else:
if not found:
print("Could not find given email")
# mutt waits for key press if external command returns with code != 0
# even if wait_key is not set. This is good for us as we want to see
# the error messages
sys.exit(1)
else:
sys.exit(1)
if __name__ == "__main__":
main()