1 ;;; ol-gnus.el --- Links to Gnus Groups and Messages -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2019 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: https://orgmode.org
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 <https://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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'.
41 ;;; Declare external functions and variables
43 (declare-function gnus-activate-group
"gnus-start" (group &optional scan dont-check method dont-sub-check
))
44 (declare-function gnus-find-method-for-group
"gnus" (group &optional info
))
45 (declare-function gnus-article-show-summary
"gnus-art" ())
46 (declare-function gnus-group-group-name
"gnus-group")
47 (declare-function gnus-group-jump-to-group
"gnus-group" (group &optional prompt
))
48 (declare-function gnus-group-read-group
"gnus-group" (&optional all no-article group select-articles
))
49 (declare-function message-fetch-field
"message" (header &optional not-all
))
50 (declare-function message-generate-headers
"message" (headers))
51 (declare-function message-narrow-to-headers
"message")
52 (declare-function message-tokenize-header
"message" (header &optional separator
))
53 (declare-function message-unquote-tokens
"message" (elems))
54 (declare-function nnvirtual-map-article
"nnvirtual" (article))
56 (defvar gnus-newsgroup-name
)
57 (defvar gnus-summary-buffer
)
58 (defvar gnus-other-frame-object
)
61 ;;; Customization variables
63 (defcustom org-gnus-prefer-web-links nil
64 "If non-nil, `org-store-link' creates web links to Google groups or Gmane.
65 \\<org-mode-map>When nil, Gnus will be used for such links.
66 Using a prefix argument to the command `\\[org-store-link]' (`org-store-link')
67 negates this setting for the duration of the command."
68 :group
'org-link-store
71 (defcustom org-gnus-no-server nil
72 "Should Gnus be started using `gnus-no-server'?"
75 :package-version
'(Org .
"8.0")
79 ;;; Install the link type
81 (org-link-set-parameters "gnus"
82 :follow
#'org-gnus-open
83 :store
#'org-gnus-store-link
)
87 (defun org-gnus-group-link (group)
88 "Create a link to the Gnus group GROUP.
89 If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
90 non-nil, create a link to groups.google.com or gmane.org.
91 Otherwise create a link to the group inside Gnus.
93 If `org-store-link' was called with a prefix arg the meaning of
94 `org-gnus-prefer-web-links' is reversed."
95 (let ((unprefixed-group (replace-regexp-in-string "^[^:]+:" "" group
)))
96 (if (and (string-prefix-p "nntp" group
) ;; Only for nntp groups
97 (org-xor current-prefix-arg
98 org-gnus-prefer-web-links
))
99 (concat (if (string-match "gmane" unprefixed-group
)
100 "http://news.gmane.org/"
101 "http://groups.google.com/group/")
103 (concat "gnus:" group
))))
105 (defun org-gnus-article-link (group newsgroups message-id x-no-archive
)
106 "Create a link to a Gnus article.
108 The article is specified by its MESSAGE-ID. Additional
109 parameters are the Gnus GROUP, the NEWSGROUPS the article was
110 posted to and the X-NO-ARCHIVE header value of that article.
112 If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
113 non-nil, create a link to groups.google.com or gmane.org.
114 Otherwise create a link to the article inside Gnus.
116 If `org-store-link' was called with a prefix arg the meaning of
117 `org-gnus-prefer-web-links' is reversed."
118 (if (and (org-xor current-prefix-arg org-gnus-prefer-web-links
)
119 newsgroups
;make web links only for nntp groups
120 (not x-no-archive
)) ;and if X-No-Archive isn't set
121 (format (if (string-match-p "gmane\\." newsgroups
)
122 "http://mid.gmane.org/%s"
123 "http://groups.google.com/groups/search?as_umsgid=%s")
124 (url-encode-url message-id
))
125 (concat "gnus:" group
"#" message-id
)))
127 (defun org-gnus-store-link ()
128 "Store a link to a Gnus folder or message."
131 (let ((group (gnus-group-group-name)))
133 (org-link-store-props :type
"gnus" :group group
)
134 (let ((description (org-gnus-group-link group
)))
135 (org-link-add-props :link description
:description description
)
137 ((or `gnus-summary-mode
`gnus-article-mode
)
139 (pcase (gnus-find-method-for-group gnus-newsgroup-name
)
142 (car (nnvirtual-map-article (gnus-summary-article-number)))))
145 (nnir-article-group (gnus-summary-article-number))))
146 (_ gnus-newsgroup-name
)))
147 (header (if (eq major-mode
'gnus-article-mode
)
148 ;; When in an article, first move to summary
149 ;; buffer, with point on the summary of the
150 ;; current article before extracting headers.
151 (save-window-excursion
153 (gnus-article-show-summary)
154 (gnus-summary-article-header)))
155 (gnus-summary-article-header)))
156 (from (mail-header-from header
))
157 (message-id (org-unbracket-string "<" ">" (mail-header-id header
)))
158 (date (org-trim (mail-header-date header
)))
159 ;; Remove text properties of subject string to avoid Emacs
161 (subject (org-no-properties
162 (copy-sequence (mail-header-subject header
))))
163 (to (cdr (assq 'To
(mail-header-extra header
))))
164 newsgroups x-no-archive
)
165 ;; Fetching an article is an expensive operation; newsgroup and
166 ;; x-no-archive are only needed for web links.
167 (when (org-xor current-prefix-arg org-gnus-prefer-web-links
)
168 ;; Make sure the original article buffer is up-to-date.
169 (save-window-excursion (gnus-summary-select-article))
170 (setq to
(or to
(gnus-fetch-original-field "To")))
171 (setq newsgroups
(gnus-fetch-original-field "Newsgroups"))
172 (setq x-no-archive
(gnus-fetch-original-field "x-no-archive")))
173 (org-link-store-props :type
"gnus" :from from
:date date
:subject subject
174 :message-id message-id
:group group
:to to
)
175 (let ((link (org-gnus-article-link
176 group newsgroups message-id x-no-archive
))
177 (description (org-link-email-description)))
178 (org-link-add-props :link link
:description description
)
181 (setq org-store-link-plist nil
) ;reset
184 (message-narrow-to-headers)
185 (unless (message-fetch-field "Message-ID")
186 (message-generate-headers '(Message-ID)))
187 (goto-char (point-min))
188 (re-search-forward "^Message-ID:" nil t
)
189 (put-text-property (line-beginning-position) (line-end-position)
190 'message-deletable nil
)
191 (let ((gcc (org-last (message-unquote-tokens
192 (message-tokenize-header
193 (mail-fetch-field "gcc" nil t
) " ,"))))
194 (id (org-unbracket-string "<" ">"
195 (mail-fetch-field "Message-ID")))
196 (to (mail-fetch-field "To"))
197 (from (mail-fetch-field "From"))
198 (subject (mail-fetch-field "Subject"))
199 newsgroup xarchive
) ;those are always nil for gcc
200 (unless gcc
(error "Can not create link: No Gcc header found"))
201 (org-link-store-props :type
"gnus" :from from
:subject subject
202 :message-id id
:group gcc
:to to
)
203 (let ((link (org-gnus-article-link gcc newsgroup id xarchive
))
204 (description (org-link-email-description)))
205 (org-link-add-props :link link
:description description
)
208 (defun org-gnus-open-nntp (path)
209 "Follow the nntp: link specified by PATH."
210 (let* ((spec (split-string path
"/"))
211 (server (split-string (nth 2 spec
) "@"))
213 (article (nth 4 spec
)))
214 (org-gnus-follow-link
215 (format "nntp+%s:%s" (or (cdr server
) (car server
)) group
)
218 (defun org-gnus-open (path)
219 "Follow the Gnus message or folder link specified by PATH."
220 (unless (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path
)
221 (error "Error in Gnus link %S" path
))
222 (let ((group (match-string-no-properties 1 path
))
223 (article (match-string-no-properties 3 path
)))
224 (org-gnus-follow-link group article
)))
226 (defun org-gnus-follow-link (&optional group article
)
227 "Follow a Gnus link to GROUP and ARTICLE."
229 (funcall (cdr (assq 'gnus org-link-frame-setup
)))
230 (when gnus-other-frame-object
(select-frame gnus-other-frame-object
))
231 (let ((group (org-no-properties group
))
232 (article (org-no-properties article
)))
235 (gnus-activate-group group
)
237 (let ((msg "Couldn't follow Gnus link. Summary couldn't be opened."))
238 (pcase (gnus-find-method-for-group group
)
240 (if (gnus-group-read-group t nil group
)
241 (gnus-summary-goto-article article nil t
)
246 (while (and (not group-opened
)
247 ;; Stop on integer overflows.
249 (setq group-opened
(gnus-group-read-group articles t group
))
250 (setq articles
(if (< articles
16)
254 (gnus-summary-goto-article article nil t
)
257 (message "Couldn't follow Gnus link. The linked group is empty."))))
258 (group (gnus-group-jump-to-group group
)))))
260 (defun org-gnus-no-new-news ()
261 "Like `\\[gnus]' but doesn't check for new news."
262 (cond ((gnus-alive-p) nil
)
263 (org-gnus-no-server (gnus-no-server))
268 ;;; ol-gnus.el ends here