Add 2010 to copyright years.
[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, 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 ;; 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-when-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 "Emacs 23.1")
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 (eval-when-compile
662   (when (featurep 'xemacs)
663     ;; The `kbd' macro requires that the `read-kbd-macro' macro is available.
664     (require 'edmacro)))
665
666 (defun nnir-group-mode-hook ()
667   (define-key gnus-group-mode-map (kbd "G G")
668     'gnus-group-make-nnir-group))
669 (add-hook 'gnus-group-mode-hook 'nnir-group-mode-hook)
670
671 ;; Why is this needed? Is this for compatibility with old/new gnusae? Using
672 ;; gnus-group-server instead works for me.  -- Justus Piater
673 (defmacro nnir-group-server (group)
674   "Return the server for a newsgroup GROUP.
675 The returned format is as `gnus-server-to-method' needs it.  See
676 `gnus-group-real-prefix' and `gnus-group-real-name'."
677   `(let ((gname ,group))
678      (if (string-match "^\\([^:]+\\):" gname)
679          (progn
680            (setq gname (match-string 1 gname))
681            (if (string-match "^\\([^+]+\\)\\+\\(.+\\)$" gname)
682                (format "%s:%s" (match-string 1 gname) (match-string 2 gname))
683              (concat gname ":")))
684        (format "%s:%s" (car gnus-select-method) (cadr gnus-select-method)))))
685
686 ;; Summary mode commands.
687
688 (defun gnus-summary-nnir-goto-thread ()
689   "Only applies to nnir groups.  Go to group this article came from
690 and show thread that contains this article."
691   (interactive)
692   (unless (eq 'nnir (car (gnus-find-method-for-group gnus-newsgroup-name)))
693     (error "Can't execute this command unless in nnir group"))
694   (let* ((cur (gnus-summary-article-number))
695          (group (nnir-artlist-artitem-group nnir-artlist cur))
696          (backend-number (nnir-artlist-artitem-number nnir-artlist cur))
697          server backend-group)
698     (setq server (nnir-group-server group))
699     (setq backend-group (gnus-group-real-name group))
700     (gnus-group-read-ephemeral-group
701      backend-group
702      (gnus-server-to-method server)
703      t                                  ; activate
704      (cons (current-buffer)
705            'summary)                    ; window config
706      nil
707      (list backend-number))
708     (gnus-summary-limit (list backend-number))
709     (gnus-summary-refer-thread)))
710
711 (if (fboundp 'eval-after-load)
712     (eval-after-load "gnus-sum"
713       '(define-key gnus-summary-goto-map
714          "T" 'gnus-summary-nnir-goto-thread))
715   (add-hook 'gnus-summary-mode-hook
716             (function (lambda ()
717                         (define-key gnus-summary-goto-map
718                           "T" 'gnus-summary-nnir-goto-thread)))))
719
720
721
722 ;; Gnus backend interface functions.
723
724 (deffoo nnir-open-server (server &optional definitions)
725   ;; Just set the server variables appropriately.
726   (nnoo-change-server 'nnir server definitions))
727
728 (deffoo nnir-request-group (group &optional server fast)
729   "GROUP is the query string."
730   (nnir-possibly-change-server server)
731   ;; Check for cache and return that if appropriate.
732   (if (and (equal group nnir-current-query)
733            (equal gnus-group-marked nnir-current-group-marked)
734            (or (null server)
735                (equal server nnir-current-server)))
736       nnir-artlist
737     ;; Cache miss.
738     (setq nnir-artlist (nnir-run-query group)))
739   (save-excursion
740     (set-buffer nntp-server-buffer)
741     (if (zerop (length nnir-artlist))
742         (progn
743           (setq nnir-current-query nil
744                 nnir-current-server nil
745                 nnir-current-group-marked nil
746                 nnir-artlist nil)
747           (nnheader-report 'nnir "Search produced empty results."))
748       ;; Remember data for cache.
749       (setq nnir-current-query group)
750       (when server (setq nnir-current-server server))
751       (setq nnir-current-group-marked gnus-group-marked)
752       (nnheader-insert "211 %d %d %d %s\n"
753                        (nnir-artlist-length nnir-artlist) ; total #
754                        1              ; first #
755                        (nnir-artlist-length nnir-artlist) ; last #
756                        group))))     ; group name
757
758 (deffoo nnir-retrieve-headers (articles &optional group server fetch-old)
759   (save-excursion
760     (let ((artlist (copy-sequence articles))
761           art artitem artgroup artno artrsv artfullgroup
762           novitem novdata foo server)
763       (while (not (null artlist))
764         (setq art (car artlist))
765         (or (numberp art)
766             (nnheader-report
767              'nnir
768              "nnir-retrieve-headers doesn't grok message ids: %s"
769              art))
770         (setq artitem (nnir-artlist-article nnir-artlist art))
771         (setq artrsv (nnir-artitem-rsv artitem))
772         (setq artfullgroup (nnir-artitem-group artitem))
773         (setq artno (nnir-artitem-number artitem))
774         (setq artgroup (gnus-group-real-name artfullgroup))
775         (setq server (nnir-group-server artfullgroup))
776         ;; retrieve NOV or HEAD data for this article, transform into
777         ;; NOV data and prepend to `novdata'
778         (set-buffer nntp-server-buffer)
779         (nnir-possibly-change-server server)
780         (let ((gnus-override-method
781                (gnus-server-to-method server)))
782           (case (setq foo (gnus-retrieve-headers (list artno) artfullgroup nil))
783             (nov
784              (goto-char (point-min))
785              (setq novitem (nnheader-parse-nov))
786              (unless novitem
787                (pop-to-buffer nntp-server-buffer)
788                (error
789                 "nnheader-parse-nov returned nil for article %s in group %s"
790                 artno artfullgroup)))
791             (headers
792              (goto-char (point-min))
793              (setq novitem (nnheader-parse-head))
794              (unless novitem
795                (pop-to-buffer nntp-server-buffer)
796                (error
797                 "nnheader-parse-head returned nil for article %s in group %s"
798                 artno artfullgroup)))
799             (t (error "Unknown header type %s while requesting article %s of group %s"
800                       foo artno artfullgroup))))
801         ;; replace article number in original group with article number
802         ;; in nnir group
803         (mail-header-set-number novitem art)
804         (mail-header-set-from novitem
805                               (mail-header-from novitem))
806         (mail-header-set-subject
807          novitem
808          (format "[%d: %s/%d] %s"
809                  artrsv artgroup artno
810                  (mail-header-subject novitem)))
811         ;;-(mail-header-set-extra novitem nil)
812         (push novitem novdata)
813         (setq artlist (cdr artlist)))
814       (setq novdata (nreverse novdata))
815       (set-buffer nntp-server-buffer) (erase-buffer)
816       (mapc 'nnheader-insert-nov novdata)
817       'nov)))
818
819 (deffoo nnir-request-article (article
820                               &optional group server to-buffer)
821   (if (stringp article)
822       (nnheader-report
823        'nnir
824        "nnir-retrieve-headers doesn't grok message ids: %s"
825        article)
826     (save-excursion
827       (let* ((artitem (nnir-artlist-article nnir-artlist
828                                             article))
829              (artfullgroup (nnir-artitem-group artitem))
830              (artno (nnir-artitem-number artitem))
831              ;; Bug?
832              ;; Why must we bind nntp-server-buffer here?  It won't
833              ;; work if `buf' is used, say.  (Of course, the set-buffer
834              ;; line below must then be updated, too.)
835              (nntp-server-buffer (or to-buffer nntp-server-buffer)))
836         (set-buffer nntp-server-buffer)
837         (erase-buffer)
838         (message "Requesting article %d from group %s"
839                  artno artfullgroup)
840         (gnus-request-article artno artfullgroup nntp-server-buffer)
841         (cons artfullgroup artno)))))
842
843
844 (nnoo-define-skeleton nnir)
845
846
847 (defmacro nnir-add-result (dirnam artno score prefix server artlist)
848   "Ask `nnir-compose-result' to construct a result vector,
849 and if it is non-nil, add it to artlist."
850   `(let ((result (nnir-compose-result ,dirnam ,artno ,score ,prefix ,server)))
851      (when (not (null result))
852        (push result ,artlist))))
853
854 (autoload 'nnmaildir-base-name-to-article-number "nnmaildir")
855
856 ;; Helper function currently used by the Swish++ and Namazu backends;
857 ;; perhaps useful for other backends as well
858 (defun nnir-compose-result (dirnam article score prefix server)
859   "Extract the group from dirnam, and create a result vector
860 ready to be added to the list of search results."
861
862   ;; remove nnir-*-remove-prefix from beginning of dirnam filename
863   (when (string-match (concat "^" prefix) dirnam)
864     (setq dirnam (replace-match "" t t dirnam)))
865
866   (when (file-readable-p (concat prefix dirnam article))
867     ;; remove trailing slash and, for nnmaildir, cur/new/tmp
868     (setq dirnam
869           (substring dirnam 0 (if (string= server "nnmaildir:") -5 -1)))
870
871     ;; Set group to dirnam without any leading dots or slashes,
872     ;; and with all subsequent slashes replaced by dots
873     (let ((group (gnus-replace-in-string
874                  (gnus-replace-in-string dirnam "^[./\\]" "" t)
875                  "[/\\]" "." t)))
876
877     (vector (nnir-group-full-name group server)
878             (if (string= server "nnmaildir:")
879                 (nnmaildir-base-name-to-article-number
880                  (substring article 0 (string-match ":" article))
881                  group nil)
882               (string-to-number article))
883             (string-to-number score)))))
884
885 ;;; Search Engine Interfaces:
886
887 ;; freeWAIS-sf interface.
888 (defun nnir-run-waissearch (query server &optional group)
889   "Run given query agains waissearch.  Returns vector of (group name, file name)
890 pairs (also vectors, actually)."
891   (when group
892     (error "The freeWAIS-sf backend cannot search specific groups"))
893   (save-excursion
894     (let ((qstring (cdr (assq 'query query)))
895           (prefix (nnir-read-server-parm 'nnir-wais-remove-prefix server))
896           artlist score artno dirnam)
897       (set-buffer (get-buffer-create nnir-tmp-buffer))
898       (erase-buffer)
899       (message "Doing WAIS query %s..." query)
900       (call-process nnir-wais-program
901                     nil                 ; input from /dev/null
902                     t                   ; output to current buffer
903                     nil                 ; don't redisplay
904                     "-d" (nnir-read-server-parm 'nnir-wais-database server) ; database to search
905                     qstring)
906       (message "Massaging waissearch output...")
907       ;; remove superfluous lines
908       (keep-lines "Score:")
909       ;; extract data from result lines
910       (goto-char (point-min))
911       (while (re-search-forward
912               "Score: +\\([0-9]+\\).*'\\([0-9]+\\) +\\([^']+\\)/'" nil t)
913         (setq score (match-string 1)
914               artno (match-string 2)
915               dirnam (match-string 3))
916         (unless (string-match prefix dirnam)
917           (nnheader-report 'nnir "Dir name %s doesn't contain prefix %s"
918                            dirnam prefix))
919         (setq group (gnus-replace-in-string
920                      (replace-match "" t t dirnam) "/" "."))
921         (push (vector (nnir-group-full-name group server)
922                       (string-to-number artno)
923                       (string-to-number score))
924               artlist))
925       (message "Massaging waissearch output...done")
926       (apply 'vector
927              (sort artlist
928                    (function (lambda (x y)
929                                (> (nnir-artitem-rsv x)
930                                   (nnir-artitem-rsv y)))))))))
931
932 ;; IMAP interface.
933 ;; todo:
934 ;; nnir invokes this two (2) times???!
935 ;; we should not use nnimap at all but open our own server connection
936 ;; we should not LIST * but use nnimap-list-pattern from defs
937 ;; send queries as literals
938 ;; handle errors
939
940 (autoload 'nnimap-open-server "nnimap")
941 (defvar nnimap-server-buffer) ;; nnimap.el
942 (autoload 'imap-mailbox-select "imap")
943 (autoload 'imap-search "imap")
944 (autoload 'imap-quote-specials "imap")
945
946 (defun nnir-run-imap (query srv &optional group-option)
947   "Run a search against an IMAP back-end server.
948 This uses a custom query language parser; see `nnir-imap-make-query' for
949 details on the language and supported extensions"
950   (save-excursion
951     (let ((qstring (cdr (assq 'query query)))
952           (server (cadr (gnus-server-to-method srv)))
953           (group (or group-option (gnus-group-group-name)))
954           (defs (caddr (gnus-server-to-method srv)))
955           (criteria (or (cdr (assq 'criteria query))
956                         nnir-imap-search-field))
957           artlist buf)
958       (message "Opening server %s" server)
959       (condition-case ()
960           (when (nnimap-open-server server defs) ;; xxx
961             (setq buf nnimap-server-buffer) ;; xxx
962             (message "Searching %s..." group)
963             (let ((arts 0)
964                   (mbx (gnus-group-real-name group)))
965               (when (imap-mailbox-select mbx nil buf)
966                 (mapc
967                  (lambda (artnum)
968                    (push (vector group artnum 1) artlist)
969                    (setq arts (1+ arts)))
970                  (imap-search (nnir-imap-make-query criteria qstring) buf))
971                 (message "Searching %s... %d matches" mbx arts)))
972             (message "Searching %s...done" group))
973         (quit nil))
974       (reverse artlist))))
975
976 (defun nnir-imap-make-query (criteria qstring)
977   "Parse the query string and criteria into an appropriate IMAP search
978 expression, returning the string query to make.
979
980 This implements a little language designed to return the expected results
981 to an arbitrary query string to the end user.
982
983 The search is always case-insensitive, as defined by RFC2060, and supports
984 the following features (inspired by the Google search input language):
985
986 Automatic \"and\" queries
987     If you specify multiple words then they will be treated as an \"and\"
988     expression intended to match all components.
989
990 Phrase searches
991     If you wrap your query in double-quotes then it will be treated as a
992     literal string.
993
994 Negative terms
995     If you precede a term with \"-\" then it will negate that.
996
997 \"OR\" queries
998     If you include an upper-case \"OR\" in your search it will cause the
999     term before it and the term after it to be treated as alternatives.
1000
1001 In future the following will be added to the language:
1002  * support for date matches
1003  * support for location of text matching within the query
1004  * from/to/etc headers
1005  * additional search terms
1006  * flag based searching
1007  * anything else that the RFC supports, basically."
1008   ;; Walk through the query and turn it into an IMAP query string.
1009   (nnir-imap-query-to-imap criteria (nnir-imap-parse-query qstring)))
1010
1011
1012 (defun nnir-imap-query-to-imap (criteria query)
1013   "Turn a s-expression format query into IMAP."
1014   (mapconcat
1015    ;; Turn the expressions into IMAP text
1016    (lambda (item)
1017      (nnir-imap-expr-to-imap criteria item))
1018    ;; The query, already in s-expr format.
1019    query
1020    ;; Append a space between each expression
1021    " "))
1022
1023
1024 (defun nnir-imap-expr-to-imap (criteria expr)
1025   "Convert EXPR into an IMAP search expression on CRITERIA"
1026   ;; What sort of expression is this, eh?
1027   (cond
1028    ;; Simple string term
1029    ((stringp expr)
1030     (format "%s \"%s\"" criteria (imap-quote-specials expr)))
1031    ;; Trivial term: and
1032    ((eq expr 'and) nil)
1033    ;; Composite term: or expression
1034    ((eq (car-safe expr) 'or)
1035     (format "OR %s %s"
1036             (nnir-imap-expr-to-imap criteria (second expr))
1037             (nnir-imap-expr-to-imap criteria (third expr))))
1038    ;; Composite term: just the fax, mam
1039    ((eq (car-safe expr) 'not)
1040     (format "NOT (%s)" (nnir-imap-query-to-imap criteria (rest expr))))
1041    ;; Composite term: just expand it all.
1042    ((and (not (null expr)) (listp expr))
1043     (format "(%s)" (nnir-imap-query-to-imap criteria expr)))
1044    ;; Complex value, give up for now.
1045    (t (error "Unhandled input: %S" expr))))
1046
1047
1048 (defun nnir-imap-parse-query (string)
1049   "Turn STRING into an s-expression based query based on the IMAP
1050 query language as defined in `nnir-imap-make-query'.
1051
1052 This involves turning individual tokens into higher level terms
1053 that the search language can then understand and use."
1054   (with-temp-buffer
1055     ;; Set up the parsing environment.
1056     (insert string)
1057     (goto-char (point-min))
1058     ;; Now, collect the output terms and return them.
1059     (let (out)
1060       (while (not (nnir-imap-end-of-input))
1061         (push (nnir-imap-next-expr) out))
1062       (reverse out))))
1063
1064
1065 (defun nnir-imap-next-expr (&optional count)
1066   "Return the next expression from the current buffer."
1067   (let ((term (nnir-imap-next-term count))
1068         (next (nnir-imap-peek-symbol)))
1069     ;; Are we looking at an 'or' expression?
1070     (cond
1071      ;; Handle 'expr or expr'
1072      ((eq next 'or)
1073       (list 'or term (nnir-imap-next-expr 2)))
1074      ;; Anything else
1075      (t term))))
1076
1077
1078 (defun nnir-imap-next-term (&optional count)
1079   "Return the next TERM from the current buffer."
1080   (let ((term (nnir-imap-next-symbol count)))
1081     ;; What sort of term is this?
1082     (cond
1083      ;; and -- just ignore it
1084      ((eq term 'and) 'and)
1085      ;; negated term
1086      ((eq term 'not) (list 'not (nnir-imap-next-expr)))
1087      ;; generic term
1088      (t term))))
1089
1090
1091 (defun nnir-imap-peek-symbol ()
1092   "Return the next symbol from the current buffer, but don't consume it."
1093   (save-excursion
1094     (nnir-imap-next-symbol)))
1095
1096 (defun nnir-imap-next-symbol (&optional count)
1097   "Return the next symbol from the current buffer, or nil if we are
1098 at the end of the buffer.  If supplied COUNT skips some symbols before
1099 returning the one at the supplied position."
1100   (when (and (numberp count) (> count 1))
1101     (nnir-imap-next-symbol (1- count)))
1102   (let ((case-fold-search t))
1103     ;; end of input stream?
1104     (unless (nnir-imap-end-of-input)
1105       ;; No, return the next symbol from the stream.
1106       (cond
1107        ;; negated expression -- return it and advance one char.
1108        ((looking-at "-") (forward-char 1) 'not)
1109        ;; quoted string
1110        ((looking-at "\"") (nnir-imap-delimited-string "\""))
1111        ;; list expression -- we parse the content and return this as a list.
1112        ((looking-at "(")
1113         (nnir-imap-parse-query (nnir-imap-delimited-string ")")))
1114        ;; keyword input -- return a symbol version
1115        ((looking-at "\\band\\b") (forward-char 3) 'and)
1116        ((looking-at "\\bor\\b")  (forward-char 2) 'or)
1117        ((looking-at "\\bnot\\b") (forward-char 3) 'not)
1118        ;; Simple, boring keyword
1119        (t (let ((start (point))
1120                 (end (if (search-forward-regexp "[[:blank:]]" nil t)
1121                          (prog1
1122                              (match-beginning 0)
1123                            ;; unskip if we hit a non-blank terminal character.
1124                            (when (string-match "[^[:blank:]]" (match-string 0))
1125                              (backward-char 1)))
1126                        (goto-char (point-max)))))
1127             (buffer-substring start end)))))))
1128
1129 (defun nnir-imap-delimited-string (delimiter)
1130   "Return a delimited string from the current buffer."
1131   (let ((start (point)) end)
1132     (forward-char 1)                    ; skip the first delimiter.
1133     (while (not end)
1134       (unless (search-forward delimiter nil t)
1135         (error "Unmatched delimited input with %s in query" delimiter))
1136       (let ((here (point)))
1137         (unless (equal (buffer-substring (- here 2) (- here 1)) "\\")
1138           (setq end (point)))))
1139     (buffer-substring (1+ start) (1- end))))
1140
1141 (defun nnir-imap-end-of-input ()
1142   "Are we at the end of input?"
1143   (skip-chars-forward "[[:blank:]]")
1144   (looking-at "$"))
1145
1146
1147 ;; Swish++ interface.
1148 ;; -cc- Todo
1149 ;; Search by
1150 ;; - group
1151 ;; Sort by
1152 ;; - rank (default)
1153 ;; - article number
1154 ;; - file size
1155 ;; - group
1156 (defun nnir-run-swish++ (query server &optional group)
1157   "Run QUERY against swish++.
1158 Returns a vector of (group name, file name) pairs (also vectors,
1159 actually).
1160
1161 Tested with swish++ 4.7 on GNU/Linux and with swish++ 5.0b2 on
1162 Windows NT 4.0."
1163
1164   (when group
1165     (error "The swish++ backend cannot search specific groups"))
1166
1167   (save-excursion
1168     (let ( (qstring (cdr (assq 'query query)))
1169            (groupspec (cdr (assq 'group query)))
1170            (prefix (nnir-read-server-parm 'nnir-swish++-remove-prefix server))
1171            artlist
1172            ;; nnml-use-compressed-files might be any string, but probably this
1173            ;; is sufficient.  Note that we can't only use the value of
1174            ;; nnml-use-compressed-files because old articles might have been
1175            ;; saved with a different value.
1176            (article-pattern (if (string= server "nnmaildir:")
1177                                 ":[0-9]+"
1178                               "^[0-9]+\\(\\.[a-z0-9]+\\)?$"))
1179            score artno dirnam filenam)
1180
1181       (when (equal "" qstring)
1182         (error "swish++: You didn't enter anything"))
1183
1184       (set-buffer (get-buffer-create nnir-tmp-buffer))
1185       (erase-buffer)
1186
1187       (if groupspec
1188           (message "Doing swish++ query %s on %s..." qstring groupspec)
1189         (message "Doing swish++ query %s..." qstring))
1190
1191       (let* ((cp-list `( ,nnir-swish++-program
1192                          nil            ; input from /dev/null
1193                          t              ; output
1194                          nil            ; don't redisplay
1195                          "--config-file" ,(nnir-read-server-parm 'nnir-swish++-configuration-file server)
1196                          ,@(nnir-read-server-parm 'nnir-swish++-additional-switches server)
1197                          ,qstring       ; the query, in swish++ format
1198                          ))
1199              (exitstatus
1200               (progn
1201                 (message "%s args: %s" nnir-swish++-program
1202                          (mapconcat 'identity (cddddr cp-list) " ")) ;; ???
1203                 (apply 'call-process cp-list))))
1204         (unless (or (null exitstatus)
1205                     (zerop exitstatus))
1206           (nnheader-report 'nnir "Couldn't run swish++: %s" exitstatus)
1207           ;; swish++ failure reason is in this buffer, show it if
1208           ;; the user wants it.
1209           (when (> gnus-verbose 6)
1210             (display-buffer nnir-tmp-buffer))))
1211
1212       ;; The results are output in the format of:
1213       ;; V 4.7 Linux
1214       ;; rank relative-path-name file-size file-title
1215       ;; V 5.0b2:
1216       ;; rank relative-path-name file-size topic??
1217       ;; where rank is an integer from 1 to 100.
1218       (goto-char (point-min))
1219       (while (re-search-forward
1220               "\\(^[0-9]+\\) \\([^ ]+\\) [0-9]+ \\(.*\\)$" nil t)
1221         (setq score (match-string 1)
1222               filenam (match-string 2)
1223               artno (file-name-nondirectory filenam)
1224               dirnam (file-name-directory filenam))
1225
1226         ;; don't match directories
1227         (when (string-match article-pattern artno)
1228           (when (not (null dirnam))
1229
1230             ;; maybe limit results to matching groups.
1231             (when (or (not groupspec)
1232                       (string-match groupspec dirnam))
1233               (nnir-add-result dirnam artno score prefix server artlist)))))
1234
1235       (message "Massaging swish++ output...done")
1236
1237       ;; Sort by score
1238       (apply 'vector
1239              (sort artlist
1240                    (function (lambda (x y)
1241                                (> (nnir-artitem-rsv x)
1242                                   (nnir-artitem-rsv y)))))))))
1243
1244 ;; Swish-E interface.
1245 (defun nnir-run-swish-e (query server &optional group)
1246   "Run given query against swish-e.
1247 Returns a vector of (group name, file name) pairs (also vectors,
1248 actually).
1249
1250 Tested with swish-e-2.0.1 on Windows NT 4.0."
1251
1252   ;; swish-e crashes with empty parameter to "-w" on commandline...
1253   (when group
1254     (error "The swish-e backend cannot search specific groups"))
1255
1256   (save-excursion
1257     (let ((qstring (cdr (assq 'query query)))
1258           (prefix
1259            (or (nnir-read-server-parm 'nnir-swish-e-remove-prefix server)
1260                (error "Missing parameter `nnir-swish-e-remove-prefix'")))
1261           artlist score artno dirnam group )
1262
1263       (when (equal "" qstring)
1264         (error "swish-e: You didn't enter anything"))
1265
1266       (set-buffer (get-buffer-create nnir-tmp-buffer))
1267       (erase-buffer)
1268
1269       (message "Doing swish-e query %s..." query)
1270       (let* ((index-files
1271               (or (nnir-read-server-parm
1272                    'nnir-swish-e-index-files server)
1273                   (error "Missing parameter `nnir-swish-e-index-files'")))
1274              (additional-switches
1275               (nnir-read-server-parm
1276                'nnir-swish-e-additional-switches server))
1277              (cp-list `(,nnir-swish-e-program
1278                         nil             ; input from /dev/null
1279                         t               ; output
1280                         nil             ; don't redisplay
1281                         "-f" ,@index-files
1282                         ,@additional-switches
1283                         "-w"
1284                         ,qstring        ; the query, in swish-e format
1285                         ))
1286              (exitstatus
1287               (progn
1288                 (message "%s args: %s" nnir-swish-e-program
1289                          (mapconcat 'identity (cddddr cp-list) " "))
1290                 (apply 'call-process cp-list))))
1291         (unless (or (null exitstatus)
1292                     (zerop exitstatus))
1293           (nnheader-report 'nnir "Couldn't run swish-e: %s" exitstatus)
1294           ;; swish-e failure reason is in this buffer, show it if
1295           ;; the user wants it.
1296           (when (> gnus-verbose 6)
1297             (display-buffer nnir-tmp-buffer))))
1298
1299       ;; The results are output in the format of:
1300       ;; rank path-name file-title file-size
1301       (goto-char (point-min))
1302       (while (re-search-forward
1303               "\\(^[0-9]+\\) \\([^ ]+\\) \"\\([^\"]+\\)\" [0-9]+$" nil t)
1304         (setq score (match-string 1)
1305               artno (match-string 3)
1306               dirnam (file-name-directory (match-string 2)))
1307
1308         ;; don't match directories
1309         (when (string-match "^[0-9]+$" artno)
1310           (when (not (null dirnam))
1311
1312             ;; remove nnir-swish-e-remove-prefix from beginning of dirname
1313             (when (string-match (concat "^" prefix) dirnam)
1314               (setq dirnam (replace-match "" t t dirnam)))
1315
1316             (setq dirnam (substring dirnam 0 -1))
1317             ;; eliminate all ".", "/", "\" from beginning. Always matches.
1318             (string-match "^[./\\]*\\(.*\\)$" dirnam)
1319             ;; "/" -> "."
1320             (setq group (gnus-replace-in-string (match-string 1 dirnam) "/" "."))
1321             ;; Windows "\\" -> "."
1322             (setq group (gnus-replace-in-string group "\\\\" "."))
1323
1324             (push (vector (nnir-group-full-name group server)
1325                           (string-to-number artno)
1326                           (string-to-number score))
1327                   artlist))))
1328
1329       (message "Massaging swish-e output...done")
1330
1331       ;; Sort by score
1332       (apply 'vector
1333              (sort artlist
1334                    (function (lambda (x y)
1335                                (> (nnir-artitem-rsv x)
1336                                   (nnir-artitem-rsv y)))))))))
1337
1338 ;; HyREX interface
1339 (defun nnir-run-hyrex (query server &optional group)
1340   (save-excursion
1341     (let ((artlist nil)
1342           (groupspec (cdr (assq 'group query)))
1343           (qstring (cdr (assq 'query query)))
1344           (prefix (nnir-read-server-parm 'nnir-hyrex-remove-prefix server))
1345           score artno dirnam)
1346       (when (and group groupspec)
1347         (error (concat "It does not make sense to use a group spec"
1348                        " with process-marked groups.")))
1349       (when group
1350         (setq groupspec (gnus-group-real-name group)))
1351       (when (and group (not (equal group (nnir-group-full-name groupspec server))))
1352         (message "%s vs. %s" group (nnir-group-full-name groupspec server))
1353         (error "Server with groupspec doesn't match group !"))
1354       (set-buffer (get-buffer-create nnir-tmp-buffer))
1355       (erase-buffer)
1356       (if groupspec
1357           (message "Doing hyrex-search query %s on %s..." query groupspec)
1358         (message "Doing hyrex-search query %s..." query))
1359       (let* ((cp-list
1360               `( ,nnir-hyrex-program
1361                  nil                    ; input from /dev/null
1362                  t                      ; output
1363                  nil                    ; don't redisplay
1364                  "-i",(nnir-read-server-parm 'nnir-hyrex-index-directory server) ; index directory
1365                  ,@(nnir-read-server-parm 'nnir-hyrex-additional-switches server)
1366                  ,qstring          ; the query, in hyrex-search format
1367                  ))
1368              (exitstatus
1369               (progn
1370                 (message "%s args: %s" nnir-hyrex-program
1371                          (mapconcat 'identity (cddddr cp-list) " "))
1372                 (apply 'call-process cp-list))))
1373         (unless (or (null exitstatus)
1374                     (zerop exitstatus))
1375           (nnheader-report 'nnir "Couldn't run hyrex-search: %s" exitstatus)
1376           ;; nnir-search failure reason is in this buffer, show it if
1377           ;; the user wants it.
1378           (when (> gnus-verbose 6)
1379             (display-buffer nnir-tmp-buffer)))) ;; FIXME: Dont clear buffer !
1380       (if groupspec
1381           (message "Doing hyrex-search query \"%s\" on %s...done" qstring groupspec)
1382         (message "Doing hyrex-search query \"%s\"...done" qstring))
1383       (sit-for 0)
1384       ;; nnir-search returns:
1385       ;;   for nnml/nnfolder: "filename mailid weigth"
1386       ;;   for nnimap:        "group mailid weigth"
1387       (goto-char (point-min))
1388       (delete-non-matching-lines "^\\S + [0-9]+ [0-9]+$")
1389       ;; HyREX couldn't search directly in groups -- so filter out here.
1390       (when groupspec
1391         (keep-lines groupspec))
1392       ;; extract data from result lines
1393       (goto-char (point-min))
1394       (while (re-search-forward
1395               "\\(\\S +\\) \\([0-9]+\\) \\([0-9]+\\)" nil t)
1396         (setq dirnam (match-string 1)
1397               artno (match-string 2)
1398               score (match-string 3))
1399         (when (string-match prefix dirnam)
1400           (setq dirnam (replace-match "" t t dirnam)))
1401         (push (vector (nnir-group-full-name
1402                        (gnus-replace-in-string dirnam "/" ".") server)
1403                       (string-to-number artno)
1404                       (string-to-number score))
1405               artlist))
1406       (message "Massaging hyrex-search output...done.")
1407       (apply 'vector
1408              (sort artlist
1409                    (function (lambda (x y)
1410                                (if (string-lessp (nnir-artitem-group x)
1411                                                  (nnir-artitem-group y))
1412                                    t
1413                                  (< (nnir-artitem-number x)
1414                                     (nnir-artitem-number y)))))))
1415       )))
1416
1417 ;; Namazu interface
1418 (defun nnir-run-namazu (query server &optional group)
1419   "Run given query against Namazu.  Returns a vector of (group name, file name)
1420 pairs (also vectors, actually).
1421
1422 Tested with Namazu 2.0.6 on a GNU/Linux system."
1423   (when group
1424     (error "The Namazu backend cannot search specific groups"))
1425   (save-excursion
1426     (let ((article-pattern (if (string= server "nnmaildir:")
1427                                ":[0-9]+"
1428                              "^[0-9]+$"))
1429           artlist
1430           (qstring (cdr (assq 'query query)))
1431           (prefix (nnir-read-server-parm 'nnir-namazu-remove-prefix server))
1432           score group article
1433           (process-environment (copy-sequence process-environment)))
1434       (setenv "LC_MESSAGES" "C")
1435       (set-buffer (get-buffer-create nnir-tmp-buffer))
1436       (erase-buffer)
1437       (let* ((cp-list
1438               `( ,nnir-namazu-program
1439                  nil                    ; input from /dev/null
1440                  t                      ; output
1441                  nil                    ; don't redisplay
1442                  "-q"                   ; don't be verbose
1443                  "-a"                   ; show all matches
1444                  "-s"                   ; use short format
1445                  ,@(nnir-read-server-parm 'nnir-namazu-additional-switches server)
1446                  ,qstring               ; the query, in namazu format
1447                  ,(nnir-read-server-parm 'nnir-namazu-index-directory server) ; index directory
1448                  ))
1449              (exitstatus
1450               (progn
1451                 (message "%s args: %s" nnir-namazu-program
1452                          (mapconcat 'identity (cddddr cp-list) " "))
1453                 (apply 'call-process cp-list))))
1454         (unless (or (null exitstatus)
1455                     (zerop exitstatus))
1456           (nnheader-report 'nnir "Couldn't run namazu: %s" exitstatus)
1457           ;; Namazu failure reason is in this buffer, show it if
1458           ;; the user wants it.
1459           (when (> gnus-verbose 6)
1460             (display-buffer nnir-tmp-buffer))))
1461
1462       ;; Namazu output looks something like this:
1463       ;; 2. Re: Gnus agent expire broken (score: 55)
1464       ;; /home/henrik/Mail/mail/sent/1310 (4,138 bytes)
1465
1466       (goto-char (point-min))
1467       (while (re-search-forward
1468               "^\\([0-9]+\\.\\).*\\((score: \\([0-9]+\\)\\))\n\\([^ ]+\\)"
1469               nil t)
1470         (setq score (match-string 3)
1471               group (file-name-directory (match-string 4))
1472               article (file-name-nondirectory (match-string 4)))
1473
1474         ;; make sure article and group is sane
1475         (when (and (string-match article-pattern article)
1476                    (not (null group)))
1477           (nnir-add-result group article score prefix server artlist)))
1478
1479       ;; sort artlist by score
1480       (apply 'vector
1481              (sort artlist
1482                    (function (lambda (x y)
1483                                (> (nnir-artitem-rsv x)
1484                                   (nnir-artitem-rsv y)))))))))
1485
1486 (defun nnir-run-find-grep (query server &optional group)
1487   "Run find and grep to obtain matching articles."
1488   (let* ((method (gnus-server-to-method server))
1489          (sym (intern
1490                (concat (symbol-name (car method)) "-directory")))
1491          (directory (cadr (assoc sym (cddr method))))
1492          (regexp (cdr (assoc 'query query)))
1493          (grep-options (cdr (assoc 'grep-options query)))
1494          artlist)
1495     (unless directory
1496       (error "No directory found in method specification of server %s"
1497              server))
1498     (message "Searching %s using find-grep..." (or group server))
1499     (save-window-excursion
1500       (set-buffer (get-buffer-create nnir-tmp-buffer))
1501       (erase-buffer)
1502       (if (> gnus-verbose 6)
1503           (pop-to-buffer (current-buffer)))
1504       (cd directory) ; Using relative paths simplifies postprocessing.
1505       (let ((group
1506              (if (not group)
1507                  "."
1508                ;; Try accessing the group literally as well as
1509                ;; interpreting dots as directory separators so the
1510                ;; engine works with plain nnml as well as the Gnus Cache.
1511                (let ((group (gnus-group-real-name group)))
1512                  ;; Replace cl-func find-if.
1513                  (if (file-directory-p group)
1514                      group
1515                    (if (file-directory-p
1516                         (setq group (gnus-replace-in-string group "\\." "/" t)))
1517                        group))))))
1518         (unless group
1519           (error "Cannot locate directory for group"))
1520         (save-excursion
1521           (apply
1522            'call-process "find" nil t
1523            "find" group "-type" "f" "-name" "[0-9]*" "-exec"
1524            "grep"
1525            `("-l" ,@(and grep-options
1526                          ;; Note: the 3rd arg of `split-string' is not
1527                          ;; available in Emacs 21.
1528                          (delete "" (split-string grep-options "\\s-")))
1529              "-e" ,regexp "{}" "+"))))
1530
1531       ;; Translate relative paths to group names.
1532       (while (not (eobp))
1533         (let* ((path (delete
1534                       ""
1535                       (split-string
1536                        (buffer-substring (point) (line-end-position)) "/")))
1537                (art (string-to-number (car (last path)))))
1538           (while (string= "." (car path))
1539             (setq path (cdr path)))
1540           (let ((group (mapconcat 'identity
1541                                   ;; Replace cl-func: (subseq path 0 -1)
1542                                   (let ((end (1- (length path)))
1543                                         res)
1544                                     (while (>= (setq end (1- end)) 0)
1545                                       (push (pop path) res))
1546                                     (nreverse res))
1547                                   ".")))
1548             (push (vector (nnir-group-full-name group server) art 0)
1549                   artlist))
1550           (forward-line 1)))
1551       (message "Searching %s using find-grep...done" (or group server))
1552       artlist)))
1553
1554 ;;; Util Code:
1555
1556 (defun nnir-read-parms (query)
1557   "Reads additional search parameters according to `nnir-engines'."
1558   (let ((parmspec (caddr (assoc nnir-search-engine nnir-engines))))
1559     (cons (cons 'query query)
1560           (mapcar 'nnir-read-parm parmspec))))
1561
1562 (defun nnir-read-parm (parmspec)
1563   "Reads a single search parameter.
1564 `parmspec' is a cons cell, the car is a symbol, the cdr is a prompt."
1565   (let ((sym (car parmspec))
1566         (prompt (cdr parmspec)))
1567     (if (listp prompt)
1568         (let* ((result (apply 'completing-read prompt))
1569                (mapping (or (assoc result nnir-imap-search-arguments)
1570                             (assoc nil nnir-imap-search-arguments))))
1571           (cons sym (format (cdr mapping) result)))
1572       (cons sym (read-string prompt)))))
1573
1574 (defun nnir-run-query (query)
1575   "Invoke appropriate search engine function (see `nnir-engines').
1576 If some groups were process-marked, run the query for each of the groups
1577 and concat the results."
1578   (let ((q (car (read-from-string query))))
1579     (if gnus-group-marked
1580         (apply 'vconcat
1581                (mapcar (lambda (x)
1582                          (let ((server (nnir-group-server x))
1583                                search-func)
1584                            (setq search-func (cadr
1585                                               (assoc
1586                                                (nnir-read-server-parm 'nnir-search-engine server) nnir-engines)))
1587                            (if search-func
1588                                (funcall search-func q server x)
1589                              nil)))
1590                        gnus-group-marked)
1591                )
1592       (apply 'vconcat
1593              (mapcar (lambda (x)
1594                        (if (and (equal (cadr x) 'ok) (not (equal (cadar x) "-ephemeral")))
1595                            (let ((server (format "%s:%s" (caar x) (cadar x)))
1596                                  search-func)
1597                              (setq search-func (cadr
1598                                                 (assoc
1599                                                  (nnir-read-server-parm 'nnir-search-engine server) nnir-engines)))
1600                              (if search-func
1601                                  (funcall search-func q server nil)
1602                                nil))
1603                          nil))
1604                      gnus-opened-servers)
1605              ))
1606     ))
1607
1608 (defun nnir-read-server-parm (key server)
1609   "Returns the parameter value of for the given server, where server is of
1610 form 'backend:name'."
1611   (let ((method (gnus-server-to-method server)))
1612     (cond ((and method (assq key (cddr method)))
1613            (nth 1 (assq key (cddr method))))
1614           ((and nnir-mail-backend
1615                 (gnus-server-equal method nnir-mail-backend))
1616            (symbol-value key))
1617           (t nil))))
1618 ;;     (if method
1619 ;;       (if (assq key (cddr method))
1620 ;;        (nth 1 (assq key (cddr method)))
1621 ;;      (symbol-value key))
1622 ;;       (symbol-value key))
1623 ;;     ))
1624
1625 (defun nnir-group-full-name (shortname server)
1626   "For the given group name, return a full Gnus group name.
1627 The Gnus backend/server information is added."
1628   (gnus-group-prefixed-name shortname (gnus-server-to-method server)))
1629
1630 (defun nnir-possibly-change-server (server)
1631   (unless (and server (nnir-server-opened server))
1632     (nnir-open-server server)))
1633
1634
1635 ;; Data type article list.
1636
1637 (defun nnir-artlist-length (artlist)
1638   "Returns number of articles in artlist."
1639   (length artlist))
1640
1641 (defun nnir-artlist-article (artlist n)
1642   "Returns from ARTLIST the Nth artitem (counting starting at 1)."
1643   (elt artlist (1- n)))
1644
1645 (defun nnir-artitem-group (artitem)
1646   "Returns the group from the ARTITEM."
1647   (elt artitem 0))
1648
1649 (defun nnir-artlist-artitem-group (artlist n)
1650   "Returns from ARTLIST the group of the Nth artitem (counting from 1)."
1651   (nnir-artitem-group (nnir-artlist-article artlist n)))
1652
1653 (defun nnir-artitem-number (artitem)
1654   "Returns the number from the ARTITEM."
1655   (elt artitem 1))
1656
1657 (defun nnir-artlist-artitem-number (artlist n)
1658   "Returns from ARTLIST the number of the Nth artitem (counting from 1)."
1659   (nnir-artitem-number (nnir-artlist-article artlist n)))
1660
1661 (defun nnir-artitem-rsv (artitem)
1662   "Returns the Retrieval Status Value (RSV, score) from the ARTITEM."
1663   (elt artitem 2))
1664
1665 (defun nnir-artlist-artitem-rsv (artlist n)
1666   "Returns from ARTLIST the Retrieval Status Value of the Nth artitem
1667 \(counting from 1)."
1668   (nnir-artitem-rsv (nnir-artlist-article artlist n)))
1669
1670 ;; unused?
1671 (defun nnir-artlist-groups (artlist)
1672   "Returns a list of all groups in the given ARTLIST."
1673   (let ((res nil)
1674         (with-dups nil))
1675     ;; from each artitem, extract group component
1676     (setq with-dups (mapcar 'nnir-artitem-group artlist))
1677     ;; remove duplicates from above
1678     (mapc (function (lambda (x) (add-to-list 'res x)))
1679             with-dups)
1680     res))
1681
1682
1683 ;; The end.
1684 (provide 'nnir)
1685
1686 ;; arch-tag: 9b3fecf8-4397-4bbb-bf3c-6ac3cbbc6664
1687 ;;; nnir.el ends here