feedsnake.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. ;;
  2. ;; Copyright 2022, Jaidyn Levesque <jadedctrl@posteo.at>
  3. ;;
  4. ;; This program is free software: you can redistribute it and/or
  5. ;; modify it under the terms of the GNU General Public License as
  6. ;; published by the Free Software Foundation, either version 3 of
  7. ;; the License, or (at your option) any later version.
  8. ;;
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. ;;
  17. (load "date-strings.scm")
  18. (load "named-format.scm")
  19. ;; Misc helper functions used in both feedsnake and feedsnake-unix
  20. (module feedsnake-helpers
  21. (alist-car-ref)
  22. (import scheme
  23. (chicken base))
  24. ;; Just car's the value of alist-ref (if it exists)
  25. (define (alist-car-ref key alist)
  26. (let ([value (alist-ref key alist)])
  27. (if value
  28. (car value)
  29. #f))))
  30. ;; The main feedsnake module; parses atom feeds into alists and strings
  31. (module feedsnake
  32. (updated-feed-string read-feed entries-since entry->string)
  33. (import scheme
  34. (chicken base) (chicken condition) (chicken io) (chicken pathname) (chicken port)
  35. srfi-1 srfi-13 srfi-19 srfi-69
  36. date-strings
  37. feedsnake-helpers
  38. http-client
  39. named-format
  40. atom rss)
  41. ;; Read the given port into a feedsnake-feed (alist), no matter the format! c:<
  42. (define (read-feed in-port)
  43. (let (;;[rss (rss:read in-port)]
  44. [atom (read-atom-feed in-port)])
  45. (if atom
  46. (atom-doc->feedsnake-feed atom)
  47. #f)))
  48. ;; A list of entries updated since the given date
  49. (define (entries-since feed date-utc)
  50. (let ([entry-date (lambda (entry) (car (alist-ref 'updated entry)))]
  51. [since-entries '()])
  52. (map
  53. (lambda (entry)
  54. (if (date>=? (entry-date entry) date-utc)
  55. (set! since-entries
  56. (append since-entries (list entry)))))
  57. (car (alist-ref 'entries feed)))
  58. since-entries))
  59. ;; Returns either the updated string of a feed (in comparison to old string),
  60. ;; or #f if literally nothing's changed
  61. (define (updated-feed-string url old-string)
  62. (let* ([new-string (fetch-feed-string url)]
  63. [updated? (not (eq? (hash old-string) (hash new-string)))])
  64. (if updated?
  65. new-string
  66. #f)))
  67. ;; Download a feed (AKA fetch over HTTP to a string)
  68. (define (fetch-feed-string url)
  69. (call-with-output-string
  70. (lambda (out) (fetch-http url out))))
  71. (define (entry->string entry template)
  72. (named-format
  73. template
  74. (append entry
  75. (entry-templating-parameters entry template))))
  76. ;; Returns an alist of string replacements/parameters for a given entry
  77. ;; For use with named-format
  78. (define (entry-templating-parameters entry template)
  79. (append
  80. entry
  81. (entry-url-templating-parameters entry)
  82. (entry-author-templating-parameters entry)
  83. (entry-date-templating-parameters entry)))
  84. ;; URL-related named-format templating parameters for given entry
  85. (define (entry-url-templating-parameters entry)
  86. (let ([urls (alist-car-ref 'urls entry)])
  87. `((url ,(cond
  88. [(list? urls) (car urls)]
  89. [(string? urls) urls])))))
  90. ;; Author-related named-format templating parameters for given entry
  91. (define (entry-author-templating-parameters entry)
  92. (let* ([authors (alist-car-ref 'authors entry)]
  93. [author (if authors (car authors) (alist-car-ref 'feed-title entry))])
  94. `((author ,author))))
  95. ;; Date-related named-format templating parameters for given entry
  96. (define (entry-date-templating-parameters entry)
  97. (let* ([updated (or (alist-car-ref 'updated entry) (alist-car-ref 'published entry))]
  98. [published (or (alist-car-ref 'published entry) updated)])
  99. `((updated-rfc228 ,(if updated (date->rfc228-string updated)))
  100. (published-rfc228 ,(if published (date->rfc228-string published)))
  101. (updated-mbox ,(if updated (date->mbox-string updated)))
  102. (published-mbox ,(if published (date->mbox-string published))))))
  103. ;; Parse an atom feed into a feedsnake-friendly alist
  104. (define (atom-doc->feedsnake-feed atom)
  105. `((title ,(last (feed-title atom)))
  106. (url ,(atom-feed-preferred-url atom))
  107. (authors ,(map author-name (feed-authors atom)))
  108. (updated ,(feed-updated atom))
  109. (entry-updated ,(atom-feed-latest-entry-date atom))
  110. (entries ,(map
  111. (lambda (entry)
  112. (atom-entry->feedsnake-entry entry atom))
  113. (feed-entries atom)))))
  114. ;; Parse an atom entry into a feedsnake entry :>
  115. (define (atom-entry->feedsnake-entry entry atom)
  116. (let ([published (rfc339-string->date (entry-published entry))]
  117. [updated (rfc339-string->date (entry-updated entry))]
  118. [feed-authors (map author-name (feed-authors atom))]
  119. [entry-authors (map author-name (entry-authors entry))])
  120. `((title ,(last (entry-title entry)))
  121. (updated ,(or updated published))
  122. (published ,(or published updated))
  123. (summary ,(last (entry-summary entry)))
  124. (urls ,(map (lambda (link) (atom-link->string link atom))
  125. (entry-links entry)))
  126. (authors ,(if (null? entry-authors) feed-authors entry-authors))
  127. (feed-title ,(last (feed-title atom))))))
  128. ;; The preferred/given URL for an atom feed
  129. (define (atom-feed-preferred-url atom)
  130. (car
  131. (filter
  132. (lambda (link)
  133. (string=? (link-relation link) "self"))
  134. (feed-links atom))))
  135. ;; Get an atom feed's latest date for an entry's updating/publishing
  136. (define (atom-feed-latest-entry-date atom)
  137. (let ([entry-date
  138. (lambda (entry)
  139. (or (rfc339-string->date (entry-updated entry))
  140. (rfc339-string->date (entry-published entry))))])
  141. (reduce
  142. (lambda (a b)
  143. (if (date>=? a b) a b))
  144. #f
  145. (map entry-date (feed-entries atom)))))
  146. ;; Convert an atom-link into a proper, valid url
  147. (define (atom-link->string link atom)
  148. (if (string-contains (link-uri link) "://")
  149. (link-uri link)
  150. (string-append (pathname-directory (atom-feed-preferred-url atom))
  151. "/"
  152. (link-uri link))))
  153. ;; Download a file over HTTP to the given port.
  154. (define (fetch-http url out-port)
  155. (call-with-input-request
  156. url #f
  157. (lambda (in-port) (copy-port in-port out-port))))
  158. ) ;; feedsnake module
  159. ;; The UNIX-style frontend for feedsnake
  160. (module feedsnake-unix
  161. (main update-feed-file latest-entries all-entries write-entry write-entries entry-output-path feed-files *mbox-template* *maildir-template*)
  162. (import scheme
  163. (chicken base) (chicken condition) (chicken file) (chicken file posix)
  164. (chicken io) (chicken process-context) (chicken process-context posix)
  165. srfi-1 srfi-19
  166. date-strings
  167. feedsnake feedsnake-helpers
  168. getopt-long
  169. named-format
  170. xattr)
  171. (define *maildir-template*
  172. `((entry-template
  173. ,(string-append
  174. "From: ~{{~A ||||from-name}}"
  175. "<~{{~A||feedsnake||FROM_USER||author-user||feed-title}}"
  176. "@"
  177. "~{{~A||localhost||FROM_HOST||author-domain||feed-domain}}>"
  178. "\n"
  179. "To:~{{ ~A ||You||TO_NAME||USER}}"
  180. "<~{{~A||you||TO_USER||USER}}"
  181. "@"
  182. "~{{~A||localhost||TO_HOST||HOSTNAME}}>"
  183. "\n"
  184. "Subject: ~{{~A||Unnamed post||title}}\n"
  185. "Date: ~{{~A||||updated-rfc228||published-rfc228}}\n"
  186. "\n"
  187. "~{{~{~a~^, ~}~%***~%||||urls}}\n"
  188. "~{{~A||||summary}}\n"))
  189. (multifile-output? #t)))
  190. (define *mbox-template*
  191. `((entry-template ,(string-append
  192. "From FEEDSNAKE ~{{~A||||updated-mbox||published-mbox}}\n"
  193. (car (alist-ref 'entry-template *maildir-template*))
  194. "\n"))
  195. (multifile-output? #f)))
  196. (define *default-template*
  197. (append *maildir-template*
  198. '((output-dir "./"))))
  199. (define *default-values*
  200. '((output-dir "./")))
  201. (define *default-multifile-values*
  202. '((filename-template "~{{~A||||updated||published}}.~{{~A||you||USER}}@~{{~A||localhost|HOSTNAME}}")
  203. (multifile-output? #t)))
  204. (define *default-singlefile-values*
  205. '((filename-template "feed.out")
  206. (multifile-output? #f)))
  207. (define *help-msg*
  208. (string-append
  209. "usage: feedsnake [-h] FILE...\n"
  210. "Feedsnake is a program for converting Atom feeds into mbox/maildir files.\n"
  211. "Any Atom feeds passed as an argument will be output in mbox format.\n\n"))
  212. (define *opts*
  213. '((help
  214. "Print a usage message"
  215. (single-char #\h))))
  216. ;; (outdir
  217. ;; "Output directory, used for multi-file templates (e.g., maildir)"
  218. ;; (single-char #\d)
  219. ;; (value (required DIR)))
  220. ;; (output
  221. ;; "Output file, used for single-file templates (e.g., mbox). Defaults to stdout."
  222. ;; (single-char #\o)
  223. ;; (value (required FILE)))
  224. ;; (template
  225. ;; "Output template for feed ('mbox' or 'maildir'). Defaults to 'mbox'."
  226. ;; (single-char #\t)
  227. ;; (value (required TEMPLATE)))))
  228. ;; The `main` procedure that should be called to run feedsnake-unix for use as script.
  229. ;; TODO: accept piped-in feeds
  230. (define (main)
  231. (let ([args (getopt-long (command-line-arguments) *opts*)])
  232. (if (alist-ref 'help args)
  233. (help)
  234. (map (lambda (free-arg)
  235. (if (file-exists? free-arg)
  236. (map (lambda (entry)
  237. (write-entry entry *mbox-template* (open-output-file* fileno/stdout)))
  238. (all-entries free-arg))))
  239. (alist-ref '@ args)))))
  240. ;; Prints cli usage to stderr.
  241. (define (help)
  242. (write-string *help-msg* #f (open-output-file* fileno/stderr))
  243. (write-string (usage *opts*) #f (open-output-file* fileno/stderr)))
  244. ;; Writes a given feed entry to the out-port, as per the feedsnake-unix-format template alist
  245. (define (write-entry entry template-alist out-port)
  246. (write-string
  247. (entry->string (append (get-environment-variables) entry)
  248. (alist-car-ref 'entry-template template-alist))
  249. #f
  250. out-port))
  251. ;; Write an entry to the given file (directory for multifile; normal file otherwise)
  252. (define (write-entry-to-file entry template-alist out-path)
  253. (let* ([template (if (alist-car-ref 'multifile-output? template-alist)
  254. (append template-alist *default-multifile-values* *default-values*)
  255. (append template-alist *default-singlefile-values* *default-values*))]
  256. [file-mode (if (alist-car-ref 'multifile-output? template) #:text #:append)])
  257. (call-with-output-file
  258. (entry-output-path entry template out-path)
  259. (lambda (out-port)
  260. (write-entry entry template out-port))
  261. file-mode)))
  262. ;; Writes all entries in a list to an out-path (mere convenience function)
  263. (define (write-entries entries template-alist out-path)
  264. (map (lambda (entry)
  265. (write-entry entry template-alist out-path))
  266. entries))
  267. ;; Decides the correct output path for an entry, given the template's filename rules etc.
  268. (define (entry-output-path entry template-alist base-out-path)
  269. (let ([multifile? (alist-car-ref 'multifile-output? template-alist)])
  270. (if multifile?
  271. (multifile-entry-path entry template-alist base-out-path)
  272. (singlefile-entry-path entry template-alist base-out-path))))
  273. ;; Output path for entry with a single-file template
  274. (define (singlefile-entry-path entry template-alist base-out-path)
  275. (if (directory-exists? base-out-path)
  276. (signal
  277. (make-property-condition
  278. 'exn 'location 'file
  279. 'message (string-append base-out-path " shouldn't be a directory.")))
  280. base-out-path))
  281. ;; Output path for an entry w multifile template
  282. (define (multifile-entry-path entry template-alist base-out-path)
  283. (let* ([file-leaf (named-format (alist-car-ref 'filename-template template-alist) entry)])
  284. (if (create-directory base-out-path)
  285. (string-append base-out-path "/" file-leaf)
  286. (signal
  287. (make-property-condition
  288. 'exn 'location 'file
  289. 'message (string-append base-out-path " either isn't accessible or isn't a directory."))))))
  290. ;; Switch the cached version of the feed with a newer version, if available
  291. (define (update-feed-file feed-path)
  292. (let* ([old-string (call-with-input-file feed-path
  293. (lambda (in-port) (read-string #f in-port)))]
  294. [new-string (updated-feed-string
  295. (get-xattr feed-path "user.xdg.origin.url")
  296. old-string)])
  297. (if new-string
  298. (call-with-output-file feed-path
  299. (lambda (out) (write-string new-string #f out))))
  300. new-string))
  301. ;; List of entries updated/published since last feed parsing
  302. (define (latest-entries feed-path)
  303. (let* ([feed (call-with-input-file feed-path read-feed)]
  304. [xattr-last-update (get-xattr feed-path "user.feedsnake.parsed")]
  305. [last-update (if xattr-last-update
  306. (rfc339-string->date xattr-last-update)
  307. (date->utc-date (make-date 0 0 0 0 01 01 1971)))])
  308. (set-xattr feed-path "user.feedsnake.parsed"
  309. (date->rfc339-string (current-date-utc)))
  310. (entries-since feed last-update)))
  311. ;; List of all entries of the feed
  312. (define (all-entries feed-path)
  313. (let ([feed (call-with-input-file feed-path read-feed)])
  314. (car (alist-ref 'entries feed))))
  315. ;; The user's presumed config root.
  316. (define (config-directory)
  317. (or (get-environment-variable "XDG_CONFIG_HOME")
  318. (string-append (sixth (user-information (current-user-id))) "/.config")))
  319. ;; Path of the feedsnake config directory
  320. (define (feedsnake-directory)
  321. (create-directory (string-append (config-directory) "/feedsnake") #t))
  322. ;; Path of the feeds directory
  323. (define (feeds-directory)
  324. (create-directory (string-append (feedsnake-directory) "/feeds") #t))
  325. ;; Lists all configured feeds (files in feed directory)
  326. (define (feed-files)
  327. (map (lambda (relative-path)
  328. (string-append (feeds-directory) "/" relative-path))
  329. (directory (feeds-directory))))
  330. ;; Convert a date of arbitrary timezone to UTC
  331. (define (date->utc-date date)
  332. (time-utc->date (date->time-utc date)))
  333. ;; The current date, with UTC (-0; Z) timezone
  334. (define (current-date-utc)
  335. (date->utc-date (current-date)))
  336. ) ;; feedsnake-unix module