Debian Codegraph @

src/calibre/gui2/email.py

raw
1 #!/usr/bin/env python
2 # License: GPLv3 Copyright: 2010, Kovid Goyal <[email protected]>
3
4 import os
5 import socket
6 import textwrap
7 import time
8 from collections import defaultdict
9 from functools import partial
10 from itertools import repeat
11 from threading import Thread
12 from typing import TYPE_CHECKING
13
14 from qt.core import QDialog, QDialogButtonBox, QGridLayout, QIcon, QLabel, QLineEdit, QListWidget, QListWidgetItem, QPushButton, Qt
15
16 from calibre.constants import preferred_encoding
17 from calibre.customize.ui import available_input_formats, available_output_formats
18 from calibre.ebooks.metadata import authors_to_string
19 from calibre.gui2 import Dispatcher, config, error_dialog, gprefs, warning_dialog
20 from calibre.gui2.threaded_jobs import ThreadedJob
21 from calibre.library.save_to_disk import get_components
22 from calibre.utils.config import prefs, tweaks
23 from calibre.utils.icu import primary_sort_key
24 from calibre.utils.localization import _
25 from calibre.utils.resources import get_image_path as I
26 from calibre.utils.smtp import compose_mail, extract_email_address, sendmail
27 from calibre.utils.smtp import config as email_config
28 from polyglot.binary import from_hex_unicode
29
30 if TYPE_CHECKING:
31 from calibre.gui2.ui import Main
32
33
34 class Worker(Thread):
35 def __init__(self, func, args):
36 super().__init__(name='EmailWorker', daemon=True, target=self.do_work, args=(func, args))
37 self.exception = self.tb = None
38
39 def do_work(self, func, args):
40 # time.sleep(1000)
41 try:
42 func(*args)
43 except Exception as e:
44 import traceback
45
46 self.exception = e
47 self.tb = traceback.format_exc()
48
49
50 class Sendmail:
51 MAX_RETRIES = 1
52 TIMEOUT = 25 * 60 # seconds
53
54 def __init__(self):
55 self.calculate_rate_limit()
56 self.last_send_time = time.time() - self.rate_limit
57
58 def calculate_rate_limit(self):
59 self.rate_limit = 1
60 opts = email_config().parse()
61 rh = opts.relay_host
62 if rh:
63 for suffix in tweaks['public_smtp_relay_host_suffixes']:
64 if rh.lower().endswith(suffix):
65 self.rate_limit = tweaks['public_smtp_relay_delay']
66 break
67
68 def __call__(self, attachment, aname, to, subject, text, log=None, abort=None, notifications=None):
69
70 try_count = 0
71 while True:
72 if try_count > 0 and log is not None:
73 log(f'\nRetrying in {self.rate_limit} seconds...\n')
74 worker = Worker(self.sendmail, (attachment, aname, to, subject, text, log))
75 worker.start()
76 start_time = time.time()
77 while worker.is_alive():
78 worker.join(0.2)
79 assert abort is not None
80 if abort.is_set():
81 if log:
82 log('Sending aborted by user')
83 return
84 if time.time() - start_time > self.TIMEOUT:
85 if log:
86 log('Sending timed out')
87 raise Exception(f'Sending email {subject!r} to {to!r} timed out, aborting')
88 if worker.exception is None:
89 if log:
90 log('Email successfully sent')
91 return
92 assert log is not None
93 log.error('\nSending failed...\n')
94 log.debug(worker.tb)
95 try_count += 1
96 if try_count > self.MAX_RETRIES:
97 raise worker.exception
98
99 def sendmail(self, attachment, aname, to, subject, text, log):
100 logged = False
101 while time.time() - self.last_send_time <= self.rate_limit:
102 if not logged and self.rate_limit > 0:
103 log(f'Waiting {self.rate_limit} seconds before sending, to avoid being marked as spam.\nYou can control this delay via Preferences->Tweaks')
104 logged = True
105 time.sleep(1)
106 try:
107 opts = email_config().parse()
108 from_ = opts.from_
109 if not from_:
110 from_ = 'calibre <calibre@' + socket.getfqdn() + '>'
111 with open(attachment, 'rb') as f:
112 msg = compose_mail(from_, to, text, subject, f, aname)
113 efrom = extract_email_address(from_)
114 eto = []
115 for x in to.split(','):
116 eto.append(extract_email_address(x.strip()))
117
118 def safe_debug(*args, **kwargs):
119 try:
120 return log.debug(*args, **kwargs)
121 except Exception:
122 pass
123
124 relay = opts.relay_host
125 if relay and relay == 'smtp.live.com':
126 # Microsoft changed the SMTP server
127 relay = 'smtp-mail.outlook.com'
128
129 sendmail(
130 msg,
131 efrom,
132 eto,
133 localhost=None,
134 verbose=1,
135 relay=relay,
136 username=opts.relay_username,
137 password=from_hex_unicode(opts.relay_password),
138 port=opts.relay_port,
139 encryption=opts.encryption,
140 debug_output=safe_debug,
141 )
142 finally:
143 self.last_send_time = time.time()
144
145
146 gui_sendmail = Sendmail()
147
148
149 def is_for_kindle(to):
150 return isinstance(to, str) and ('@kindle.com' in to or '@kindle.cn' in to or '@free.kindle.com' in to or '@free.kindle.cn' in to)
151
152
153 def send_mails(jobnames, callback, attachments, to_s, subjects, texts, attachment_names, job_manager):
154 for name, attachment, to, subject, text, aname in zip(jobnames, attachments, to_s, subjects, texts, attachment_names):
155 description = _('Email %(name)s to %(to)s') % dict(name=name, to=to)
156 if isinstance(to, str) and (is_for_kindle(to) or '@pbsync.com' in to):
157 # The PocketBook service is a total joke. It can't handle
158 # non-ascii, filenames that are long enough to be split up, commas, and
159 # the good lord alone knows what else. So use a random filename
160 # containing only 22 English letters and numbers
161 #
162 # And since this email is only going to be processed by automated
163 # services, make the subject+text random too as at least the amazon
164 # service can't handle non-ascii text. I don't know what baboons
165 # these companies employ to write their code. It's the height of
166 # irony that they are called "tech" companies.
167 # https://bugs.launchpad.net/calibre/+bug/1989282
168 if not is_for_kindle(to):
169 from calibre.utils.short_uuid import uuid4
170
171 # Amazon nowadays reads metadata from attachment filename instead of
172 # file internal metadata so don't nuke the filename.
173 # https://www.mobileread.com/forums/showthread.php?t=349290
174 aname = f'{uuid4()}.' + aname.rpartition('.')[-1]
175 from calibre.utils.random_ua import random_english_text
176
177 subject = random_english_text(min_words_per_sentence=3, max_words_per_sentence=9, max_num_sentences=1).rstrip('.')
178 text = random_english_text()
179 job = ThreadedJob('email', description, gui_sendmail, (attachment, aname, to, subject, text), {}, callback)
180 job_manager.run_threaded_job(job)
181
182
183 def email_news(mi, remove, get_fmts, done, job_manager):
184 opts = email_config().parse()
185 accounts = [(account, [x.strip().lower() for x in x[0].split(',')]) for account, x in opts.accounts.items() if x[1]]
186 sent_mails = []
187 for i, x in enumerate(accounts):
188 account, fmts = x
189 files = get_fmts(fmts)
190 files = [f for f in files if f is not None]
191 if not files:
192 continue
193 if opts.tags.get(account, False) and not ({t.strip() for t in opts.tags[account].split(',')} & set(mi.tags)):
194 continue
195 attachment = files[0]
196 to_s = [account]
197 subjects = [_('News:') + ' ' + mi.title]
198 texts = [_('Attached is the %s periodical downloaded by calibre.') % (mi.title,)]
199 attachment_names = [mi.title + os.path.splitext(attachment)[1]]
200 attachments = [attachment]
201 jobnames = [mi.title]
202 do_remove = []
203 if i == len(accounts) - 1:
204 do_remove = remove
205 send_mails(
206 jobnames,
207 Dispatcher(partial(done, remove=do_remove)),
208 attachments,
209 to_s,
210 subjects,
211 texts,
212 attachment_names,
213 job_manager,
214 )
215 sent_mails.append(to_s[0])
216 return sent_mails
217
218
219 plugboard_email_value = 'email'
220 plugboard_email_formats = ['epub', 'mobi', 'azw3']
221
222
223 class SelectRecipients(QDialog): # {{{
224 def __init__(self, parent=None):
225 QDialog.__init__(self, parent)
226 self._layout = l = QGridLayout(self)
227 self.setLayout(l)
228 self.setWindowIcon(QIcon(I('mail.png')))
229 self.setWindowTitle(_('Select recipients'))
230 self.recipients = r = QListWidget(self)
231 l.addWidget(r, 0, 0, 1, -1)
232 self.la = la = QLabel(_('Add a new recipient:'))
233 la.setStyleSheet('QLabel { font-weight: bold }')
234 l.addWidget(la, l.rowCount(), 0, 1, -1)
235
236 self.labels = tuple(map(QLabel, (_('&Address'), _('A&lias'), _('&Formats'), _('&Subject'))))
237 tooltips = (
238 _('The email address of the recipient'),
239 _('The optional alias (simple name) of the recipient'),
240 _('Formats to email. The first matching one will be sent (comma separated list)'),
241 _('The optional subject for email sent to this recipient'),
242 )
243
244 i = -1
245
246 def do() -> QLineEdit:
247 nonlocal i
248 i += 1
249 c = i % 2
250 row = l.rowCount() - c
251 self.labels[i].setText(str(self.labels[i].text()) + ':')
252 l.addWidget(self.labels[i], row, (2 * c))
253 le = QLineEdit(self)
254 le.setToolTip(tooltips[i])
255 self.labels[i].setBuddy(le)
256 l.addWidget(le, row, (2 * c) + 1)
257 return le
258
259 self.address, self.alias = do(), do()
260 self.formats, self.subject = do(), do()
261 self.formats.setText(prefs['output_format'].upper())
262 self.add_button = b = QPushButton(QIcon(I('plus.png')), _('&Add recipient'), self)
263 b.clicked.connect(self.add_recipient)
264 l.addWidget(b, l.rowCount(), 0, 1, -1)
265
266 self.bb = bb = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
267 l.addWidget(bb, l.rowCount(), 0, 1, -1)
268 bb.accepted.connect(self.accept)
269 bb.rejected.connect(self.reject)
270 self.setMinimumWidth(500)
271 self.setMinimumHeight(400)
272 self.resize(self.sizeHint())
273 self.init_list()
274
275 def add_recipient(self):
276 to = str(self.address.text()).strip()
277 if not to:
278 return error_dialog(self, _('Need address'), _('You must specify an address'), show=True)
279 from email.utils import parseaddr
280
281 if not parseaddr(to)[-1] or '@' not in to:
282 return error_dialog(self, _('Invalid email address'), _('The address {} is invalid').format(to), show=True)
283 formats = ','.join([x.strip().upper() for x in str(self.formats.text()).strip().split(',') if x.strip()])
284 if not formats:
285 return error_dialog(self, _('Need formats'), _('You must specify at least one format to send'), show=True)
286 opts = email_config().parse()
287 if to in opts.accounts:
288 return error_dialog(self, _('Already exists'), _('The recipient %s already exists') % to, show=True)
289 acc = opts.accounts
290 acc[to] = [formats, False, False]
291 c = email_config()
292 c.set('accounts', acc)
293 alias = str(self.alias.text()).strip()
294 if alias:
295 opts.aliases[to] = alias
296 c.set('aliases', opts.aliases)
297 subject = str(self.subject.text()).strip()
298 if subject:
299 opts.subjects[to] = subject
300 c.set('subjects', opts.subjects)
301 self.create_item(alias or to, to, checked=True)
302
303 def create_item(self, alias, key, checked=False):
304 i = QListWidgetItem(alias, self.recipients)
305 i.setFlags(Qt.ItemFlag.ItemIsEnabled | Qt.ItemFlag.ItemIsUserCheckable)
306 i.setCheckState(Qt.CheckState.Checked if checked else Qt.CheckState.Unchecked)
307 i.setData(Qt.ItemDataRole.UserRole, key)
308 self.items.append(i)
309
310 def init_list(self):
311 opts = email_config().parse()
312 self.items = []
313
314 def sk(account):
315 return primary_sort_key(opts.aliases.get(account) or account)
316
317 for key in sorted(opts.accounts or (), key=sk):
318 self.create_item(opts.aliases.get(key, key), key)
319
320 def accept(self):
321 if not self.ans:
322 return error_dialog(self, _('No recipients'), _('You must select at least one recipient'), show=True)
323 QDialog.accept(self)
324
325 @property
326 def ans(self):
327 opts = email_config().parse()
328 ans = []
329 for i in self.items:
330 if i.checkState() == Qt.CheckState.Checked:
331 to = str(i.data(Qt.ItemDataRole.UserRole) or '')
332 fmts = tuple(x.strip().upper() for x in (opts.accounts[to][0] or '').split(','))
333 subject = opts.subjects.get(to, '')
334 ans.append((to, fmts, subject))
335 return ans
336
337
338 def select_recipients(parent=None):
339 d = SelectRecipients(parent)
340 if d.exec() == QDialog.DialogCode.Accepted:
341 return d.ans
342 return ()
343
344
345 # }}}
346
347
348 class EmailMixin: # {{{
349 def send_multiple_by_mail(self: Main, recipients, delete_from_library):
350 sm = self.library_view.selectionModel()
351 assert sm is not None
352 ids = {self.library_view._model.id(r) for r in sm.selectedRows()}
353 if not ids:
354 return
355 db = self.current_db
356 db_fmt_map = {book_id: set((db.formats(book_id, index_is_id=True) or '').upper().split(',')) for book_id in ids}
357 ofmts = {x.upper() for x in available_output_formats()}
358 ifmts = {x.upper() for x in available_input_formats()}
359 bad_recipients = {}
360 auto_convert_map = defaultdict(list)
361
362 for to, fmts, subject in recipients:
363 rfmts = set(fmts)
364 ok_ids = {book_id for book_id, bfmts in db_fmt_map.items() if bfmts.intersection(rfmts)}
365 convert_ids = ids - ok_ids
366 self.send_by_mail(to, fmts, delete_from_library, subject=subject, send_ids=ok_ids, do_auto_convert=False)
367 if not rfmts.intersection(ofmts):
368 bad_recipients[to] = (convert_ids, True)
369 continue
370 outfmt = tuple(f for f in fmts if f in ofmts)[0]
371 ok_ids = {book_id for book_id in convert_ids if db_fmt_map[book_id].intersection(ifmts)}
372 bad_ids = convert_ids - ok_ids
373 if bad_ids:
374 bad_recipients[to] = (bad_ids, False)
375 if ok_ids:
376 auto_convert_map[outfmt].append((to, subject, ok_ids))
377
378 if auto_convert_map:
379 titles = {book_id for x in auto_convert_map.values() for data in x for book_id in data[2]}
380 titles = {db.new_api.field_for('title', book_id) for book_id in titles}
381 if self.auto_convert_question(_('Auto convert the following books before sending via email?'), list(titles)):
382 for ofmt, data in auto_convert_map.items():
383 ids = {bid for x in data for bid in x[2]}
384 data = [(to, subject) for to, subject, x in data]
385 self.iactions['Convert Books'].auto_convert_multiple_mail(ids, data, ofmt, delete_from_library)
386
387 if bad_recipients:
388 det_msg = []
389 titles = {book_id for x in bad_recipients.values() for book_id in x[0]}
390 titles = {book_id: db.new_api.field_for('title', book_id) for book_id in titles}
391 for to, (ids, nooutput) in bad_recipients.items():
392 msg = _('This recipient has no valid formats defined') if nooutput else _('These books have no suitable input formats for conversion')
393 det_msg.append(f'{to} - {msg}')
394 det_msg.extend('\t' + titles[bid] for bid in ids)
395 det_msg.append('\n')
396 warning_dialog(
397 self,
398 _('Could not send'),
399 _('Could not send books to some recipients. Click "Show details" for more information'),
400 det_msg='\n'.join(det_msg),
401 show=True,
402 )
403
404 def send_by_mail(self: Main, to, fmts, delete_from_library, subject='', send_ids=None, do_auto_convert=True, specific_format=None):
405 ids = [self.library_view._model.id(r) for r in self.library_view.selectionModel().selectedRows()] if send_ids is None else send_ids
406 if not ids or len(ids) == 0:
407 return
408
409 modified_metadata = []
410 files, _auto_ids = self.library_view._model.get_preferred_formats_from_ids(
411 ids,
412 fmts,
413 set_metadata=True,
414 specific_format=specific_format,
415 exclude_auto=do_auto_convert,
416 use_plugboard=plugboard_email_value,
417 plugboard_formats=plugboard_email_formats,
418 modified_metadata=modified_metadata,
419 )
420 if do_auto_convert:
421 nids = list(set(ids).difference(_auto_ids))
422 ids = [i for i in ids if i in nids]
423 else:
424 _auto_ids = []
425
426 full_metadata = self.library_view._model.metadata_for(ids, get_cover=False)
427
428 bad, remove_ids, jobnames = [], [], []
429 texts, subjects, attachments, attachment_names = [], [], [], []
430 for f, mi, id, newmi in zip(files, full_metadata, ids, modified_metadata):
431 if not newmi:
432 newmi = mi
433 t = mi.title or _('Unknown')
434 if f is None:
435 bad.append(t)
436 else:
437 remove_ids.append(id)
438 jobnames.append(t)
439 attachments.append(f)
440 if not subject:
441 subjects.append(_('E-book:') + ' ' + t)
442 else:
443 components = get_components(subject, mi, id)
444 if not components:
445 components = [mi.title]
446 subjects.append(os.path.join(*components))
447 a = authors_to_string(mi.authors or [_('Unknown')])
448 texts.append(
449 _('Attached, you will find the e-book')
450 + '\n\n'
451 + t
452 + '\n\t'
453 + _('by')
454 + ' '
455 + a
456 + '\n\n'
457 + _('in the %s format.') % os.path.splitext(f)[1][1:].upper()
458 )
459 if mi.comments and gprefs['add_comments_to_email']:
460 from calibre.ebooks.metadata import fmt_sidx
461 from calibre.utils.html2text import html2text
462
463 if mi.series:
464 sidx = fmt_sidx(
465 1.0 if mi.series_index is None else mi.series_index,
466 use_roman=config['use_roman_numerals_for_series_number'],
467 )
468 texts[-1] += '\n\n' + _('{series_index} of {series}').format(series_index=sidx, series=mi.series)
469 texts[-1] += '\n\n' + _('About this book:') + '\n\n' + textwrap.fill(html2text(mi.comments))
470 if is_for_kindle(to):
471 prefix = str(newmi.title or t)
472 else:
473 prefix = f'{t} - {a}'
474 if not isinstance(prefix, str):
475 prefix = prefix.decode(preferred_encoding, 'replace')
476 attachment_names.append(prefix + os.path.splitext(f)[1])
477 remove = remove_ids if delete_from_library else []
478
479 to_s = list(repeat(to, len(attachments)))
480 if attachments:
481 send_mails(
482 jobnames,
483 Dispatcher(partial(self.email_sent, remove=remove)),
484 attachments,
485 to_s,
486 subjects,
487 texts,
488 attachment_names,
489 self.job_manager,
490 )
491 self.status_bar.show_message(_('Sending email to') + ' ' + to, 3000)
492
493 auto = []
494 db = self.current_db.new_api
495 if _auto_ids != []:
496 for id in _auto_ids:
497 if specific_format is None:
498 dbfmts = self.current_db.formats(id, index_is_id=True)
499 formats = [f.lower() for f in (dbfmts.split(',') if dbfmts else [])]
500 if set(formats).intersection(available_input_formats()) and set(fmts).intersection(available_output_formats()):
501 auto.append(id)
502 else:
503 bad.append(db.field_for('title', id))
504 elif specific_format in list(set(fmts).intersection(set(available_output_formats()))):
505 auto.append(id)
506 else:
507 bad.append(db.field_for('title', id))
508
509 if auto != []:
510 format = specific_format if specific_format in list(set(fmts).intersection(set(available_output_formats()))) else None
511 if not format:
512 for fmt in fmts:
513 if fmt in list(set(fmts).intersection(set(available_output_formats()))):
514 format = fmt
515 break
516 if format is None:
517 bad += auto
518 else:
519 autos = [db.field_for('title', id) for id in auto]
520 if self.auto_convert_question(_('Auto convert the following books to %s before sending via email?') % format.upper(), autos):
521 self.iactions['Convert Books'].auto_convert_mail(to, fmts, delete_from_library, auto, format, subject)
522
523 if bad:
524 bad = '\n'.join(f'{i}' for i in bad)
525 d = warning_dialog(
526 self,
527 _('No suitable formats'),
528 _('Could not email the following books as no suitable formats were found:'),
529 bad,
530 )
531 d.exec()
532
533 def email_sent(self: Main, job, remove=[]):
534 if job.failed:
535 self.job_exception(job, dialog_title=_('Failed to email book'))
536 return
537
538 self.status_bar.show_message(job.description + ' ' + _('sent'), 5000)
539 if remove:
540 try:
541 next_id = self.library_view.next_id
542 self.library_view._model.delete_books_by_id(remove)
543 self.iactions['Remove Books'].library_ids_deleted2(remove, next_id=next_id)
544 except Exception:
545 import traceback
546
547 # Probably the user deleted the files, in any case, failing
548 # to delete the book is not catastrophic
549 traceback.print_exc()
550
551 def email_news(self: Main, id_):
552 mi = self.current_db.get_metadata(id_, index_is_id=True)
553 remove = [id_] if config['delete_news_from_library_on_upload'] else []
554
555 def get_fmts(fmts):
556 files, auto = self.library_view._model.get_preferred_formats_from_ids(
557 [id_],
558 fmts,
559 set_metadata=True,
560 use_plugboard=plugboard_email_value,
561 plugboard_formats=plugboard_email_formats,
562 )
563 return files
564
565 sent_mails = email_news(mi, remove, get_fmts, self.email_sent, self.job_manager)
566 if sent_mails:
567 self.status_bar.show_message(_('Sent news to') + ' ' + ', '.join(sent_mails), 3000)
568
569
570 # }}}
571
572 if __name__ == '__main__':
573 from qt.core import QApplication
574
575 app = QApplication([]) # noqa: F841
576 print(select_recipients())