]> code.delx.au - gnu-emacs/blob - lisp/gnus/nnir.el
Merge from origin/emacs-24
[gnu-emacs] / lisp / gnus / nnir.el
1 ;;; nnir.el --- search mail with various search engines -*- coding: utf-8 -*-
2
3 ;; Copyright (C) 1998-2015 Free Software Foundation, Inc.
4
5 ;; Author: Kai Großjohann <grossjohann@ls6.cs.uni-dortmund.de>
6 ;; Swish-e and Swish++ backends by:
7 ;; Christoph Conrad <christoph.conrad@gmx.de>.
8 ;; IMAP backend by: Simon Josefsson <jas@pdc.kth.se>.
9 ;; IMAP search by: Torsten Hilbrich <torsten.hilbrich <at> gmx.net>
10 ;; IMAP search improved by Daniel Pittman <daniel@rimspace.net>.
11 ;; nnmaildir support for Swish++ and Namazu backends by:
12 ;; Justus Piater <Justus <at> Piater.name>
13 ;; Keywords: news mail searching ir
14
15 ;; This file is part of GNU Emacs.
16
17 ;; GNU Emacs is free software: you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation, either version 3 of the License, or
20 ;; (at your option) any later version.
21
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
26
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29
30 ;;; Commentary:
31
32 ;; What does it do? Well, it allows you to search your mail using
33 ;; some search engine (imap, namazu, swish-e, gmane and others -- see
34 ;; later) by typing `G G' in the Group buffer. You will then get a
35 ;; buffer which shows all articles matching the query, sorted by
36 ;; Retrieval Status Value (score).
37
38 ;; When looking at the retrieval result (in the Summary buffer) you
39 ;; can type `A W' (aka M-x gnus-warp-to-article RET) on an article. You
40 ;; will be warped into the group this article came from. Typing `A T'
41 ;; (aka M-x gnus-summary-refer-thread RET) will warp to the group and
42 ;; also show the thread this article is part of.
43
44 ;; The Lisp setup may involve setting a few variables and setting up the
45 ;; search engine. You can define the variables in the server definition
46 ;; like this :
47 ;; (setq gnus-secondary-select-methods '(
48 ;; (nnimap "" (nnimap-address "localhost")
49 ;; (nnir-search-engine namazu)
50 ;; )))
51 ;; The main variable to set is `nnir-search-engine'. Choose one of
52 ;; the engines listed in `nnir-engines'. (Actually `nnir-engines' is
53 ;; an alist, type `C-h v nnir-engines RET' for more information; this
54 ;; includes examples for setting `nnir-search-engine', too.)
55
56 ;; If you use one of the local indices (namazu, find-grep, swish) you
57 ;; must also set up a search engine backend.
58
59 ;; 1. Namazu
60 ;;
61 ;; The Namazu backend requires you to have one directory containing all
62 ;; index files, this is controlled by the `nnir-namazu-index-directory'
63 ;; variable. To function the `nnir-namazu-remove-prefix' variable must
64 ;; also be correct, see the documentation for `nnir-namazu-remove-prefix'
65 ;; above.
66 ;;
67 ;; It is particularly important not to pass any any switches to namazu
68 ;; that will change the output format. Good switches to use include
69 ;; `--sort', `--ascending', `--early' and `--late'. Refer to the Namazu
70 ;; documentation for further information on valid switches.
71 ;;
72 ;; To index my mail with the `mknmz' program I use the following
73 ;; configuration file:
74 ;;
75 ;; ,----
76 ;; | package conf; # Don't remove this line!
77 ;; |
78 ;; | # Paths which will not be indexed. Don't use `^' or `$' anchors.
79 ;; | $EXCLUDE_PATH = "spam|sent";
80 ;; |
81 ;; | # Header fields which should be searchable. case-insensitive
82 ;; | $REMAIN_HEADER = "from|date|message-id|subject";
83 ;; |
84 ;; | # Searchable fields. case-insensitive
85 ;; | $SEARCH_FIELD = "from|date|message-id|subject";
86 ;; |
87 ;; | # The max length of a word.
88 ;; | $WORD_LENG_MAX = 128;
89 ;; |
90 ;; | # The max length of a field.
91 ;; | $MAX_FIELD_LENGTH = 256;
92 ;; `----
93 ;;
94 ;; My mail is stored in the directories ~/Mail/mail/, ~/Mail/lists/ and
95 ;; ~/Mail/archive/, so to index them I go to the directory set in
96 ;; `nnir-namazu-index-directory' and issue the following command.
97 ;;
98 ;; mknmz --mailnews ~/Mail/archive/ ~/Mail/mail/ ~/Mail/lists/
99 ;;
100 ;; For maximum searching efficiency I have a cron job set to run this
101 ;; command every four hours.
102
103 ;; 2. find-grep
104 ;;
105 ;; The find-grep engine simply runs find(1) to locate eligible
106 ;; articles and searches them with grep(1). This, of course, is much
107 ;; slower than using a proper search engine but OTOH doesn't require
108 ;; maintenance of an index and is still faster than using any built-in
109 ;; means for searching. The method specification of the server to
110 ;; search must include a directory for this engine to work (E.g.,
111 ;; `nnml-directory'). The tools must be POSIX compliant. GNU Find
112 ;; prior to version 4.2.12 (4.2.26 on Linux due to incorrect ARG_MAX
113 ;; handling) does not work.
114 ;; ,----
115 ;; | ;; find-grep configuration for searching the Gnus Cache
116 ;; |
117 ;; | (nnml "cache"
118 ;; | (nnml-get-new-mail nil)
119 ;; | (nnir-search-engine find-grep)
120 ;; | (nnml-directory "~/News/cache/")
121 ;; | (nnml-active-file "~/News/cache/active"))
122 ;; `----
123
124 ;; Developer information:
125
126 ;; I have tried to make the code expandable. Basically, it is divided
127 ;; into two layers. The upper layer is somewhat like the `nnvirtual'
128 ;; backend: given a specification of what articles to show from
129 ;; another backend, it creates a group containing exactly those
130 ;; articles. The lower layer issues a query to a search engine and
131 ;; produces such a specification of what articles to show from the
132 ;; other backend.
133
134 ;; The interface between the two layers consists of the single
135 ;; function `nnir-run-query', which dispatches the search to the
136 ;; proper search function. The argument of `nnir-run-query' is an
137 ;; alist with two keys: 'nnir-query-spec and 'nnir-group-spec. The
138 ;; value for 'nnir-query-spec is an alist. The only required key/value
139 ;; pair is (query . "query") specifying the search string to pass to
140 ;; the query engine. Individual engines may have other elements. The
141 ;; value of 'nnir-group-spec is a list with the specification of the
142 ;; groups/servers to search. The format of the 'nnir-group-spec is
143 ;; (("server1" ("group11" "group12")) ("server2" ("group21"
144 ;; "group22"))). If any of the group lists is absent then all groups
145 ;; on that server are searched.
146
147 ;; The output of `nnir-run-query' is supposed to be a vector, each
148 ;; element of which should in turn be a three-element vector. The
149 ;; first element should be full group name of the article, the second
150 ;; element should be the article number, and the third element should
151 ;; be the Retrieval Status Value (RSV) as returned from the search
152 ;; engine. An RSV is the score assigned to the document by the search
153 ;; engine. For Boolean search engines, the RSV is always 1000 (or 1
154 ;; or 100, or whatever you like).
155
156 ;; The sorting order of the articles in the summary buffer created by
157 ;; nnir is based on the order of the articles in the above mentioned
158 ;; vector, so that's where you can do the sorting you'd like. Maybe
159 ;; it would be nice to have a way of displaying the search result
160 ;; sorted differently?
161
162 ;; So what do you need to do when you want to add another search
163 ;; engine? You write a function that executes the query. Temporary
164 ;; data from the search engine can be put in `nnir-tmp-buffer'. This
165 ;; function should return the list of articles as a vector, as
166 ;; described above. Then, you need to register this backend in
167 ;; `nnir-engines'. Then, users can choose the backend by setting
168 ;; `nnir-search-engine' as a server variable.
169
170 ;;; Code:
171
172 ;;; Setup:
173
174 (require 'nnoo)
175 (require 'gnus-group)
176 (require 'message)
177 (require 'gnus-util)
178 (eval-when-compile
179 (require 'cl))
180
181 ;;; Internal Variables:
182
183 (defvar nnir-memo-query nil
184 "Internal: stores current query.")
185
186 (defvar nnir-memo-server nil
187 "Internal: stores current server.")
188
189 (defvar nnir-artlist nil
190 "Internal: stores search result.")
191
192 (defvar nnir-search-history ()
193 "Internal: the history for querying search options in nnir")
194
195 (defconst nnir-tmp-buffer " *nnir*"
196 "Internal: temporary buffer.")
197
198
199 ;; Imap variables
200
201 (defvar nnir-imap-search-arguments
202 '(("whole message" . "TEXT")
203 ("subject" . "SUBJECT")
204 ("to" . "TO")
205 ("from" . "FROM")
206 ("body" . "BODY")
207 ("imap" . ""))
208 "Mapping from user readable keys to IMAP search items for use in nnir")
209
210 (defvar nnir-imap-search-other "HEADER %S"
211 "The IMAP search item to use for anything other than
212 `nnir-imap-search-arguments'. By default this is the name of an
213 email header field")
214
215 (defvar nnir-imap-search-argument-history ()
216 "The history for querying search options in nnir")
217
218 ;;; Helper macros
219
220 ;; Data type article list.
221
222 (defmacro nnir-artlist-length (artlist)
223 "Returns number of articles in artlist."
224 `(length ,artlist))
225
226 (defmacro nnir-artlist-article (artlist n)
227 "Returns from ARTLIST the Nth artitem (counting starting at 1)."
228 `(when (> ,n 0)
229 (elt ,artlist (1- ,n))))
230
231 (defmacro nnir-artitem-group (artitem)
232 "Returns the group from the ARTITEM."
233 `(elt ,artitem 0))
234
235 (defmacro nnir-artitem-number (artitem)
236 "Returns the number from the ARTITEM."
237 `(elt ,artitem 1))
238
239 (defmacro nnir-artitem-rsv (artitem)
240 "Returns the Retrieval Status Value (RSV, score) from the ARTITEM."
241 `(elt ,artitem 2))
242
243 (defmacro nnir-article-group (article)
244 "Returns the group for ARTICLE"
245 `(nnir-artitem-group (nnir-artlist-article nnir-artlist ,article)))
246
247 (defmacro nnir-article-number (article)
248 "Returns the number for ARTICLE"
249 `(nnir-artitem-number (nnir-artlist-article nnir-artlist ,article)))
250
251 (defmacro nnir-article-rsv (article)
252 "Returns the rsv for ARTICLE"
253 `(nnir-artitem-rsv (nnir-artlist-article nnir-artlist ,article)))
254
255 (defsubst nnir-article-ids (article)
256 "Returns the pair `(nnir id . real id)' of ARTICLE"
257 (cons article (nnir-article-number article)))
258
259 (defmacro nnir-categorize (sequence keyfunc &optional valuefunc)
260 "Sorts a sequence into categories and returns a list of the form
261 `((key1 (element11 element12)) (key2 (element21 element22))'.
262 The category key for a member of the sequence is obtained
263 as `(keyfunc member)' and the corresponding element is just
264 `member'. If `valuefunc' is non-nil, the element of the list
265 is `(valuefunc member)'."
266 `(unless (null ,sequence)
267 (let (value)
268 (mapc
269 (lambda (member)
270 (let ((y (,keyfunc member))
271 (x ,(if valuefunc
272 `(,valuefunc member)
273 'member)))
274 (if (assoc y value)
275 (push x (cadr (assoc y value)))
276 (push (list y (list x)) value))))
277 ,sequence)
278 value)))
279
280 ;;; Finish setup:
281
282 (require 'gnus-sum)
283
284 (eval-when-compile
285 (autoload 'nnimap-buffer "nnimap")
286 (autoload 'nnimap-command "nnimap")
287 (autoload 'nnimap-capability "nnimap")
288 (autoload 'nnimap-wait-for-line "nnimap")
289 (autoload 'nnimap-change-group "nnimap")
290 (autoload 'nnimap-make-thread-query "nnimap")
291 (autoload 'gnus-registry-action "gnus-registry")
292 (autoload 'gnus-registry-get-id-key "gnus-registry")
293 (autoload 'gnus-group-topic-name "gnus-topic"))
294
295
296 (nnoo-declare nnir)
297 (nnoo-define-basics nnir)
298
299 (gnus-declare-backend "nnir" 'mail 'virtual)
300
301
302 ;;; User Customizable Variables:
303
304 (defgroup nnir nil
305 "Search groups in Gnus with assorted search engines."
306 :group 'gnus)
307
308 (defcustom nnir-ignored-newsgroups ""
309 "*A regexp to match newsgroups in the active file that should
310 be skipped when searching."
311 :version "24.1"
312 :type '(regexp)
313 :group 'nnir)
314
315 (defcustom nnir-summary-line-format nil
316 "*The format specification of the lines in an nnir summary buffer.
317
318 All the items from `gnus-summary-line-format' are available, along
319 with three items unique to nnir summary buffers:
320
321 %Z Search retrieval score value (integer)
322 %G Article original full group name (string)
323 %g Article original short group name (string)
324
325 If nil this will use `gnus-summary-line-format'."
326 :version "24.1"
327 :type '(choice (const :tag "gnus-summary-line-format" nil) string)
328 :group 'nnir)
329
330 (defcustom nnir-retrieve-headers-override-function nil
331 "*If non-nil, a function that accepts an article list and group
332 and populates the `nntp-server-buffer' with the retrieved
333 headers. Must return either 'nov or 'headers indicating the
334 retrieved header format.
335
336 If this variable is nil, or if the provided function returns nil for a search
337 result, `gnus-retrieve-headers' will be called instead."
338 :version "24.1"
339 :type '(choice (const :tag "gnus-retrieve-headers" nil) function)
340 :group 'nnir)
341
342 (defcustom nnir-imap-default-search-key "whole message"
343 "*The default IMAP search key for an nnir search. Must be one of
344 the keys in `nnir-imap-search-arguments'. To use raw imap queries
345 by default set this to \"imap\"."
346 :version "24.1"
347 :type `(choice ,@(mapcar (lambda (elem) (list 'const (car elem)))
348 nnir-imap-search-arguments))
349 :group 'nnir)
350
351 (defcustom nnir-swish++-configuration-file
352 (expand-file-name "~/Mail/swish++.conf")
353 "*Configuration file for swish++."
354 :type '(file)
355 :group 'nnir)
356
357 (defcustom nnir-swish++-program "search"
358 "*Name of swish++ search executable."
359 :type '(string)
360 :group 'nnir)
361
362 (defcustom nnir-swish++-additional-switches '()
363 "*A list of strings, to be given as additional arguments to swish++.
364
365 Note that this should be a list. Ie, do NOT use the following:
366 (setq nnir-swish++-additional-switches \"-i -w\") ; wrong
367 Instead, use this:
368 (setq nnir-swish++-additional-switches '(\"-i\" \"-w\"))"
369 :type '(repeat (string))
370 :group 'nnir)
371
372 (defcustom nnir-swish++-remove-prefix (concat (getenv "HOME") "/Mail/")
373 "*The prefix to remove from each file name returned by swish++
374 in order to get a group name (albeit with / instead of .). This is a
375 regular expression.
376
377 This variable is very similar to `nnir-namazu-remove-prefix', except
378 that it is for swish++, not Namazu."
379 :type '(regexp)
380 :group 'nnir)
381
382 ;; Swish-E.
383 ;; URL: http://swish-e.org/
384 ;; Variables `nnir-swish-e-index-file', `nnir-swish-e-program' and
385 ;; `nnir-swish-e-additional-switches'
386
387 (make-obsolete-variable 'nnir-swish-e-index-file
388 'nnir-swish-e-index-files "Emacs 23.1")
389 (defcustom nnir-swish-e-index-file
390 (expand-file-name "~/Mail/index.swish-e")
391 "*Index file for swish-e.
392 This could be a server parameter.
393 It is never consulted once `nnir-swish-e-index-files', which should be
394 used instead, has been customized."
395 :type '(file)
396 :group 'nnir)
397
398 (defcustom nnir-swish-e-index-files
399 (list nnir-swish-e-index-file)
400 "*List of index files for swish-e.
401 This could be a server parameter."
402 :type '(repeat (file))
403 :group 'nnir)
404
405 (defcustom nnir-swish-e-program "swish-e"
406 "*Name of swish-e search executable.
407 This cannot be a server parameter."
408 :type '(string)
409 :group 'nnir)
410
411 (defcustom nnir-swish-e-additional-switches '()
412 "*A list of strings, to be given as additional arguments to swish-e.
413
414 Note that this should be a list. Ie, do NOT use the following:
415 (setq nnir-swish-e-additional-switches \"-i -w\") ; wrong
416 Instead, use this:
417 (setq nnir-swish-e-additional-switches '(\"-i\" \"-w\"))
418
419 This could be a server parameter."
420 :type '(repeat (string))
421 :group 'nnir)
422
423 (defcustom nnir-swish-e-remove-prefix (concat (getenv "HOME") "/Mail/")
424 "*The prefix to remove from each file name returned by swish-e
425 in order to get a group name (albeit with / instead of .). This is a
426 regular expression.
427
428 This variable is very similar to `nnir-namazu-remove-prefix', except
429 that it is for swish-e, not Namazu.
430
431 This could be a server parameter."
432 :type '(regexp)
433 :group 'nnir)
434
435 ;; HyREX engine, see <URL:http://ls6-www.cs.uni-dortmund.de/>
436
437 (defcustom nnir-hyrex-program "nnir-search"
438 "*Name of the nnir-search executable."
439 :type '(string)
440 :group 'nnir)
441
442 (defcustom nnir-hyrex-additional-switches '()
443 "*A list of strings, to be given as additional arguments for nnir-search.
444 Note that this should be a list. Ie, do NOT use the following:
445 (setq nnir-hyrex-additional-switches \"-ddl ddl.xml -c nnir\") ; wrong !
446 Instead, use this:
447 (setq nnir-hyrex-additional-switches '(\"-ddl\" \"ddl.xml\" \"-c\" \"nnir\"))"
448 :type '(repeat (string))
449 :group 'nnir)
450
451 (defcustom nnir-hyrex-index-directory (getenv "HOME")
452 "*Index directory for HyREX."
453 :type '(directory)
454 :group 'nnir)
455
456 (defcustom nnir-hyrex-remove-prefix (concat (getenv "HOME") "/Mail/")
457 "*The prefix to remove from each file name returned by HyREX
458 in order to get a group name (albeit with / instead of .).
459
460 For example, suppose that HyREX returns file names such as
461 \"/home/john/Mail/mail/misc/42\". For this example, use the following
462 setting: (setq nnir-hyrex-remove-prefix \"/home/john/Mail/\")
463 Note the trailing slash. Removing this prefix gives \"mail/misc/42\".
464 `nnir' knows to remove the \"/42\" and to replace \"/\" with \".\" to
465 arrive at the correct group name, \"mail.misc\"."
466 :type '(directory)
467 :group 'nnir)
468
469 ;; Namazu engine, see <URL:http://www.namazu.org/>
470
471 (defcustom nnir-namazu-program "namazu"
472 "*Name of Namazu search executable."
473 :type '(string)
474 :group 'nnir)
475
476 (defcustom nnir-namazu-index-directory (expand-file-name "~/Mail/namazu/")
477 "*Index directory for Namazu."
478 :type '(directory)
479 :group 'nnir)
480
481 (defcustom nnir-namazu-additional-switches '()
482 "*A list of strings, to be given as additional arguments to namazu.
483 The switches `-q', `-a', and `-s' are always used, very few other switches
484 make any sense in this context.
485
486 Note that this should be a list. Ie, do NOT use the following:
487 (setq nnir-namazu-additional-switches \"-i -w\") ; wrong
488 Instead, use this:
489 (setq nnir-namazu-additional-switches '(\"-i\" \"-w\"))"
490 :type '(repeat (string))
491 :group 'nnir)
492
493 (defcustom nnir-namazu-remove-prefix (concat (getenv "HOME") "/Mail/")
494 "*The prefix to remove from each file name returned by Namazu
495 in order to get a group name (albeit with / instead of .).
496
497 For example, suppose that Namazu returns file names such as
498 \"/home/john/Mail/mail/misc/42\". For this example, use the following
499 setting: (setq nnir-namazu-remove-prefix \"/home/john/Mail/\")
500 Note the trailing slash. Removing this prefix gives \"mail/misc/42\".
501 `nnir' knows to remove the \"/42\" and to replace \"/\" with \".\" to
502 arrive at the correct group name, \"mail.misc\"."
503 :type '(directory)
504 :group 'nnir)
505
506 (defcustom nnir-notmuch-program "notmuch"
507 "*Name of notmuch search executable."
508 :version "24.1"
509 :type '(string)
510 :group 'nnir)
511
512 (defcustom nnir-notmuch-additional-switches '()
513 "*A list of strings, to be given as additional arguments to notmuch.
514
515 Note that this should be a list. Ie, do NOT use the following:
516 (setq nnir-notmuch-additional-switches \"-i -w\") ; wrong
517 Instead, use this:
518 (setq nnir-notmuch-additional-switches '(\"-i\" \"-w\"))"
519 :version "24.1"
520 :type '(repeat (string))
521 :group 'nnir)
522
523 (defcustom nnir-notmuch-remove-prefix (concat (getenv "HOME") "/Mail/")
524 "*The prefix to remove from each file name returned by notmuch
525 in order to get a group name (albeit with / instead of .). This is a
526 regular expression.
527
528 This variable is very similar to `nnir-namazu-remove-prefix', except
529 that it is for notmuch, not Namazu."
530 :version "24.1"
531 :type '(regexp)
532 :group 'nnir)
533
534 ;;; Developer Extension Variable:
535
536 (defvar nnir-engines
537 `((imap nnir-run-imap
538 ((criteria
539 "Imap Search in" ; Prompt
540 ,(mapcar 'car nnir-imap-search-arguments) ; alist for completing
541 nil ; allow any user input
542 nil ; initial value
543 nnir-imap-search-argument-history ; the history to use
544 ,nnir-imap-default-search-key ; default
545 )))
546 (gmane nnir-run-gmane
547 ((gmane-author . "Gmane Author: ")))
548 (swish++ nnir-run-swish++
549 ((swish++-group . "Swish++ Group spec (regexp): ")))
550 (swish-e nnir-run-swish-e
551 ((swish-e-group . "Swish-e Group spec (regexp): ")))
552 (namazu nnir-run-namazu
553 ())
554 (notmuch nnir-run-notmuch
555 ())
556 (hyrex nnir-run-hyrex
557 ((hyrex-group . "Hyrex Group spec (regexp): ")))
558 (find-grep nnir-run-find-grep
559 ((grep-options . "Grep options: "))))
560 "Alist of supported search engines.
561 Each element in the alist is a three-element list (ENGINE FUNCTION ARGS).
562 ENGINE is a symbol designating the searching engine. FUNCTION is also
563 a symbol, giving the function that does the search. The third element
564 ARGS is a list of cons pairs (PARAM . PROMPT). When issuing a query,
565 the FUNCTION will issue a query for each of the PARAMs, using PROMPT.
566
567 The value of `nnir-search-engine' must be one of the ENGINE symbols.
568 For example, for searching a server using namazu include
569 (nnir-search-engine namazu)
570 in the server definition. Note that you have to set additional
571 variables for most backends. For example, the `namazu' backend
572 needs the variables `nnir-namazu-program',
573 `nnir-namazu-index-directory' and `nnir-namazu-remove-prefix'.
574
575 Add an entry here when adding a new search engine.")
576
577 (defcustom nnir-method-default-engines '((nnimap . imap) (nntp . gmane))
578 "*Alist of default search engines keyed by server method."
579 :version "24.1"
580 :group 'nnir
581 :type `(repeat (cons (choice (const nnimap) (const nntp) (const nnspool)
582 (const nneething) (const nndir) (const nnmbox)
583 (const nnml) (const nnmh) (const nndraft)
584 (const nnfolder) (const nnmaildir))
585 (choice
586 ,@(mapcar (lambda (elem) (list 'const (car elem)))
587 nnir-engines)))))
588
589 ;; Gnus glue.
590
591 (defun gnus-group-make-nnir-group (nnir-extra-parms &optional specs)
592 "Create an nnir group. Prompt for a search query and determine
593 the groups to search as follows: if called from the *Server*
594 buffer search all groups belonging to the server on the current
595 line; if called from the *Group* buffer search any marked groups,
596 or the group on the current line, or all the groups under the
597 current topic. Calling with a prefix-arg prompts for additional
598 search-engine specific constraints. A non-nil `specs' arg must be
599 an alist with `nnir-query-spec' and `nnir-group-spec' keys, and
600 skips all prompting."
601 (interactive "P")
602 (let* ((group-spec
603 (or (cdr (assq 'nnir-group-spec specs))
604 (if (gnus-server-server-name)
605 (list (list (gnus-server-server-name)))
606 (nnir-categorize
607 (or gnus-group-marked
608 (if (gnus-group-group-name)
609 (list (gnus-group-group-name))
610 (cdr (assoc (gnus-group-topic-name) gnus-topic-alist))))
611 gnus-group-server))))
612 (query-spec
613 (or (cdr (assq 'nnir-query-spec specs))
614 (apply
615 'append
616 (list (cons 'query
617 (read-string "Query: " nil 'nnir-search-history)))
618 (when nnir-extra-parms
619 (mapcar
620 (lambda (x)
621 (nnir-read-parms (nnir-server-to-search-engine (car x))))
622 group-spec))))))
623 (gnus-group-read-ephemeral-group
624 (concat "nnir-" (message-unique-id))
625 (list 'nnir "nnir")
626 nil
627 ; (cons (current-buffer) gnus-current-window-configuration)
628 nil
629 nil nil
630 (list
631 (cons 'nnir-specs (list (cons 'nnir-query-spec query-spec)
632 (cons 'nnir-group-spec group-spec)))
633 (cons 'nnir-artlist nil)))))
634
635 (defun gnus-summary-make-nnir-group (nnir-extra-parms)
636 "Search a group from the summary buffer."
637 (interactive "P")
638 (gnus-warp-to-article)
639 (let ((spec
640 (list
641 (cons 'nnir-group-spec
642 (list (list
643 (gnus-group-server gnus-newsgroup-name)
644 (list gnus-newsgroup-name)))))))
645 (gnus-group-make-nnir-group nnir-extra-parms spec)))
646
647
648 ;; Gnus backend interface functions.
649
650 (deffoo nnir-open-server (server &optional definitions)
651 ;; Just set the server variables appropriately.
652 (let ((backend (car (gnus-server-to-method server))))
653 (if backend
654 (nnoo-change-server backend server definitions)
655 (add-hook 'gnus-summary-mode-hook 'nnir-mode)
656 (nnoo-change-server 'nnir server definitions))))
657
658 (deffoo nnir-request-group (group &optional server dont-check info)
659 (nnir-possibly-change-group group server)
660 (let ((pgroup (gnus-group-guess-full-name-from-command-method group))
661 length)
662 ;; Check for cached search result or run the query and cache the
663 ;; result.
664 (unless (and nnir-artlist dont-check)
665 (gnus-group-set-parameter
666 pgroup 'nnir-artlist
667 (setq nnir-artlist
668 (nnir-run-query
669 (gnus-group-get-parameter pgroup 'nnir-specs t))))
670 (nnir-request-update-info pgroup (gnus-get-info pgroup)))
671 (with-current-buffer nntp-server-buffer
672 (if (zerop (setq length (nnir-artlist-length nnir-artlist)))
673 (progn
674 (nnir-close-group group)
675 (nnheader-report 'nnir "Search produced empty results."))
676 (nnheader-insert "211 %d %d %d %s\n"
677 length ; total #
678 1 ; first #
679 length ; last #
680 group)))) ; group name
681 nnir-artlist)
682
683 (deffoo nnir-retrieve-headers (articles &optional group server fetch-old)
684 (with-current-buffer nntp-server-buffer
685 (let ((gnus-inhibit-demon t)
686 (articles-by-group (nnir-categorize
687 articles nnir-article-group nnir-article-ids))
688 headers)
689 (while (not (null articles-by-group))
690 (let* ((group-articles (pop articles-by-group))
691 (artgroup (car group-articles))
692 (articleids (cadr group-articles))
693 (artlist (sort (mapcar 'cdr articleids) '<))
694 (server (gnus-group-server artgroup))
695 (gnus-override-method (gnus-server-to-method server))
696 parsefunc)
697 ;; (nnir-possibly-change-group nil server)
698 (erase-buffer)
699 (case (setq gnus-headers-retrieved-by
700 (or
701 (and
702 nnir-retrieve-headers-override-function
703 (funcall nnir-retrieve-headers-override-function
704 artlist artgroup))
705 (gnus-retrieve-headers artlist artgroup nil)))
706 (nov
707 (setq parsefunc 'nnheader-parse-nov))
708 (headers
709 (setq parsefunc 'nnheader-parse-head))
710 (t (error "Unknown header type %s while requesting articles \
711 of group %s" gnus-headers-retrieved-by artgroup)))
712 (goto-char (point-min))
713 (while (not (eobp))
714 (let* ((novitem (funcall parsefunc))
715 (artno (and novitem
716 (mail-header-number novitem)))
717 (art (car (rassq artno articleids))))
718 (when art
719 (mail-header-set-number novitem art)
720 (push novitem headers))
721 (forward-line 1)))))
722 (setq headers
723 (sort headers
724 (lambda (x y)
725 (< (mail-header-number x) (mail-header-number y)))))
726 (erase-buffer)
727 (mapc 'nnheader-insert-nov headers)
728 'nov)))
729
730 (deffoo nnir-request-article (article &optional group server to-buffer)
731 (nnir-possibly-change-group group server)
732 (if (and (stringp article)
733 (not (eq 'nnimap (car (gnus-server-to-method server)))))
734 (nnheader-report
735 'nnir
736 "nnir-request-article only groks message ids for nnimap servers: %s"
737 server)
738 (save-excursion
739 (let ((article article)
740 query)
741 (when (stringp article)
742 (setq gnus-override-method (gnus-server-to-method server))
743 (setq query
744 (list
745 (cons 'query (format "HEADER Message-ID %s" article))
746 (cons 'criteria "")
747 (cons 'shortcut t)))
748 (unless (and nnir-artlist (equal query nnir-memo-query)
749 (equal server nnir-memo-server))
750 (setq nnir-artlist (nnir-run-imap query server)
751 nnir-memo-query query
752 nnir-memo-server server))
753 (setq article 1))
754 (unless (zerop (nnir-artlist-length nnir-artlist))
755 (let ((artfullgroup (nnir-article-group article))
756 (artno (nnir-article-number article)))
757 (message "Requesting article %d from group %s"
758 artno artfullgroup)
759 (if to-buffer
760 (with-current-buffer to-buffer
761 (let ((gnus-article-decode-hook nil))
762 (gnus-request-article-this-buffer artno artfullgroup)))
763 (gnus-request-article artno artfullgroup))
764 (cons artfullgroup artno)))))))
765
766 (deffoo nnir-request-move-article (article group server accept-form
767 &optional last internal-move-group)
768 (nnir-possibly-change-group group server)
769 (let* ((artfullgroup (nnir-article-group article))
770 (artno (nnir-article-number article))
771 (to-newsgroup (nth 1 accept-form))
772 (to-method (gnus-find-method-for-group to-newsgroup))
773 (from-method (gnus-find-method-for-group artfullgroup))
774 (move-is-internal (gnus-server-equal from-method to-method)))
775 (unless (gnus-check-backend-function
776 'request-move-article artfullgroup)
777 (error "The group %s does not support article moving" artfullgroup))
778 (gnus-request-move-article
779 artno
780 artfullgroup
781 (nth 1 from-method)
782 accept-form
783 last
784 (and move-is-internal
785 to-newsgroup ; Not respooling
786 (gnus-group-real-name to-newsgroup)))))
787
788 (deffoo nnir-request-expire-articles (articles group &optional server force)
789 (nnir-possibly-change-group group server)
790 (if force
791 (let ((articles-by-group (nnir-categorize
792 articles nnir-article-group nnir-article-ids))
793 not-deleted)
794 (while (not (null articles-by-group))
795 (let* ((group-articles (pop articles-by-group))
796 (artgroup (car group-articles))
797 (articleids (cadr group-articles))
798 (artlist (sort (mapcar 'cdr articleids) '<)))
799 (unless (gnus-check-backend-function 'request-expire-articles
800 artgroup)
801 (error "The group %s does not support article deletion" artgroup))
802 (unless (gnus-check-server (gnus-find-method-for-group artgroup))
803 (error "Couldn't open server for group %s" artgroup))
804 (push (gnus-request-expire-articles
805 artlist artgroup force)
806 not-deleted)))
807 (sort (delq nil not-deleted) '<))
808 articles))
809
810 (deffoo nnir-warp-to-article ()
811 (nnir-possibly-change-group gnus-newsgroup-name)
812 (let* ((cur (if (> (gnus-summary-article-number) 0)
813 (gnus-summary-article-number)
814 (error "Can't warp to a pseudo-article")))
815 (backend-article-group (nnir-article-group cur))
816 (backend-article-number (nnir-article-number cur))
817 (quit-config (gnus-ephemeral-group-p gnus-newsgroup-name)))
818
819 ;; what should we do here? we could leave all the buffers around
820 ;; and assume that we have to exit from them one by one. or we can
821 ;; try to clean up directly
822
823 ;;first exit from the nnir summary buffer.
824 ; (gnus-summary-exit)
825 ;; and if the nnir summary buffer in turn came from another
826 ;; summary buffer we have to clean that summary up too.
827 ; (when (not (eq (cdr quit-config) 'group))
828 ; (gnus-summary-exit))
829 (gnus-summary-read-group-1 backend-article-group t t nil
830 nil (list backend-article-number))))
831
832 (deffoo nnir-request-update-mark (group article mark)
833 (let ((artgroup (nnir-article-group article))
834 (artnumber (nnir-article-number article)))
835 (when (and artgroup artnumber)
836 (gnus-request-update-mark artgroup artnumber mark))))
837
838 (deffoo nnir-request-set-mark (group actions &optional server)
839 (nnir-possibly-change-group group server)
840 (let (mlist)
841 (dolist (action actions)
842 (destructuring-bind (range action marks) action
843 (let ((articles-by-group (nnir-categorize
844 (gnus-uncompress-range range)
845 nnir-article-group nnir-article-number)))
846 (dolist (artgroup articles-by-group)
847 (push (list
848 (car artgroup)
849 (list (gnus-compress-sequence
850 (sort (cadr artgroup) '<)) action marks)) mlist)))))
851 (dolist (request (nnir-categorize mlist car cadr))
852 (gnus-request-set-mark (car request) (cadr request)))))
853
854
855 (deffoo nnir-request-update-info (group info &optional server)
856 (nnir-possibly-change-group group server)
857 ;; clear out all existing marks.
858 (gnus-info-set-marks info nil)
859 (gnus-info-set-read info nil)
860 (let ((group (gnus-group-guess-full-name-from-command-method group))
861 (articles-by-group
862 (nnir-categorize
863 (gnus-uncompress-range (cons 1 (nnir-artlist-length nnir-artlist)))
864 nnir-article-group nnir-article-ids)))
865 (gnus-set-active group
866 (cons 1 (nnir-artlist-length nnir-artlist)))
867 (while (not (null articles-by-group))
868 (let* ((group-articles (pop articles-by-group))
869 (articleids (reverse (cadr group-articles)))
870 (group-info (gnus-get-info (car group-articles)))
871 (marks (gnus-info-marks group-info))
872 (read (gnus-info-read group-info)))
873 (gnus-info-set-read
874 info
875 (gnus-add-to-range
876 (gnus-info-read info)
877 (delq nil
878 (mapcar
879 #'(lambda (art)
880 (when (gnus-member-of-range (cdr art) read) (car art)))
881 articleids))))
882 (dolist (mark marks)
883 (destructuring-bind (type . range) mark
884 (gnus-add-marked-articles
885 group type
886 (delq nil
887 (mapcar
888 #'(lambda (art)
889 (when (gnus-member-of-range (cdr art) range) (car art)))
890 articleids)))))))))
891
892
893 (deffoo nnir-close-group (group &optional server)
894 (nnir-possibly-change-group group server)
895 (let ((pgroup (gnus-group-guess-full-name-from-command-method group)))
896 (when (and nnir-artlist (not (gnus-ephemeral-group-p pgroup)))
897 (gnus-group-set-parameter pgroup 'nnir-artlist nnir-artlist))
898 (setq nnir-artlist nil)
899 (when (gnus-ephemeral-group-p pgroup)
900 (gnus-kill-ephemeral-group pgroup)
901 (setq gnus-ephemeral-servers
902 (delq (assq 'nnir gnus-ephemeral-servers)
903 gnus-ephemeral-servers)))))
904 ;; (gnus-opened-servers-remove
905 ;; (car (assoc '(nnir "nnir-ephemeral" (nnir-address "nnir"))
906 ;; gnus-opened-servers))))
907
908
909
910
911 (defmacro nnir-add-result (dirnam artno score prefix server artlist)
912 "Ask `nnir-compose-result' to construct a result vector,
913 and if it is non-nil, add it to artlist."
914 `(let ((result (nnir-compose-result ,dirnam ,artno ,score ,prefix ,server)))
915 (when (not (null result))
916 (push result ,artlist))))
917
918 (autoload 'nnmaildir-base-name-to-article-number "nnmaildir")
919
920 ;; Helper function currently used by the Swish++ and Namazu backends;
921 ;; perhaps useful for other backends as well
922 (defun nnir-compose-result (dirnam article score prefix server)
923 "Extract the group from dirnam, and create a result vector
924 ready to be added to the list of search results."
925
926 ;; remove nnir-*-remove-prefix from beginning of dirnam filename
927 (when (string-match (concat "^" prefix) dirnam)
928 (setq dirnam (replace-match "" t t dirnam)))
929
930 (when (file-readable-p (concat prefix dirnam article))
931 ;; remove trailing slash and, for nnmaildir, cur/new/tmp
932 (setq dirnam
933 (substring dirnam 0
934 (if (string-match "\\`nnmaildir:" (gnus-group-server server))
935 -5 -1)))
936
937 ;; Set group to dirnam without any leading dots or slashes,
938 ;; and with all subsequent slashes replaced by dots
939 (let ((group (gnus-replace-in-string
940 (gnus-replace-in-string dirnam "^[./\\]" "" t)
941 "[/\\]" "." t)))
942
943 (vector (gnus-group-full-name group server)
944 (if (string-match "\\`nnmaildir:" (gnus-group-server server))
945 (nnmaildir-base-name-to-article-number
946 (substring article 0 (string-match ":" article))
947 group nil)
948 (string-to-number article))
949 (string-to-number score)))))
950
951 ;;; Search Engine Interfaces:
952
953 ;; imap interface
954 (defun nnir-run-imap (query srv &optional groups)
955 "Run a search against an IMAP back-end server.
956 This uses a custom query language parser; see `nnir-imap-make-query' for
957 details on the language and supported extensions."
958 (save-excursion
959 (let ((qstring (cdr (assq 'query query)))
960 (server (cadr (gnus-server-to-method srv)))
961 (defs (caddr (gnus-server-to-method srv)))
962 (criteria (or (cdr (assq 'criteria query))
963 (cdr (assoc nnir-imap-default-search-key
964 nnir-imap-search-arguments))))
965 (gnus-inhibit-demon t)
966 (groups (or groups (nnir-get-active srv))))
967 (message "Opening server %s" server)
968 (apply
969 'vconcat
970 (catch 'found
971 (mapcar
972 #'(lambda (group)
973 (let (artlist)
974 (condition-case ()
975 (when (nnimap-change-group
976 (gnus-group-short-name group) server)
977 (with-current-buffer (nnimap-buffer)
978 (message "Searching %s..." group)
979 (let* ((arts 0)
980 (literal+ (nnimap-capability "LITERAL+"))
981 (search (split-string
982 (if (string= criteria "")
983 qstring
984 (nnir-imap-make-query
985 criteria qstring))
986 "\n"))
987 (coding (upcase
988 (replace-regexp-in-string
989 "-\\(unix\\|dos\\|mac\\)" ""
990 (symbol-name
991 (cdr default-process-coding-system)))))
992 call result)
993 (setq call (nnimap-send-command
994 "UID SEARCH CHARSET %s %s" coding (pop search)))
995 (while search ; Non-ascii search terms
996 (unless literal+
997 (nnimap-wait-for-line "^\\+\\(.*\\)\n"))
998 (process-send-string (get-buffer-process (current-buffer)) (pop search))
999 (process-send-string (get-buffer-process (current-buffer))
1000 (if (nnimap-newlinep nnimap-object)
1001 "\n"
1002 "\r\n")))
1003 (setq result (nnimap-get-response call))
1004 (mapc
1005 (lambda (artnum)
1006 (let ((artn (string-to-number artnum)))
1007 (when (> artn 0)
1008 (push (vector group artn 100)
1009 artlist)
1010 (when (assq 'shortcut query)
1011 (throw 'found (list artlist)))
1012 (setq arts (1+ arts)))))
1013 (and (car result)
1014 (cdr (assoc "SEARCH" (cdr result)))))
1015 (message "Searching %s... %d matches" group arts)))
1016 (message "Searching %s...done" group))
1017 (quit nil))
1018 (nreverse artlist)))
1019 groups))))))
1020
1021 (defun nnir-imap-make-query (criteria qstring)
1022 "Parse the query string and criteria into an appropriate IMAP search
1023 expression, returning the string query to make.
1024
1025 This implements a little language designed to return the expected results
1026 to an arbitrary query string to the end user.
1027
1028 The search is always case-insensitive, as defined by RFC2060, and supports
1029 the following features (inspired by the Google search input language):
1030
1031 Automatic \"and\" queries
1032 If you specify multiple words then they will be treated as an \"and\"
1033 expression intended to match all components.
1034
1035 Phrase searches
1036 If you wrap your query in double-quotes then it will be treated as a
1037 literal string.
1038
1039 Negative terms
1040 If you precede a term with \"-\" then it will negate that.
1041
1042 \"OR\" queries
1043 If you include an upper-case \"OR\" in your search it will cause the
1044 term before it and the term after it to be treated as alternatives.
1045
1046 In future the following will be added to the language:
1047 * support for date matches
1048 * support for location of text matching within the query
1049 * from/to/etc headers
1050 * additional search terms
1051 * flag based searching
1052 * anything else that the RFC supports, basically."
1053 ;; Walk through the query and turn it into an IMAP query string.
1054 (nnir-imap-query-to-imap criteria (nnir-imap-parse-query qstring)))
1055
1056
1057 (defun nnir-imap-query-to-imap (criteria query)
1058 "Turn a s-expression format query into IMAP."
1059 (mapconcat
1060 ;; Turn the expressions into IMAP text
1061 (lambda (item)
1062 (nnir-imap-expr-to-imap criteria item))
1063 ;; The query, already in s-expr format.
1064 query
1065 ;; Append a space between each expression
1066 " "))
1067
1068
1069 (defun nnir-imap-expr-to-imap (criteria expr)
1070 "Convert EXPR into an IMAP search expression on CRITERIA"
1071 ;; What sort of expression is this, eh?
1072 (let ((literal+ (nnimap-capability "LITERAL+")))
1073 (cond
1074 ;; Simple string term
1075 ((stringp expr)
1076 (format "%s %S" criteria expr))
1077 ;; Trivial term: and
1078 ((eq expr 'and) nil)
1079 ;; Composite term: or expression
1080 ((eq (car-safe expr) 'or)
1081 (format "OR %s %s"
1082 (nnir-imap-expr-to-imap criteria (second expr))
1083 (nnir-imap-expr-to-imap criteria (third expr))))
1084 ;; Composite term: just the fax, mam
1085 ((eq (car-safe expr) 'not)
1086 (format "NOT (%s)" (nnir-imap-query-to-imap criteria (rest expr))))
1087 ;; Composite term: non-ascii search term
1088 ((numberp (car-safe expr))
1089 (format "%s {%d%s}\n%s" criteria (car expr)
1090 (if literal+ "+" "") (second expr)))
1091 ;; Composite term: just expand it all.
1092 ((and (not (null expr)) (listp expr))
1093 (format "(%s)" (nnir-imap-query-to-imap criteria expr)))
1094 ;; Complex value, give up for now.
1095 (t (error "Unhandled input: %S" expr)))))
1096
1097
1098 (defun nnir-imap-parse-query (string)
1099 "Turn STRING into an s-expression based query based on the IMAP
1100 query language as defined in `nnir-imap-make-query'.
1101
1102 This involves turning individual tokens into higher level terms
1103 that the search language can then understand and use."
1104 (with-temp-buffer
1105 ;; Set up the parsing environment.
1106 (insert string)
1107 (goto-char (point-min))
1108 ;; Now, collect the output terms and return them.
1109 (let (out)
1110 (while (not (nnir-imap-end-of-input))
1111 (push (nnir-imap-next-expr) out))
1112 (reverse out))))
1113
1114
1115 (defun nnir-imap-next-expr (&optional count)
1116 "Return the next expression from the current buffer."
1117 (let ((term (nnir-imap-next-term count))
1118 (next (nnir-imap-peek-symbol)))
1119 ;; Are we looking at an 'or' expression?
1120 (cond
1121 ;; Handle 'expr or expr'
1122 ((eq next 'or)
1123 (list 'or term (nnir-imap-next-expr 2)))
1124 ;; Anything else
1125 (t term))))
1126
1127
1128 (defun nnir-imap-next-term (&optional count)
1129 "Return the next TERM from the current buffer."
1130 (let ((term (nnir-imap-next-symbol count)))
1131 ;; What sort of term is this?
1132 (cond
1133 ;; and -- just ignore it
1134 ((eq term 'and) 'and)
1135 ;; negated term
1136 ((eq term 'not) (list 'not (nnir-imap-next-expr)))
1137 ;; non-ascii search string
1138 ((and (stringp term)
1139 (not (= (string-bytes term)
1140 (length term))))
1141 (list (string-bytes term) term))
1142 ;; generic term
1143 (t term))))
1144
1145
1146 (defun nnir-imap-peek-symbol ()
1147 "Return the next symbol from the current buffer, but don't consume it."
1148 (save-excursion
1149 (nnir-imap-next-symbol)))
1150
1151 (defun nnir-imap-next-symbol (&optional count)
1152 "Return the next symbol from the current buffer, or nil if we are
1153 at the end of the buffer. If supplied COUNT skips some symbols before
1154 returning the one at the supplied position."
1155 (when (and (numberp count) (> count 1))
1156 (nnir-imap-next-symbol (1- count)))
1157 (let ((case-fold-search t))
1158 ;; end of input stream?
1159 (unless (nnir-imap-end-of-input)
1160 ;; No, return the next symbol from the stream.
1161 (cond
1162 ;; negated expression -- return it and advance one char.
1163 ((looking-at "-") (forward-char 1) 'not)
1164 ;; quoted string
1165 ((looking-at "\"") (nnir-imap-delimited-string "\""))
1166 ;; list expression -- we parse the content and return this as a list.
1167 ((looking-at "(")
1168 (nnir-imap-parse-query (nnir-imap-delimited-string ")")))
1169 ;; keyword input -- return a symbol version
1170 ((looking-at "\\band\\b") (forward-char 3) 'and)
1171 ((looking-at "\\bor\\b") (forward-char 2) 'or)
1172 ((looking-at "\\bnot\\b") (forward-char 3) 'not)
1173 ;; Simple, boring keyword
1174 (t (let ((start (point))
1175 (end (if (search-forward-regexp "[[:blank:]]" nil t)
1176 (prog1
1177 (match-beginning 0)
1178 ;; unskip if we hit a non-blank terminal character.
1179 (when (string-match "[^[:blank:]]" (match-string 0))
1180 (backward-char 1)))
1181 (goto-char (point-max)))))
1182 (buffer-substring start end)))))))
1183
1184 (defun nnir-imap-delimited-string (delimiter)
1185 "Return a delimited string from the current buffer."
1186 (let ((start (point)) end)
1187 (forward-char 1) ; skip the first delimiter.
1188 (while (not end)
1189 (unless (search-forward delimiter nil t)
1190 (error "Unmatched delimited input with %s in query" delimiter))
1191 (let ((here (point)))
1192 (unless (equal (buffer-substring (- here 2) (- here 1)) "\\")
1193 (setq end (point)))))
1194 (buffer-substring (1+ start) (1- end))))
1195
1196 (defun nnir-imap-end-of-input ()
1197 "Are we at the end of input?"
1198 (skip-chars-forward "[[:blank:]]")
1199 (looking-at "$"))
1200
1201
1202 ;; Swish++ interface.
1203 ;; -cc- Todo
1204 ;; Search by
1205 ;; - group
1206 ;; Sort by
1207 ;; - rank (default)
1208 ;; - article number
1209 ;; - file size
1210 ;; - group
1211 (defun nnir-run-swish++ (query server &optional group)
1212 "Run QUERY against swish++.
1213 Returns a vector of (group name, file name) pairs (also vectors,
1214 actually).
1215
1216 Tested with swish++ 4.7 on GNU/Linux and with swish++ 5.0b2 on
1217 Windows NT 4.0."
1218
1219 ;; (when group
1220 ;; (error "The swish++ backend cannot search specific groups"))
1221
1222 (save-excursion
1223 (let ( (qstring (cdr (assq 'query query)))
1224 (groupspec (cdr (assq 'swish++-group query)))
1225 (prefix (nnir-read-server-parm 'nnir-swish++-remove-prefix server))
1226 artlist
1227 ;; nnml-use-compressed-files might be any string, but probably this
1228 ;; is sufficient. Note that we can't only use the value of
1229 ;; nnml-use-compressed-files because old articles might have been
1230 ;; saved with a different value.
1231 (article-pattern (if (string-match "\\`nnmaildir:"
1232 (gnus-group-server server))
1233 ":[0-9]+"
1234 "^[0-9]+\\(\\.[a-z0-9]+\\)?$"))
1235 score artno dirnam filenam)
1236
1237 (when (equal "" qstring)
1238 (error "swish++: You didn't enter anything"))
1239
1240 (set-buffer (get-buffer-create nnir-tmp-buffer))
1241 (erase-buffer)
1242
1243 (if groupspec
1244 (message "Doing swish++ query %s on %s..." qstring groupspec)
1245 (message "Doing swish++ query %s..." qstring))
1246
1247 (let* ((cp-list `( ,nnir-swish++-program
1248 nil ; input from /dev/null
1249 t ; output
1250 nil ; don't redisplay
1251 "--config-file" ,(nnir-read-server-parm 'nnir-swish++-configuration-file server)
1252 ,@(nnir-read-server-parm 'nnir-swish++-additional-switches server)
1253 ,qstring ; the query, in swish++ format
1254 ))
1255 (exitstatus
1256 (progn
1257 (message "%s args: %s" nnir-swish++-program
1258 (mapconcat 'identity (cddddr cp-list) " ")) ;; ???
1259 (apply 'call-process cp-list))))
1260 (unless (or (null exitstatus)
1261 (zerop exitstatus))
1262 (nnheader-report 'nnir "Couldn't run swish++: %s" exitstatus)
1263 ;; swish++ failure reason is in this buffer, show it if
1264 ;; the user wants it.
1265 (when (> gnus-verbose 6)
1266 (display-buffer nnir-tmp-buffer))))
1267
1268 ;; The results are output in the format of:
1269 ;; V 4.7 Linux
1270 ;; rank relative-path-name file-size file-title
1271 ;; V 5.0b2:
1272 ;; rank relative-path-name file-size topic??
1273 ;; where rank is an integer from 1 to 100.
1274 (goto-char (point-min))
1275 (while (re-search-forward
1276 "\\(^[0-9]+\\) \\([^ ]+\\) [0-9]+ \\(.*\\)$" nil t)
1277 (setq score (match-string 1)
1278 filenam (match-string 2)
1279 artno (file-name-nondirectory filenam)
1280 dirnam (file-name-directory filenam))
1281
1282 ;; don't match directories
1283 (when (string-match article-pattern artno)
1284 (when (not (null dirnam))
1285
1286 ;; maybe limit results to matching groups.
1287 (when (or (not groupspec)
1288 (string-match groupspec dirnam))
1289 (nnir-add-result dirnam artno score prefix server artlist)))))
1290
1291 (message "Massaging swish++ output...done")
1292
1293 ;; Sort by score
1294 (apply 'vector
1295 (sort artlist
1296 (function (lambda (x y)
1297 (> (nnir-artitem-rsv x)
1298 (nnir-artitem-rsv y)))))))))
1299
1300 ;; Swish-E interface.
1301 (defun nnir-run-swish-e (query server &optional group)
1302 "Run given query against swish-e.
1303 Returns a vector of (group name, file name) pairs (also vectors,
1304 actually).
1305
1306 Tested with swish-e-2.0.1 on Windows NT 4.0."
1307
1308 ;; swish-e crashes with empty parameter to "-w" on commandline...
1309 ;; (when group
1310 ;; (error "The swish-e backend cannot search specific groups"))
1311
1312 (save-excursion
1313 (let ((qstring (cdr (assq 'query query)))
1314 (prefix
1315 (or (nnir-read-server-parm 'nnir-swish-e-remove-prefix server)
1316 (error "Missing parameter `nnir-swish-e-remove-prefix'")))
1317 artlist score artno dirnam group )
1318
1319 (when (equal "" qstring)
1320 (error "swish-e: You didn't enter anything"))
1321
1322 (set-buffer (get-buffer-create nnir-tmp-buffer))
1323 (erase-buffer)
1324
1325 (message "Doing swish-e query %s..." query)
1326 (let* ((index-files
1327 (or (nnir-read-server-parm
1328 'nnir-swish-e-index-files server)
1329 (error "Missing parameter `nnir-swish-e-index-files'")))
1330 (additional-switches
1331 (nnir-read-server-parm
1332 'nnir-swish-e-additional-switches server))
1333 (cp-list `(,nnir-swish-e-program
1334 nil ; input from /dev/null
1335 t ; output
1336 nil ; don't redisplay
1337 "-f" ,@index-files
1338 ,@additional-switches
1339 "-w"
1340 ,qstring ; the query, in swish-e format
1341 ))
1342 (exitstatus
1343 (progn
1344 (message "%s args: %s" nnir-swish-e-program
1345 (mapconcat 'identity (cddddr cp-list) " "))
1346 (apply 'call-process cp-list))))
1347 (unless (or (null exitstatus)
1348 (zerop exitstatus))
1349 (nnheader-report 'nnir "Couldn't run swish-e: %s" exitstatus)
1350 ;; swish-e failure reason is in this buffer, show it if
1351 ;; the user wants it.
1352 (when (> gnus-verbose 6)
1353 (display-buffer nnir-tmp-buffer))))
1354
1355 ;; The results are output in the format of:
1356 ;; rank path-name file-title file-size
1357 (goto-char (point-min))
1358 (while (re-search-forward
1359 "\\(^[0-9]+\\) \\([^ ]+\\) \"\\([^\"]+\\)\" [0-9]+$" nil t)
1360 (setq score (match-string 1)
1361 artno (match-string 3)
1362 dirnam (file-name-directory (match-string 2)))
1363
1364 ;; don't match directories
1365 (when (string-match "^[0-9]+$" artno)
1366 (when (not (null dirnam))
1367
1368 ;; remove nnir-swish-e-remove-prefix from beginning of dirname
1369 (when (string-match (concat "^" prefix) dirnam)
1370 (setq dirnam (replace-match "" t t dirnam)))
1371
1372 (setq dirnam (substring dirnam 0 -1))
1373 ;; eliminate all ".", "/", "\" from beginning. Always matches.
1374 (string-match "^[./\\]*\\(.*\\)$" dirnam)
1375 ;; "/" -> "."
1376 (setq group (gnus-replace-in-string (match-string 1 dirnam) "/" "."))
1377 ;; Windows "\\" -> "."
1378 (setq group (gnus-replace-in-string group "\\\\" "."))
1379
1380 (push (vector (gnus-group-full-name group server)
1381 (string-to-number artno)
1382 (string-to-number score))
1383 artlist))))
1384
1385 (message "Massaging swish-e output...done")
1386
1387 ;; Sort by score
1388 (apply 'vector
1389 (sort artlist
1390 (function (lambda (x y)
1391 (> (nnir-artitem-rsv x)
1392 (nnir-artitem-rsv y)))))))))
1393
1394 ;; HyREX interface
1395 (defun nnir-run-hyrex (query server &optional group)
1396 (save-excursion
1397 (let ((artlist nil)
1398 (groupspec (cdr (assq 'hyrex-group query)))
1399 (qstring (cdr (assq 'query query)))
1400 (prefix (nnir-read-server-parm 'nnir-hyrex-remove-prefix server))
1401 score artno dirnam)
1402 (when (and (not groupspec) group)
1403 (setq groupspec
1404 (regexp-opt
1405 (mapcar (lambda (x) (gnus-group-real-name x)) group))))
1406 (set-buffer (get-buffer-create nnir-tmp-buffer))
1407 (erase-buffer)
1408 (message "Doing hyrex-search query %s..." query)
1409 (let* ((cp-list
1410 `( ,nnir-hyrex-program
1411 nil ; input from /dev/null
1412 t ; output
1413 nil ; don't redisplay
1414 "-i",(nnir-read-server-parm 'nnir-hyrex-index-directory server) ; index directory
1415 ,@(nnir-read-server-parm 'nnir-hyrex-additional-switches server)
1416 ,qstring ; the query, in hyrex-search format
1417 ))
1418 (exitstatus
1419 (progn
1420 (message "%s args: %s" nnir-hyrex-program
1421 (mapconcat 'identity (cddddr cp-list) " "))
1422 (apply 'call-process cp-list))))
1423 (unless (or (null exitstatus)
1424 (zerop exitstatus))
1425 (nnheader-report 'nnir "Couldn't run hyrex-search: %s" exitstatus)
1426 ;; nnir-search failure reason is in this buffer, show it if
1427 ;; the user wants it.
1428 (when (> gnus-verbose 6)
1429 (display-buffer nnir-tmp-buffer)))) ;; FIXME: Don't clear buffer !
1430 (message "Doing hyrex-search query \"%s\"...done" qstring)
1431 (sit-for 0)
1432 ;; nnir-search returns:
1433 ;; for nnml/nnfolder: "filename mailid weight"
1434 ;; for nnimap: "group mailid weight"
1435 (goto-char (point-min))
1436 (delete-non-matching-lines "^\\S + [0-9]+ [0-9]+$")
1437 ;; HyREX doesn't search directly in groups -- so filter out here.
1438 (when groupspec
1439 (keep-lines groupspec))
1440 ;; extract data from result lines
1441 (goto-char (point-min))
1442 (while (re-search-forward
1443 "\\(\\S +\\) \\([0-9]+\\) \\([0-9]+\\)" nil t)
1444 (setq dirnam (match-string 1)
1445 artno (match-string 2)
1446 score (match-string 3))
1447 (when (string-match prefix dirnam)
1448 (setq dirnam (replace-match "" t t dirnam)))
1449 (push (vector (gnus-group-full-name
1450 (gnus-replace-in-string dirnam "/" ".") server)
1451 (string-to-number artno)
1452 (string-to-number score))
1453 artlist))
1454 (message "Massaging hyrex-search output...done.")
1455 (apply 'vector
1456 (sort artlist
1457 (function (lambda (x y)
1458 (if (string-lessp (nnir-artitem-group x)
1459 (nnir-artitem-group y))
1460 t
1461 (< (nnir-artitem-number x)
1462 (nnir-artitem-number y)))))))
1463 )))
1464
1465 ;; Namazu interface
1466 (defun nnir-run-namazu (query server &optional group)
1467 "Run given query against Namazu. Returns a vector of (group name, file name)
1468 pairs (also vectors, actually).
1469
1470 Tested with Namazu 2.0.6 on a GNU/Linux system."
1471 ;; (when group
1472 ;; (error "The Namazu backend cannot search specific groups"))
1473 (save-excursion
1474 (let ((article-pattern (if (string-match "\\`nnmaildir:"
1475 (gnus-group-server server))
1476 ":[0-9]+"
1477 "^[0-9]+$"))
1478 artlist
1479 (qstring (cdr (assq 'query query)))
1480 (prefix (nnir-read-server-parm 'nnir-namazu-remove-prefix server))
1481 score group article
1482 (process-environment (copy-sequence process-environment)))
1483 (setenv "LC_MESSAGES" "C")
1484 (set-buffer (get-buffer-create nnir-tmp-buffer))
1485 (erase-buffer)
1486 (let* ((cp-list
1487 `( ,nnir-namazu-program
1488 nil ; input from /dev/null
1489 t ; output
1490 nil ; don't redisplay
1491 "-q" ; don't be verbose
1492 "-a" ; show all matches
1493 "-s" ; use short format
1494 ,@(nnir-read-server-parm 'nnir-namazu-additional-switches server)
1495 ,qstring ; the query, in namazu format
1496 ,(nnir-read-server-parm 'nnir-namazu-index-directory server) ; index directory
1497 ))
1498 (exitstatus
1499 (progn
1500 (message "%s args: %s" nnir-namazu-program
1501 (mapconcat 'identity (cddddr cp-list) " "))
1502 (apply 'call-process cp-list))))
1503 (unless (or (null exitstatus)
1504 (zerop exitstatus))
1505 (nnheader-report 'nnir "Couldn't run namazu: %s" exitstatus)
1506 ;; Namazu failure reason is in this buffer, show it if
1507 ;; the user wants it.
1508 (when (> gnus-verbose 6)
1509 (display-buffer nnir-tmp-buffer))))
1510
1511 ;; Namazu output looks something like this:
1512 ;; 2. Re: Gnus agent expire broken (score: 55)
1513 ;; /home/henrik/Mail/mail/sent/1310 (4,138 bytes)
1514
1515 (goto-char (point-min))
1516 (while (re-search-forward
1517 "^\\([0-9,]+\\.\\).*\\((score: \\([0-9]+\\)\\))\n\\([^ ]+\\)"
1518 nil t)
1519 (setq score (match-string 3)
1520 group (file-name-directory (match-string 4))
1521 article (file-name-nondirectory (match-string 4)))
1522
1523 ;; make sure article and group is sane
1524 (when (and (string-match article-pattern article)
1525 (not (null group)))
1526 (nnir-add-result group article score prefix server artlist)))
1527
1528 ;; sort artlist by score
1529 (apply 'vector
1530 (sort artlist
1531 (function (lambda (x y)
1532 (> (nnir-artitem-rsv x)
1533 (nnir-artitem-rsv y)))))))))
1534
1535 (defun nnir-run-notmuch (query server &optional group)
1536 "Run QUERY against notmuch.
1537 Returns a vector of (group name, file name) pairs (also vectors,
1538 actually)."
1539
1540 ;; (when group
1541 ;; (error "The notmuch backend cannot search specific groups"))
1542
1543 (save-excursion
1544 (let ( (qstring (cdr (assq 'query query)))
1545 (groupspec (cdr (assq 'notmuch-group query)))
1546 (prefix (nnir-read-server-parm 'nnir-notmuch-remove-prefix server))
1547 artlist
1548 (article-pattern (if (string-match "\\`nnmaildir:"
1549 (gnus-group-server server))
1550 ":[0-9]+"
1551 "^[0-9]+$"))
1552 artno dirnam filenam)
1553
1554 (when (equal "" qstring)
1555 (error "notmuch: You didn't enter anything"))
1556
1557 (set-buffer (get-buffer-create nnir-tmp-buffer))
1558 (erase-buffer)
1559
1560 (if groupspec
1561 (message "Doing notmuch query %s on %s..." qstring groupspec)
1562 (message "Doing notmuch query %s..." qstring))
1563
1564 (let* ((cp-list `( ,nnir-notmuch-program
1565 nil ; input from /dev/null
1566 t ; output
1567 nil ; don't redisplay
1568 "search"
1569 "--format=text"
1570 "--output=files"
1571 ,@(nnir-read-server-parm 'nnir-notmuch-additional-switches server)
1572 ,qstring ; the query, in notmuch format
1573 ))
1574 (exitstatus
1575 (progn
1576 (message "%s args: %s" nnir-notmuch-program
1577 (mapconcat 'identity (cddddr cp-list) " ")) ;; ???
1578 (apply 'call-process cp-list))))
1579 (unless (or (null exitstatus)
1580 (zerop exitstatus))
1581 (nnheader-report 'nnir "Couldn't run notmuch: %s" exitstatus)
1582 ;; notmuch failure reason is in this buffer, show it if
1583 ;; the user wants it.
1584 (when (> gnus-verbose 6)
1585 (display-buffer nnir-tmp-buffer))))
1586
1587 ;; The results are output in the format of:
1588 ;; absolute-path-name
1589 (goto-char (point-min))
1590 (while (not (eobp))
1591 (setq filenam (buffer-substring-no-properties (line-beginning-position)
1592 (line-end-position))
1593 artno (file-name-nondirectory filenam)
1594 dirnam (file-name-directory filenam))
1595 (forward-line 1)
1596
1597 ;; don't match directories
1598 (when (string-match article-pattern artno)
1599 (when (not (null dirnam))
1600
1601 ;; maybe limit results to matching groups.
1602 (when (or (not groupspec)
1603 (string-match groupspec dirnam))
1604 (nnir-add-result dirnam artno "" prefix server artlist)))))
1605
1606 (message "Massaging notmuch output...done")
1607
1608 artlist)))
1609
1610 (defun nnir-run-find-grep (query server &optional grouplist)
1611 "Run find and grep to obtain matching articles."
1612 (let* ((method (gnus-server-to-method server))
1613 (sym (intern
1614 (concat (symbol-name (car method)) "-directory")))
1615 (directory (cadr (assoc sym (cddr method))))
1616 (regexp (cdr (assoc 'query query)))
1617 (grep-options (cdr (assoc 'grep-options query)))
1618 (grouplist (or grouplist (nnir-get-active server))))
1619 (unless directory
1620 (error "No directory found in method specification of server %s"
1621 server))
1622 (apply
1623 'vconcat
1624 (mapcar (lambda (x)
1625 (let ((group x)
1626 artlist)
1627 (message "Searching %s using find-grep..."
1628 (or group server))
1629 (save-window-excursion
1630 (set-buffer (get-buffer-create nnir-tmp-buffer))
1631 (if (> gnus-verbose 6)
1632 (pop-to-buffer (current-buffer)))
1633 (cd directory) ; Using relative paths simplifies
1634 ; postprocessing.
1635 (let ((group
1636 (if (not group)
1637 "."
1638 ;; Try accessing the group literally as
1639 ;; well as interpreting dots as directory
1640 ;; separators so the engine works with
1641 ;; plain nnml as well as the Gnus Cache.
1642 (let ((group (gnus-group-real-name group)))
1643 ;; Replace cl-func find-if.
1644 (if (file-directory-p group)
1645 group
1646 (if (file-directory-p
1647 (setq group
1648 (gnus-replace-in-string
1649 group
1650 "\\." "/" t)))
1651 group))))))
1652 (unless group
1653 (error "Cannot locate directory for group"))
1654 (save-excursion
1655 (apply
1656 'call-process "find" nil t
1657 "find" group "-maxdepth" "1" "-type" "f"
1658 "-name" "[0-9]*" "-exec"
1659 "grep"
1660 `("-l" ,@(and grep-options
1661 (split-string grep-options "\\s-" t))
1662 "-e" ,regexp "{}" "+"))))
1663
1664 ;; Translate relative paths to group names.
1665 (while (not (eobp))
1666 (let* ((path (split-string
1667 (buffer-substring
1668 (point)
1669 (line-end-position)) "/" t))
1670 (art (string-to-number (car (last path)))))
1671 (while (string= "." (car path))
1672 (setq path (cdr path)))
1673 (let ((group (mapconcat 'identity
1674 ;; Replace cl-func:
1675 ;; (subseq path 0 -1)
1676 (let ((end (1- (length path)))
1677 res)
1678 (while
1679 (>= (setq end (1- end)) 0)
1680 (push (pop path) res))
1681 (nreverse res))
1682 ".")))
1683 (push
1684 (vector (gnus-group-full-name group server) art 0)
1685 artlist))
1686 (forward-line 1)))
1687 (message "Searching %s using find-grep...done"
1688 (or group server))
1689 artlist)))
1690 grouplist))))
1691
1692 (declare-function mm-url-insert "mm-url" (url &optional follow-refresh))
1693 (declare-function mm-url-encode-www-form-urlencoded "mm-url" (pairs))
1694
1695 ;; gmane interface
1696 (defun nnir-run-gmane (query srv &optional groups)
1697 "Run a search against a gmane back-end server."
1698 (let* ((case-fold-search t)
1699 (qstring (cdr (assq 'query query)))
1700 (server (cadr (gnus-server-to-method srv)))
1701 (groupspec (mapconcat
1702 (lambda (x)
1703 (if (gnus-string-match-p "gmane" x)
1704 (format "group:%s" (gnus-group-short-name x))
1705 (error "Can't search non-gmane groups: %s" x)))
1706 groups " "))
1707 (authorspec
1708 (if (assq 'gmane-author query)
1709 (format "author:%s" (cdr (assq 'gmane-author query))) ""))
1710 (search (format "%s %s %s"
1711 qstring groupspec authorspec))
1712 (gnus-inhibit-demon t)
1713 artlist)
1714 (require 'mm-url)
1715 (with-current-buffer (get-buffer-create nnir-tmp-buffer)
1716 (erase-buffer)
1717 (mm-url-insert
1718 (concat
1719 "http://search.gmane.org/nov.php"
1720 "?"
1721 (mm-url-encode-www-form-urlencoded
1722 `(("query" . ,search)
1723 ("HITSPERPAGE" . "999")))))
1724 (unless (featurep 'xemacs) (set-buffer-multibyte t))
1725 (mm-decode-coding-region (point-min) (point-max) 'utf-8)
1726 (goto-char (point-min))
1727 (forward-line 1)
1728 (while (not (eobp))
1729 (unless (or (eolp) (looking-at "\x0d"))
1730 (let ((header (nnheader-parse-nov)))
1731 (let ((xref (mail-header-xref header))
1732 (xscore (string-to-number (cdr (assoc 'X-Score
1733 (mail-header-extra header))))))
1734 (when (string-match " \\([^:]+\\)[:/]\\([0-9]+\\)" xref)
1735 (push
1736 (vector
1737 (gnus-group-prefixed-name (match-string 1 xref) srv)
1738 (string-to-number (match-string 2 xref)) xscore)
1739 artlist)))))
1740 (forward-line 1)))
1741 (apply 'vector (nreverse (mm-delete-duplicates artlist)))))
1742
1743 ;;; Util Code:
1744
1745 (defun gnus-nnir-group-p (group)
1746 "Say whether GROUP is nnir or not."
1747 (if (gnus-group-prefixed-p group)
1748 (eq 'nnir (car (gnus-find-method-for-group group)))
1749 (and group (string-match "^nnir" group))))
1750
1751 (defun nnir-read-parms (nnir-search-engine)
1752 "Reads additional search parameters according to `nnir-engines'."
1753 (let ((parmspec (caddr (assoc nnir-search-engine nnir-engines))))
1754 (mapcar 'nnir-read-parm parmspec)))
1755
1756 (defun nnir-read-parm (parmspec)
1757 "Reads a single search parameter.
1758 `parmspec' is a cons cell, the car is a symbol, the cdr is a prompt."
1759 (let ((sym (car parmspec))
1760 (prompt (cdr parmspec)))
1761 (if (listp prompt)
1762 (let* ((result (apply 'gnus-completing-read prompt))
1763 (mapping (or (assoc result nnir-imap-search-arguments)
1764 (cons nil nnir-imap-search-other))))
1765 (cons sym (format (cdr mapping) result)))
1766 (cons sym (read-string prompt)))))
1767
1768 (defun nnir-run-query (specs)
1769 "Invoke appropriate search engine function (see `nnir-engines')."
1770 (apply 'vconcat
1771 (mapcar
1772 (lambda (x)
1773 (let* ((server (car x))
1774 (search-engine (nnir-server-to-search-engine server))
1775 (search-func (cadr (assoc search-engine nnir-engines))))
1776 (and search-func
1777 (funcall search-func (cdr (assq 'nnir-query-spec specs))
1778 server (cadr x)))))
1779 (cdr (assq 'nnir-group-spec specs)))))
1780
1781 (defun nnir-server-to-search-engine (server)
1782 (or (nnir-read-server-parm 'nnir-search-engine server t)
1783 (cdr (assoc (car (gnus-server-to-method server))
1784 nnir-method-default-engines))))
1785
1786 (defun nnir-read-server-parm (key server &optional not-global)
1787 "Returns the parameter value corresponding to `key' for
1788 `server'. If no server-specific value is found consult the global
1789 environment unless `not-global' is non-nil."
1790 (let ((method (gnus-server-to-method server)))
1791 (cond ((and method (assq key (cddr method)))
1792 (nth 1 (assq key (cddr method))))
1793 ((and (not not-global) (boundp key)) (symbol-value key))
1794 (t nil))))
1795
1796 (defun nnir-possibly-change-group (group &optional server)
1797 (or (not server) (nnir-server-opened server) (nnir-open-server server))
1798 (when (gnus-nnir-group-p group)
1799 (setq nnir-artlist (gnus-group-get-parameter
1800 (gnus-group-prefixed-name
1801 (gnus-group-short-name group) '(nnir "nnir"))
1802 'nnir-artlist t))))
1803
1804 (defun nnir-server-opened (&optional server)
1805 (let ((backend (car (gnus-server-to-method server))))
1806 (nnoo-current-server-p (or backend 'nnir) server)))
1807
1808 (defun nnir-search-thread (header)
1809 "Make an nnir group based on the thread containing the article
1810 header. The current server will be searched. If the registry is
1811 installed, the server that the registry reports the current
1812 article came from is also searched."
1813 (let* ((query
1814 (list (cons 'query (nnimap-make-thread-query header))
1815 (cons 'criteria "")))
1816 (server
1817 (list (list (gnus-method-to-server
1818 (gnus-find-method-for-group gnus-newsgroup-name)))))
1819 (registry-group (and
1820 (gnus-bound-and-true-p 'gnus-registry-enabled)
1821 (car (gnus-registry-get-id-key
1822 (mail-header-id header) 'group))))
1823 (registry-server
1824 (and registry-group
1825 (gnus-method-to-server
1826 (gnus-find-method-for-group registry-group)))))
1827 (when registry-server (add-to-list 'server (list registry-server)))
1828 (gnus-group-make-nnir-group nil (list
1829 (cons 'nnir-query-spec query)
1830 (cons 'nnir-group-spec server)))
1831 (gnus-summary-goto-subject (gnus-id-to-article (mail-header-id header)))))
1832
1833 (defun nnir-get-active (srv)
1834 (let ((method (gnus-server-to-method srv))
1835 groups)
1836 (gnus-request-list method)
1837 (with-current-buffer nntp-server-buffer
1838 (let ((cur (current-buffer))
1839 name)
1840 (goto-char (point-min))
1841 (unless (or (null nnir-ignored-newsgroups)
1842 (string= nnir-ignored-newsgroups ""))
1843 (delete-matching-lines nnir-ignored-newsgroups))
1844 (if (eq (car method) 'nntp)
1845 (while (not (eobp))
1846 (ignore-errors
1847 (push (mm-string-as-unibyte
1848 (gnus-group-full-name
1849 (buffer-substring
1850 (point)
1851 (progn
1852 (skip-chars-forward "^ \t")
1853 (point))) method))
1854 groups))
1855 (forward-line))
1856 (while (not (eobp))
1857 (ignore-errors
1858 (push (mm-string-as-unibyte
1859 (if (eq (char-after) ?\")
1860 (gnus-group-full-name (read cur) method)
1861 (let ((p (point)) (name ""))
1862 (skip-chars-forward "^ \t\\\\")
1863 (setq name (buffer-substring p (point)))
1864 (while (eq (char-after) ?\\)
1865 (setq p (1+ (point)))
1866 (forward-char 2)
1867 (skip-chars-forward "^ \t\\\\")
1868 (setq name (concat name (buffer-substring
1869 p (point)))))
1870 (gnus-group-full-name name method))))
1871 groups))
1872 (forward-line)))))
1873 groups))
1874
1875 (defun nnir-registry-action (action data-header from &optional to method)
1876 "Call `gnus-registry-action' with the original article group."
1877 (gnus-registry-action
1878 action
1879 data-header
1880 (nnir-article-group (mail-header-number data-header))
1881 to
1882 method))
1883
1884 (defun nnir-mode ()
1885 (when (eq (car (gnus-find-method-for-group gnus-newsgroup-name)) 'nnir)
1886 (setq gnus-summary-line-format
1887 (or nnir-summary-line-format gnus-summary-line-format))
1888 (when (gnus-bound-and-true-p 'gnus-registry-enabled)
1889 (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action t)
1890 (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action t)
1891 (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action t)
1892 (add-hook 'gnus-summary-article-delete-hook 'nnir-registry-action t t)
1893 (add-hook 'gnus-summary-article-move-hook 'nnir-registry-action t t)
1894 (add-hook 'gnus-summary-article-expire-hook 'nnir-registry-action t t))))
1895
1896
1897 (defun gnus-summary-create-nnir-group ()
1898 (interactive)
1899 (or (nnir-server-opened "") (nnir-open-server "nnir"))
1900 (let ((name (gnus-read-group "Group name: "))
1901 (method '(nnir ""))
1902 (pgroup
1903 (gnus-group-guess-full-name-from-command-method gnus-newsgroup-name)))
1904 (with-current-buffer gnus-group-buffer
1905 (gnus-group-make-group
1906 name method nil
1907 (gnus-group-find-parameter pgroup)))))
1908
1909
1910 (deffoo nnir-request-create-group (group &optional server args)
1911 (message "Creating nnir group %s" group)
1912 (let* ((group (gnus-group-prefixed-name group '(nnir "nnir")))
1913 (specs (assq 'nnir-specs args))
1914 (query-spec
1915 (or (cdr (assq 'nnir-query-spec specs))
1916 (list (cons 'query
1917 (read-string "Query: " nil 'nnir-search-history)))))
1918 (group-spec
1919 (or (cdr (assq 'nnir-group-spec specs))
1920 (list (list (read-string "Server: " nil nil)))))
1921 (nnir-specs (list (cons 'nnir-query-spec query-spec)
1922 (cons 'nnir-group-spec group-spec))))
1923 (gnus-group-set-parameter group 'nnir-specs nnir-specs)
1924 (gnus-group-set-parameter
1925 group 'nnir-artlist
1926 (or (cdr (assq 'nnir-artlist args))
1927 (nnir-run-query nnir-specs)))
1928 (nnir-request-update-info group (gnus-get-info group)))
1929 t)
1930
1931 (deffoo nnir-request-delete-group (group &optional force server)
1932 t)
1933
1934 (deffoo nnir-request-list (&optional server)
1935 t)
1936
1937 (deffoo nnir-request-scan (group method)
1938 t)
1939
1940 (deffoo nnir-request-close ()
1941 t)
1942
1943 (nnoo-define-skeleton nnir)
1944
1945 ;; The end.
1946 (provide 'nnir)
1947
1948 ;;; nnir.el ends here