org-gnus.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. ;;; org-gnus.el --- Support for links to Gnus groups and messages from within Org-mode
  2. ;; Copyright (C) 2004-2016 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-mode.
  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. (org-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. When nil, Gnus will be used for such links.
  40. Using a prefix arg 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-nnimap-query-article-no-from-file nil
  45. "If non-nil, `org-gnus-follow-link' will try to translate
  46. Message-Ids to article numbers by querying the .overview file.
  47. Normally, this translation is done by querying the IMAP server,
  48. which is usually very fast. Unfortunately, some (maybe badly
  49. configured) IMAP servers don't support this operation quickly.
  50. So if following a link to a Gnus article takes ages, try setting
  51. this variable to t."
  52. :group 'org-link-store
  53. :version "24.1"
  54. :type 'boolean)
  55. (defcustom org-gnus-no-server nil
  56. "Should Gnus be started using `gnus-no-server'?"
  57. :group 'org-gnus
  58. :version "24.4"
  59. :package-version '(Org . "8.0")
  60. :type 'boolean)
  61. ;; Install the link type
  62. (org-add-link-type "gnus" 'org-gnus-open)
  63. (add-hook 'org-store-link-functions 'org-gnus-store-link)
  64. ;; Implementation
  65. (defun org-gnus-nnimap-cached-article-number (group server message-id)
  66. "Return cached article number (uid) of message in GROUP on SERVER.
  67. MESSAGE-ID is the message-id header field that identifies the
  68. message. If the uid is not cached, return nil."
  69. (with-temp-buffer
  70. (let ((nov (and (fboundp 'nnimap-group-overview-filename)
  71. ;; nnimap-group-overview-filename was removed from
  72. ;; Gnus in September 2010, and therefore should
  73. ;; only be present in Emacs 23.1.
  74. (nnimap-group-overview-filename group server))))
  75. (when (and nov (file-exists-p nov))
  76. (mm-insert-file-contents nov)
  77. (set-buffer-modified-p nil)
  78. (goto-char (point-min))
  79. (catch 'found
  80. (while (search-forward message-id nil t)
  81. (let ((hdr (split-string (thing-at-point 'line) "\t")))
  82. (if (string= (nth 4 hdr) message-id)
  83. (throw 'found (nth 0 hdr))))))))))
  84. (defun org-gnus-group-link (group)
  85. "Create a link to the Gnus group GROUP.
  86. If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
  87. non-nil, create a link to groups.google.com or gmane.org.
  88. Otherwise create a link to the group 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. (let ((unprefixed-group (replace-regexp-in-string "^[^:]+:" "" group)))
  92. (if (and (string-match "^nntp" group) ;; Only for nntp groups
  93. (org-xor current-prefix-arg
  94. org-gnus-prefer-web-links))
  95. (concat (if (string-match "gmane" unprefixed-group)
  96. "http://news.gmane.org/"
  97. "http://groups.google.com/group/")
  98. unprefixed-group)
  99. (concat "gnus:" group))))
  100. (defun org-gnus-article-link (group newsgroups message-id x-no-archive)
  101. "Create a link to a Gnus article.
  102. The article is specified by its MESSAGE-ID. Additional
  103. parameters are the Gnus GROUP, the NEWSGROUPS the article was
  104. posted to and the X-NO-ARCHIVE header value of that article.
  105. If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
  106. non-nil, create a link to groups.google.com or gmane.org.
  107. Otherwise create a link to the article inside Gnus.
  108. If `org-store-link' was called with a prefix arg the meaning of
  109. `org-gnus-prefer-web-links' is reversed."
  110. (if (and (org-xor current-prefix-arg org-gnus-prefer-web-links)
  111. newsgroups ;; Make web links only for nntp groups
  112. (not x-no-archive)) ;; and if X-No-Archive isn't set.
  113. (format (if (string-match "gmane\\." newsgroups)
  114. "http://mid.gmane.org/%s"
  115. "http://groups.google.com/groups/search?as_umsgid=%s")
  116. (org-fixup-message-id-for-http message-id))
  117. (concat "gnus:" group "#" message-id)))
  118. (defun org-gnus-store-link ()
  119. "Store a link to a Gnus folder or message."
  120. (cond
  121. ((eq major-mode 'gnus-group-mode)
  122. (let* ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  123. (gnus-group-group-name)) ; version
  124. ((fboundp 'gnus-group-name)
  125. (gnus-group-name))
  126. (t "???")))
  127. desc link)
  128. (when group
  129. (org-store-link-props :type "gnus" :group group)
  130. (setq desc (org-gnus-group-link group)
  131. link desc)
  132. (org-add-link-props :link link :description desc)
  133. link)))
  134. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  135. (let* ((group gnus-newsgroup-name)
  136. (header (with-current-buffer gnus-summary-buffer
  137. (gnus-summary-article-header)))
  138. (from (mail-header-from header))
  139. (message-id (org-remove-angle-brackets (mail-header-id header)))
  140. (date (org-trim (mail-header-date header)))
  141. (date-ts (and date
  142. (ignore-errors
  143. (format-time-string
  144. (org-time-stamp-format t)
  145. (date-to-time date)))))
  146. (date-ts-ia (and date
  147. (ignore-errors
  148. (format-time-string
  149. (org-time-stamp-format t t)
  150. (date-to-time date)))))
  151. (subject (copy-sequence (mail-header-subject header)))
  152. (to (cdr (assq 'To (mail-header-extra header))))
  153. newsgroups x-no-archive desc link)
  154. (when (eq (car (gnus-find-method-for-group gnus-newsgroup-name))
  155. 'nnvirtual)
  156. (setq group (car (nnvirtual-map-article
  157. (gnus-summary-article-number)))))
  158. ;; Remove text properties of subject string to avoid Emacs bug
  159. ;; #3506
  160. (set-text-properties 0 (length subject) nil subject)
  161. ;; Fetching an article is an expensive operation; newsgroup and
  162. ;; x-no-archive are only needed for web links.
  163. (when (org-xor current-prefix-arg org-gnus-prefer-web-links)
  164. ;; Make sure the original article buffer is up-to-date
  165. (save-window-excursion (gnus-summary-select-article))
  166. (setq to (or to (gnus-fetch-original-field "To"))
  167. newsgroups (gnus-fetch-original-field "Newsgroups")
  168. x-no-archive (gnus-fetch-original-field "x-no-archive")))
  169. (org-store-link-props :type "gnus" :from from :subject subject
  170. :message-id message-id :group group :to to)
  171. (when date
  172. (org-add-link-props :date date :date-timestamp date-ts
  173. :date-timestamp-inactive date-ts-ia))
  174. (setq desc (org-email-link-description)
  175. link (org-gnus-article-link
  176. group newsgroups message-id x-no-archive))
  177. (org-add-link-props :link link :description desc)
  178. link))
  179. ((eq major-mode 'message-mode)
  180. (setq org-store-link-plist nil) ; reset
  181. (save-excursion
  182. (save-restriction
  183. (message-narrow-to-headers)
  184. (and (not (message-fetch-field "Message-ID"))
  185. (message-generate-headers '(Message-ID)))
  186. (goto-char (point-min))
  187. (re-search-forward "^Message-ID: *.*$" nil t)
  188. (put-text-property (match-beginning 0) (match-end 0) 'message-deletable nil)
  189. (let ((gcc (car (last
  190. (message-unquote-tokens
  191. (message-tokenize-header (mail-fetch-field "gcc" nil t) " ,")))))
  192. (id (org-remove-angle-brackets (mail-fetch-field "Message-ID")))
  193. (to (mail-fetch-field "To"))
  194. (from (mail-fetch-field "From"))
  195. (subject (mail-fetch-field "Subject"))
  196. desc link
  197. newsgroup xarchive) ; those are always nil for gcc
  198. (and (not gcc)
  199. (error "Can not create link: No Gcc header found"))
  200. (org-store-link-props :type "gnus" :from from :subject subject
  201. :message-id id :group gcc :to to)
  202. (setq desc (org-email-link-description)
  203. link (org-gnus-article-link
  204. gcc newsgroup id xarchive))
  205. (org-add-link-props :link link :description desc)
  206. link))))))
  207. (defun org-gnus-open-nntp (path)
  208. "Follow the nntp: link specified by PATH."
  209. (let* ((spec (split-string path "/"))
  210. (server (split-string (nth 2 spec) "@"))
  211. (group (nth 3 spec))
  212. (article (nth 4 spec)))
  213. (org-gnus-follow-link
  214. (format "nntp+%s:%s" (or (cdr server) (car server)) group)
  215. article)))
  216. (defun org-gnus-open (path)
  217. "Follow the Gnus message or folder link specified by PATH."
  218. (let (group article)
  219. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  220. (error "Error in Gnus link"))
  221. (setq group (match-string 1 path)
  222. article (match-string 3 path))
  223. (when group
  224. (setq group (org-no-properties group)))
  225. (when article
  226. (setq article (org-no-properties article)))
  227. (org-gnus-follow-link group article)))
  228. (defun org-gnus-follow-link (&optional group article)
  229. "Follow a Gnus link to GROUP and ARTICLE."
  230. (require 'gnus)
  231. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  232. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  233. (setq group (org-no-properties group))
  234. (setq article (org-no-properties article))
  235. (cond ((and group article)
  236. (gnus-activate-group group)
  237. (condition-case nil
  238. (let* ((method (gnus-find-method-for-group group))
  239. (backend (car method))
  240. (server (cadr method)))
  241. (cond
  242. ((eq backend 'nndoc)
  243. (if (gnus-group-read-group t nil group)
  244. (gnus-summary-goto-article article nil t)
  245. (message "Couldn't follow gnus link. %s"
  246. "The summary couldn't be opened.")))
  247. (t
  248. (let ((articles 1)
  249. group-opened)
  250. (when (and (eq backend 'nnimap)
  251. org-gnus-nnimap-query-article-no-from-file)
  252. (setq article
  253. (or (org-gnus-nnimap-cached-article-number
  254. (nth 1 (split-string group ":"))
  255. server (concat "<" article ">")) article)))
  256. (while (and (not group-opened)
  257. ;; stop on integer overflows
  258. (> articles 0))
  259. (setq group-opened (gnus-group-read-group
  260. articles t group)
  261. articles (if (< articles 16)
  262. (1+ articles)
  263. (* articles 2))))
  264. (if group-opened
  265. (gnus-summary-goto-article article nil t)
  266. (message "Couldn't follow gnus link. %s"
  267. "The summary couldn't be opened."))))))
  268. (quit (message "Couldn't follow gnus link. %s"
  269. "The linked group is empty."))))
  270. (group (gnus-group-jump-to-group group))))
  271. (defun org-gnus-no-new-news ()
  272. "Like `\\[gnus]' but doesn't check for new news."
  273. (if (not (gnus-alive-p)) (if org-gnus-no-server (gnus-no-server) (gnus))))
  274. (provide 'org-gnus)
  275. ;;; org-gnus.el ends here