-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewsboat-sendmail.py
executable file
·569 lines (450 loc) · 22.5 KB
/
newsboat-sendmail.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#!/usr/bin/env python3
#
# newsboat-sendmail.py by lenormf
# A companion script that sends unread RSS items in Newsboat through email
#
import os
import sys
import fcntl
import shlex
import logging
import sqlite3
import pathlib
import datetime
import argparse
import subprocess
import configparser
import email.message
import bs4
import trafilatura
def str_to_bool(
value,
trueish_values=["yes", "1", "enabled", "true"],
falseish_values=["no", "0", "disabled", "false"],
):
if value in trueish_values:
return True
elif value in falseish_values:
return False
raise ValueError("Unable to infer true-ness of value: %s" % value)
class Defaults:
FILENAME_CFG = "sendmail.cfg"
CMD_SENDMAIL = "sendmail -F newsboat-sendmail -- {emails}"
TMPL_SUBJECT = "[RSS][{rss_feed_title}] {rss_item_title}"
CONTENT_TYPE = "html"
TMPL_BODY = """<!DOCTYPE html>
<html>
<body>
<ul>
<li>Feed: {rss_feed_title}</li>
<li>Title: {rss_item_title}</li>
<li>Author: {rss_item_author}</li>
<li>Date: {rss_item_date}</li>
<li>Link: {rss_item_link}</li>
</ul>
<p>{rss_item_content_reflinks}</p>
</body>
</html>"""
FMT_DATE = "%a %b %d %Y"
FULL_TEXT = False
class NewsboatError(Exception): pass
class NewsboatSendmailConfig(configparser.ConfigParser):
def __init__(self):
super().__init__(interpolation=None)
self["DEFAULT"]["sendmail_cmd"] = Defaults.CMD_SENDMAIL
self["DEFAULT"]["emails"] = ""
self["DEFAULT"]["subject_template"] = Defaults.TMPL_SUBJECT
self["DEFAULT"]["content_type"] = Defaults.CONTENT_TYPE
self["DEFAULT"]["body_template"] = Defaults.TMPL_BODY
self["DEFAULT"]["date_format"] = Defaults.FMT_DATE
# NOTE: Only string options are supported
self["DEFAULT"]["full_text"] = "yes" if Defaults.FULL_TEXT else "no"
def ReadFile(self, path):
if path not in self.read(path):
raise NewsboatError("unable to parse file: %s" % path)
class NewsboatConfig:
def __init__(self, path):
self._config = configparser.ConfigParser(
strict=False,
delimiters=(' ', '\t'),
comment_prefixes=('#'),
converters={
"qstring": NewsboatConfig.parse_qstring,
},
)
try:
with open(path, "r") as fin:
data = fin.read()
except OSError as e:
raise NewsboatError("unable to open configuration file: %s" % e)
try:
self._config.read_string("[DEFAULT]\n%s" % data)
except configparser.Error as e:
raise NewsboatError("unable to parse configuration file: %s" % e)
def __contains__(self, key):
return self._config.has_option("DEFAULT", key)
def __getitem__(self, key):
raw_value = self._config["DEFAULT"][key]
if raw_value.startswith('"'):
return self._config["DEFAULT"].getqstring(key)
else:
return raw_value
@staticmethod
def parse_qstring(s):
try:
# XXX: shouldn't happen, but if it ever does, at least the script won't wait for data on stdin
assert s is not None
return shlex.split(s)[0]
except ValueError:
raise NewsboatError("unable to parse value: %s" % s)
class NewsboatBase:
CMD_RELOAD = ["newsboat", "-x", "reload"]
FILENAME_CACHE = "cache.db"
def __init__(self, config):
self.config = config
home = os.getenv("HOME")
xdg_config_home = os.getenv("XDG_CONFIG_HOME")
xdg_data_home = os.getenv("XDG_DATA_HOME")
self.dir_config = xdg_config_home or os.path.join(home, ".config")
self.dir_data = xdg_data_home or os.path.join(home, ".local", "share")
self.dir_config = os.path.join(self.dir_config, "newsboat")
self.dir_data = os.path.join(self.dir_data, "newsboat")
if not os.path.isdir(self.dir_config):
logging.debug("the XDG configuration directory doesn't exit, rolling back to the HOME-based path")
self.dir_config = os.path.join(home, ".newsboat")
if not os.path.isdir(self.dir_data):
logging.debug("the XDG data directory doesn't exit, rolling back to the HOME-based path")
self.dir_data = os.path.join(home, ".newsboat")
self.path_cache = os.path.join(self.dir_data, NewsboatBase.FILENAME_CACHE)
path_newsboat_config = os.path.join(self.dir_config, "config")
if os.path.isfile(path_newsboat_config):
logging.info("loading the Newsboat configuration file")
newsboat_config = NewsboatConfig(path_newsboat_config)
if "cache-file" in newsboat_config:
self.path_cache = newsboat_config["cache-file"]
self.path_cache_lock = "%s.lock" % self.path_cache
logging.debug("configuration directory: %s", self.dir_config)
logging.debug("data directory: %s", self.dir_data)
logging.debug("path to the cache: %s", self.path_cache)
logging.debug("path to the cache lock file: %s", self.path_cache_lock)
def LoadConfig(self, path=None):
path_config = path or os.path.join(self.dir_config, Defaults.FILENAME_CFG)
if os.path.isfile(path_config):
logging.info("loading settings from file: %s", path_config)
self.config.ReadFile(path_config)
else:
logging.info("no configuration file, ignoring")
def Lock(self): pass
def Unlock(self): pass
def Update(self): pass
def Sendmail(self, callback_sendmail_command):
def flatten_string(s):
return ' '.join(s.split('\n'))
try:
logging.info("connecting to the database")
db = sqlite3.connect(self.path_cache)
db.row_factory = sqlite3.Row
except sqlite3.Error as e:
raise NewsboatError("Unable to connect to the database: %s" % e)
try:
logging.info("iterating over all feeds")
# FIXME: catch encoding errors
for rss_feed in db.execute("SELECT * FROM `rss_feed`"):
logging.info("iterating over all unread items in feed: %s", rss_feed["title"])
if self.config.has_section(rss_feed["url"]):
logging.debug("loading settings from section with url: %s", rss_feed["url"])
feed_config = self.config[rss_feed["url"]]
else:
logging.debug("loading defaults settings for the feed")
feed_config = self.config.defaults()
if not feed_config["sendmail_cmd"]:
raise NewsboatError("no sendmail command set")
elif feed_config["content_type"] not in ["html", "plain"]:
raise NewsboatError("invalid content type value")
try:
emails = shlex.split(feed_config["emails"])
if not emails:
raise NewsboatError("no email addresses set")
emails = shlex.join(emails)
except ValueError as e:
raise NewsboatError("unable to parse email addresses: %s" % e)
try:
sendmail_command = shlex.split(feed_config["sendmail_cmd"].format(emails=emails))
sendmail_command = shlex.join(sendmail_command)
except ValueError as e:
raise NewsboatError("unable to parse the sendmail command: %s" % e)
logging.debug("joined sendmail command: %s", sendmail_command)
context_feed = {
"rss_feed_title": flatten_string(rss_feed["title"]),
"rss_feed_alias": flatten_string(feed_config.get("alias", "")),
}
rss_nb_items = db.execute("SELECT COUNT(*) FROM `rss_item` WHERE `unread` = 1 AND `feedurl` = ?", (rss_feed["rssurl"],)).fetchone()[0]
logging.info("number of unread items in the feed: %d", rss_nb_items)
idx_item = 0
for rss_item in db.execute("SELECT * FROM `rss_item` WHERE `unread` = 1 AND `feedurl` = ? ORDER BY `pubDate` ASC", (rss_feed["rssurl"],)):
envelope = email.message.EmailMessage()
idx_item += 1
logging.info("handling item #%d", idx_item)
try:
rss_item_date = datetime.date.fromtimestamp(rss_item["pubDate"])
except OverflowError:
rss_item_date = datetime.date.today()
except OSError:
rss_item_date = datetime.date.fromtimestamp(0)
try:
rss_item_date = rss_item_date.strftime(feed_config["date_format"])
except ValueError as e:
raise NewsboatError("invalid time format: %s", e)
context_item = {
"rss_item_title": flatten_string(rss_item["title"]).strip(),
"rss_item_link": flatten_string(rss_item["url"]),
"rss_item_author": flatten_string(rss_item["author"]),
"rss_item_date": rss_item_date,
"rss_item_index": idx_item,
"rss_nb_items": rss_nb_items,
}
context_full = {**context_feed, **context_item}
subject = feed_config["subject_template"].format(**context_full).strip()
if not subject:
logging.warn("the email's subject is empty")
envelope["Subject"] = subject
rss_item_content = rss_item["content"]
rss_item_content_full_text = rss_item_content
if str_to_bool(feed_config["full_text"].lower()):
result = trafilatura.fetch_url(rss_item["url"])
if result is None:
logging.error("Unable to fetch URL")
rss_item_content_full_text = ""
else:
contents = None
try:
contents = trafilatura.extract(
result,
url=rss_item["url"],
favor_precision=True,
favor_recall=True,
output_format="xml",
include_formatting=True,
include_images=True,
include_links=True,
include_tables=True,
include_comments=False,
)
except:
logging.exception("Unable to extract page contents")
if contents is None:
logging.error("Unable to extract page contents")
rss_item_content_full_text = ""
else:
logging.debug("extracted full page contents: %s", contents)
rss_item_content_full_text = contents
content_type = feed_config["content_type"]
# TODO: class dedicated to handling a given content type
if content_type == "html":
try:
soup = bs4.BeautifulSoup(rss_item_content, features="html.parser")
# NOTE: BeautifulSoup doesn't do any parsing, so exceptions are implementation dependent - we consider any to be a fatal error
except:
logging.error("unable to parse HTML, skipping")
continue
rss_item_content_astext = soup.get_text(strip=True)
links = []
for n, a in enumerate(soup.find_all("a")):
# Ignore links with no text
link_text = a.string.strip() if a.string is not None else ""
if not a.string:
continue
# Just flatten tags with no link
if "href" not in a.attrs:
a.replace_with(link_text)
else:
link = a["href"]
if link.startswith("#"):
link = os.path.join(context_item["rss_item_link"], link)
links.append(link)
a.replace_with("{}[{}]".format(link_text, n))
if links:
ul = soup.new_tag("ul")
for n, link in enumerate(links):
li = soup.new_tag("li")
li.string = "[{}]: {}".format(n, link)
ul.insert(n, li)
soup.append(ul)
rss_item_content_reflinks = str(soup)
else:
rss_item_content_astext = rss_item_content
rss_item_content_reflinks = rss_item_content
# TODO: only pass HTML-related values when the content type is HTML?
contents = feed_config["body_template"].format(
**context_full,
rss_item_content=rss_item_content,
rss_item_content_astext=rss_item_content_astext,
rss_item_content_reflinks=rss_item_content_reflinks,
rss_item_content_full_text=rss_item_content_full_text,
)
if not contents:
logging.warn("the email's body is empty")
envelope.set_content(contents, subtype=content_type)
logging.debug("sendmail command: %s", sendmail_command)
logging.debug("email envelope: %s", envelope)
if callback_sendmail_command(sendmail_command, envelope):
try:
logging.info("marking item as read")
db.execute("UPDATE `rss_item` SET `unread` = 0 WHERE `id` = ?", (rss_item["id"],))
db.commit()
except sqlite3.Error as e:
raise NewsboatError("unable to mark item as read: %s" % e)
except sqlite3.Error as e:
raise NewsboatError("unable to read items: %s" % e)
logging.info("closing the connection to the database")
db.close()
def ShowFeedLinks(self):
try:
logging.info("connecting to the database")
db = sqlite3.connect(self.path_cache)
db.row_factory = sqlite3.Row
except sqlite3.Error as e:
raise NewsboatError("Unable to connect to the database: %s" % e)
try:
logging.info("iterating over all feeds")
for rss_feed in db.execute("SELECT `url` FROM `rss_feed`"):
print(rss_feed["url"])
except sqlite3.Error as e:
raise NewsboatError("unable to read items: %s" % e)
logging.info("closing the connection to the database")
db.close()
class Newsboat(NewsboatBase):
def __init__(self, config):
super().__init__(config)
self._lock_fd = None
def Lock(self):
p = pathlib.Path(self.path_cache_lock)
logging.info("locking the database cache")
try:
p.parent.mkdir(parents=True, exist_ok=True)
except (OSError, PermissionError) as e:
raise NewsboatError("unable to create cache lock file: %s" % e)
try:
self._lock_fd = os.open(p, os.O_WRONLY | os.O_CREAT, mode=0o600)
fcntl.lockf(self._lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except OSError as e:
raise NewsboatError("unable to acquire the lock-file, is Newsboat running? %s" % e)
# NOTE: assume that getting a PID and writing to the lock-file will go as expected
my_pid = "%d" % os.getpid()
os.write(self._lock_fd, my_pid.encode())
def Unlock(self):
logging.info("unlocking the database cache")
try:
fcntl.lockf(self._lock_fd, fcntl.LOCK_UN)
os.close(self._lock_fd)
os.unlink(self.path_cache_lock)
except OSError as e:
raise NewsboatError("unable to release the lock-file: %s" % e)
def Update(self):
logging.info("updating the database cache")
try:
subprocess.check_call(NewsboatBase.CMD_RELOAD)
except (OSError, subprocess.CalledProcessError) as e:
raise NewsboatError("Unable to update the database: %s" % e)
def Sendmail(self):
def callback_sendmail_command(sendmail_command, envelope):
try:
logging.info("sending email")
p = subprocess.Popen(sendmail_command, shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# FIXME: email might need to be flattened
# https://github.com/rss2email/rss2email/blob/3654c9c15d2feffa331816c37f225f020fbb5474/rss2email/email.py#L257
stdout, stderr = p.communicate(envelope.as_bytes())
exit_code = p.wait()
if stdout:
logging.debug("stdout: %s", stdout.decode("utf-8", "replace"))
if stderr:
logging.debug("stderr: %s", stderr.decode("utf-8", "replace"))
if exit_code:
raise NewsboatError("the sendmail command exited with error code: %d" % exit_code)
except (OSError, subprocess.CalledProcessError) as e:
raise NewsboatError("Unable to send email: %s" % e)
return True
super().Sendmail(callback_sendmail_command)
class NewsboatDryRun(NewsboatBase):
def __init__(self, config):
super().__init__(config)
def Lock(self):
print("write: %s" % self.path_cache_lock)
def Unlock(self):
print("unlink: %s" % self.path_cache_lock)
def Update(self):
print("exec: %s" % NewsboatBase.CMD_RELOAD)
def Sendmail(self):
def callback_sendmail_command(sendmail_command, envelope):
print("exec: %s" % sendmail_command)
return False
super().Sendmail(callback_sendmail_command)
class CliOptions(argparse.Namespace):
def __init__(self, args):
parser = argparse.ArgumentParser(description="Newsboat Sendmail - A companion script that sends unread RSS items in Newsboat through email")
parser.add_argument("-v", "--verbose", action="store_true", help="display information messages")
parser.add_argument("-d", "--debug", action="store_true", help="display debug messages")
parser.add_argument("-n", "--dry-run", action="store_true", help="do not modify the database, only print commands that would otherwise have been run")
parser.add_argument("-u", "--update", action="store_true", help="update the database before sending the emails")
parser.add_argument("-x", "--no-config", action="store_true", help="do not load any configuration file")
parser.add_argument("-s", "--show-rss-links", action="store_true", help="display all feed links computed by Newsboat, and quit")
parser.add_argument("-f", "--full-text", action="store_true", help="forcibly set the content of each item with the page it points to")
parser.add_argument("-C", "--config", help="path to the configuration file")
parser.add_argument("-S", "--sendmail-cmd", help="command using a sendmail-compatible tool to send emails")
parser.add_argument("-E", "--emails", help="list of email addresses to send the feed items to, shell-quoted")
parser.add_argument("-J", "--subject-template", help="template of the subject of outgoing emails")
parser.add_argument("-T", "--content-type", choices=["html", "plain"], help="send plain text emails, or HTML ones")
parser.add_argument("-B", "--body-template", help="template of the body of outgoing emails")
parser.parse_args(args, self)
def main(av):
cli_options = CliOptions(av[1:])
exit_code = 0
logging_level = logging.WARN
if cli_options.debug:
logging_level = logging.DEBUG
elif cli_options.verbose:
logging_level = logging.INFO
logging.basicConfig(level=logging_level,
format="[%(asctime)s][%(levelname)s]: %(message)s")
if cli_options.dry_run:
logging.info("dry-run mode enabled, emails will not be sent, the database will not be modified, and the commands that would otherwise modify it will be printed")
try:
newsboat_sendmail_config = NewsboatSendmailConfig()
newsboat = NewsboatDryRun(newsboat_sendmail_config) if cli_options.dry_run else Newsboat(newsboat_sendmail_config)
if cli_options.show_rss_links:
newsboat.ShowFeedLinks()
return exit_code
if not cli_options.no_config:
newsboat.LoadConfig(cli_options.config)
if cli_options.sendmail_cmd:
newsboat_sendmail_config["DEFAULT"]["sendmail_cmd"] = cli_options.sendmail_cmd
if cli_options.emails:
newsboat_sendmail_config["DEFAULT"]["emails"] = cli_options.emails
if cli_options.subject_template:
newsboat_sendmail_config["DEFAULT"]["subject_template"] = cli_options.subject_template
if cli_options.body_template:
newsboat_sendmail_config["DEFAULT"]["body_template"] = cli_options.body_template
if cli_options.full_text:
newsboat_sendmail_config["DEFAULT"]["full_text"] = "yes" if cli_options.full_text else "no"
if cli_options.update:
newsboat.Update()
newsboat.Lock()
try:
newsboat.Sendmail()
except NewsboatError as e:
logging.error("%s", e)
exit_code = 2
except KeyboardInterrupt:
logging.info("process interrupted, quitting")
newsboat.Unlock()
except NewsboatError as e:
logging.error("%s", e)
exit_code = 1
except KeyboardInterrupt:
logging.info("process interrupted, quitting")
return exit_code
if __name__ == "__main__":
sys.exit(main(sys.argv))