nnir.el 69 KB

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