ol-gnus.el 10 KB

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