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