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