org-gnus.el 9.7 KB

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