]> code.delx.au - gnu-emacs/blob - lisp/gnus/nnir.el
lisp/gnus/nnir.el (nnir-run-imap): Enable non-ASCII IMAP searches
[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 (cond
1073 ;; Simple string term
1074 ((stringp expr)
1075 (format "%s %S" criteria expr))
1076 ;; Trivial term: and
1077 ((eq expr 'and) nil)
1078 ;; Composite term: or expression
1079 ((eq (car-safe expr) 'or)
1080 (format "OR %s %s"
1081 (nnir-imap-expr-to-imap criteria (second expr))
1082 (nnir-imap-expr-to-imap criteria (third expr))))
1083 ;; Composite term: just the fax, mam
1084 ((eq (car-safe expr) 'not)
1085 (format "NOT (%s)" (nnir-imap-query-to-imap criteria (rest expr))))
1086 ;; Composite term: non-ascii search term
1087 ((numberp (car-safe expr))
1088 (format "%s {%d%s}\n%s" criteria (car expr)
1089 (if literal+ "+" "") (second expr)))
1090 ;; Composite term: just expand it all.
1091 ((and (not (null expr)) (listp expr))
1092 (format "(%s)" (nnir-imap-query-to-imap criteria expr)))
1093 ;; Complex value, give up for now.
1094 (t (error "Unhandled input: %S" expr))))
1095
1096
1097 (defun nnir-imap-parse-query (string)
1098 "Turn STRING into an s-expression based query based on the IMAP
1099 query language as defined in `nnir-imap-make-query'.
1100
1101 This involves turning individual tokens into higher level terms
1102 that the search language can then understand and use."
1103 (with-temp-buffer
1104 ;; Set up the parsing environment.
1105 (insert string)
1106 (goto-char (point-min))
1107 ;; Now, collect the output terms and return them.
1108 (let (out)
1109 (while (not (nnir-imap-end-of-input))
1110 (push (nnir-imap-next-expr) out))
1111 (reverse out))))
1112
1113
1114 (defun nnir-imap-next-expr (&optional count)
1115 "Return the next expression from the current buffer."
1116 (let ((term (nnir-imap-next-term count))
1117 (next (nnir-imap-peek-symbol)))
1118 ;; Are we looking at an 'or' expression?
1119 (cond
1120 ;; Handle 'expr or expr'
1121 ((eq next 'or)
1122 (list 'or term (nnir-imap-next-expr 2)))
1123 ;; Anything else
1124 (t term))))
1125
1126
1127 (defun nnir-imap-next-term (&optional count)
1128 "Return the next TERM from the current buffer."
1129 (let ((term (nnir-imap-next-symbol count)))
1130 ;; What sort of term is this?
1131 (cond
1132 ;; and -- just ignore it
1133 ((eq term 'and) 'and)
1134 ;; negated term
1135 ((eq term 'not) (list 'not (nnir-imap-next-expr)))
1136 ;; non-ascii search string
1137 ((and (stringp term)
1138 (not (= (string-bytes term)
1139 (length term))))
1140 (list (string-bytes term) term))
1141 ;; generic term
1142 (t term))))
1143
1144
1145 (defun nnir-imap-peek-symbol ()
1146 "Return the next symbol from the current buffer, but don't consume it."
1147 (save-excursion
1148 (nnir-imap-next-symbol)))
1149
1150 (defun nnir-imap-next-symbol (&optional count)
1151 "Return the next symbol from the current buffer, or nil if we are
1152 at the end of the buffer. If supplied COUNT skips some symbols before
1153 returning the one at the supplied position."
1154 (when (and (numberp count) (> count 1))
1155 (nnir-imap-next-symbol (1- count)))
1156 (let ((case-fold-search t))
1157 ;; end of input stream?
1158 (unless (nnir-imap-end-of-input)
1159 ;; No, return the next symbol from the stream.
1160 (cond
1161 ;; negated expression -- return it and advance one char.
1162 ((looking-at "-") (forward-char 1) 'not)
1163 ;; quoted string
1164 ((looking-at "\"") (nnir-imap-delimited-string "\""))
1165 ;; list expression -- we parse the content and return this as a list.
1166 ((looking-at "(")
1167 (nnir-imap-parse-query (nnir-imap-delimited-string ")")))
1168 ;; keyword input -- return a symbol version
1169 ((looking-at "\\band\\b") (forward-char 3) 'and)
1170 ((looking-at "\\bor\\b") (forward-char 2) 'or)
1171 ((looking-at "\\bnot\\b") (forward-char 3) 'not)
1172 ;; Simple, boring keyword
1173 (t (let ((start (point))
1174 (end (if (search-forward-regexp "[[:blank:]]" nil t)
1175 (prog1
1176 (match-beginning 0)
1177 ;; unskip if we hit a non-blank terminal character.
1178 (when (string-match "[^[:blank:]]" (match-string 0))
1179 (backward-char 1)))
1180 (goto-char (point-max)))))
1181 (buffer-substring start end)))))))
1182
1183 (defun nnir-imap-delimited-string (delimiter)
1184 "Return a delimited string from the current buffer."
1185 (let ((start (point)) end)
1186 (forward-char 1) ; skip the first delimiter.
1187 (while (not end)
1188 (unless (search-forward delimiter nil t)
1189 (error "Unmatched delimited input with %s in query" delimiter))
1190 (let ((here (point)))
1191 (unless (equal (buffer-substring (- here 2) (- here 1)) "\\")
1192 (setq end (point)))))
1193 (buffer-substring (1+ start) (1- end))))
1194
1195 (defun nnir-imap-end-of-input ()
1196 "Are we at the end of input?"
1197 (skip-chars-forward "[[:blank:]]")
1198 (looking-at "$"))
1199
1200
1201 ;; Swish++ interface.
1202 ;; -cc- Todo
1203 ;; Search by
1204 ;; - group
1205 ;; Sort by
1206 ;; - rank (default)
1207 ;; - article number
1208 ;; - file size
1209 ;; - group
1210 (defun nnir-run-swish++ (query server &optional group)
1211 "Run QUERY against swish++.
1212 Returns a vector of (group name, file name) pairs (also vectors,
1213 actually).
1214
1215 Tested with swish++ 4.7 on GNU/Linux and with swish++ 5.0b2 on
1216 Windows NT 4.0."
1217
1218 ;; (when group
1219 ;; (error "The swish++ backend cannot search specific groups"))
1220
1221 (save-excursion
1222 (let ( (qstring (cdr (assq 'query query)))
1223 (groupspec (cdr (assq 'swish++-group query)))
1224 (prefix (nnir-read-server-parm 'nnir-swish++-remove-prefix server))
1225 artlist
1226 ;; nnml-use-compressed-files might be any string, but probably this
1227 ;; is sufficient. Note that we can't only use the value of
1228 ;; nnml-use-compressed-files because old articles might have been
1229 ;; saved with a different value.
1230 (article-pattern (if (string-match "\\`nnmaildir:"
1231 (gnus-group-server server))
1232 ":[0-9]+"
1233 "^[0-9]+\\(\\.[a-z0-9]+\\)?$"))
1234 score artno dirnam filenam)
1235
1236 (when (equal "" qstring)
1237 (error "swish++: You didn't enter anything"))
1238
1239 (set-buffer (get-buffer-create nnir-tmp-buffer))
1240 (erase-buffer)
1241
1242 (if groupspec
1243 (message "Doing swish++ query %s on %s..." qstring groupspec)
1244 (message "Doing swish++ query %s..." qstring))
1245
1246 (let* ((cp-list `( ,nnir-swish++-program
1247 nil ; input from /dev/null
1248 t ; output
1249 nil ; don't redisplay
1250 "--config-file" ,(nnir-read-server-parm 'nnir-swish++-configuration-file server)
1251 ,@(nnir-read-server-parm 'nnir-swish++-additional-switches server)
1252 ,qstring ; the query, in swish++ format
1253 ))
1254 (exitstatus
1255 (progn
1256 (message "%s args: %s" nnir-swish++-program
1257 (mapconcat 'identity (cddddr cp-list) " ")) ;; ???
1258 (apply 'call-process cp-list))))
1259 (unless (or (null exitstatus)
1260 (zerop exitstatus))
1261 (nnheader-report 'nnir "Couldn't run swish++: %s" exitstatus)
1262 ;; swish++ failure reason is in this buffer, show it if
1263 ;; the user wants it.
1264 (when (> gnus-verbose 6)
1265 (display-buffer nnir-tmp-buffer))))
1266
1267 ;; The results are output in the format of:
1268 ;; V 4.7 Linux
1269 ;; rank relative-path-name file-size file-title
1270 ;; V 5.0b2:
1271 ;; rank relative-path-name file-size topic??
1272 ;; where rank is an integer from 1 to 100.
1273 (goto-char (point-min))
1274 (while (re-search-forward
1275 "\\(^[0-9]+\\) \\([^ ]+\\) [0-9]+ \\(.*\\)$" nil t)
1276 (setq score (match-string 1)
1277 filenam (match-string 2)
1278 artno (file-name-nondirectory filenam)
1279 dirnam (file-name-directory filenam))
1280
1281 ;; don't match directories
1282 (when (string-match article-pattern artno)
1283 (when (not (null dirnam))
1284
1285 ;; maybe limit results to matching groups.
1286 (when (or (not groupspec)
1287 (string-match groupspec dirnam))
1288 (nnir-add-result dirnam artno score prefix server artlist)))))
1289
1290 (message "Massaging swish++ output...done")
1291
1292 ;; Sort by score
1293 (apply 'vector
1294 (sort artlist
1295 (function (lambda (x y)
1296 (> (nnir-artitem-rsv x)
1297 (nnir-artitem-rsv y)))))))))
1298
1299 ;; Swish-E interface.
1300 (defun nnir-run-swish-e (query server &optional group)
1301 "Run given query against swish-e.
1302 Returns a vector of (group name, file name) pairs (also vectors,
1303 actually).
1304
1305 Tested with swish-e-2.0.1 on Windows NT 4.0."
1306
1307 ;; swish-e crashes with empty parameter to "-w" on commandline...
1308 ;; (when group
1309 ;; (error "The swish-e backend cannot search specific groups"))
1310
1311 (save-excursion
1312 (let ((qstring (cdr (assq 'query query)))
1313 (prefix
1314 (or (nnir-read-server-parm 'nnir-swish-e-remove-prefix server)
1315 (error "Missing parameter `nnir-swish-e-remove-prefix'")))
1316 artlist score artno dirnam group )
1317
1318 (when (equal "" qstring)
1319 (error "swish-e: You didn't enter anything"))
1320
1321 (set-buffer (get-buffer-create nnir-tmp-buffer))
1322 (erase-buffer)
1323
1324 (message "Doing swish-e query %s..." query)
1325 (let* ((index-files
1326 (or (nnir-read-server-parm
1327 'nnir-swish-e-index-files server)
1328 (error "Missing parameter `nnir-swish-e-index-files'")))
1329 (additional-switches
1330 (nnir-read-server-parm
1331 'nnir-swish-e-additional-switches server))
1332 (cp-list `(,nnir-swish-e-program
1333 nil ; input from /dev/null
1334 t ; output
1335 nil ; don't redisplay
1336 "-f" ,@index-files
1337 ,@additional-switches
1338 "-w"
1339 ,qstring ; the query, in swish-e format
1340 ))
1341 (exitstatus
1342 (progn
1343 (message "%s args: %s" nnir-swish-e-program
1344 (mapconcat 'identity (cddddr cp-list) " "))
1345 (apply 'call-process cp-list))))
1346 (unless (or (null exitstatus)
1347 (zerop exitstatus))
1348 (nnheader-report 'nnir "Couldn't run swish-e: %s" exitstatus)
1349 ;; swish-e failure reason is in this buffer, show it if
1350 ;; the user wants it.
1351 (when (> gnus-verbose 6)
1352 (display-buffer nnir-tmp-buffer))))
1353
1354 ;; The results are output in the format of:
1355 ;; rank path-name file-title file-size
1356 (goto-char (point-min))
1357 (while (re-search-forward
1358 "\\(^[0-9]+\\) \\([^ ]+\\) \"\\([^\"]+\\)\" [0-9]+$" nil t)
1359 (setq score (match-string 1)
1360 artno (match-string 3)
1361 dirnam (file-name-directory (match-string 2)))
1362
1363 ;; don't match directories
1364 (when (string-match "^[0-9]+$" artno)
1365 (when (not (null dirnam))
1366
1367 ;; remove nnir-swish-e-remove-prefix from beginning of dirname
1368 (when (string-match (concat "^" prefix) dirnam)
1369 (setq dirnam (replace-match "" t t dirnam)))
1370
1371 (setq dirnam (substring dirnam 0 -1))
1372 ;; eliminate all ".", "/", "\" from beginning. Always matches.
1373 (string-match "^[./\\]*\\(.*\\)$" dirnam)
1374 ;; "/" -> "."
1375 (setq group (gnus-replace-in-string (match-string 1 dirnam) "/" "."))
1376 ;; Windows "\\" -> "."
1377 (setq group (gnus-replace-in-string group "\\\\" "."))
1378
1379 (push (vector (gnus-group-full-name group server)
1380 (string-to-number artno)
1381 (string-to-number score))
1382 artlist))))
1383
1384 (message "Massaging swish-e output...done")
1385
1386 ;; Sort by score
1387 (apply 'vector
1388 (sort artlist
1389 (function (lambda (x y)
1390 (> (nnir-artitem-rsv x)
1391 (nnir-artitem-rsv y)))))))))
1392
1393 ;; HyREX interface
1394 (defun nnir-run-hyrex (query server &optional group)
1395 (save-excursion
1396 (let ((artlist nil)
1397 (groupspec (cdr (assq 'hyrex-group query)))
1398 (qstring (cdr (assq 'query query)))
1399 (prefix (nnir-read-server-parm 'nnir-hyrex-remove-prefix server))
1400 score artno dirnam)
1401 (when (and (not groupspec) group)
1402 (setq groupspec
1403 (regexp-opt
1404 (mapcar (lambda (x) (gnus-group-real-name x)) group))))
1405 (set-buffer (get-buffer-create nnir-tmp-buffer))
1406 (erase-buffer)
1407 (message "Doing hyrex-search query %s..." query)
1408 (let* ((cp-list
1409 `( ,nnir-hyrex-program
1410 nil ; input from /dev/null
1411 t ; output
1412 nil ; don't redisplay
1413 "-i",(nnir-read-server-parm 'nnir-hyrex-index-directory server) ; index directory
1414 ,@(nnir-read-server-parm 'nnir-hyrex-additional-switches server)
1415 ,qstring ; the query, in hyrex-search format
1416 ))
1417 (exitstatus
1418 (progn
1419 (message "%s args: %s" nnir-hyrex-program
1420 (mapconcat 'identity (cddddr cp-list) " "))
1421 (apply 'call-process cp-list))))
1422 (unless (or (null exitstatus)
1423 (zerop exitstatus))
1424 (nnheader-report 'nnir "Couldn't run hyrex-search: %s" exitstatus)
1425 ;; nnir-search failure reason is in this buffer, show it if
1426 ;; the user wants it.
1427 (when (> gnus-verbose 6)
1428 (display-buffer nnir-tmp-buffer)))) ;; FIXME: Don't clear buffer !
1429 (message "Doing hyrex-search query \"%s\"...done" qstring)
1430 (sit-for 0)
1431 ;; nnir-search returns:
1432 ;; for nnml/nnfolder: "filename mailid weight"
1433 ;; for nnimap: "group mailid weight"
1434 (goto-char (point-min))
1435 (delete-non-matching-lines "^\\S + [0-9]+ [0-9]+$")
1436 ;; HyREX doesn't search directly in groups -- so filter out here.
1437 (when groupspec
1438 (keep-lines groupspec))
1439 ;; extract data from result lines
1440 (goto-char (point-min))
1441 (while (re-search-forward
1442 "\\(\\S +\\) \\([0-9]+\\) \\([0-9]+\\)" nil t)
1443 (setq dirnam (match-string 1)
1444 artno (match-string 2)
1445 score (match-string 3))
1446 (when (string-match prefix dirnam)
1447 (setq dirnam (replace-match "" t t dirnam)))
1448 (push (vector (gnus-group-full-name
1449 (gnus-replace-in-string dirnam "/" ".") server)
1450 (string-to-number artno)
1451 (string-to-number score))
1452 artlist))
1453 (message "Massaging hyrex-search output...done.")
1454 (apply 'vector
1455 (sort artlist
1456 (function (lambda (x y)
1457 (if (string-lessp (nnir-artitem-group x)
1458 (nnir-artitem-group y))
1459 t
1460 (< (nnir-artitem-number x)
1461 (nnir-artitem-number y)))))))
1462 )))
1463
1464 ;; Namazu interface
1465 (defun nnir-run-namazu (query server &optional group)
1466 "Run given query against Namazu. Returns a vector of (group name, file name)
1467 pairs (also vectors, actually).
1468
1469 Tested with Namazu 2.0.6 on a GNU/Linux system."
1470 ;; (when group
1471 ;; (error "The Namazu backend cannot search specific groups"))
1472 (save-excursion
1473 (let ((article-pattern (if (string-match "\\`nnmaildir:"
1474 (gnus-group-server server))
1475 ":[0-9]+"
1476 "^[0-9]+$"))
1477 artlist
1478 (qstring (cdr (assq 'query query)))
1479 (prefix (nnir-read-server-parm 'nnir-namazu-remove-prefix server))
1480 score group article
1481 (process-environment (copy-sequence process-environment)))
1482 (setenv "LC_MESSAGES" "C")
1483 (set-buffer (get-buffer-create nnir-tmp-buffer))
1484 (erase-buffer)
1485 (let* ((cp-list
1486 `( ,nnir-namazu-program
1487 nil ; input from /dev/null
1488 t ; output
1489 nil ; don't redisplay
1490 "-q" ; don't be verbose
1491 "-a" ; show all matches
1492 "-s" ; use short format
1493 ,@(nnir-read-server-parm 'nnir-namazu-additional-switches server)
1494 ,qstring ; the query, in namazu format
1495 ,(nnir-read-server-parm 'nnir-namazu-index-directory server) ; index directory
1496 ))
1497 (exitstatus
1498 (progn
1499 (message "%s args: %s" nnir-namazu-program
1500 (mapconcat 'identity (cddddr cp-list) " "))
1501 (apply 'call-process cp-list))))
1502 (unless (or (null exitstatus)
1503 (zerop exitstatus))
1504 (nnheader-report 'nnir "Couldn't run namazu: %s" exitstatus)
1505 ;; Namazu failure reason is in this buffer, show it if
1506 ;; the user wants it.
1507 (when (> gnus-verbose 6)
1508 (display-buffer nnir-tmp-buffer))))
1509
1510 ;; Namazu output looks something like this:
1511 ;; 2. Re: Gnus agent expire broken (score: 55)
1512 ;; /home/henrik/Mail/mail/sent/1310 (4,138 bytes)
1513
1514 (goto-char (point-min))
1515 (while (re-search-forward
1516 "^\\([0-9,]+\\.\\).*\\((score: \\([0-9]+\\)\\))\n\\([^ ]+\\)"
1517 nil t)
1518 (setq score (match-string 3)
1519 group (file-name-directory (match-string 4))
1520 article (file-name-nondirectory (match-string 4)))
1521
1522 ;; make sure article and group is sane
1523 (when (and (string-match article-pattern article)
1524 (not (null group)))
1525 (nnir-add-result group article score prefix server artlist)))
1526
1527 ;; sort artlist by score
1528 (apply 'vector
1529 (sort artlist
1530 (function (lambda (x y)
1531 (> (nnir-artitem-rsv x)
1532 (nnir-artitem-rsv y)))))))))
1533
1534 (defun nnir-run-notmuch (query server &optional group)
1535 "Run QUERY against notmuch.
1536 Returns a vector of (group name, file name) pairs (also vectors,
1537 actually)."
1538
1539 ;; (when group
1540 ;; (error "The notmuch backend cannot search specific groups"))
1541
1542 (save-excursion
1543 (let ( (qstring (cdr (assq 'query query)))
1544 (groupspec (cdr (assq 'notmuch-group query)))
1545 (prefix (nnir-read-server-parm 'nnir-notmuch-remove-prefix server))
1546 artlist
1547 (article-pattern (if (string-match "\\`nnmaildir:"
1548 (gnus-group-server server))
1549 ":[0-9]+"
1550 "^[0-9]+$"))
1551 artno dirnam filenam)
1552
1553 (when (equal "" qstring)
1554 (error "notmuch: You didn't enter anything"))
1555
1556 (set-buffer (get-buffer-create nnir-tmp-buffer))
1557 (erase-buffer)
1558
1559 (if groupspec
1560 (message "Doing notmuch query %s on %s..." qstring groupspec)
1561 (message "Doing notmuch query %s..." qstring))
1562
1563 (let* ((cp-list `( ,nnir-notmuch-program
1564 nil ; input from /dev/null
1565 t ; output
1566 nil ; don't redisplay
1567 "search"
1568 "--format=text"
1569 "--output=files"
1570 ,@(nnir-read-server-parm 'nnir-notmuch-additional-switches server)
1571 ,qstring ; the query, in notmuch format
1572 ))
1573 (exitstatus
1574 (progn
1575 (message "%s args: %s" nnir-notmuch-program
1576 (mapconcat 'identity (cddddr cp-list) " ")) ;; ???
1577 (apply 'call-process cp-list))))
1578 (unless (or (null exitstatus)
1579 (zerop exitstatus))
1580 (nnheader-report 'nnir "Couldn't run notmuch: %s" exitstatus)
1581 ;; notmuch failure reason is in this buffer, show it if
1582 ;; the user wants it.
1583 (when (> gnus-verbose 6)
1584 (display-buffer nnir-tmp-buffer))))
1585
1586 ;; The results are output in the format of:
1587 ;; absolute-path-name
1588 (goto-char (point-min))
1589 (while (not (eobp))
1590 (setq filenam (buffer-substring-no-properties (line-beginning-position)
1591 (line-end-position))
1592 artno (file-name-nondirectory filenam)
1593 dirnam (file-name-directory filenam))
1594 (forward-line 1)
1595
1596 ;; don't match directories
1597 (when (string-match article-pattern artno)
1598 (when (not (null dirnam))
1599
1600 ;; maybe limit results to matching groups.
1601 (when (or (not groupspec)
1602 (string-match groupspec dirnam))
1603 (nnir-add-result dirnam artno "" prefix server artlist)))))
1604
1605 (message "Massaging notmuch output...done")
1606
1607 artlist)))
1608
1609 (defun nnir-run-find-grep (query server &optional grouplist)
1610 "Run find and grep to obtain matching articles."
1611 (let* ((method (gnus-server-to-method server))
1612 (sym (intern
1613 (concat (symbol-name (car method)) "-directory")))
1614 (directory (cadr (assoc sym (cddr method))))
1615 (regexp (cdr (assoc 'query query)))
1616 (grep-options (cdr (assoc 'grep-options query)))
1617 (grouplist (or grouplist (nnir-get-active server))))
1618 (unless directory
1619 (error "No directory found in method specification of server %s"
1620 server))
1621 (apply
1622 'vconcat
1623 (mapcar (lambda (x)
1624 (let ((group x)
1625 artlist)
1626 (message "Searching %s using find-grep..."
1627 (or group server))
1628 (save-window-excursion
1629 (set-buffer (get-buffer-create nnir-tmp-buffer))
1630 (if (> gnus-verbose 6)
1631 (pop-to-buffer (current-buffer)))
1632 (cd directory) ; Using relative paths simplifies
1633 ; postprocessing.
1634 (let ((group
1635 (if (not group)
1636 "."
1637 ;; Try accessing the group literally as
1638 ;; well as interpreting dots as directory
1639 ;; separators so the engine works with
1640 ;; plain nnml as well as the Gnus Cache.
1641 (let ((group (gnus-group-real-name group)))
1642 ;; Replace cl-func find-if.
1643 (if (file-directory-p group)
1644 group
1645 (if (file-directory-p
1646 (setq group
1647 (gnus-replace-in-string
1648 group
1649 "\\." "/" t)))
1650 group))))))
1651 (unless group
1652 (error "Cannot locate directory for group"))
1653 (save-excursion
1654 (apply
1655 'call-process "find" nil t
1656 "find" group "-maxdepth" "1" "-type" "f"
1657 "-name" "[0-9]*" "-exec"
1658 "grep"
1659 `("-l" ,@(and grep-options
1660 (split-string grep-options "\\s-" t))
1661 "-e" ,regexp "{}" "+"))))
1662
1663 ;; Translate relative paths to group names.
1664 (while (not (eobp))
1665 (let* ((path (split-string
1666 (buffer-substring
1667 (point)
1668 (line-end-position)) "/" t))
1669 (art (string-to-number (car (last path)))))
1670 (while (string= "." (car path))
1671 (setq path (cdr path)))
1672 (let ((group (mapconcat 'identity
1673 ;; Replace cl-func:
1674 ;; (subseq path 0 -1)
1675 (let ((end (1- (length path)))
1676 res)
1677 (while
1678 (>= (setq end (1- end)) 0)
1679 (push (pop path) res))
1680 (nreverse res))
1681 ".")))
1682 (push
1683 (vector (gnus-group-full-name group server) art 0)
1684 artlist))
1685 (forward-line 1)))
1686 (message "Searching %s using find-grep...done"
1687 (or group server))
1688 artlist)))
1689 grouplist))))
1690
1691 (declare-function mm-url-insert "mm-url" (url &optional follow-refresh))
1692 (declare-function mm-url-encode-www-form-urlencoded "mm-url" (pairs))
1693
1694 ;; gmane interface
1695 (defun nnir-run-gmane (query srv &optional groups)
1696 "Run a search against a gmane back-end server."
1697 (let* ((case-fold-search t)
1698 (qstring (cdr (assq 'query query)))
1699 (server (cadr (gnus-server-to-method srv)))
1700 (groupspec (mapconcat
1701 (lambda (x)
1702 (if (gnus-string-match-p "gmane" x)
1703 (format "group:%s" (gnus-group-short-name x))
1704 (error "Can't search non-gmane groups: %s" x)))
1705 groups " "))
1706 (authorspec
1707 (if (assq 'gmane-author query)
1708 (format "author:%s" (cdr (assq 'gmane-author query))) ""))
1709 (search (format "%s %s %s"
1710 qstring groupspec authorspec))
1711 (gnus-inhibit-demon t)
1712 artlist)
1713 (require 'mm-url)
1714 (with-current-buffer (get-buffer-create nnir-tmp-buffer)
1715 (erase-buffer)
1716 (mm-url-insert
1717 (concat
1718 "http://search.gmane.org/nov.php"
1719 "?"
1720 (mm-url-encode-www-form-urlencoded
1721 `(("query" . ,search)
1722 ("HITSPERPAGE" . "999")))))
1723 (unless (featurep 'xemacs) (set-buffer-multibyte t))
1724 (mm-decode-coding-region (point-min) (point-max) 'utf-8)
1725 (goto-char (point-min))
1726 (forward-line 1)
1727 (while (not (eobp))
1728 (unless (or (eolp) (looking-at "\x0d"))
1729 (let ((header (nnheader-parse-nov)))
1730 (let ((xref (mail-header-xref header))
1731 (xscore (string-to-number (cdr (assoc 'X-Score
1732 (mail-header-extra header))))))
1733 (when (string-match " \\([^:]+\\)[:/]\\([0-9]+\\)" xref)
1734 (push
1735 (vector
1736 (gnus-group-prefixed-name (match-string 1 xref) srv)
1737 (string-to-number (match-string 2 xref)) xscore)
1738 artlist)))))
1739 (forward-line 1)))
1740 (apply 'vector (nreverse (mm-delete-duplicates artlist)))))
1741
1742 ;;; Util Code:
1743
1744 (defun gnus-nnir-group-p (group)
1745 "Say whether GROUP is nnir or not."
1746 (if (gnus-group-prefixed-p group)
1747 (eq 'nnir (car (gnus-find-method-for-group group)))
1748 (and group (string-match "^nnir" group))))
1749
1750 (defun nnir-read-parms (nnir-search-engine)
1751 "Reads additional search parameters according to `nnir-engines'."
1752 (let ((parmspec (caddr (assoc nnir-search-engine nnir-engines))))
1753 (mapcar 'nnir-read-parm parmspec)))
1754
1755 (defun nnir-read-parm (parmspec)
1756 "Reads a single search parameter.
1757 `parmspec' is a cons cell, the car is a symbol, the cdr is a prompt."
1758 (let ((sym (car parmspec))
1759 (prompt (cdr parmspec)))
1760 (if (listp prompt)
1761 (let* ((result (apply 'gnus-completing-read prompt))
1762 (mapping (or (assoc result nnir-imap-search-arguments)
1763 (cons nil nnir-imap-search-other))))
1764 (cons sym (format (cdr mapping) result)))
1765 (cons sym (read-string prompt)))))
1766
1767 (defun nnir-run-query (specs)
1768 "Invoke appropriate search engine function (see `nnir-engines')."
1769 (apply 'vconcat
1770 (mapcar
1771 (lambda (x)
1772 (let* ((server (car x))
1773 (search-engine (nnir-server-to-search-engine server))
1774 (search-func (cadr (assoc search-engine nnir-engines))))
1775 (and search-func
1776 (funcall search-func (cdr (assq 'nnir-query-spec specs))
1777 server (cadr x)))))
1778 (cdr (assq 'nnir-group-spec specs)))))
1779
1780 (defun nnir-server-to-search-engine (server)
1781 (or (nnir-read-server-parm 'nnir-search-engine server t)
1782 (cdr (assoc (car (gnus-server-to-method server))
1783 nnir-method-default-engines))))
1784
1785 (defun nnir-read-server-parm (key server &optional not-global)
1786 "Returns the parameter value corresponding to `key' for
1787 `server'. If no server-specific value is found consult the global
1788 environment unless `not-global' is non-nil."
1789 (let ((method (gnus-server-to-method server)))
1790 (cond ((and method (assq key (cddr method)))
1791 (nth 1 (assq key (cddr method))))
1792 ((and (not not-global) (boundp key)) (symbol-value key))
1793 (t nil))))
1794
1795 (defun nnir-possibly-change-group (group &optional server)
1796 (or (not server) (nnir-server-opened server) (nnir-open-server server))
1797 (when (gnus-nnir-group-p group)
1798 (setq nnir-artlist (gnus-group-get-parameter
1799 (gnus-group-prefixed-name
1800 (gnus-group-short-name group) '(nnir "nnir"))
1801 'nnir-artlist t))))
1802
1803 (defun nnir-server-opened (&optional server)
1804 (let ((backend (car (gnus-server-to-method server))))
1805 (nnoo-current-server-p (or backend 'nnir) server)))
1806
1807 (defun nnir-search-thread (header)
1808 "Make an nnir group based on the thread containing the article
1809 header. The current server will be searched. If the registry is
1810 installed, the server that the registry reports the current
1811 article came from is also searched."
1812 (let* ((query
1813 (list (cons 'query (nnimap-make-thread-query header))
1814 (cons 'criteria "")))
1815 (server
1816 (list (list (gnus-method-to-server
1817 (gnus-find-method-for-group gnus-newsgroup-name)))))
1818 (registry-group (and
1819 (gnus-bound-and-true-p 'gnus-registry-enabled)
1820 (car (gnus-registry-get-id-key
1821 (mail-header-id header) 'group))))
1822 (registry-server
1823 (and registry-group
1824 (gnus-method-to-server
1825 (gnus-find-method-for-group registry-group)))))
1826 (when registry-server (add-to-list 'server (list registry-server)))
1827 (gnus-group-make-nnir-group nil (list
1828 (cons 'nnir-query-spec query)
1829 (cons 'nnir-group-spec server)))
1830 (gnus-summary-goto-subject (gnus-id-to-article (mail-header-id header)))))
1831
1832 (defun nnir-get-active (srv)
1833 (let ((method (gnus-server-to-method srv))
1834 groups)
1835 (gnus-request-list method)
1836 (with-current-buffer nntp-server-buffer
1837 (let ((cur (current-buffer))
1838 name)
1839 (goto-char (point-min))
1840 (unless (or (null nnir-ignored-newsgroups)
1841 (string= nnir-ignored-newsgroups ""))
1842 (delete-matching-lines nnir-ignored-newsgroups))
1843 (if (eq (car method) 'nntp)
1844 (while (not (eobp))
1845 (ignore-errors
1846 (push (mm-string-as-unibyte
1847 (gnus-group-full-name
1848 (buffer-substring
1849 (point)
1850 (progn
1851 (skip-chars-forward "^ \t")
1852 (point))) method))
1853 groups))
1854 (forward-line))
1855 (while (not (eobp))
1856 (ignore-errors
1857 (push (mm-string-as-unibyte
1858 (if (eq (char-after) ?\")
1859 (gnus-group-full-name (read cur) method)
1860 (let ((p (point)) (name ""))
1861 (skip-chars-forward "^ \t\\\\")
1862 (setq name (buffer-substring p (point)))
1863 (while (eq (char-after) ?\\)
1864 (setq p (1+ (point)))
1865 (forward-char 2)
1866 (skip-chars-forward "^ \t\\\\")
1867 (setq name (concat name (buffer-substring
1868 p (point)))))
1869 (gnus-group-full-name name method))))
1870 groups))
1871 (forward-line)))))
1872 groups))
1873
1874 (defun nnir-registry-action (action data-header from &optional to method)
1875 "Call `gnus-registry-action' with the original article group."
1876 (gnus-registry-action
1877 action
1878 data-header
1879 (nnir-article-group (mail-header-number data-header))
1880 to
1881 method))
1882
1883 (defun nnir-mode ()
1884 (when (eq (car (gnus-find-method-for-group gnus-newsgroup-name)) 'nnir)
1885 (setq gnus-summary-line-format
1886 (or nnir-summary-line-format gnus-summary-line-format))
1887 (when (gnus-bound-and-true-p 'gnus-registry-enabled)
1888 (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action t)
1889 (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action t)
1890 (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action t)
1891 (add-hook 'gnus-summary-article-delete-hook 'nnir-registry-action t t)
1892 (add-hook 'gnus-summary-article-move-hook 'nnir-registry-action t t)
1893 (add-hook 'gnus-summary-article-expire-hook 'nnir-registry-action t t))))
1894
1895
1896 (defun gnus-summary-create-nnir-group ()
1897 (interactive)
1898 (or (nnir-server-opened "") (nnir-open-server "nnir"))
1899 (let ((name (gnus-read-group "Group name: "))
1900 (method '(nnir ""))
1901 (pgroup
1902 (gnus-group-guess-full-name-from-command-method gnus-newsgroup-name)))
1903 (with-current-buffer gnus-group-buffer
1904 (gnus-group-make-group
1905 name method nil
1906 (gnus-group-find-parameter pgroup)))))
1907
1908
1909 (deffoo nnir-request-create-group (group &optional server args)
1910 (message "Creating nnir group %s" group)
1911 (let* ((group (gnus-group-prefixed-name group '(nnir "nnir")))
1912 (specs (assq 'nnir-specs args))
1913 (query-spec
1914 (or (cdr (assq 'nnir-query-spec specs))
1915 (list (cons 'query
1916 (read-string "Query: " nil 'nnir-search-history)))))
1917 (group-spec
1918 (or (cdr (assq 'nnir-group-spec specs))
1919 (list (list (read-string "Server: " nil nil)))))
1920 (nnir-specs (list (cons 'nnir-query-spec query-spec)
1921 (cons 'nnir-group-spec group-spec))))
1922 (gnus-group-set-parameter group 'nnir-specs nnir-specs)
1923 (gnus-group-set-parameter
1924 group 'nnir-artlist
1925 (or (cdr (assq 'nnir-artlist args))
1926 (nnir-run-query nnir-specs)))
1927 (nnir-request-update-info group (gnus-get-info group)))
1928 t)
1929
1930 (deffoo nnir-request-delete-group (group &optional force server)
1931 t)
1932
1933 (deffoo nnir-request-list (&optional server)
1934 t)
1935
1936 (deffoo nnir-request-scan (group method)
1937 t)
1938
1939 (deffoo nnir-request-close ()
1940 t)
1941
1942 (nnoo-define-skeleton nnir)
1943
1944 ;; The end.
1945 (provide 'nnir)
1946
1947 ;;; nnir.el ends here