ol-gnus.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. ;;; ol-gnus.el --- Links to Gnus Groups and Messages -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2004-2022 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten.dominik@gmail.com>
  4. ;; Tassilo Horn <tassilo at member dot fsf dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; URL: https://orgmode.org
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;; This file implements links to Gnus groups and messages from within Org.
  24. ;; Org mode loads this module by default - if this is not what you want,
  25. ;; configure the variable `org-modules'.
  26. ;;; Code:
  27. (require 'org-macs)
  28. (org-assert-version)
  29. (require 'gnus-sum)
  30. (require 'gnus-util)
  31. (require 'nnheader)
  32. (or (require 'nnselect nil t) ; Emacs >= 28
  33. (require 'nnir nil t)) ; Emacs < 28
  34. (require 'ol)
  35. ;;; Declare external functions and variables
  36. (declare-function gnus-activate-group "gnus-start" (group &optional scan dont-check method dont-sub-check))
  37. (declare-function gnus-find-method-for-group "gnus" (group &optional info))
  38. (declare-function gnus-article-show-summary "gnus-art" ())
  39. (declare-function gnus-group-group-name "gnus-group")
  40. (declare-function gnus-group-jump-to-group "gnus-group" (group &optional prompt))
  41. (declare-function gnus-group-read-group "gnus-group" (&optional all no-article group select-articles))
  42. (declare-function message-fetch-field "message" (header &optional not-all))
  43. (declare-function message-generate-headers "message" (headers))
  44. (declare-function message-narrow-to-headers "message")
  45. (declare-function message-tokenize-header "message" (header &optional separator))
  46. (declare-function message-unquote-tokens "message" (elems))
  47. (declare-function nnvirtual-map-article "nnvirtual" (article))
  48. (defvar gnus-newsgroup-name)
  49. (defvar gnus-summary-buffer)
  50. (defvar gnus-other-frame-object)
  51. ;;; Customization variables
  52. (defcustom org-gnus-prefer-web-links nil
  53. "If non-nil, `org-store-link' creates web links to Google groups.
  54. \\<org-mode-map>When nil, Gnus will be used for such links.
  55. Using a prefix argument to the command `\\[org-store-link]' (`org-store-link')
  56. negates this setting for the duration of the command."
  57. :group 'org-link-store
  58. :type 'boolean)
  59. (defcustom org-gnus-no-server nil
  60. "Should Gnus be started using `gnus-no-server'?"
  61. :group 'org-gnus
  62. :version "24.4"
  63. :package-version '(Org . "8.0")
  64. :type 'boolean)
  65. ;;; Install the link type
  66. (org-link-set-parameters "gnus"
  67. :follow #'org-gnus-open
  68. :store #'org-gnus-store-link)
  69. ;;; Implementation
  70. (defun org-gnus-group-link (group)
  71. "Create a link to the Gnus group GROUP.
  72. If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
  73. non-nil, create a link to groups.google.com. Otherwise create a
  74. link to the group inside Gnus.
  75. If `org-store-link' was called with a prefix arg the meaning of
  76. `org-gnus-prefer-web-links' is reversed."
  77. (let ((unprefixed-group (replace-regexp-in-string "^[^:]+:" "" group)))
  78. (if (and (string-prefix-p "nntp" group) ;; Only for nntp groups
  79. (org-xor current-prefix-arg
  80. org-gnus-prefer-web-links))
  81. (concat "https://groups.google.com/group/" unprefixed-group)
  82. (concat "gnus:" group))))
  83. (defun org-gnus-article-link (group newsgroups message-id x-no-archive)
  84. "Create a link to a Gnus article.
  85. The article is specified by its MESSAGE-ID. Additional
  86. parameters are the Gnus GROUP, the NEWSGROUPS the article was
  87. posted to and the X-NO-ARCHIVE header value of that article.
  88. If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
  89. non-nil, create a link to groups.google.com.
  90. Otherwise create a link to the article inside Gnus.
  91. If `org-store-link' was called with a prefix arg the meaning of
  92. `org-gnus-prefer-web-links' is reversed."
  93. (if (and (org-xor current-prefix-arg org-gnus-prefer-web-links)
  94. newsgroups ;make web links only for nntp groups
  95. (not x-no-archive)) ;and if X-No-Archive isn't set
  96. (format "https://groups.google.com/groups/search?as_umsgid=%s"
  97. (url-encode-url message-id))
  98. (concat "gnus:" group "#" message-id)))
  99. (defun org-gnus-store-link ()
  100. "Store a link to a Gnus folder or message."
  101. (pcase major-mode
  102. (`gnus-group-mode
  103. (let ((group (gnus-group-group-name)))
  104. (when group
  105. (org-link-store-props :type "gnus" :group group)
  106. (let ((description (org-gnus-group-link group)))
  107. (org-link-add-props :link description :description description)
  108. description))))
  109. ((or `gnus-summary-mode `gnus-article-mode)
  110. (let* ((group
  111. (pcase (gnus-find-method-for-group gnus-newsgroup-name)
  112. (`(nnvirtual . ,_)
  113. (save-excursion
  114. (car (nnvirtual-map-article (gnus-summary-article-number)))))
  115. (`(,(or `nnselect `nnir) . ,_) ; nnir is for Emacs < 28.
  116. (save-excursion
  117. (cond
  118. ((fboundp 'nnselect-article-group)
  119. (nnselect-article-group (gnus-summary-article-number)))
  120. ((fboundp 'nnir-article-group)
  121. (nnir-article-group (gnus-summary-article-number)))
  122. (t
  123. (error "No article-group variant bound")))))
  124. (_ gnus-newsgroup-name)))
  125. (header (if (eq major-mode 'gnus-article-mode)
  126. ;; When in an article, first move to summary
  127. ;; buffer, with point on the summary of the
  128. ;; current article before extracting headers.
  129. (save-window-excursion
  130. (save-excursion
  131. (gnus-article-show-summary)
  132. (gnus-summary-article-header)))
  133. (gnus-summary-article-header)))
  134. (from (mail-header-from header))
  135. (message-id (org-unbracket-string "<" ">" (mail-header-id header)))
  136. (date (org-trim (mail-header-date header)))
  137. ;; Remove text properties of subject string to avoid Emacs
  138. ;; bug #3506.
  139. (subject (org-no-properties
  140. (copy-sequence (mail-header-subject header))))
  141. (to (cdr (assq 'To (mail-header-extra header))))
  142. newsgroups x-no-archive)
  143. ;; Fetching an article is an expensive operation; newsgroup and
  144. ;; x-no-archive are only needed for web links.
  145. (when (org-xor current-prefix-arg org-gnus-prefer-web-links)
  146. ;; Make sure the original article buffer is up-to-date.
  147. (save-window-excursion (gnus-summary-select-article))
  148. (setq to (or to (gnus-fetch-original-field "To")))
  149. (setq newsgroups (gnus-fetch-original-field "Newsgroups"))
  150. (setq x-no-archive (gnus-fetch-original-field "x-no-archive")))
  151. (org-link-store-props :type "gnus" :from from :date date :subject subject
  152. :message-id message-id :group group :to to)
  153. (let ((link (org-gnus-article-link
  154. group newsgroups message-id x-no-archive))
  155. (description (org-link-email-description)))
  156. (org-link-add-props :link link :description description)
  157. link)))
  158. (`message-mode
  159. (setq org-store-link-plist nil) ;reset
  160. (save-excursion
  161. (save-restriction
  162. (message-narrow-to-headers)
  163. (unless (message-fetch-field "Message-ID")
  164. (message-generate-headers '(Message-ID)))
  165. (goto-char (point-min))
  166. (re-search-forward "^Message-ID:" nil t)
  167. (put-text-property (line-beginning-position) (line-end-position)
  168. 'message-deletable nil)
  169. (let ((gcc (org-last (message-unquote-tokens
  170. (message-tokenize-header
  171. (mail-fetch-field "gcc" nil t) " ,"))))
  172. (id (org-unbracket-string "<" ">"
  173. (mail-fetch-field "Message-ID")))
  174. (to (mail-fetch-field "To"))
  175. (from (mail-fetch-field "From"))
  176. (subject (mail-fetch-field "Subject"))
  177. ) ;; newsgroup xarchive ;those are always nil for gcc
  178. (unless gcc (error "Can not create link: No Gcc header found"))
  179. (org-link-store-props :type "gnus" :from from :subject subject
  180. :message-id id :group gcc :to to)
  181. (let ((link (org-gnus-article-link gcc nil id nil)) ;;newsgroup xarchive
  182. (description (org-link-email-description)))
  183. (org-link-add-props :link link :description description)
  184. link)))))))
  185. (defun org-gnus-open-nntp (path)
  186. "Follow the nntp: link specified by PATH."
  187. (let* ((spec (split-string path "/"))
  188. (server (split-string (nth 2 spec) "@"))
  189. (group (nth 3 spec))
  190. (article (nth 4 spec)))
  191. (org-gnus-follow-link
  192. (format "nntp+%s:%s" (or (cdr server) (car server)) group)
  193. article)))
  194. (defun org-gnus-open (path _)
  195. "Follow the Gnus message or folder link specified by PATH."
  196. (unless (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path)
  197. (error "Error in Gnus link %S" path))
  198. (let ((group (match-string-no-properties 1 path))
  199. (article (match-string-no-properties 3 path)))
  200. (org-gnus-follow-link group article)))
  201. (defun org-gnus-follow-link (&optional group article)
  202. "Follow a Gnus link to GROUP and ARTICLE."
  203. (require 'gnus)
  204. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  205. (when gnus-other-frame-object (select-frame gnus-other-frame-object))
  206. (let ((group (org-no-properties group))
  207. (article (org-no-properties article)))
  208. (cond
  209. ((and group article)
  210. (gnus-activate-group group)
  211. (condition-case nil
  212. (let ((msg "Couldn't follow Gnus link. Summary couldn't be opened."))
  213. (pcase (gnus-find-method-for-group group)
  214. (`(nndoc . ,_)
  215. (if (gnus-group-read-group t nil group)
  216. (gnus-summary-goto-article article nil t)
  217. (message msg)))
  218. (_
  219. (let ((articles 1)
  220. group-opened)
  221. (while (and (not group-opened)
  222. ;; Stop on integer overflows. Note: We
  223. ;; can drop this once we require at least
  224. ;; Emacs 27, which supports bignums.
  225. (> articles 0))
  226. (setq group-opened (gnus-group-read-group articles t group))
  227. (setq articles (if (< articles 16)
  228. (1+ articles)
  229. (* articles 2))))
  230. (if group-opened
  231. (gnus-summary-goto-article article nil t)
  232. (message msg))))))
  233. (quit
  234. (message "Couldn't follow Gnus link. The linked group is empty."))))
  235. (group (gnus-group-jump-to-group group)))))
  236. (defun org-gnus-no-new-news ()
  237. "Like `\\[gnus]' but doesn't check for new news."
  238. (cond ((gnus-alive-p) nil)
  239. (org-gnus-no-server (gnus-no-server))
  240. (t (gnus))))
  241. (provide 'ol-gnus)
  242. ;;; ol-gnus.el ends here