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