org-gnus.el 9.1 KB

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