]> code.delx.au - gnu-emacs-elpa/blob - packages/gnorb/gnorb-gnus.el
* packages/gnorb/gnorb.el: Mention dependency on cl-lib.
[gnu-emacs-elpa] / packages / gnorb / gnorb-gnus.el
1 ;;; gnorb-gnus.el --- The gnus-centric fuctions of gnorb
2
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
4
5 ;; Author: Eric Abrahamsen <eric@ericabrahamsen.net>
6 ;; Keywords:
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;;
24
25 ;;; Code:
26
27 (require 'gnorb-utils)
28
29 (declare-function org-gnus-article-link "org-gnus"
30 (group newsgroups message-id x-no-archive))
31 (declare-function org-gnus-follow-link "org-gnus"
32 (group article))
33
34 (defgroup gnorb-gnus nil
35 "The Gnus bits of Gnorb."
36 :tag "Gnorb Gnus"
37 :group 'gnorb)
38
39
40 (defcustom gnorb-gnus-mail-search-backends
41 '((notmuch (lambda (terms)
42 (mapconcat
43 (lambda (m)
44 (replace-regexp-in-string "\\." "\\\\." m))
45 terms " OR "))
46 notmuch-search)
47 (mairix (lambda (terms)
48 (mapconcat 'identity
49 terms ","))
50 mairix-search)
51 (namazu (lambda (terms)
52 (mapconcat 'identity
53 terms " or "))
54 namazu-search))
55 "Various backends for mail search.
56
57 An alist of backends, where each element consists of three parts:
58 the symbol name of the backend, a lambda form which receives a
59 list of email addresses and returns a properly-formatted search
60 string, and the symbol name of the function used to initiate the
61 search."
62 :group 'gnorb-gnus
63 :type 'list)
64
65 (defcustom gnorb-gnus-mail-search-backend nil
66 "Mail search backend currently in use. One of the three symbols
67 notmuch, namazu, or mairix."
68 :group 'gnorb-gnus
69 :type 'symbol)
70
71 (defcustom gnorb-gnus-capture-always-attach nil
72 "Always prompt about attaching attachments when capturing from
73 a Gnus message, even if the template being used hasn't
74 specified the :gnus-attachments key.
75
76 Basically behave as if all attachments have \":gnus-attachments t\"."
77 :group 'gnorb-gnus
78 :type 'boolean)
79
80 (defcustom gnorb-gnus-new-todo-capture-key nil
81 "Key for the capture template to use when creating a new TODO
82 from an outgoing message."
83 :group 'gnorb-gnus
84 :type 'string)
85
86 (defcustom gnorb-gnus-hint-relevant-article t
87 "When opening a gnus message, should gnorb let you know if the
88 message is relevant to an existing TODO?"
89 :group 'gnorb-gnus
90 :type 'boolean)
91
92 (defcustom gnorb-gnus-summary-mark-format-letter "g"
93 "Format letter to be used as part of your
94 `gnus-summary-line-format', to indicate in the *Summary* buffer
95 which articles might be relevant to TODOs. Since this is a user
96 format code, it should be prefixed with %u, eg %ug. It will
97 result in the insertion of the value of
98 `gnorb-gnus-summary-mark', for relevant messages, or
99 else a space."
100 :group 'gnorb-gnus
101 :type 'string)
102
103 (defcustom gnorb-gnus-summary-mark "ยก"
104 "Default mark to insert in the summary format line of articles
105 that are likely relevant to existing TODO headings."
106 :group 'gnorb-gnus
107 :type 'string)
108
109 (defcustom gnorb-gnus-trigger-refile-targets
110 '((org-agenda-files :maxlevel . 4))
111 "A value to use as an equivalent of `org-refile-targets' (which
112 see) when offering trigger targets for
113 `gnorb-gnus-incoming-do-todo'."
114 :group 'gnorb-gnus
115 :type 'list)
116
117 (defcustom gnorb-gnus-sent-groups nil
118 "A list of strings indicating sent mail groups.
119
120 In some cases, Gnorb can't detect where your sent messages are
121 stored (ie if you're using IMAP sent mail folders instead of
122 local archiving. If you want Gnorb to be able to find sent
123 messages, this option can help it do that. It should be set to a
124 list of strings, which are assumed to be fully qualified
125 server+group combinations, ie \"nnimap+Server:[Gmail]/Sent
126 Mail\", or something similar. This only has to be done once for
127 each message."
128 :group 'gnorb-gnus
129 :type 'list)
130
131 (defvar gnorb-gnus-capture-attachments nil
132 "Holding place for attachment names during the capture
133 process.")
134
135 ;;; What follows is a very careful copy-pasta of bits and pieces from
136 ;;; mm-decode.el and gnus-art.el. Voodoo was involved.
137
138 ;;;###autoload
139 (defun gnorb-gnus-article-org-attach (n)
140 "Save MIME part N, which is the numerical prefix, of the
141 article under point as an attachment to the specified org
142 heading."
143 (interactive "P")
144 (gnus-article-part-wrapper n 'gnorb-gnus-attach-part))
145
146 ;;;###autoload
147 (defun gnorb-gnus-mime-org-attach ()
148 "Save the MIME part under point as an attachment to the
149 specified org heading."
150 (interactive)
151 (gnus-article-check-buffer)
152 (let ((data (get-text-property (point) 'gnus-data)))
153 (when data
154 (gnorb-gnus-attach-part data))))
155
156 (defun gnorb-gnus-attach-part (handle &optional org-heading)
157 "Attach HANDLE to an existing org heading."
158 (let* ((filename (gnorb-gnus-save-part handle))
159 (org-refile-targets gnorb-gnus-trigger-refile-targets)
160 (ref-msg-ids
161 (concat (gnus-fetch-original-field "references") " "
162 (gnus-fetch-original-field "in-reply-to")))
163 (rel-heading
164 (when gnorb-tracking-enabled
165 (car (gnorb-find-visit-candidates
166 ref-msg-ids))))
167 (org-heading
168 (if (and rel-heading
169 (y-or-n-p (message
170 "Attach part to %s"
171 (gnorb-pretty-outline rel-heading))))
172 rel-heading
173 (org-refile-get-location "Attach part to" nil t))))
174 (require 'org-attach)
175 (save-window-excursion
176 (if (stringp org-heading)
177 (org-id-goto org-heading)
178 (progn
179 (find-file (nth 1 org-heading))
180 (goto-char (nth 3 org-heading))))
181 (org-attach-attach filename nil 'mv))))
182
183 (defun gnorb-gnus-save-part (handle)
184 (let ((filename (or (mail-content-type-get
185 (mm-handle-disposition handle) 'filename)
186 (mail-content-type-get
187 (mm-handle-type handle) 'name))))
188 (setq filename
189 (gnus-map-function mm-file-name-rewrite-functions
190 (file-name-nondirectory filename)))
191 (setq filename (expand-file-name filename gnorb-tmp-dir))
192 (mm-save-part-to-file handle filename)
193 filename))
194
195 (defun gnorb-gnus-collect-all-attachments (&optional capture-p store)
196 "Collect all the attachments from the message under point, and
197 save them into `gnorb-tmp-dir'."
198 (save-window-excursion
199 (when capture-p
200 (set-buffer (org-capture-get :original-buffer)))
201 (unless (memq major-mode '(gnus-summary-mode gnus-article-mode))
202 (error "Only works in Gnus summary or article buffers"))
203 (let ((article (gnus-summary-article-number))
204 mime-handles)
205 (when (or (null gnus-current-article)
206 (null gnus-article-current)
207 (/= article (cdr gnus-article-current))
208 (not (equal (car gnus-article-current) gnus-newsgroup-name)))
209 (gnus-summary-display-article article))
210 (gnus-eval-in-buffer-window gnus-article-buffer
211 (setq mime-handles (cl-remove-if-not
212 (lambda (h)
213 (let ((disp (mm-handle-disposition (cdr h))))
214 (and (member (car disp)
215 '("inline" "attachment"))
216 (mail-content-type-get disp 'filename))))
217 gnus-article-mime-handle-alist)))
218 (when mime-handles
219 (dolist (h mime-handles)
220 (let ((filename
221 (gnorb-gnus-save-part (cdr h))))
222 (when (or capture-p store)
223 (push filename gnorb-gnus-capture-attachments))))))))
224
225 ;;; Make the above work in the capture process
226
227 (defun gnorb-gnus-capture-attach ()
228 (when (and (or gnorb-gnus-capture-always-attach
229 (org-capture-get :gnus-attachments))
230 (with-current-buffer
231 (org-capture-get :original-buffer)
232 (memq major-mode '(gnus-summary-mode gnus-article-mode))))
233 (require 'org-attach)
234 (setq gnorb-gnus-capture-attachments nil)
235 (gnorb-gnus-collect-all-attachments t)
236 (map-y-or-n-p
237 (lambda (a)
238 (format "Attach %s to capture heading? "
239 (file-name-nondirectory a)))
240 (lambda (a) (org-attach-attach a nil 'mv))
241 gnorb-gnus-capture-attachments
242 '("file" "files" "attach"))
243 (setq gnorb-gnus-capture-attachments nil)))
244
245 (add-hook 'org-capture-mode-hook 'gnorb-gnus-capture-attach)
246
247 (defun gnorb-gnus-capture-abort-cleanup ()
248 (when (and org-note-abort
249 (org-capture-get :gnus-attachments))
250 ;; FIXME: Yuck: setting `abort-note' will fail as soon as org-capture.el is
251 ;; compiled with lexical-binding!
252 (condition-case nil
253 (progn (org-attach-delete-all)
254 (setq abort-note 'clean)
255 ;; remove any gnorb-mail-header values here
256 )
257 (error
258 (setq abort-note 'dirty)))))
259
260 (add-hook 'org-capture-prepare-finalize-hook
261 'gnorb-gnus-capture-abort-cleanup)
262
263 ;;; Storing, removing, and acting on Org headers in messages.
264
265 (defvar gnorb-gnus-message-info nil
266 "Place to store the To, Subject, Date, and Message-ID headers
267 of the currently-sending or last-sent message.")
268
269 (defun gnorb-gnus-check-outgoing-headers ()
270 "Save the value of the `gnorb-mail-header' for the current
271 message; multiple header values returned as a string. Also save
272 information about the outgoing message into
273 `gnorb-gnus-message-info'."
274 (save-restriction
275 (message-narrow-to-headers)
276 (setq gnorb-gnus-message-info nil)
277 (let* ((org-ids (mail-fetch-field gnorb-mail-header nil nil t))
278 (msg-id (mail-fetch-field "Message-ID"))
279 (refs (mail-fetch-field "References"))
280 (in-reply-to (mail-fetch-field "In-Reply-To"))
281 (to (if (message-news-p)
282 (mail-fetch-field "Newsgroups")
283 (mail-fetch-field "To")))
284 (from (mail-fetch-field "From"))
285 (subject (mail-fetch-field "Subject"))
286 (date (mail-fetch-field "Date"))
287 ;; If we can get a link, that's awesome.
288 (gcc (mail-fetch-field "Gcc"))
289 (link (or (and gcc
290 (org-store-link nil))
291 nil))
292 (group (ignore-errors (car (split-string link "#")))))
293 ;; If we can't make a real link, then save some information so
294 ;; we can fake it.
295 (when in-reply-to
296 (setq refs (concat refs " " in-reply-to)))
297 (when refs
298 (setq refs (gnus-extract-references refs)))
299 (setq gnorb-gnus-message-info
300 `(:subject ,subject :msg-id ,msg-id
301 :to ,to :from ,from
302 :link ,link :date ,date :refs ,refs
303 :group ,group))
304 (if org-ids
305 (progn
306 (require 'gnorb-org)
307 (setq gnorb-message-org-ids org-ids)
308 ;; `gnorb-org-setup-message' may have put this here, but
309 ;; if we're working from a draft, or triggering this from
310 ;; a reply, it might not be there yet.
311 (add-to-list 'message-exit-actions
312 'gnorb-org-restore-after-send t))
313 (setq gnorb-message-org-ids nil)))))
314
315 (add-hook 'message-header-hook 'gnorb-gnus-check-outgoing-headers)
316
317 ;;;###autoload
318 (defun gnorb-gnus-outgoing-do-todo (&optional arg)
319 "Call this function to use the message currently being composed
320 as an email todo action. If it's a new message, or a reply to a
321 message that isn't referenced by any TODOs, a new TODO will be
322 created. If it references an existing TODO, you'll be prompted to
323 trigger a state-change or a note on that TODO.
324
325 Otherwise, you can call it with a prefix arg to associate the
326 sending/sent message with an existing Org subtree, and trigger an
327 action on that subtree.
328
329 If a new todo is made, it needs a capture template: set
330 `gnorb-gnus-new-todo-capture-key' to the string key for the
331 appropriate capture template. If you're using a gnus-based
332 archive method (ie you have `gnus-message-archive-group' set to
333 something, and your outgoing messages have a \"Fcc\" header),
334 then a real link will be made to the outgoing message, and all
335 the gnus-type escapes will be available (see the Info
336 manual (org) Template expansion section). If you don't, then the
337 %:subject, %:to, %:toname, %:toaddress, and %:date escapes for
338 the outgoing message will still be available -- nothing else will
339 work."
340 (interactive "P")
341 (let ((org-refile-targets gnorb-gnus-trigger-refile-targets)
342 (compose-marker (make-marker))
343 header-ids ref-ids rel-headings gnorb-window-conf
344 reply-id reply-group in-reply-to)
345 (when arg
346 (setq rel-headings
347 (org-refile-get-location "Trigger action on" nil t))
348 (setq rel-headings
349 (list (list (save-window-excursion
350 (find-file (nth 1 rel-headings))
351 (goto-char (nth 3 rel-headings))
352 (org-id-get-create))))))
353 (if (not (eq major-mode 'message-mode))
354 ;; The message is already sent, so we're relying on whatever was
355 ;; stored into `gnorb-gnus-message-info'.
356 (if arg
357 (progn
358 (push (car rel-headings) gnorb-message-org-ids)
359 (gnorb-org-restore-after-send))
360 (setq ref-ids (plist-get gnorb-gnus-message-info :refs))
361 (if ref-ids
362 ;; the message might be relevant to some TODO
363 ;; heading(s). But if there had been org-id
364 ;; headers, they would already have been
365 ;; handled when the message was sent.
366 (progn
367 (setq rel-headings (gnorb-find-visit-candidates ref-ids))
368 (if (not rel-headings)
369 (gnorb-gnus-outgoing-make-todo-1)
370 (dolist (h rel-headings)
371 (push h gnorb-message-org-ids))
372 (gnorb-org-restore-after-send)))
373 ;; not relevant, just make a new TODO
374 (gnorb-gnus-outgoing-make-todo-1)))
375 ;; We are still in the message composition buffer, so let's see
376 ;; what we've got.
377
378 ;; What we want is a link to the original message we're replying
379 ;; to, if this is actually a reply.
380 (when message-reply-headers
381 (setq reply-id (aref message-reply-headers 4)))
382 ;; Save-excursion won't work, because point will move if we
383 ;; insert headings.
384 (move-marker compose-marker (point))
385 (save-restriction
386 (widen)
387 (message-narrow-to-headers-or-head)
388 (setq header-ids (mail-fetch-field gnorb-mail-header nil nil t))
389 ;; With a prefix arg we do not check references, because the
390 ;; whole point is to add new references. We still want to know
391 ;; what org id headers are present, though, so we don't add
392 ;; duplicates.
393 (setq ref-ids (unless arg (mail-fetch-field "References" t)))
394 (setq in-reply-to (unless arg (mail-fetch-field "In-Reply-to" t)))
395 (when in-reply-to
396 (setq ref-ids (concat ref-ids " " in-reply-to)))
397 (setq reply-group (when (mail-fetch-field "X-Draft-From" t)
398 (car-safe (read (mail-fetch-field "X-Draft-From" t)))))
399 ;; when it's a reply, store a link to the reply just in case.
400 ;; This is pretty embarrassing -- we follow a link just to
401 ;; create a link. But I'm not going to recreate all of
402 ;; `org-store-link' by hand.
403 (when (and reply-group reply-id)
404 (save-window-excursion
405 (org-gnus-follow-link reply-group reply-id)
406 (call-interactively 'org-store-link)))
407 (when ref-ids
408 ;; if the References header points to any message ids that are
409 ;; tracked by TODO headings...
410 (setq rel-headings (gnorb-find-visit-candidates ref-ids)))
411 (when rel-headings
412 (goto-char (point-min))
413 (dolist (h (delete-dups rel-headings))
414 ;; then get the org-ids of those headings, and insert
415 ;; them into this message as headers. If the id was
416 ;; already present in a header, don't add it again.
417 (unless (member h header-ids)
418 (goto-char (point-at-bol))
419 (open-line 1)
420 (message-insert-header
421 (intern gnorb-mail-header)
422 h)
423 ;; tell the rest of the function that this is a relevant
424 ;; message
425 (push h header-ids)))))
426 (goto-char compose-marker)
427 (add-to-list
428 'message-exit-actions
429 (if header-ids
430 'gnorb-org-restore-after-send
431 'gnorb-gnus-outgoing-make-todo-1)
432 t)
433 (message
434 (if header-ids
435 "Message will trigger TODO state-changes after sending"
436 "A TODO will be made from this message after it's sent")))))
437
438 (defvar org-capture-link-is-already-stored)
439
440 (defun gnorb-gnus-outgoing-make-todo-1 ()
441 (unless gnorb-gnus-new-todo-capture-key
442 (error "No capture template key set, customize gnorb-gnus-new-todo-capture-key"))
443 (let* ((link (plist-get gnorb-gnus-message-info :link))
444 (group (plist-get gnorb-gnus-message-info :group))
445 (date (plist-get gnorb-gnus-message-info :date))
446 (date-ts (and date
447 (ignore-errors
448 (format-time-string
449 (org-time-stamp-format t)
450 (date-to-time date)))))
451 (date-ts-ia (and date
452 (ignore-errors
453 (format-time-string
454 (org-time-stamp-format t t)
455 (date-to-time date)))))
456 (msg-id (plist-get gnorb-gnus-message-info :msg-id))
457 (sender (plist-get gnorb-gnus-message-info :from))
458 (subject (plist-get gnorb-gnus-message-info :subject))
459 ;; Convince Org we already have a link stored, even if we
460 ;; don't.
461 (org-capture-link-is-already-stored t))
462 (if link
463 ;; Even if you make a link to not-yet-sent messages, even if
464 ;; you've saved the draft and it has a Date header, that
465 ;; header isn't saved into the link plist. So fake that, too.
466 (org-add-link-props
467 :date date
468 :date-timestamp date-ts
469 :date-timestamp-inactive date-ts-ia
470 :annotation link)
471 (org-store-link-props
472 :subject (plist-get gnorb-gnus-message-info :subject)
473 :to (plist-get gnorb-gnus-message-info :to)
474 :date date
475 :date-timestamp date-ts
476 :date-timestamp-inactive date-ts-ia
477 :message-id msg-id
478 :annotation link))
479 (org-capture nil gnorb-gnus-new-todo-capture-key)
480 (when msg-id
481 (org-entry-put (point) gnorb-org-msg-id-key msg-id)
482 (gnorb-registry-make-entry msg-id sender subject (org-id-get-create) group))))
483
484 ;;; If an incoming message should trigger state-change for a Org todo,
485 ;;; call this function on it.
486
487 ;;;###autoload
488 (defun gnorb-gnus-incoming-do-todo (arg headers &optional id)
489 "Call this function from a received gnus message to store a
490 link to the message, prompt for a related Org heading, visit the
491 heading, and either add a note or trigger a TODO state change.
492 Set `gnorb-trigger-todo-default' to 'note or 'todo (you can
493 get the non-default behavior by calling this function with a
494 prefix argument), or to 'prompt to always be prompted.
495
496 In some cases, Gnorb can guess for you which Org heading you
497 probably want to trigger, which can save some time. It does this
498 by looking in the References header, and seeing if any of the IDs
499 there match the value of the `gnorb-org-msg-id-key' property for
500 any headings. In order for this to work, you will have to have
501 loaded org-id, and have the variable `org-id-track-globally' set
502 to t (it is, by default)."
503 (interactive (gnus-interactive "P\nH"))
504 (when (not (memq major-mode '(gnus-summary-mode gnus-article-mode)))
505 (user-error "Only works in gnus summary or article mode"))
506 ;; We should only store a link if it's not already at the head of
507 ;; `org-stored-links'. There's some duplicate storage, at
508 ;; present. Take a look at calling it non-interactively.
509 (setq gnorb-window-conf (current-window-configuration))
510 (move-marker gnorb-return-marker (point))
511 (setq gnorb-gnus-message-info nil)
512 (let* ((msg-id (mail-header-id headers))
513 (from (mail-header-from headers))
514 (subject (mail-header-subject headers))
515 (date (mail-header-date headers))
516 (to (cdr (assoc 'To (mail-header-extra headers))))
517 (group gnus-newsgroup-name)
518 (link (call-interactively 'org-store-link))
519 (org-refile-targets gnorb-gnus-trigger-refile-targets)
520 (ref-msg-ids (mail-header-references headers))
521 (offer-heading
522 (when (and (not id) ref-msg-ids gnorb-tracking-enabled)
523 (if org-id-track-globally
524 ;; for now we're basically ignoring the fact that
525 ;; multiple candidates could exist; just do the first
526 ;; one.
527 (car (gnorb-find-visit-candidates
528 ref-msg-ids))
529 (message "Gnorb can't check for relevant headings unless `org-id-track-globally' is t")
530 (sit-for 1))))
531 targ)
532 (setq gnorb-gnus-message-info
533 `(:subject ,subject :msg-id ,msg-id
534 :to ,to :from ,from
535 :link ,link :date ,date :refs ,ref-msg-ids
536 :group ,group))
537 (gnorb-gnus-collect-all-attachments nil t)
538 ;; Delete other windows, users can restore with
539 ;; `gnorb-restore-layout'.
540 (delete-other-windows)
541 (if id
542 (gnorb-trigger-todo-action arg id)
543 (if (and offer-heading
544 (y-or-n-p (format "Trigger action on %s"
545 (gnorb-pretty-outline offer-heading))))
546 (gnorb-trigger-todo-action arg offer-heading)
547 (setq targ (org-refile-get-location
548 "Trigger heading" nil t))
549 (find-file (nth 1 targ))
550 (goto-char (nth 3 targ))
551 (gnorb-trigger-todo-action arg)))))
552
553 ;;;###autoload
554 (defun gnorb-gnus-search-messages (str &optional ret)
555 "Initiate a search for gnus message links in an org subtree.
556 The arg STR can be one of two things: an Org heading id value
557 \(IDs should be prefixed with \"id+\"\), in which case links will
558 be collected from that heading, or a string corresponding to an
559 Org tags search, in which case links will be collected from all
560 matching headings.
561
562 In either case, once a collection of links have been made, they
563 will all be displayed in an ephemeral group on the \"nngnorb\"
564 server. There must be an active \"nngnorb\" server for this to
565 work."
566 (interactive)
567 (let ((nnir-address
568 (or (gnus-method-to-server '(nngnorb))
569 (user-error
570 "Please add a \"nngnorb\" backend to your gnus installation."))))
571 (when (version= "5.13" gnus-version-number)
572 (setq nnir-current-query nil
573 nnir-current-server nil
574 nnir-current-group-marked nil
575 nnir-artlist nil))
576 (gnus-group-read-ephemeral-group
577 ;; in 24.4, the group name is mostly decorative. in 24.3, the
578 ;; query itself is read from there. It should look like (concat
579 ;; "nnir:" (prin1-to-string '((query str))))
580 (if (version= "5.13" gnus-version-number)
581 (concat "nnir:" (prin1-to-string `((query ,str))))
582 (concat "gnorb-" str))
583 (if (version= "5.13" gnus-version-number)
584 (list 'nnir nnir-address)
585 (list 'nnir "nnir"))
586 nil
587 ret ;; it's possible you can't just put an arbitrary form in
588 ;; here, which sucks.
589 nil nil
590 ;; the following seems to simply be ignored under gnus 5.13
591 (list (cons 'nnir-specs (list (cons 'nnir-query-spec `((query . ,str)))
592 (cons 'nnir-group-spec `((,nnir-address nil)))))
593 (cons 'nnir-artlist nil)))
594 (gnorb-summary-minor-mode)))
595
596 ;;; Automatic noticing of relevant messages
597
598 ;; likely hooks for the summary buffer include:
599 ;; `gnus-parse-headers-hook'
600
601 ;; BBDB puts its notice stuff in the `gnus-article-prepare-hook',
602 ;; which seems as good a spot as any.
603
604 (defun gnorb-gnus-hint-relevant-message ()
605 "When opening an article buffer, check the message to see if it
606 is relevant to any existing TODO headings. If so, flash a message
607 to that effect. This function is added to the
608 `gnus-article-prepare-hook'. It will only do anything if the
609 option `gnorb-gnus-hint-relevant-article' is non-nil."
610 (when (and gnorb-tracking-enabled
611 gnorb-gnus-hint-relevant-article
612 (not (memq (car (gnus-find-method-for-group
613 gnus-newsgroup-name))
614 '(nnvirtual nnir))))
615 (let* ((ref-ids (concat
616 (gnus-fetch-original-field "references") " "
617 (gnus-fetch-original-field "in-reply-to")))
618 (msg-id (gnus-fetch-original-field "message-id"))
619 (assoc-heading
620 (gnus-registry-get-id-key msg-id 'gnorb-ids))
621 (key
622 (where-is-internal 'gnorb-gnus-incoming-do-todo
623 nil t))
624 rel-headings)
625 (cond (assoc-heading
626 (message "Message is associated with %s"
627 (gnorb-pretty-outline (car assoc-heading) t)))
628 (ref-ids
629 (when (setq rel-headings
630 (gnorb-find-visit-candidates ref-ids))
631 (message "Possible relevant todo %s, trigger with %s"
632 (gnorb-pretty-outline (car rel-headings) t)
633 (if key
634 (key-description key)
635 "M-x gnorb-gnus-incoming-do-todo"))))))))
636
637 (add-hook 'gnus-article-prepare-hook 'gnorb-gnus-hint-relevant-message)
638
639 (defun gnorb-gnus-insert-format-letter-maybe (header)
640 (if (and gnorb-tracking-enabled
641 (not (memq (car (gnus-find-method-for-group
642 gnus-newsgroup-name))
643 '(nnvirtual nnir))))
644 (let ((ref-ids (mail-header-references header))
645 (msg-id (mail-header-message-id header)))
646 (if (or (gnus-registry-get-id-key msg-id 'gnorb-ids)
647 (and ref-ids
648 (gnorb-find-visit-candidates ref-ids)))
649 gnorb-gnus-summary-mark
650 " "))
651 " "))
652
653 (fset (intern (concat "gnus-user-format-function-"
654 gnorb-gnus-summary-mark-format-letter))
655 (lambda (header)
656 (gnorb-gnus-insert-format-letter-maybe header)))
657
658 ;;;###autoload
659 (defun gnorb-gnus-view ()
660 "Display the first relevant TODO heading for the message under point"
661 ;; this is pretty barebones, need to make sure we have a valid
662 ;; article buffer to access, and think about what to do for
663 ;; window-configuration!
664
665 ;; boy is this broken now.
666 (interactive)
667 (let ((refs (gnus-fetch-original-field "references"))
668 rel-headings)
669 (when refs
670 (setq rel-headings (gnorb-find-visit-candidates refs))
671 (delete-other-windows)
672 (org-id-goto (car rel-headings)))))
673
674 (provide 'gnorb-gnus)
675 ;;; gnorb-gnus.el ends here