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