1 ;;; nnir.el --- search mail with various search engines
2 ;; Copyright (C) 1998 Kai Großjohann
4 ;; Author: Kai Großjohann <grossjohann@ls6.cs.uni-dortmund.de>
5 ;; Keywords: news, mail, searching, ir, glimpse, wais
7 ;; This file is not part of GNU Emacs.
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 2, or (at your option)
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.
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., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; The most recent version of this can always be fetched from the
27 ;; following FTP site:
28 ;; ls6-ftp.cs.uni-dortmund.de:/pub/src/emacs
30 ;; This code is still in the development stage but I'd like other
31 ;; people to have a look at it. Please do not hesitate to contact me
34 ;; What does it do? Well, it allows you to index your mail using some
35 ;; search engine (freeWAIS-sf and Glimpse are currently supported),
36 ;; then type `G G' in the Group buffer and issue a query to the search
37 ;; engine. You will then get a buffer which shows all articles
38 ;; matching the query, sorted by Retrieval Status Value (score).
40 ;; When looking at the retrieval result (in the Summary buffer) you
41 ;; can type `G T' (aka M-x gnus-summary-nnir-goto-thread RET) on an
42 ;; article. You will be teleported into the group this article came
43 ;; from, showing the thread this article is part of. (See below for
46 ;; The Lisp installation is simple: just put this file on your
47 ;; load-path, byte-compile it, and load it from ~/.gnus or something.
48 ;; This will install a new command `G G' in your Group buffer for
49 ;; searching your mail. Note that you also need to configure a number
50 ;; of variables, as described below.
54 ;; * Currently, this expects that you use nnml or another
55 ;; one-file-per-message backend.
56 ;; * It can only search one mail backend.
57 ;; * There are restrictions to the Glimpse setup.
58 ;; * There are restrictions to the Wais setup.
59 ;; * gnus-summary-nnir-goto-thread: Fetches whole group first, before
60 ;; limiting to the right articles. This is much too slow, of
61 ;; course. May issue a query for number of articles to fetch; you
62 ;; must accept the default of all articles at this point or things
65 ;; The Lisp setup involves setting a few variables and setting up the
66 ;; search engine. The first variable to set is `nnir-mail-backend'.
67 ;; For me, `gnus-secondary-select-methods' contains just one select
68 ;; method, and this is also what I put in `nnir-mail-backend'. Type
69 ;; `C-h v nnir-mail-backend RET' for more information -- the variable
70 ;; documentation includes more details and a few examples. The second
71 ;; variable to set is `nnir-search-engine'. Choose one of the engines
72 ;; listed in `nnir-engines'. (Actually `nnir-engines' is an alist,
73 ;; type `C-h v nnir-engines RET' for more information; this includes
74 ;; examples for setting `nnir-search-engine', too.)
76 ;; You must also set up a search engine. I'll tell you about the two
77 ;; search engines currently supported:
81 ;; As always with freeWAIS-sf, you need a so-called `format file'. I
82 ;; use the following file:
85 ;; | # Kai's format file for freeWAIS-sf for indexing mails.
86 ;; | # Each mail is in a file, much like the MH format.
88 ;; | # Document separator should never match -- each file is a document.
89 ;; | record-sep: /^@this regex should never match@$/
91 ;; | # Searchable fields specification.
93 ;; | region: /^[sS]ubject:/ /^[sS]ubject: */
94 ;; | subject "Subject header" stemming TEXT BOTH
97 ;; | region: /^([tT][oO]|[cC][cC]):/ /^([tT][oO]|[cC][cC]): */
98 ;; | to "To and Cc headers" SOUNDEX BOTH
101 ;; | region: /^[fF][rR][oO][mM]:/ /^[fF][rR][oO][mM]: */
102 ;; | from "From header" SOUNDEX BOTH
106 ;; | stemming TEXT GLOBAL
107 ;; | end: /^@this regex should never match@$/
110 ;; 1998-07-22: waisindex would dump core on me for large articles with
111 ;; the above settings. I used /^$/ as the end regex for the global
112 ;; field. That seemed to work okay.
114 ;; There is a Perl module called `WAIS.pm' which is available from
115 ;; CPAN as well as ls6-ftp.cs.uni-dortmund.de:/pub/wais/Perl. This
116 ;; module comes with a nifty tool called `makedb', which I use for
117 ;; indexing. Here's my `makedb.conf':
120 ;; | # Config file for makedb
122 ;; | # Global options
123 ;; | waisindex = /usr/local/bin/waisindex
124 ;; | wais_opt = -stem -t fields
125 ;; | # `-stem' option necessary when `stemming' is specified for the
126 ;; | # global field in the *.fmt file
129 ;; | homedir = /home/kai
131 ;; | # The mail database.
133 ;; | files = `find $homedir/Mail -name \*[0-9] -print`
134 ;; | dbdir = $homedir/.wais
138 ;; The Lisp setup involves the `nnir-wais-*' variables. The most
139 ;; difficult to understand variable is probably
140 ;; `nnir-wais-remove-prefix'. Here's what it does: the output of
141 ;; `waissearch' basically contains the file name and the (full)
142 ;; directory name. As Gnus works with group names rather than
143 ;; directory names, the directory name is transformed into a group
144 ;; name as follows: first, a prefix is removed from the (full)
145 ;; directory name, then all `/' are replaced with `.'. The variable
146 ;; `nnir-wais-remove-prefix' should contain a regex matching exactly
147 ;; this prefix. It defaults to `$HOME/Mail/' (note the trailing
152 ;; The code expects you to have one Glimpse index which contains all
153 ;; your mail files. The Lisp setup involves setting the
154 ;; `nnir-glimpse-*' variables. The most difficult to understand
155 ;; variable is probably `nnir-glimpse-remove-prefix', it corresponds
156 ;; to `nnir-wais-remove-prefix', see above. The `nnir-glimpse-home'
157 ;; variable should be set to the value of the `-H' option which allows
158 ;; one to search this Glimpse index. I have indexed my whole home
159 ;; directory with Glimpse, so I assume a default of `$HOME'.
163 ;; The Namazu backend requires you to have one directory containing all
164 ;; index files, this is controlled by the `nnir-namazu-index-directory'
165 ;; variable. To function the `nnir-namazu-remove-prefix' variable must
166 ;; also be correct, see the documentation for `nnir-wais-remove-prefix'
169 ;; It is particularly important not to pass any any switches to namazu
170 ;; that will change the output format. Good switches to use include
171 ;; `--sort', `--ascending', `--early' and `--late'. Refer to the Namazu
172 ;; documentation for further information on valid switches.
174 ;; To index my mail with the `mknmz' program I use the following
175 ;; configuration file:
178 ;; | package conf; # Don't remove this line!
180 ;; | # Paths which will not be indexed. Don't use `^' or `$' anchors.
181 ;; | $EXCLUDE_PATH = "spam|sent";
183 ;; | # Header fields which should be searchable. case-insensitive
184 ;; | $REMAIN_HEADER = "from|date|message-id|subject";
186 ;; | # Searchable fields. case-insensitive
187 ;; | $SEARCH_FIELD = "from|date|message-id|subject";
189 ;; | # The max length of a word.
190 ;; | $WORD_LENG_MAX = 128;
192 ;; | # The max length of a field.
193 ;; | $MAX_FIELD_LENGTH = 256;
196 ;; My mail is stored in the directories ~/Mail/mail/, ~/Mail/lists/ and
197 ;; ~/Mail/archive/, so to index them I go to the directory set in
198 ;; `nnir-namazu-index-directory' and issue the following command.
200 ;; mknmz --mailnews ~/Mail/archive/ ~/Mail/mail/ ~/Mail/lists/
202 ;; For maximum searching efficiency I have a cron job set to run this
203 ;; command every four hours.
205 ;; Developer information:
207 ;; I have tried to make the code expandable. Basically, it is divided
208 ;; into two layers. The upper layer is somewhat like the `nnvirtual'
209 ;; or `nnkiboze' backends: given a specification of what articles to
210 ;; show from another backend, it creates a group containing exactly
211 ;; those articles. The lower layer issues a query to a search engine
212 ;; and produces such a specification of what articles to show from the
215 ;; The interface between the two layers consists of the single
216 ;; function `nnir-run-query', which just selects the appropriate
217 ;; function for the search engine one is using. The input to
218 ;; `nnir-run-query' is a string, representing the query as input by
219 ;; the user. The output of `nnir-run-query' is supposed to be a
220 ;; vector, each element of which should in turn be a three-element
221 ;; vector. The first element should be group name of the article, the
222 ;; second element should be the article number, and the third element
223 ;; should be the Retrieval Status Value (RSV) as returned from the
224 ;; search engine. An RSV is the score assigned to the document by the
225 ;; search engine. For Boolean search engines like Glimpse, the RSV is
226 ;; always 1000 (or 1 or 100, or whatever you like).
228 ;; The sorting order of the articles in the summary buffer created by
229 ;; nnir is based on the order of the articles in the above mentioned
230 ;; vector, so that's where you can do the sorting you'd like. Maybe
231 ;; it would be nice to have a way of displaying the search result
232 ;; sorted differently?
234 ;; So what do you need to do when you want to add another search
235 ;; engine? You write a function that executes the query. Temporary
236 ;; data from the search engine can be put in `nnir-tmp-buffer'. This
237 ;; function should return the list of articles as a vector, as
238 ;; described above. Then, you need to register this backend in
239 ;; `nnir-engines'. Then, users can choose the backend by setting
240 ;; `nnir-search-engine'.
242 ;; Todo, or future ideas:
244 ;; * Make it so that Glimpse can also be called without `-F'.
246 ;; * It should be possible to restrict search to certain groups.
248 ;; * There is currently no error checking.
250 ;; * The summary buffer display is currently really ugly, with all the
251 ;; added information in the subjects. How could I make this
254 ;; * A function which can be called from an nnir summary buffer which
255 ;; teleports you into the group the current article came from and
256 ;; shows you the whole thread this article is part of.
257 ;; Implementation suggestions?
258 ;; (1998-07-24: There is now a preliminary implementation, but
259 ;; it is much too slow and quite fragile.)
261 ;; * Support other mail backends. In particular, probably quite a few
262 ;; people use nnfolder. How would one go about searching nnfolders
263 ;; and producing the right data needed? The group name and the RSV
264 ;; are simple, but what about the article number?
266 ;; * Support compressed mail files. Probably, just stripping off the
267 ;; `.gz' or `.Z' file name extension is sufficient.
269 ;; * Support a find/grep combination.
271 ;; * At least for imap, the query is performed twice.
273 ;; * Support multiple mail backends. The information that is needed
274 ;; by nnir could be put in the server parameters. (Use sensible
275 ;; default values, though: include the name of the backend in the
276 ;; default value such that people do not have to mess with the
277 ;; server parameters if they don't want to.) It is not clear how to
278 ;; do the user interface, though. Hm. Maybe offer the user a
279 ;; completable list of backends to search? Or use the
280 ;; process-marked groups to find out which backends to search? Or
281 ;; always search all backends?
284 ;; Have you got other ideas?
288 (defconst nnir-version "1.72"
293 (require 'gnus-group)
296 (require 'gnus-util))
299 (nnoo-define-basics nnir)
301 (gnus-declare-backend "nnir" 'mail)
303 ;;; Developer Extension Variable:
306 '((glimpse nnir-run-glimpse
307 ((group . "Group spec: ")))
308 (wais nnir-run-waissearch
310 (excite nnir-run-excite-search
314 (swish++ nnir-run-swish++
315 ((group . "Group spec: ")))
316 (swish-e nnir-run-swish-e
317 ((group . "Group spec: ")))
318 (namazu nnir-run-namazu
320 "Alist of supported search engines.
321 Each element in the alist is a three-element list (ENGINE FUNCTION ARGS).
322 ENGINE is a symbol designating the searching engine. FUNCTION is also
323 a symbol, giving the function that does the search. The third element
324 ARGS is a list of cons pairs (PARAM . PROMPT). When issuing a query,
325 the FUNCTION will issue a query for each of the PARAMs, using PROMPT.
327 The value of `nnir-search-engine' must be one of the ENGINE symbols.
328 For example, use the following line for searching using freeWAIS-sf:
329 (setq nnir-search-engine 'wais)
330 Use the following line if you read your mail via IMAP and your IMAP
331 server supports searching:
332 (setq nnir-search-engine 'imap)
333 Note that you have to set additional variables for most backends. For
334 example, the `wais' backend needs the variables `nnir-wais-program',
335 `nnir-wais-database' and `nnir-wais-remove-prefix'.
337 Add an entry here when adding a new search engine.")
339 ;;; User Customizable Variables:
342 "Search nnmh and nnml groups in Gnus with Glimpse, freeWAIS-sf, or EWS."
348 ;; If `nil', use server parameters to find out which server to search. CCC
350 (defcustom nnir-mail-backend '(nnml "")
351 "*Specifies which backend should be searched.
352 More precisely, this is used to determine from which backend to fetch the
355 This must be equal to an existing server, so maybe it is best to use
356 something like the following:
357 (setq nnir-mail-backend (nth 0 gnus-secondary-select-methods))
358 The above line works fine if the mail backend you want to search is
359 the first element of gnus-secondary-select-methods (`nth' starts counting
364 ;; Search engine to use.
366 (defcustom nnir-search-engine 'wais
367 "*The search engine to use. Must be a symbol.
368 See `nnir-engines' for a list of supported engines, and for example
369 settings of `nnir-search-engine'."
375 (defcustom nnir-glimpse-program "glimpse"
376 "*Name of Glimpse executable."
380 (defcustom nnir-glimpse-home (getenv "HOME")
381 "*Value of `-H' glimpse option.
382 `~' and environment variables must be expanded, see the functions
383 `expand-file-name' and `substitute-in-file-name'."
387 (defcustom nnir-glimpse-remove-prefix (concat (getenv "HOME") "/Mail/")
388 "*The prefix to remove from each file name returned by Glimpse
389 in order to get a group name (albeit with / instead of .). This is a
392 For example, suppose that Glimpse returns file names such as
393 \"/home/john/Mail/mail/misc/42\". For this example, use the following
394 setting: (setq nnir-glimpse-remove-prefix \"/home/john/Mail/\")
395 Note the trailing slash. Removing this prefix gives \"mail/misc/42\".
396 `nnir' knows to remove the \"/42\" and to replace \"/\" with \".\" to
397 arrive at the correct group name, \"mail.misc\"."
401 (defcustom nnir-glimpse-additional-switches '("-i")
402 "*A list of strings, to be given as additional arguments to glimpse.
403 The switches `-H', `-W', `-l' and `-y' are always used -- calling
404 glimpse without them does not make sense in our situation.
405 Suggested elements to put here are `-i' and `-w'.
407 Note that this should be a list. Ie, do NOT use the following:
408 (setq nnir-glimpse-additional-switches \"-i -w\") ; wrong!
410 (setq nnir-glimpse-additional-switches '(\"-i\" \"-w\"))"
411 :type '(repeat (string))
416 (defcustom nnir-wais-program "waissearch"
417 "*Name of waissearch executable."
421 (defcustom nnir-wais-database (expand-file-name "~/.wais/mail")
422 "*Name of Wais database containing the mail.
424 Note that this should be a file name without extension. For example,
425 if you have a file /home/john/.wais/mail.fmt, use this:
426 (setq nnir-wais-database \"/home/john/.wais/mail\")
427 The string given here is passed to `waissearch -d' as-is."
431 (defcustom nnir-wais-remove-prefix (concat (getenv "HOME") "/Mail/")
432 "*The prefix to remove from each directory name returned by waissearch
433 in order to get a group name (albeit with / instead of .). This is a
436 This variable is similar to `nnir-glimpse-remove-prefix', only for Wais,
441 ;; EWS (Excite for Web Servers) engine.
443 (defcustom nnir-excite-aquery-program "aquery.pl"
444 "*Name of the EWS query program. Should be `aquery.pl' or a path to same."
448 (defcustom nnir-excite-collection "Mail"
449 "*Name of the EWS collection to search."
453 (defcustom nnir-excite-remove-prefix (concat (getenv "HOME") "/Mail/")
454 "*The prefix to remove from each file name returned by EWS
455 in order to get a group name (albeit with / instead of .). This is a
458 This variable is very similar to `nnir-glimpse-remove-prefix', except
459 that it is for EWS, not Glimpse."
463 (defcustom nnir-imap-default-charset nil
464 "*Name of the charset of the strings that appear in the search criteria."
465 :type '(choice (const nil) symbol)
468 ;; Swish++. Next three variables Copyright (C) 2000, 2001 Christoph
469 ;; Conrad <christoph.conrad@gmx.de>.
470 ;; Swish++ home page: http://homepage.mac.com/pauljlucas/software/swish/
472 (defcustom nnir-swish++-configuration-file
473 (expand-file-name "~/Mail/swish++.conf")
474 "*Configuration file for swish++."
478 (defcustom nnir-swish++-program "search"
479 "*Name of swish++ search executable."
483 (defcustom nnir-swish++-additional-switches '()
484 "*A list of strings, to be given as additional arguments to swish++.
486 Note that this should be a list. Ie, do NOT use the following:
487 (setq nnir-swish++-additional-switches \"-i -w\") ; wrong
489 (setq nnir-swish++-additional-switches '(\"-i\" \"-w\"))"
490 :type '(repeat (string))
493 (defcustom nnir-swish++-remove-prefix (concat (getenv "HOME") "/Mail/")
494 "*The prefix to remove from each file name returned by swish++
495 in order to get a group name (albeit with / instead of .). This is a
498 This variable is very similar to `nnir-glimpse-remove-prefix', except
499 that it is for swish++, not Glimpse."
503 ;; Swish-E. Next three variables Copyright (C) 2000 Christoph Conrad
504 ;; <christoph.conrad@gmx.de>.
505 ;; URL: http://sunsite.berkeley.edu/SWISH-E/
506 ;; New version: http://www.boe.es/swish-e
508 (defcustom nnir-swish-e-index-file
509 (expand-file-name "~/Mail/index.swish-e")
510 "*Index file for swish-e."
514 (defcustom nnir-swish-e-program "swish-e"
515 "*Name of swish-e search executable."
519 (defcustom nnir-swish-e-additional-switches '()
520 "*A list of strings, to be given as additional arguments to swish-e.
522 Note that this should be a list. Ie, do NOT use the following:
523 (setq nnir-swish-e-additional-switches \"-i -w\") ; wrong
525 (setq nnir-swish-e-additional-switches '(\"-i\" \"-w\"))"
526 :type '(repeat (string))
529 (defcustom nnir-swish-e-remove-prefix (concat (getenv "HOME") "/Mail/")
530 "*The prefix to remove from each file name returned by swish-e
531 in order to get a group name (albeit with / instead of .). This is a
534 This variable is very similar to `nnir-glimpse-remove-prefix', except
535 that it is for swish-e, not Glimpse."
539 ;; Namazu engine, see <URL:http://ww.namazu.org/>
541 (defcustom nnir-namazu-program "namazu"
542 "*Name of Namazu search executable."
546 (defcustom nnir-namazu-index-directory (expand-file-name "~/Mail/namazu/")
547 "*Index directory for Namazu."
551 (defcustom nnir-namazu-additional-switches '()
552 "*A list of strings, to be given as additional arguments to namazu.
553 The switches `-q', `-a', and `-s' are always used, very few other switches
554 make any sense in this context.
556 Note that this should be a list. Ie, do NOT use the following:
557 (setq nnir-namazu-additional-switches \"-i -w\") ; wrong
559 (setq nnir-namazu-additional-switches '(\"-i\" \"-w\"))"
560 :type '(repeat (string))
563 (defcustom nnir-namazu-remove-prefix (concat (getenv "HOME") "/Mail/")
564 "*The prefix to remove from each file name returned by Namazu
565 in order to get a group name (albeit with / instead of .).
567 This variable is very similar to `nnir-glimpse-remove-prefix', except
568 that it is for Namazu, not Glimpse."
572 ;;; Internal Variables:
574 (defvar nnir-current-query nil
575 "Internal: stores current query (= group name).")
577 (defvar nnir-current-server nil
578 "Internal: stores current server (does it ever change?).")
580 (defvar nnir-current-group-marked nil
581 "Internal: stores current list of process-marked groups.")
583 (defvar nnir-artlist nil
584 "Internal: stores search result.")
586 (defvar nnir-tmp-buffer " *nnir*"
587 "Internal: temporary buffer.")
593 (defun gnus-group-make-nnir-group (extra-parms query)
594 "Create an nnir group. Asks for query."
595 (interactive "P\nsQuery: ")
598 (setq parms (nnir-read-parms query))
599 (setq parms (list (cons 'query query))))
600 (gnus-group-read-ephemeral-group
601 (concat "nnir:" (prin1-to-string parms)) '(nnir "") t
602 (cons (current-buffer)
603 gnus-current-window-configuration)
606 ;; Emacs 19 compatibility?
607 (or (fboundp 'kbd) (defalias 'kbd 'read-kbd-macro))
609 (defun nnir-group-mode-hook ()
610 (define-key gnus-group-mode-map
611 (if (fboundp 'read-kbd-macro)
613 "GG") ; XEmacs 19 compat
614 'gnus-group-make-nnir-group))
615 (add-hook 'gnus-group-mode-hook
617 (unless (string-match "T-gnus" gnus-version)
618 (nnir-group-mode-hook))))
622 ;; Summary mode commands.
624 (defun gnus-summary-nnir-goto-thread ()
625 "Only applies to nnir groups. Go to group this article came from
626 and show thread that contains this article."
628 (unless (eq 'nnir (car (gnus-find-method-for-group gnus-newsgroup-name)))
629 (error "Can't execute this command unless in nnir group."))
630 (let* ((cur (gnus-summary-article-number))
631 (backend-group (nnir-artlist-artitem-group nnir-artlist cur))
632 (backend-number (nnir-artlist-artitem-number nnir-artlist cur)))
633 (gnus-group-read-ephemeral-group
637 (cons (current-buffer)
638 'summary) ; window config
640 (list backend-number))
641 (gnus-summary-limit (list backend-number))
642 (gnus-summary-refer-thread)))
644 (if (fboundp 'eval-after-load)
645 (eval-after-load "gnus-sum"
646 '(define-key gnus-summary-goto-map
647 "T" 'gnus-summary-nnir-goto-thread))
648 (add-hook 'gnus-summary-mode-hook
650 (define-key gnus-summary-goto-map
651 "T" 'gnus-summary-nnir-goto-thread)))))
655 ;; Gnus backend interface functions.
657 (deffoo nnir-open-server (server &optional definitions)
658 ;; Just set the server variables appropriately.
659 (nnoo-change-server 'nnir server definitions))
661 (deffoo nnir-request-group (group &optional server fast)
662 "GROUP is the query string."
663 (nnir-possibly-change-server server)
664 ;; Check for cache and return that if appropriate.
665 (if (and (equal group nnir-current-query)
666 (equal gnus-group-marked nnir-current-group-marked)
668 (equal server nnir-current-server)))
671 (setq nnir-artlist (nnir-run-query group))
673 (set-buffer nntp-server-buffer)
674 (if (zerop (length nnir-artlist))
676 (setq nnir-current-query nil
677 nnir-current-server nil
678 nnir-current-group-marked nil
680 (nnheader-report 'nnir "Search produced empty results."))
681 ;; Remember data for cache.
682 (setq nnir-current-query group)
683 (when server (setq nnir-current-server server))
684 (setq nnir-current-group-marked gnus-group-marked)
685 (nnheader-insert "211 %d %d %d %s\n"
686 (nnir-artlist-length nnir-artlist) ; total #
688 (nnir-artlist-length nnir-artlist) ; last #
689 group))))) ; group name
691 (deffoo nnir-retrieve-headers (articles &optional group server fetch-old)
693 (let ((artlist (copy-sequence articles))
697 (artgroup nil) (artno nil)
703 (while (not (null artlist))
704 (setq art (car artlist))
708 "nnir-retrieve-headers doesn't grok message ids: %s"
710 (setq artitem (nnir-artlist-article nnir-artlist art))
711 (setq artrsv (nnir-artitem-rsv artitem))
712 (setq artgroup (nnir-artitem-group artitem))
713 (setq artno (nnir-artitem-number artitem))
714 (setq artfullgroup (nnir-group-full-name artgroup))
715 ;; retrieve NOV or HEAD data for this article, transform into
716 ;; NOV data and prepend to `novdata'
717 (set-buffer nntp-server-buffer)
718 (case (setq foo (gnus-retrieve-headers (list artno) artfullgroup nil))
720 (goto-char (point-min))
721 (setq novitem (nnheader-parse-nov))
723 (pop-to-buffer nntp-server-buffer)
725 "nnheader-parse-nov returned nil for article %s in group %s"
726 artno artfullgroup)))
728 (goto-char (point-min))
729 (setq novitem (nnheader-parse-head))
731 (pop-to-buffer nntp-server-buffer)
733 "nnheader-parse-head returned nil for article %s in group %s"
734 artno artfullgroup)))
735 (t (nnheader-report 'nnir "Don't support header type %s." foo)))
736 ;; replace article number in original group with article number
738 (mail-header-set-number novitem idx)
739 (mail-header-set-from novitem
740 (mail-header-from novitem))
741 (mail-header-set-subject
743 (format "[%d: %s/%d] %s"
744 artrsv artgroup artno
745 (mail-header-subject novitem)))
746 ;;-(mail-header-set-extra novitem nil)
747 (push novitem novdata)
748 (setq artlist (cdr artlist))
750 (setq novdata (nreverse novdata))
751 (set-buffer nntp-server-buffer) (erase-buffer)
752 (mapcar 'nnheader-insert-nov novdata)
755 (deffoo nnir-request-article (article
756 &optional group server to-buffer)
758 (let* ((artitem (nnir-artlist-article nnir-artlist
760 (artgroup (nnir-artitem-group artitem))
761 (artno (nnir-artitem-number artitem))
763 ;; Why must we bind nntp-server-buffer here? It won't
764 ;; work if `buf' is used, say. (Of course, the set-buffer
765 ;; line below must then be updated, too.)
766 (nntp-server-buffer (or to-buffer nntp-server-buffer)))
767 (set-buffer nntp-server-buffer)
769 (message "Requesting article %d from group %s"
771 (nnir-group-full-name artgroup))
772 (gnus-request-article artno (nnir-group-full-name artgroup)
774 (cons artgroup artno))))
777 (nnoo-define-skeleton nnir)
779 ;;; Search Engine Interfaces:
781 ;; Glimpse interface.
782 (defun nnir-run-glimpse (query &optional group)
783 "Run given query against glimpse. Returns a vector of (group name, file name)
784 pairs (also vectors, actually)."
787 (groupspec (cdr (assq 'group query)))
788 (qstring (cdr (assq 'query query))))
789 (when (and group groupspec)
790 (error (concat "It does not make sense to use a group spec"
791 " with process-marked groups.")))
793 (setq groupspec (gnus-group-real-name group)))
794 (set-buffer (get-buffer-create nnir-tmp-buffer))
797 (message "Doing glimpse query %s on %s..." query groupspec)
798 (message "Doing glimpse query %s..." query))
800 `( ,nnir-glimpse-program
801 nil ; input from /dev/null
803 nil ; don't redisplay
804 "-H" ,nnir-glimpse-home ; search home dir
805 "-W" ; match pattern in file
806 "-l" "-y" ; misc options
807 ,@nnir-glimpse-additional-switches
808 "-F" ,nnir-glimpse-remove-prefix ; restrict output to mail
809 ,qstring ; the query, in glimpse format
813 (message "%s args: %s" nnir-glimpse-program
814 (mapconcat 'identity (cddddr cp-list) " "))
815 (apply 'call-process cp-list))))
816 (unless (or (null exitstatus)
818 (nnheader-report 'nnir "Couldn't run glimpse: %s" exitstatus)
819 ;; Glimpse failure reason is in this buffer, show it if
820 ;; the user wants it.
821 (when (> gnus-verbose 6)
822 (display-buffer nnir-tmp-buffer))))
824 (keep-lines groupspec))
826 (message "Doing glimpse query %s on %s...done" query groupspec)
827 (message "Doing glimpse query %s...done" query))
829 ;; CCC: The following work of extracting group name and article
830 ;; number from the Glimpse output can probably better be done by
831 ;; just going through the buffer once, and plucking out the
832 ;; right information from each line.
833 ;; remove superfluous stuff from glimpse output
834 (goto-char (point-min))
835 (delete-non-matching-lines "/[0-9]+$")
836 ;;(delete-matching-lines "\\.overview~?$")
837 (goto-char (point-min))
838 (while (re-search-forward (concat "^" nnir-glimpse-remove-prefix) nil t)
840 ;; separate group name from article number with \t
841 ;; XEmacs compatible version
842 (goto-char (point-max))
843 (while (re-search-backward "/[0-9]+$" nil t)
846 ; Emacs compatible version
847 ; (goto-char (point-min))
848 ; (while (re-search-forward "\\(/\\)[0-9]+$" nil t)
849 ; (replace-match "\t" t t nil 1))
850 ;; replace / with . in group names
851 (subst-char-in-region (point-min) (point-max) ?/ ?. t)
852 ;; massage buffer to contain some Lisp;
853 ;; this depends on the artlist encoding internals
854 ;; maybe this dependency should be removed?
855 (goto-char (point-min))
858 (skip-chars-forward "^\t")
861 (insert " 1000 ]") ; 1000 = score
864 (goto-char (point-min))
865 (insert "(setq artlist [\n")
868 (function (lambda (x y)
869 (if (string-lessp (nnir-artitem-group x)
870 (nnir-artitem-group y))
872 (< (nnir-artitem-number x)
873 (nnir-artitem-number y))))))
876 ;; freeWAIS-sf interface.
877 (defun nnir-run-waissearch (query &optional group)
878 "Run given query agains waissearch. Returns vector of (group name, file name)
879 pairs (also vectors, actually)."
881 (error "The freeWAIS-sf backend cannot search specific groups."))
883 (let ((qstring (cdr (assq 'query query)))
885 (score nil) (artno nil) (dirnam nil) (group nil))
886 (set-buffer (get-buffer-create nnir-tmp-buffer))
888 (message "Doing WAIS query %s..." query)
889 (call-process nnir-wais-program
890 nil ; input from /dev/null
891 t ; output to current buffer
892 nil ; don't redisplay
893 "-d" nnir-wais-database ; database to search
895 (message "Massaging waissearch output...")
896 ;; remove superfluous lines
897 (keep-lines "Score:")
898 ;; extract data from result lines
899 (goto-char (point-min))
900 (while (re-search-forward
901 "Score: +\\([0-9]+\\).*'\\([0-9]+\\) +\\([^']+\\)/'" nil t)
902 (setq score (match-string 1)
903 artno (match-string 2)
904 dirnam (match-string 3))
905 (unless (string-match nnir-wais-remove-prefix dirnam)
906 (nnheader-report 'nnir "Dir name %s doesn't contain prefix %s"
907 dirnam nnir-wais-remove-prefix))
908 (setq group (substitute ?. ?/ (replace-match "" t t dirnam)))
910 (string-to-int artno)
911 (string-to-int score))
913 (message "Massaging waissearch output...done")
916 (function (lambda (x y)
917 (> (nnir-artitem-rsv x)
918 (nnir-artitem-rsv y)))))))))
920 ;; EWS (Excite for Web Servers) interface
921 (defun nnir-run-excite-search (query &optional group)
922 "Run a given query against EWS. Returns vector of (group name, file name)
923 pairs (also vectors, actually)."
925 (error "Searching specific groups not implemented for EWS."))
927 (let ((qstring (cdr (assq 'query query)))
928 artlist group article-num article)
929 (setq nnir-current-query query)
930 (set-buffer (get-buffer-create nnir-tmp-buffer))
932 (message "Doing EWS query %s..." qstring)
933 (call-process nnir-excite-aquery-program
934 nil ; input from /dev/null
935 t ; output to current buffer
936 nil ; don't redisplay
937 nnir-excite-collection
938 (if (string= (substring qstring 0 1) "(")
940 (format "(concept %s)" qstring)))
941 (message "Gathering query output...")
943 (goto-char (point-min))
944 (while (re-search-forward
945 "^[0-9]+\\s-[0-9]+\\s-[0-9]+\\s-\\(\\S-*\\)" nil t)
946 (setq article (match-string 1))
947 (unless (string-match
948 (concat "^" (regexp-quote nnir-excite-remove-prefix)
949 "\\(.*\\)/\\([0-9]+\\)") article)
950 (nnheader-report 'nnir "Dir name %s doesn't contain prefix %s"
951 article nnir-excite-remove-prefix))
952 (setq group (substitute ?. ?/ (match-string 1 article)))
953 (setq article-num (match-string 2 article))
954 (setq artlist (vconcat artlist (vector (vector group
955 (string-to-int article-num)
957 (message "Gathering query output...done")
960 ;; IMAP interface. The following function is Copyright (C) 1998 Simon
961 ;; Josefsson <jas@pdc.kth.se>.
963 ;; nnir invokes this two (2) times???!
964 ;; we should not use nnimap at all but open our own server connection
965 ;; we should not LIST * but use nnimap-list-pattern from defs
966 ;; send queries as literals
970 (defvar nnimap-server-buffer))
972 (defun nnir-run-imap (query &optional group)
977 (error "Must specify groups for IMAP searching."))
979 (let ((qstring (cdr (assq 'query query)))
980 (server (cadr nnir-mail-backend))
981 (defs (caddr nnir-mail-backend))
983 (message "Opening server %s" server)
985 (when (nnimap-open-server server defs) ;; xxx
986 (setq buf nnimap-server-buffer) ;; xxx
987 (message "Searching %s..." group)
989 (mbx (gnus-group-real-name group))
990 (multibyte-p (mm-multibyte-p))
991 charset coding-system)
992 (when (imap-mailbox-select mbx nil buf)
995 (mm-enable-multibyte))
997 (setq charset (car (mm-find-mime-charset-region
998 (point-min)(point-max)))))
1000 (setq charset nnir-imap-default-charset))
1003 (push (vector mbx artnum 1) artlist)
1004 (setq arts (1+ arts)))
1005 (if (and (not (eq charset 'us-ascii))
1006 (setq coding-system (mm-charset-to-coding-system
1009 (concat "CHARSET " (symbol-name charset) " TEXT \""
1010 (mm-encode-coding-string qstring coding-system)
1012 (imap-search (concat "TEXT \"" qstring "\"") buf)))
1013 (message "Searching %s... %d matches" mbx arts)))
1014 (message "Searching %s...done" group))
1016 (reverse artlist))))
1018 ;; Swish++ interface. The following function is Copyright (C) 2000,
1019 ;; 2001 Christoph Conrad <christoph.conrad@gmx.de>.
1028 (defun nnir-run-swish++ (query &optional group)
1029 "Run given query against swish++.
1030 Returns a vector of (group name, file name) pairs (also vectors,
1033 Tested with swish++ 4.7 on GNU/Linux and with with swish++ 5.0b2 on
1037 (error "The swish++ backend cannot search specific groups."))
1040 (let ( (qstring (cdr (assq 'query query)))
1041 (groupspec (cdr (assq 'group query)))
1043 (score nil) (artno nil) (dirnam nil) (group nil) )
1045 (when (equal "" qstring)
1046 (error "swish++: You didn't enter anything."))
1048 (set-buffer (get-buffer-create nnir-tmp-buffer))
1052 (message "Doing swish++ query %s on %s..." qstring groupspec)
1053 (message "Doing swish++ query %s..." qstring))
1055 (let* ((cp-list `( ,nnir-swish++-program
1056 nil ; input from /dev/null
1058 nil ; don't redisplay
1059 "--config-file" ,nnir-swish++-configuration-file
1060 ,@nnir-swish++-additional-switches
1061 ,qstring ; the query, in swish++ format
1065 (message "%s args: %s" nnir-swish++-program
1066 (mapconcat 'identity (cddddr cp-list) " "));; ???
1067 (apply 'call-process cp-list))))
1068 (unless (or (null exitstatus)
1070 (nnheader-report 'nnir "Couldn't run swish++: %s" exitstatus)
1071 ;; swish++ failure reason is in this buffer, show it if
1072 ;; the user wants it.
1073 (when (> gnus-verbose 6)
1074 (display-buffer nnir-tmp-buffer))))
1076 ;; The results are output in the format of:
1078 ;; rank relative-path-name file-size file-title
1080 ;; rank relative-path-name file-size topic??
1081 ;; where rank is an integer from 1 to 100.
1082 (goto-char (point-min))
1083 (while (re-search-forward
1084 "\\(^[0-9]+\\) \\([^ ]+\\) [0-9]+ \\(.*\\)$" nil t)
1085 (setq score (match-string 1)
1086 artno (file-name-nondirectory (match-string 2))
1087 dirnam (file-name-directory (match-string 2)))
1089 ;; don't match directories
1090 (when (string-match "^[0-9]+$" artno)
1091 (when (not (null dirnam))
1093 ; maybe limit results to matching groups.
1094 (when (or (not groupspec)
1095 (string-match groupspec dirnam))
1097 ;; remove nnir-swish++-remove-prefix from beginning of dirname
1098 (when (string-match (concat "^" nnir-swish++-remove-prefix)
1100 (setq dirnam (replace-match "" t t dirnam)))
1102 (setq dirnam (substring dirnam 0 -1))
1103 ;; eliminate all ".", "/", "\" from beginning. Always matches.
1104 (string-match "^[./\\]*\\(.*\\)$" dirnam)
1106 (setq group (substitute ?. ?/ (match-string 1 dirnam)))
1108 (setq group (substitute ?. ?\\ group))
1111 (string-to-int artno)
1112 (string-to-int score))
1115 (message "Massaging swish++ output...done")
1120 (function (lambda (x y)
1121 (> (nnir-artitem-rsv x)
1122 (nnir-artitem-rsv y)))))))))
1124 ;; Swish-E interface. The following function is Copyright (C) 2000,
1125 ;; 2001 by Christoph Conrad <christoph.conrad@gmx.de>.
1126 (defun nnir-run-swish-e (query &optional group)
1127 "Run given query against swish-e.
1128 Returns a vector of (group name, file name) pairs (also vectors,
1131 Tested with swish-e-2.0.1 on Windows NT 4.0."
1133 ;; swish-e crashes with empty parameter to "-w" on commandline...
1135 (error "The swish-e backend cannot search specific groups."))
1138 (let ( (qstring (cdr (assq 'query query)))
1140 (score nil) (artno nil) (dirnam nil) (group nil) )
1142 (when (equal "" qstring)
1143 (error "swish-e: You didn't enter anything."))
1145 (set-buffer (get-buffer-create nnir-tmp-buffer))
1148 (message "Doing swish-e query %s..." query)
1149 (let* ((cp-list `( ,nnir-swish-e-program
1150 nil ; input from /dev/null
1152 nil ; don't redisplay
1153 "-f" ,nnir-swish-e-index-file
1154 ,@nnir-swish-e-additional-switches
1156 ,qstring ; the query, in swish-e format
1160 (message "%s args: %s" nnir-swish-e-program
1161 (mapconcat 'identity (cddddr cp-list) " "))
1162 (apply 'call-process cp-list))))
1163 (unless (or (null exitstatus)
1165 (nnheader-report 'nnir "Couldn't run swish-e: %s" exitstatus)
1166 ;; swish-e failure reason is in this buffer, show it if
1167 ;; the user wants it.
1168 (when (> gnus-verbose 6)
1169 (display-buffer nnir-tmp-buffer))))
1171 ;; The results are output in the format of:
1172 ;; rank path-name file-title file-size
1173 (goto-char (point-min))
1174 (while (re-search-forward
1175 "\\(^[0-9]+\\) \\([^ ]+\\) \"\\([^\"]+\\)\" [0-9]+$" nil t)
1176 (setq score (match-string 1)
1177 artno (match-string 3)
1178 dirnam (file-name-directory (match-string 2)))
1180 ;; don't match directories
1181 (when (string-match "^[0-9]+$" artno)
1182 (when (not (null dirnam))
1184 ;; remove nnir-swish-e-remove-prefix from beginning of dirname
1185 (when (string-match (concat "^" nnir-swish-e-remove-prefix)
1187 (setq dirnam (replace-match "" t t dirnam)))
1189 (setq dirnam (substring dirnam 0 -1))
1190 ;; eliminate all ".", "/", "\" from beginning. Always matches.
1191 (string-match "^[./\\]*\\(.*\\)$" dirnam)
1193 (setq group (substitute ?. ?/ (match-string 1 dirnam)))
1194 ;; Windows "\\" -> "."
1195 (setq group (substitute ?. ?\\ group))
1198 (string-to-int artno)
1199 (string-to-int score))
1202 (message "Massaging swish-e output...done")
1207 (function (lambda (x y)
1208 (> (nnir-artitem-rsv x)
1209 (nnir-artitem-rsv y)))))))))
1212 (defun nnir-run-namazu (query &optional group)
1213 "Run given query against Namazu. Returns a vector of (group name, file name)
1214 pairs (also vectors, actually).
1216 Tested with Namazu 2.0.6 on a GNU/Linux system."
1218 (error "The Namazu backend cannot search specific groups"))
1222 (qstring (cdr (assq 'query query)))
1227 (set-buffer (get-buffer-create nnir-tmp-buffer))
1230 `( ,nnir-namazu-program
1231 nil ; input from /dev/null
1233 nil ; don't redisplay
1234 "-q" ; don't be verbose
1235 "-a" ; show all matches
1236 "-s" ; use short format
1237 ,@nnir-namazu-additional-switches
1238 ,qstring ; the query, in namazu format
1239 ,nnir-namazu-index-directory ; index directory
1242 (let ((process-environment (copy-sequence process-environment)))
1244 (setenv "LC_ALL" "C")
1245 (message "%s args: %s" nnir-namazu-program
1246 (mapconcat 'identity (cddddr cp-list) " "))
1247 (apply 'call-process cp-list))))
1248 (unless (or (null exitstatus)
1250 (nnheader-report 'nnir "Couldn't run namazu: %s" exitstatus)
1251 ;; Namazu failure reason is in this buffer, show it if
1252 ;; the user wants it.
1253 (when (> gnus-verbose 6)
1254 (display-buffer nnir-tmp-buffer))))
1256 ;; Namazu output looks something like this:
1257 ;; 2. Re: Gnus agent expire broken (score: 55)
1258 ;; /home/henrik/Mail/mail/sent/1310 (4,138 bytes)
1260 (goto-char (point-min))
1261 (while (re-search-forward
1262 "^\\([0-9]+\\.\\).*\\((score: \\([0-9]+\\)\\))\n\\([^ ]+\\)"
1264 (setq score (match-string 3)
1265 group (file-name-directory (match-string 4))
1266 article (file-name-nondirectory (match-string 4)))
1268 ;; make sure article and group is sane
1269 (when (and (string-match "^[0-9]+$" article)
1271 (when (string-match (concat "^" nnir-namazu-remove-prefix) group)
1272 (setq group (replace-match "" t t group)))
1274 ;; remove trailing slash from groupname
1275 (setq group (substring group 0 -1))
1277 ;; stuff results into artlist vector
1278 (push (vector (substitute ?. ?/ group)
1279 (string-to-int article)
1280 (string-to-int score)) artlist)))
1282 ;; sort artlist by score
1285 (function (lambda (x y)
1286 (> (nnir-artitem-rsv x)
1287 (nnir-artitem-rsv y)))))))))
1291 (defun nnir-read-parms (query)
1292 "Reads additional search parameters according to `nnir-engines'."
1293 (let ((parmspec (caddr (assoc nnir-search-engine nnir-engines))))
1294 (cons (cons 'query query)
1295 (mapcar 'nnir-read-parm parmspec))))
1297 (defun nnir-read-parm (parmspec)
1298 "Reads a single search parameter.
1299 `parmspec' is a cons cell, the car is a symbol, the cdr is a prompt."
1300 (let ((sym (car parmspec))
1301 (prompt (cdr parmspec)))
1302 (cons sym (read-string prompt))))
1304 (defun nnir-run-query (query)
1305 "Invoke appropriate search engine function (see `nnir-engines').
1306 If some groups were process-marked, run the query for each of the groups
1307 and concat the results."
1308 (let ((search-func (cadr (assoc nnir-search-engine nnir-engines)))
1309 (q (car (read-from-string query))))
1310 (if gnus-group-marked
1313 (funcall search-func q x))
1315 (funcall search-func q nil))))
1317 (defun nnir-group-full-name (shortname)
1318 "For the given group name, return a full Gnus group name.
1319 The Gnus backend/server information is added."
1320 (gnus-group-prefixed-name shortname nnir-mail-backend))
1322 (defun nnir-possibly-change-server (server)
1323 (unless (and server (nnir-server-opened server))
1324 (nnir-open-server server)))
1327 ;; Data type article list.
1329 (defun nnir-artlist-length (artlist)
1330 "Returns number of articles in artlist."
1333 (defun nnir-artlist-article (artlist n)
1334 "Returns from ARTLIST the Nth artitem (counting starting at 1)."
1335 (elt artlist (1- n)))
1337 (defun nnir-artitem-group (artitem)
1338 "Returns the group from the ARTITEM."
1341 (defun nnir-artlist-artitem-group (artlist n)
1342 "Returns from ARTLIST the group of the Nth artitem (counting from 1)."
1343 (nnir-artitem-group (nnir-artlist-article artlist n)))
1345 (defun nnir-artitem-number (artitem)
1346 "Returns the number from the ARTITEM."
1349 (defun nnir-artlist-artitem-number (artlist n)
1350 "Returns from ARTLIST the number of the Nth artitem (counting from 1)."
1351 (nnir-artitem-number (nnir-artlist-article artlist n)))
1353 (defun nnir-artitem-rsv (artitem)
1354 "Returns the Retrieval Status Value (RSV, score) from the ARTITEM."
1357 (defun nnir-artlist-artitem-rsv (artlist n)
1358 "Returns from ARTLIST the Retrieval Status Value of the Nth artitem
1360 (nnir-artitem-rsv (nnir-artlist-article artlist n)))
1363 (defun nnir-artlist-groups (artlist)
1364 "Returns a list of all groups in the given ARTLIST."
1367 ;; from each artitem, extract group component
1368 (setq with-dups (mapcar 'nnir-artitem-group artlist))
1369 ;; remove duplicates from above
1370 (mapcar (function (lambda (x) (add-to-list 'res x)))