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