org-gnus.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. ;;; org-gnus.el --- Support for links to Gnus groups and messages from within Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; Tassilo Horn <tassilo at member dot fsf dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 7.5
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;; This file implements links to Gnus groups and messages from within Org-mode.
  26. ;; Org-mode loads this module by default - if this is not what you want,
  27. ;; configure the variable `org-modules'.
  28. ;;; Code:
  29. (require 'org)
  30. (eval-when-compile (require 'gnus-sum))
  31. ;; Declare external functions and variables
  32. (declare-function message-fetch-field "message" (header &optional not-all))
  33. (declare-function message-narrow-to-head-1 "message" nil)
  34. (declare-function nnimap-group-overview-filename "nnimap" (group server))
  35. ;; The following line suppresses a compiler warning stemming from gnus-sum.el
  36. (declare-function gnus-summary-last-subject "gnus-sum" nil)
  37. ;; Customization variables
  38. (when (fboundp 'defvaralias)
  39. (defvaralias 'org-usenet-links-prefer-google 'org-gnus-prefer-web-links))
  40. (defcustom org-gnus-prefer-web-links nil
  41. "If non-nil, `org-store-link' creates web links to Google groups or Gmane.
  42. When nil, Gnus will be used for such links.
  43. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  44. negates this setting for the duration of the command."
  45. :group 'org-link-store
  46. :type 'boolean)
  47. (defcustom org-gnus-nnimap-query-article-no-from-file nil
  48. "If non-nil, `org-gnus-follow-link' will try to translate
  49. Message-Ids to article numbers by querying the .overview file.
  50. Normally, this translation is done by querying the IMAP server,
  51. which is usually very fast. Unfortunately, some (maybe badly
  52. configured) IMAP servers don't support this operation quickly.
  53. So if following a link to a Gnus article takes ages, try setting
  54. this variable to `t'."
  55. :group 'org-link-store
  56. :type 'boolean)
  57. ;; Install the link type
  58. (org-add-link-type "gnus" 'org-gnus-open)
  59. (add-hook 'org-store-link-functions 'org-gnus-store-link)
  60. ;; Implementation
  61. (defun org-gnus-nnimap-cached-article-number (group server message-id)
  62. "Return cached article number (uid) of message in GROUP on SERVER.
  63. MESSAGE-ID is the message-id header field that identifies the
  64. message. If the uid is not cached, return nil."
  65. (with-temp-buffer
  66. (let ((nov (nnimap-group-overview-filename group server)))
  67. (when (file-exists-p nov)
  68. (mm-insert-file-contents nov)
  69. (set-buffer-modified-p nil)
  70. (goto-char (point-min))
  71. (catch 'found
  72. (while (search-forward message-id nil t)
  73. (let ((hdr (split-string (thing-at-point 'line) "\t")))
  74. (if (string= (nth 4 hdr) message-id)
  75. (throw 'found (nth 0 hdr))))))))))
  76. (defun org-gnus-group-link (group)
  77. "Create a link to the Gnus group GROUP.
  78. If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
  79. non-nil, create a link to groups.google.com or gmane.org.
  80. Otherwise create a link to the group inside Gnus.
  81. If `org-store-link' was called with a prefix arg the meaning of
  82. `org-gnus-prefer-web-links' is reversed."
  83. (let ((unprefixed-group (replace-regexp-in-string "^[^:]+:" "" group)))
  84. (if (and (string-match "^nntp" group) ;; Only for nntp groups
  85. (org-xor current-prefix-arg
  86. org-gnus-prefer-web-links))
  87. (org-make-link (if (string-match "gmane" unprefixed-group)
  88. "http://news.gmane.org/"
  89. "http://groups.google.com/group/")
  90. unprefixed-group)
  91. (org-make-link "gnus:" group))))
  92. (defun org-gnus-article-link (group newsgroups message-id x-no-archive)
  93. "Create a link to a Gnus article.
  94. The article is specified by its MESSAGE-ID. Additional
  95. parameters are the Gnus GROUP, the NEWSGROUPS the article was
  96. posted to and the X-NO-ARCHIVE header value of that article.
  97. If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
  98. non-nil, create a link to groups.google.com or gmane.org.
  99. Otherwise create a link to the article inside Gnus.
  100. If `org-store-link' was called with a prefix arg the meaning of
  101. `org-gnus-prefer-web-links' is reversed."
  102. (if (and (org-xor current-prefix-arg org-gnus-prefer-web-links)
  103. newsgroups ;; Make web links only for nntp groups
  104. (not x-no-archive)) ;; and if X-No-Archive isn't set.
  105. (format (if (string-match "gmane\\." newsgroups)
  106. "http://mid.gmane.org/%s"
  107. "http://groups.google.com/groups/search?as_umsgid=%s")
  108. (org-fixup-message-id-for-http message-id))
  109. (org-make-link "gnus:" group "#" message-id)))
  110. (defun org-gnus-store-link ()
  111. "Store a link to a Gnus folder or message."
  112. (cond
  113. ((eq major-mode 'gnus-group-mode)
  114. (let* ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  115. (gnus-group-group-name)) ; version
  116. ((fboundp 'gnus-group-name)
  117. (gnus-group-name))
  118. (t "???")))
  119. desc link)
  120. (when group
  121. (org-store-link-props :type "gnus" :group group)
  122. (setq desc (org-gnus-group-link group)
  123. link desc)
  124. (org-add-link-props :link link :description desc)
  125. link)))
  126. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  127. (let* ((group gnus-newsgroup-name)
  128. (header (with-current-buffer gnus-summary-buffer
  129. (gnus-summary-article-header)))
  130. (from (mail-header-from header))
  131. (message-id (org-remove-angle-brackets (mail-header-id header)))
  132. (date (org-trim (mail-header-date header)))
  133. (date-ts (and date
  134. (ignore-errors
  135. (format-time-string
  136. (org-time-stamp-format t)
  137. (date-to-time date)))))
  138. (date-ts-ia (and date
  139. (ignore-errors
  140. (format-time-string
  141. (org-time-stamp-format t t)
  142. (date-to-time date)))))
  143. (subject (copy-sequence (mail-header-subject header)))
  144. (to (cdr (assq 'To (mail-header-extra header))))
  145. newsgroups x-no-archive desc link)
  146. ;; Remove text properties of subject string to avoid Emacs bug
  147. ;; #3506
  148. (set-text-properties 0 (length subject) nil subject)
  149. ;; Fetching an article is an expensive operation; newsgroup and
  150. ;; x-no-archive are only needed for web links.
  151. (when (org-xor current-prefix-arg org-gnus-prefer-web-links)
  152. ;; Make sure the original article buffer is up-to-date
  153. (save-window-excursion (gnus-summary-select-article))
  154. (setq to (or to (gnus-fetch-original-field "To"))
  155. newsgroups (gnus-fetch-original-field "Newsgroups")
  156. x-no-archive (gnus-fetch-original-field "x-no-archive")))
  157. (org-store-link-props :type "gnus" :from from :subject subject
  158. :message-id message-id :group group :to to)
  159. (when date
  160. (org-add-link-props :date date :date-timestamp date-ts
  161. :date-timestamp-inactive date-ts-ia))
  162. (setq desc (org-email-link-description)
  163. link (org-gnus-article-link
  164. group newsgroups message-id x-no-archive))
  165. (org-add-link-props :link link :description desc)
  166. link))
  167. ((eq major-mode 'message-mode)
  168. (setq org-store-link-plist nil) ; reset
  169. (save-excursion
  170. (save-restriction
  171. (message-narrow-to-headers)
  172. (and (not (message-fetch-field "Message-ID"))
  173. (message-generate-headers '(Message-ID)))
  174. (goto-char (point-min))
  175. (re-search-forward "^Message-ID: *.*$" nil t)
  176. (put-text-property (match-beginning 0) (match-end 0) 'message-deletable nil)
  177. (let ((gcc (car (last
  178. (message-unquote-tokens
  179. (message-tokenize-header (mail-fetch-field "gcc" nil t) " ,")))))
  180. (id (org-remove-angle-brackets (mail-fetch-field "Message-ID")))
  181. (to (mail-fetch-field "To"))
  182. (from (mail-fetch-field "From"))
  183. (subject (mail-fetch-field "Subject"))
  184. desc link
  185. newsgroup xarchive) ; those are always nil for gcc
  186. (and (not gcc)
  187. (error "Can not create link: No Gcc header found."))
  188. (org-store-link-props :type "gnus" :from from :subject subject
  189. :message-id id :group gcc :to to)
  190. (setq desc (org-email-link-description)
  191. link (org-gnus-article-link
  192. gcc newsgroup id xarchive))
  193. (org-add-link-props :link link :description desc)
  194. link))))))
  195. (defun org-gnus-open-nntp (path)
  196. "Follow the nntp: link specified by PATH."
  197. (let* ((spec (split-string path "/"))
  198. (server (split-string (nth 2 spec) "@"))
  199. (group (nth 3 spec))
  200. (article (nth 4 spec)))
  201. (org-gnus-follow-link
  202. (format "nntp+%s:%s" (or (cdr server) (car server)) group)
  203. article)))
  204. (defun org-gnus-open (path)
  205. "Follow the Gnus message or folder link specified by PATH."
  206. (let (group article)
  207. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  208. (error "Error in Gnus link"))
  209. (setq group (match-string 1 path)
  210. article (match-string 3 path))
  211. (when group
  212. (setq group (org-substring-no-properties group)))
  213. (when article
  214. (setq article (org-substring-no-properties article)))
  215. (org-gnus-follow-link group article)))
  216. (defun org-gnus-follow-link (&optional group article)
  217. "Follow a Gnus link to GROUP and ARTICLE."
  218. (require 'gnus)
  219. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  220. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  221. (when group
  222. (setq group (org-substring-no-properties group)))
  223. (when article
  224. (setq article (org-substring-no-properties article)))
  225. (cond ((and group article)
  226. (gnus-activate-group group t)
  227. (condition-case nil
  228. (let* ((method (gnus-find-method-for-group group))
  229. (backend (car method))
  230. (server (cadr method)))
  231. (cond
  232. ((eq backend 'nndoc)
  233. (if (gnus-group-read-group t nil group)
  234. (gnus-summary-goto-article article nil t)
  235. (message "Couldn't follow gnus link. %s"
  236. "The summary couldn't be opened.")))
  237. (t
  238. (let ((articles 1)
  239. group-opened)
  240. (when (and (eq backend 'nnimap)
  241. org-gnus-nnimap-query-article-no-from-file)
  242. (setq article
  243. (or (org-gnus-nnimap-cached-article-number
  244. (nth 1 (split-string group ":"))
  245. server (concat "<" article ">")) article)))
  246. (while (and (not group-opened)
  247. ;; stop on integer overflows
  248. (> articles 0))
  249. (setq group-opened (gnus-group-read-group
  250. articles nil group)
  251. articles (if (< articles 16)
  252. (1+ articles)
  253. (* articles 2))))
  254. (if group-opened
  255. (gnus-summary-goto-article article nil t)
  256. (message "Couldn't follow gnus link. %s"
  257. "The summary couldn't be opened."))))))
  258. (quit (message "Couldn't follow gnus link. %s"
  259. "The linked group is empty."))))
  260. (group (gnus-group-jump-to-group group))))
  261. (defun org-gnus-no-new-news ()
  262. "Like `M-x gnus' but doesn't check for new news."
  263. (if (not (gnus-alive-p)) (gnus)))
  264. (provide 'org-gnus)
  265. ;; arch-tag: 512e0840-58fa-45b3-b456-71e10fa2376d
  266. ;;; org-gnus.el ends here