org-gnus.el 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. (car (nnvirtual-map-article (gnus-summary-article-number))))
  100. (`(nnir . ,_)
  101. (nnir-article-group (gnus-summary-article-number)))
  102. (_ gnus-newsgroup-name)))
  103. (header (with-current-buffer gnus-summary-buffer
  104. (gnus-summary-article-header)))
  105. (from (mail-header-from header))
  106. (message-id (org-unbracket-string "<" ">" (mail-header-id header)))
  107. (date (org-trim (mail-header-date header)))
  108. ;; Remove text properties of subject string to avoid Emacs
  109. ;; bug #3506.
  110. (subject (org-no-properties
  111. (copy-sequence (mail-header-subject header))))
  112. (to (cdr (assq 'To (mail-header-extra header))))
  113. newsgroups x-no-archive)
  114. ;; Fetching an article is an expensive operation; newsgroup and
  115. ;; x-no-archive are only needed for web links.
  116. (when (org-xor current-prefix-arg org-gnus-prefer-web-links)
  117. ;; Make sure the original article buffer is up-to-date.
  118. (save-window-excursion (gnus-summary-select-article))
  119. (setq to (or to (gnus-fetch-original-field "To")))
  120. (setq newsgroups (gnus-fetch-original-field "Newsgroups"))
  121. (setq x-no-archive (gnus-fetch-original-field "x-no-archive")))
  122. (org-store-link-props :type "gnus" :from from :date date :subject subject
  123. :message-id message-id :group group :to to)
  124. (let ((link (org-gnus-article-link
  125. group newsgroups message-id x-no-archive))
  126. (description (org-email-link-description)))
  127. (org-add-link-props :link link :description description)
  128. link)))
  129. (`message-mode
  130. (setq org-store-link-plist nil) ;reset
  131. (save-excursion
  132. (save-restriction
  133. (message-narrow-to-headers)
  134. (unless (message-fetch-field "Message-ID")
  135. (message-generate-headers '(Message-ID)))
  136. (goto-char (point-min))
  137. (re-search-forward "^Message-ID:" nil t)
  138. (put-text-property (line-beginning-position) (line-end-position)
  139. 'message-deletable nil)
  140. (let ((gcc (org-last (message-unquote-tokens
  141. (message-tokenize-header
  142. (mail-fetch-field "gcc" nil t) " ,"))))
  143. (id (org-unbracket-string "<" ">"
  144. (mail-fetch-field "Message-ID")))
  145. (to (mail-fetch-field "To"))
  146. (from (mail-fetch-field "From"))
  147. (subject (mail-fetch-field "Subject"))
  148. newsgroup xarchive) ;those are always nil for gcc
  149. (unless gcc (error "Can not create link: No Gcc header found"))
  150. (org-store-link-props :type "gnus" :from from :subject subject
  151. :message-id id :group gcc :to to)
  152. (let ((link (org-gnus-article-link gcc newsgroup id xarchive))
  153. (description (org-email-link-description)))
  154. (org-add-link-props :link link :description description)
  155. link)))))))
  156. (defun org-gnus-open-nntp (path)
  157. "Follow the nntp: link specified by PATH."
  158. (let* ((spec (split-string path "/"))
  159. (server (split-string (nth 2 spec) "@"))
  160. (group (nth 3 spec))
  161. (article (nth 4 spec)))
  162. (org-gnus-follow-link
  163. (format "nntp+%s:%s" (or (cdr server) (car server)) group)
  164. article)))
  165. (defun org-gnus-open (path)
  166. "Follow the Gnus message or folder link specified by PATH."
  167. (unless (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path)
  168. (error "Error in Gnus link %S" path))
  169. (let ((group (match-string-no-properties 1 path))
  170. (article (match-string-no-properties 3 path)))
  171. (org-gnus-follow-link group article)))
  172. (defun org-gnus-follow-link (&optional group article)
  173. "Follow a Gnus link to GROUP and ARTICLE."
  174. (require 'gnus)
  175. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  176. (when gnus-other-frame-object (select-frame gnus-other-frame-object))
  177. (let ((group (org-no-properties group))
  178. (article (org-no-properties article)))
  179. (cond
  180. ((and group article)
  181. (gnus-activate-group group)
  182. (condition-case nil
  183. (let ((msg "Couldn't follow Gnus link. Summary couldn't be opened."))
  184. (pcase (gnus-find-method-for-group group)
  185. (`(nndoc . ,_)
  186. (if (gnus-group-read-group t nil group)
  187. (gnus-summary-goto-article article nil t)
  188. (message msg)))
  189. (_
  190. (let ((articles 1)
  191. group-opened)
  192. (while (and (not group-opened)
  193. ;; Stop on integer overflows.
  194. (> articles 0))
  195. (setq group-opened (gnus-group-read-group articles t group))
  196. (setq articles (if (< articles 16)
  197. (1+ articles)
  198. (* articles 2))))
  199. (if group-opened
  200. (gnus-summary-goto-article article nil t)
  201. (message msg))))))
  202. (quit
  203. (message "Couldn't follow Gnus link. The linked group is empty."))))
  204. (group (gnus-group-jump-to-group group)))))
  205. (defun org-gnus-no-new-news ()
  206. "Like `\\[gnus]' but doesn't check for new news."
  207. (cond ((gnus-alive-p) nil)
  208. (org-gnus-no-server (gnus-no-server))
  209. (t (gnus))))
  210. (provide 'org-gnus)
  211. ;;; org-gnus.el ends here