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
|
import os
import glob
import logging
import email
import tempfile
from twisted.internet.defer import inlineCallbacks
import threading
from alot import buffers
from alot.commands import Command, registerCommand
from alot import settings
from alot import helper
from alot.commands import globals
from alot.helper import string_decode
MODE = 'envelope'
@registerCommand(MODE, 'attach', arguments=[
(['path'], {'help':'file(s) to attach (accepts wildcads)'})])
class AttachCommand(Command):
"""attach files to the mail"""
def __init__(self, path=None, **kwargs):
"""
:param path: files to attach (globable string)
:type path: str
"""
Command.__init__(self, **kwargs)
self.path = path
def apply(self, ui):
envelope = ui.current_buffer.envelope
if self.path:
files = filter(os.path.isfile,
glob.glob(os.path.expanduser(self.path)))
if not files:
ui.notify('no matches, abort')
return
else:
ui.notify('no files specified, abort')
logging.info("attaching: %s" % files)
for path in files:
envelope.attachments.append(helper.mimewrap(path))
ui.current_buffer.rebuild()
@registerCommand(MODE, 'refine', arguments=[
(['key'], {'help':'header to refine'})])
class RefineCommand(Command):
"""prompt to change the value of a header"""
def __init__(self, key='', **kwargs):
"""
:param key: key of the header to change
:type key: str
"""
Command.__init__(self, **kwargs)
self.key = key
def apply(self, ui):
value = ui.current_buffer.envelope.get(self.key, '')
cmdstring = 'set %s %s' % (self.key, value)
ui.apply_command(globals.PromptCommand(cmdstring))
@registerCommand(MODE, 'send')
class SendCommand(Command):
"""send mail"""
@inlineCallbacks
def apply(self, ui):
currentbuffer = ui.current_buffer # needed to close later
envelope = currentbuffer.envelope
frm = envelope.get('From')
sname, saddr = email.Utils.parseaddr(frm)
omit_signature = False
# determine account to use for sending
account = ui.accountman.get_account_by_address(saddr)
if account == None:
if not ui.accountman.get_accounts():
ui.notify('no accounts set', priority='error')
return
else:
account = ui.accountman.get_accounts()[0]
omit_signature = True
# attach signature file if present
if account.signature and not omit_signature:
sig = os.path.expanduser(account.signature)
if os.path.isfile(sig):
if account.signature_filename:
name = account.signature_filename
else:
name = None
envelope.attach(sig, filename=name)
else:
ui.notify('could not locate signature: %s' % sig,
priority='error')
if (yield ui.choice('send without signature',
select='yes', cancel='no')) == 'no':
return
# send
clearme = ui.notify('sending..', timeout=-1)
def afterwards(returnvalue):
ui.clear_notify([clearme])
if returnvalue == 'success': # sucessfully send mail
ui.buffer_close(currentbuffer)
ui.notify('mail send successful')
else:
ui.notify('failed to send: %s' % returnvalue,
priority='error')
write_fd = ui.mainloop.watch_pipe(afterwards)
def thread_code():
mail = envelope.construct_mail()
os.write(write_fd, account.send_mail(mail) or 'success')
thread = threading.Thread(target=thread_code)
thread.start()
@registerCommand(MODE, 'edit')
class EditCommand(Command):
"""edit mail"""
def __init__(self, envelope=None, **kwargs):
"""
:param envelope: email to edit
:type envelope: :class:`~alot.message.Envelope`
"""
self.envelope = envelope
self.openNew = (envelope != None)
Command.__init__(self, **kwargs)
def apply(self, ui):
ebuffer = ui.current_buffer
if not self.envelope:
self.envelope = ui.current_buffer.envelope
#determine editable headers
edit_headers = set(settings.config.getstringlist('general',
'edit_headers_whitelist'))
if '*' in edit_headers:
edit_headers = set(self.envelope.headers.keys())
blacklist = set(settings.config.getstringlist('general',
'edit_headers_blacklist'))
if '*' in blacklist:
blacklist = set(self.envelope.headers.keys())
self.edit_headers = edit_headers - blacklist
ui.logger.info('editable headers: %s' % blacklist)
def openEnvelopeFromTmpfile():
# This parses the input from the tempfile.
# we do this ourselves here because we want to be able to
# just type utf-8 encoded stuff into the tempfile and let alot
# worry about encodings.
# get input
f = open(tf.name)
os.unlink(tf.name)
enc = settings.config.get('general', 'editor_writes_encoding')
template = string_decode(f.read(), enc)
f.close()
# call post-edit translate hook
translate = settings.config.get_hook('post_edit_translate')
if translate:
template = translate(template, ui=ui, dbm=ui.dbman,
aman=ui.accountman, log=ui.logger,
config=settings.config)
self.envelope.parse_template(template)
if self.openNew:
ui.buffer_open(buffers.EnvelopeBuffer(ui, self.envelope))
else:
ebuffer.envelope = self.envelope
ebuffer.rebuild()
# decode header
headertext = u''
for key in edit_headers:
value = self.envelope.headers.get(key, '')
headertext += '%s: %s\n' % (key, value)
bodytext = self.envelope.body
# call pre-edit translate hook
translate = settings.config.get_hook('pre_edit_translate')
if translate:
bodytext = translate(bodytext, ui=ui, dbm=ui.dbman,
aman=ui.accountman, log=ui.logger,
config=settings.config)
#write stuff to tempfile
tf = tempfile.NamedTemporaryFile(delete=False)
content = bodytext
if headertext:
content = '%s%s' % (headertext, content)
tf.write(content.encode('utf-8'))
tf.flush()
tf.close()
cmd = globals.EditCommand(tf.name, on_success=openEnvelopeFromTmpfile,
refocus=False)
ui.apply_command(cmd)
@registerCommand(MODE, 'set', arguments=[
# TODO
#(['--append'], {'action': 'store_true', 'help':'keep previous value'}),
(['key'], {'help':'header to refine'}),
(['value'], {'nargs':'+', 'help':'value'})])
class SetCommand(Command):
"""set header value"""
def __init__(self, key, value, append=False, **kwargs):
"""
:param key: key of the header to change
:type key: str
:param value: new value
:type value: str
"""
self.key = key
self.value = ' '.join(value)
self.append = append
Command.__init__(self, **kwargs)
def apply(self, ui):
ui.current_buffer.envelope[self.key] = self.value
ui.current_buffer.rebuild()
@registerCommand(MODE, 'unset', arguments=[
(['key'], {'help':'header to refine'})])
class UnsetCommand(Command):
"""remove header field"""
def __init__(self, key, **kwargs):
"""
:param key: key of the header to remove
:type key: str
"""
self.key = key
Command.__init__(self, **kwargs)
def apply(self, ui):
del(ui.current_buffer.envelope[self.key])
ui.current_buffer.rebuild()
@registerCommand(MODE, 'toggleheaders')
class ToggleHeaderCommand(Command):
"""toggle display of all headers"""
def apply(self, ui):
ui.current_buffer.toggle_all_headers()
|