ol-gnus.el 10 KB

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