(nnmairix-group-regexp, nnmairix-valid-backends): Convert from free
[emacs.git] / lisp / gnus / nnmairix.el
blobaa635b2d99cdd2cacfd394700ae472e9906963cc
1 ;;; nnmairix.el --- Mairix back end for Gnus, the Emacs newsreader
3 ;; Copyright (C) 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: David Engster <dengste@eml.cc>
6 ;; Keywords: mail searching
7 ;; Version: 0.5
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; THIS IS BETA SOFTWARE! This back end should not mess up or
29 ;; even delete your mails, but having a backup is always a good idea.
31 ;; This is a back end for using the mairix search engine with
32 ;; Gnus. Mairix is a tool for searching words in locally stored
33 ;; mail. Mairix is very fast which allows using it efficiently for
34 ;; "smart folders", e.g. folders which are associated with search
35 ;; queries. Of course, you can also use this back end just for
36 ;; calling mairix with some search query.
38 ;; Mairix is written by Richard Curnow. More information can be found at
39 ;; http://www.rpcurnow.force9.co.uk/mairix/
41 ;; For details about setting up mairix&Gnus&nnmairix.el, look at the
42 ;; emacswiki:
44 ;; http://www.emacswiki.org/cgi-bin/wiki/GnusMairix
46 ;; The newest version of nnmairix.el can be found at
48 ;; http://www.emacswiki.org/cgi-bin/emacs/nnmairix.el
50 ;; For impatient people, here's the setup in a nutshell:
52 ;; This back end requires an installed mairix binary which is
53 ;; configured to index your mail folder. You don't have to specify a
54 ;; search folder (but it does no harm, either). Visit the man page of
55 ;; mairix and mairixrc for details.
57 ;; Put nnmairix.el into your search path and "(require 'nnmarix)" into
58 ;; your .gnus. Then call nnmairix-create-default-group (or 'G b
59 ;; c'). This function will ask for all necessary information to create
60 ;; a mairix server in Gnus with the default search folder. This
61 ;; default search folder will be used for all temporary searches: call
62 ;; nnmairix-search ('G b s') and enter a mairix query (like
63 ;; f:test@example.com). To create a mairix group for one specific
64 ;; search query, use 'G b g'. See the emacswiki or the source for more
65 ;; information.
67 ;; Commentary on the code: nnmairix sits between Gnus and the "real"
68 ;; back end which handles the mail (currently nnml, nnimap and
69 ;; nnmaildir were tested). I know this is all a bit hacky, but so far
70 ;; it works for me. This is the first back end I've written for Gnus,
71 ;; so I'd appreciate any comments, suggestions, bug reports (and, of
72 ;; course, patches) for improving nnmairix.
74 ;; nnmairix does not use an active file, since I wanted to contain the
75 ;; back end "inside Gnus" as much as possible without the need of an
76 ;; external file. It stores the query/folder information in the group
77 ;; parameters instead. This also implies that once you kill a mairix
78 ;; group, it's gone for good. I don't think that this is really
79 ;; problematic, since I don't see the need in unsubscribing and
80 ;; re-subscribing search groups
82 ;; Every mairix server is "responsible" for one mairix installation,
83 ;; i.e. you can have several mairix servers for different mairix
84 ;; configurations. Not that I think anyone will actually do this, but
85 ;; I thought it would be a "nice to have feature"...
87 ;; KNOWN BUGS:
88 ;; * When using Maildir: path and filename of a mail can change due to
89 ;; reading/replying/etc. This can lead to dangling symlinks in
90 ;; nnmairix groups and it depends on the back end how well it deals
91 ;; with that (some IMAP servers out there may not be amused). Update the
92 ;; database ('G b u') and the group to fix it.
93 ;; * Mairix does only support us-ascii characters.
95 ;; TODO/MISSING FEATURES:
96 ;; * Possibility to propagate flags like seen, replied, ticked
97 ;; to original message
98 ;; * Support of more back ends (nnmh, nnfolder, nnmbox...)?
99 ;; * Maybe use an active file instead of group parameters?
100 ;; * Use "-a" when updating groups which are not newly created
102 ;;; Changelog:
104 ;; 02/06/2008 - version 0.5
106 ;; * New function: nnmairix-goto-original-article. Uses the
107 ;; registry or the mail file path for determining original group.
109 ;; * Deal with empty Xref header
111 ;; * Changed summary mode keybindings since the old ones were
112 ;; already taken
114 ;; (Thanks to Tassilo Horn and Ted Zlatanov for their help)
116 ;; 01/07/2008 - version 0.4
118 ;; * New/fixed doc strings and code cleanup.
120 ;; 18/11/2007 - version 0.3
122 ;; * Fixed bugs when dealing with nnml and native servers
124 ;; * Make variables customizable
126 ;; 10/10/2007 - version 0.2
128 ;; * Use nnml-directory/directory server variables for nnml and
129 ;; nnmaildir backends as path for search folders. This way it
130 ;; becomes independent of 'base' setting in .mairixirc (but not for
131 ;; nnimap).
133 ;; * As a result: Changed nnmairix-backend-to-server so that user
134 ;; is asked when more than one nnmairix server exists and we do not
135 ;; know which one is responsible for current backend.
137 ;; * Rename files when using nnml backends so that there are no
138 ;; holes in article numbers. This should fix all problems regarding
139 ;; wrong article counts with nnml.
141 ;; * More commands for creating queries (using widgets or the
142 ;; minibuffer).
144 ;; * Fixed bug in nnmairix-create-search-group-from-message
146 ;; * Changed copyright to FSF
148 ;; (Thanks to Georg C. F. Greve and Bastien for suggestions and
149 ;; ideas!)
151 ;; 10/03/2007 - version 0.1 - first release
154 ;;; Code:
156 (require 'nnoo)
157 (require 'gnus-group)
158 (require 'gnus-sum)
159 (require 'message)
160 (require 'nnml)
161 (require 'widget)
163 (nnoo-declare nnmairix)
165 ;;; === Keymaps
167 ;; Group mode
168 (defun nnmairix-group-mode-hook ()
169 "Nnmairix group mode keymap."
170 (define-key gnus-group-mode-map
171 (kbd "G b") (make-sparse-keymap))
172 (define-key gnus-group-mode-map
173 (kbd "G b g") 'nnmairix-create-search-group)
174 (define-key gnus-group-mode-map
175 (kbd "G b c") 'nnmairix-create-server-and-default-group)
176 (define-key gnus-group-mode-map
177 (kbd "G b q") 'nnmairix-group-change-query-this-group)
178 (define-key gnus-group-mode-map
179 (kbd "G b t") 'nnmairix-group-toggle-threads-this-group)
180 (define-key gnus-group-mode-map
181 (kbd "G b u") 'nnmairix-update-database)
182 (define-key gnus-group-mode-map
183 (kbd "G b s") 'nnmairix-search)
184 (define-key gnus-group-mode-map
185 (kbd "G b i") 'nnmairix-search-interactive)
186 (define-key gnus-group-mode-map
187 (kbd "G b m") 'nnmairix-widget-search))
189 ;; Summary mode
190 (defun nnmairix-summary-mode-hook ()
191 "Nnmairix summary mode keymap."
192 (define-key gnus-summary-mode-map
193 (kbd "$ t") 'nnmairix-search-thread-this-article)
194 (define-key gnus-summary-mode-map
195 (kbd "$ f") 'nnmairix-search-from-this-article)
196 (define-key gnus-summary-mode-map
197 (kbd "$ m") 'nnmairix-widget-search-from-this-article)
198 (define-key gnus-summary-mode-map
199 (kbd "$ g") 'nnmairix-create-search-group-from-message)
200 (define-key gnus-summary-mode-map
201 (kbd "$ o") 'nnmairix-goto-original-article))
203 (add-hook 'gnus-group-mode-hook 'nnmairix-group-mode-hook)
204 (add-hook 'gnus-summary-mode-hook 'nnmairix-summary-mode-hook)
207 ;; Customizable stuff
209 (defgroup nnmairix nil
210 "Backend for the Mairix mail search engine."
211 :group 'gnus)
213 (defcustom nnmairix-group-prefix "zz_mairix"
214 "Prefix for mairix search groups on back end server.
215 nnmairix will create these groups automatically on the back end
216 server for each nnmairix search group. The name on the back end
217 server will be this prefix plus a random number. You can delete
218 unused nnmairix groups on the back end using
219 `nnmairix-purge-old-groups'."
220 :version "23.1"
221 :type 'string
222 :group 'nnmairix)
224 (defcustom nnmairix-mairix-output-buffer "*mairix output*"
225 "Buffer used for mairix output."
226 :version "23.1"
227 :type 'string
228 :group 'nnmairix)
230 (defcustom nnmairix-customize-query-buffer "*mairix query*"
231 "Name of the buffer for customizing Mairix queries."
232 :version "23.1"
233 :type 'string
234 :group 'nnmairix)
236 (defcustom nnmairix-mairix-update-options '("-F" "-Q")
237 "Options when calling mairix for updating the database.
238 The default is '-F' and '-Q' for making updates faster. You
239 should call mairix without these options from time to
240 time (e.g. via cron job)."
241 :version "23.1"
242 :type '(repeat string)
243 :group 'nnmairix)
245 (defcustom nnmairix-mairix-synchronous-update nil
246 "Set this to t if you want Emacs to wait for mairix updating the database."
247 :version "23.1"
248 :type 'boolean
249 :group 'nnmairix)
251 (defcustom nnmairix-rename-files-for-nnml t
252 "Rename nnml mail files so that they are consecutively numbered.
253 When using nnml as backend, mairix might produce holes in the
254 article numbers which will produce wrong article counts by
255 Gnus. This option controls whether nnmairix should rename the
256 files consecutively."
257 :version "23.1"
258 :type 'boolean
259 :group 'nnmairix)
261 (defcustom nnmairix-widget-fields-list
262 '(("from" "f" "From") ("to" "t" "To") ("cc" "c" "Cc")
263 ("subject" "s" "Subject") ("to" "tc" "To or Cc")
264 ("from" "a" "Address") (nil "b" "Body") (nil "n" "Attachment")
265 ("Message-ID" "m" "Message ID") (nil "s" "Size") (nil "d" "Date"))
266 "Fields that should be editable during interactive query customization.
268 Header, corresponding mairix command and description for editable
269 fields in interactive query customization. The header specifies
270 which header contents should be inserted into the editable field
271 when creating a Mairix query based on the current message (can be
272 nil for disabling this)."
273 :version "23.1"
274 :type '(repeat (list
275 (choice :tag "Field"
276 (const :tag "none" nil)
277 (const :tag "From" "from")
278 (const :tag "To" "to")
279 (const :tag "Cc" "cc")
280 (const :tag "Subject" "subject")
281 (const :tag "Message ID" "Message-ID"))
282 (string :tag "Command")
283 (string :tag "Description")))
284 :group 'nnmairix)
286 (defcustom nnmairix-widget-select-window-function
287 (lambda () (select-window (get-largest-window)))
288 "Function for selecting the window for customizing the mairix query.
289 The default chooses the largest window in the current frame."
290 :version "23.1"
291 :type 'function
292 :group 'nnmairix)
294 ;; ==== Other variables
296 (defvar nnmairix-widget-other
297 '(threads flags)
298 "Other editable mairix commands when using customization widgets.
299 Currently there are 'threads and 'flags.")
301 (defvar nnmairix-interactive-query-parameters
302 '((?f "from" "f" "From") (?t "to" "t" "To") (?c "to" "tc" "To or Cc")
303 (?a "from" "a" "Address") (?s "subject" "s" "Subject") (?b nil "b" "Body")
304 (?d nil "d" "Date") (?n nil "n" "Attachment"))
305 "Things that should be editable during interactive query generation.
306 Every list element consists of the following entries: Keystroke,
307 message field (if any), mairix command and description.")
309 (defvar nnmairix-delete-and-create-on-change '(nnimap nnmaildir nnml)
310 "Controls on which backends groups should be deleted and re-created.
311 This variable is a list of back ends where the search group should
312 be completely deleted and re-created when the query or thread
313 parameter changes. I know this is rather \"brute force\" and maybe
314 even dangerous (you have backups, right?), but it should be used at
315 least for nnimap since some IMAP servers are really not amused when
316 mailbox content changes behind their back. It usually also corrects
317 the problem of \"holes\" in the article numbers which often lead to a
318 wrong count of total articles shown by Gnus.")
320 ;;; === Server variables
322 (defvoo nnmairix-backend nil
323 "Backend where mairix stores its searches.")
325 (defvoo nnmairix-backend-server nil
326 "Name of the server where mairix stores its searches.")
328 (defvoo nnmairix-mairix-command "mairix"
329 "Command to call mairix for this nnmairix server.")
331 (defvoo nnmairix-hidden-folders nil
332 "Set this to t if the back end server uses hidden directories for
333 its maildir mail folders (e.g. the Dovecot IMAP server or mutt).")
335 (defvoo nnmairix-default-group nil
336 "Default search group. This is the group which is used for all
337 temporary searches, e.g. nnmairix-search.")
339 ;;; === Internal variables
341 (defconst nnmairix-group-regexp
342 (format "%s-\\(.*\\)-[0-9]+" nnmairix-group-prefix)
343 "Regexp for mairix groups on back end.")
345 (defconst nnmairix-valid-backends '(nnimap nnml nnmaildir)
346 "Backends supported by nnmairix.
347 Other backends might or might not work.")
349 (defvar nnmairix-last-server nil
350 "Last chosen server.")
352 (defvar nnmairix-current-server nil
353 "Current server.")
355 ;;; === Gnus backend functions
357 (nnoo-define-basics nnmairix)
359 (gnus-declare-backend "nnmairix" 'mail 'address)
361 (deffoo nnmairix-open-server (server &optional definitions)
362 ;; just set server variables
363 (setq nnmairix-current-server server)
364 (nnoo-change-server 'nnmairix server definitions))
366 (deffoo nnmairix-request-group (group &optional server fast)
367 ;; Call mairix and request group on back end server
368 (when server (nnmairix-open-server server))
369 (let* ((qualgroup (if server
370 (gnus-group-prefixed-name group (list 'nnmairix server))
371 group))
372 (query (gnus-group-get-parameter qualgroup 'query t))
373 (folder (gnus-group-get-parameter qualgroup 'folder))
374 (threads (gnus-group-get-parameter qualgroup 'threads))
375 (backendmethod (gnus-server-to-method
376 (format "%s:%s" (symbol-name nnmairix-backend)
377 nnmairix-backend-server)))
378 rval mfolder folderpath)
379 (cond
380 ((not folder)
381 ;; No folder parameter -> error
382 (nnheader-report 'nnmairix "Check folder parameter for group %s" group)
383 nil)
384 ((not query)
385 ;; No query -> return empty group
386 (save-excursion
387 (set-buffer nntp-server-buffer)
388 (erase-buffer)
389 (insert (concat "211 0 1 0 " group))
392 ;; For maildir++ folders: create a hidden directory (prepend dot)
393 (setq mfolder (if (and nnmairix-hidden-folders
394 (not (string-match "^\\." folder)))
395 (concat "." folder)
396 folder))
397 ;; For nnml and nnmaildir, precede mfolder with directory where mail
398 ;; is actually stored so that it's independent of 'base' setting
399 ;; in .mairixrc.
400 (when (eq nnmairix-backend 'nnml)
401 (setq folderpath (cadr (assoc 'nnml-directory backendmethod)))
402 ;; if nnml-directory is not explicitly set, use global value
403 (when (not folderpath)
404 (setq folderpath nnml-directory)))
405 (when (eq nnmairix-backend 'nnmaildir)
406 (setq folderpath
407 (cadr (assoc 'directory backendmethod))))
408 (when folderpath
409 (setq mfolder
410 (concat
411 (file-name-as-directory
412 (expand-file-name
413 folderpath))
414 mfolder)))
415 ;; If (not fast), call Mairix binary
416 (setq rval
417 (if fast 0
418 (nnmairix-call-mairix-binary
419 (split-string nnmairix-mairix-command)
420 mfolder query threads)))
421 ;; Check return value
422 (cond
423 ((zerop rval) ; call was succesful
424 (nnmairix-call-backend
425 "open-server" nnmairix-backend-server)
426 ;; If we're dealing with nnml, rename files
427 ;; consecutively and make new active file for this
428 ;; group
429 (when (eq nnmairix-backend 'nnml)
430 (when nnmairix-rename-files-for-nnml
431 (nnmairix-rename-files-consecutively mfolder))
432 (nnml-generate-nov-databases-directory mfolder))
433 (nnmairix-call-backend
434 "request-scan" folder nnmairix-backend-server)
435 (if fast
437 (nnmairix-request-group-with-article-number-correction folder qualgroup)))
438 ((and (= rval 1)
439 (save-excursion (set-buffer nnmairix-mairix-output-buffer)
440 (goto-char (point-min))
441 (looking-at "^Matched 0 messages")))
442 ;; No messages found -> return empty group
443 (nnheader-message 5 "Mairix: No matches found.")
444 (set-buffer nntp-server-buffer)
445 (erase-buffer)
446 (insert (concat "211 0 1 0 " group))
448 ;; Everything else is an error
450 (nnheader-report
451 'nnmairix "Error running marix. See buffer %s for details"
452 nnmairix-mairix-output-buffer)
453 nil))))))
456 (deffoo nnmairix-request-create-group (group &optional server args)
457 (let ((qualgroup (if server (gnus-group-prefixed-name group (list 'nnmairix server))
458 group))
459 (exist t)
460 (count 0)
461 groupname info)
462 (when server (nnmairix-open-server server))
463 (gnus-group-add-parameter qualgroup '(query . nil))
464 (gnus-group-add-parameter qualgroup '(threads . nil))
465 (while exist
466 (setq count (1+ count))
467 (setq groupname (format "%s-%s-%s" nnmairix-group-prefix group
468 (number-to-string count)))
469 (setq exist (nnmairix-call-backend
470 "request-group" groupname nnmairix-backend-server)))
471 (nnmairix-call-backend
472 "request-create-group" groupname nnmairix-backend-server)
473 (gnus-group-add-parameter qualgroup '(folder . nil))
474 (gnus-group-set-parameter qualgroup 'folder groupname))
478 (deffoo nnmairix-retrieve-headers (articles group &optional server fetch-old)
479 (when server (nnmairix-open-server server))
480 (let* ((folder (nnmairix-get-backend-folder group server))
481 (corr (nnmairix-get-numcorr group server))
482 (numcorr 0)
483 rval)
484 (when (and corr
485 (not (zerop (cadr corr)))
486 (numberp (car articles)))
487 (setq numcorr (cadr corr))
488 (setq articles
489 (mapcar
490 (lambda (arg) (- arg numcorr))
491 articles)))
492 (setq rval (nnmairix-call-backend
493 "retrieve-headers" articles folder nnmairix-backend-server fetch-old))
494 (when (eq rval 'nov)
495 (nnmairix-replace-group-and-numbers articles folder group numcorr)
496 rval)))
498 (deffoo nnmairix-request-article (article &optional group server to-buffer)
499 (when server (nnmairix-open-server server))
500 (let ((folder (nnmairix-get-backend-folder group server))
501 (corr (nnmairix-get-numcorr group server)))
502 (when (and
503 (numberp article)
504 corr
505 (not (zerop (cadr corr))))
506 (setq article (- article (cadr corr))))
507 (nnmairix-call-backend
508 "request-article" article folder nnmairix-backend-server to-buffer))
511 (deffoo nnmairix-close-group (group &optional server)
512 ;; Should we do something here?
513 nil)
516 (deffoo nnmairix-request-list (&optional server)
517 (when server (nnmairix-open-server server))
518 (if (nnmairix-call-backend "request-list" nnmairix-backend-server)
519 (let (cpoint cur qualgroup folder)
520 (save-excursion
521 (set-buffer nntp-server-buffer)
522 (goto-char (point-min))
523 (setq cpoint (point))
524 (while (re-search-forward nnmairix-group-regexp (point-max) t)
525 (setq cur (match-string 1)
526 qualgroup (gnus-group-prefixed-name cur
527 (list 'nnmairix server)))
528 (if (and (gnus-group-entry qualgroup)
529 (string= (match-string 0)
530 (gnus-group-get-parameter qualgroup 'folder)))
531 (progn
532 (replace-match cur)
533 (delete-region cpoint (point-at-bol))
534 (forward-line)
535 (setq cpoint (point)))
536 (forward-line)))
537 (delete-region cpoint (point-max)))
539 nil))
542 (nnoo-define-skeleton nnmairix)
545 ;;; === Interactive functions
547 (defun nnmairix-create-search-group (server group query threads)
548 "Create on SERVER nnmairix search group GROUP with QUERY.
549 If THREADS is t, include whole threads from found messages. If
550 called interactively, user will be asked for parameters."
551 (interactive
552 (list
553 (gnus-server-to-method (car (nnmairix-get-server)))
554 (read-string "Group name: ")
555 (read-string "Query: ")
556 (y-or-n-p "Include threads? ")))
557 (when (and (stringp query)
558 (string-match "\\s-" query))
559 (setq query (split-string query)))
560 (when (not (listp query))
561 (setq query (list query)))
562 (when (and server group query)
563 (save-excursion
564 (let ((groupname (gnus-group-prefixed-name group server))
565 info)
566 (set-buffer gnus-group-buffer)
567 (gnus-group-make-group group server)
568 (gnus-group-set-parameter groupname 'query query)
569 (gnus-group-set-parameter groupname 'threads threads)
570 (nnmairix-update-and-clear-marks groupname)))))
572 (defun nnmairix-search-interactive ()
573 "Create mairix search interactively with the minibuffer."
574 (interactive)
575 (let ((char-header nnmairix-interactive-query-parameters)
576 header finished query achar)
577 (while (not finished)
578 (while (not achar)
579 (message "Query (%s): " (nnmairix-create-message-line-for-search))
580 (setq achar (read-char))
581 (when (not (assoc achar char-header))
582 (setq achar nil)))
583 (setq header (read-string
584 (concat "Match " (nth 3 (assoc achar char-header)) " on: ")))
585 (push (concat (nth 2 (assoc achar char-header)) ":" header) query)
586 (setq finished (not (y-or-n-p "Add another search query? "))
587 achar nil))
588 (nnmairix-search
589 (mapconcat 'identity query " ")
590 (car (nnmairix-get-server))
591 (y-or-n-p "Include whole threads? "))))
593 (defun nnmairix-create-search-group-from-message ()
594 "Interactively create search group with query based on current message."
595 (interactive)
596 (let ((char-header nnmairix-interactive-query-parameters)
597 (server (nnmairix-backend-to-server gnus-current-select-method))
598 query achar header finished group threads cq)
599 (when (or (not (gnus-buffer-live-p gnus-article-buffer))
600 (not (gnus-buffer-live-p gnus-summary-buffer)))
601 (error "No article or summary buffer"))
602 (when (not server)
603 (error "No nnmairix server found for back end %s:%s"
604 (symbol-name (car gnus-current-select-method))
605 (nth 1 gnus-current-select-method)))
606 (while (not finished)
607 (save-excursion
608 (gnus-summary-toggle-header 1)
609 (while (not achar)
610 (message "Query (%s): " (nnmairix-create-message-line-for-search))
611 (setq achar (read-char))
612 (when (not (assoc achar char-header))
613 (setq achar nil)))
614 (set-buffer gnus-article-buffer)
615 (setq header nil)
616 (when (setq cq (nth 1 (assoc achar char-header)))
617 (setq header
618 (nnmairix-replace-illegal-chars
619 (gnus-fetch-field (nth 1 (assoc achar char-header))))))
620 (setq header (read-string
621 (concat "Match " (nth 3 (assoc achar char-header)) " on: ")
622 header))
623 (push (concat (nth 2 (assoc achar char-header)) ":" header) query)
624 (setq finished (not (y-or-n-p "Add another search query? "))
625 achar nil)))
626 (setq threads (y-or-n-p "Include whole threads? "))
627 (setq group (read-string "Group name: "))
628 (set-buffer gnus-summary-buffer)
629 (message "Creating group %s on server %s with query %s." group
630 (gnus-method-to-server server) (mapconcat 'identity query " "))
631 (nnmairix-create-search-group server group query threads)))
633 (defun nnmairix-create-server-and-default-group ()
634 "Interactively create new nnmairix server with default search group.
635 All necessary information will be queried from the user."
636 (interactive)
637 (let* ((name (read-string "Name of the mairix server: "))
638 (server (completing-read "Back end server (TAB for completion): "
639 (nnmairix-get-valid-servers)))
640 (mairix (read-string "Command to call mairix: " "mairix"))
641 (defaultgroup (read-string "Default search group: "))
642 (backend (symbol-name (car (gnus-server-to-method server))))
643 (servername (nth 1 (gnus-server-to-method server)))
644 (hidden (and (string-match "^nn\\(imap\\|maildir\\)$" backend)
645 (y-or-n-p
646 "Does the back end server work with maildir++ (i.e. hidden directories)? ")))
647 create)
649 (apply (intern (format "%s-%s" backend "open-server"))
650 (list servername))
652 (when (and hidden
653 (string-match "^\\." defaultgroup))
654 (setq defaultgroup (substring defaultgroup 1)))
655 ;; Create default search group
656 (gnus-group-make-group
657 defaultgroup (list 'nnmairix name (list 'nnmairix-backend (intern backend))
658 (list 'nnmairix-backend-server servername)
659 (list 'nnmairix-mairix-command mairix)
660 (list 'nnmairix-hidden-folders hidden)
661 (list 'nnmairix-default-group defaultgroup)))))
664 (defun nnmairix-group-change-query-this-group (&optional query)
665 "Set QUERY for group under cursor."
666 (interactive)
667 (let* ((group (gnus-group-group-name))
668 (method (gnus-find-method-for-group group))
669 (oldquery (gnus-group-get-parameter group 'query t)))
670 (if (eq (car method) 'nnmairix)
671 (progn
672 (when (listp oldquery)
673 (setq oldquery (mapconcat 'identity oldquery " ")))
674 (setq query (or query
675 (read-string "New query: " oldquery)))
676 (when (stringp query)
677 (setq query (split-string query)))
678 (when query
679 (gnus-group-set-parameter group 'query query)
680 (nnmairix-update-and-clear-marks group)))
681 (error "This is no nnmairix group"))))
684 (defun nnmairix-group-toggle-threads-this-group (&optional threads)
685 "Toggle threads parameter for this group.
686 If THREADS is a positive number, set threads parameter to t.
687 If THREADS is a negative number, set it to nil."
688 (interactive)
689 (let* ((group (gnus-group-group-name))
690 (method (gnus-find-method-for-group group))
691 (getthreads (or threads
692 (not (gnus-group-get-parameter group 'threads)))))
693 (if (eq (car method) 'nnmairix)
694 (progn
695 (when (numberp getthreads)
696 (setq getthreads (> getthreads 0)))
697 (gnus-group-set-parameter group 'threads getthreads)
698 (if getthreads
699 (message "Threads activated for group %s" group)
700 (message "Threads deacitavted for group %s" group))
701 (nnmairix-update-and-clear-marks group))
702 (error "This is no nnmairix group"))))
705 (defun nnmairix-search (query &optional server threads)
706 "Sends QUERY to nnmairix backend SERVER, using default its search group.
708 Default search group is automatically entered and results are shown.
709 If THREADS is t, enable threads.
710 If THREADS is a negative number, disable threads.
711 Otherwise, leave threads parameter as it is."
712 (interactive (list (read-string "Query: ")))
713 (when (not server)
714 (setq server (car (nnmairix-get-server))))
715 (if (not server)
716 (error "No opened nnmairix server found")
717 (setq server (gnus-server-to-method server)))
718 (nnmairix-open-server (nth 1 server))
719 (let* ((qualgroup (gnus-group-prefixed-name nnmairix-default-group
720 (list 'nnmairix (nth 1 server)))))
721 (set-buffer gnus-group-buffer)
722 (when (stringp query)
723 (setq query (split-string query)))
724 (gnus-group-set-parameter qualgroup 'query query)
725 (if (symbolp threads)
726 (when (eq threads 't)
727 (gnus-group-set-parameter qualgroup 'threads t))
728 (when (< threads 0)
729 (gnus-group-set-parameter qualgroup 'threads nil)))
730 (nnmairix-update-and-clear-marks qualgroup)
731 (when (not (zerop (gnus-group-unread qualgroup)))
732 (gnus-group-read-group nil t qualgroup))))
734 (defun nnmairix-search-thread-this-article ()
735 "Search thread for the current article.
736 This is effectively a shortcut for calling `nnmairix-search'
737 with m:msgid of the current article and enabled threads."
738 (interactive)
739 (let* ((server
740 (nnmairix-backend-to-server gnus-current-select-method))
741 mid)
742 (if server
743 (if (gnus-buffer-live-p gnus-article-buffer)
744 (progn
745 (save-excursion
746 (set-buffer gnus-article-buffer)
747 (gnus-summary-toggle-header 1)
748 (setq mid (message-fetch-field "Message-ID")))
749 (while (string-match "[<>]" mid)
750 (setq mid (replace-match "" t t mid)))
751 (nnmairix-search (concat "m:" mid) server t))
752 (message "No article buffer."))
753 (error "No nnmairix server found for back end %s:%s"
754 (symbol-name (car gnus-current-select-method))
755 (nth 1 gnus-current-select-method)))))
757 (defun nnmairix-search-from-this-article ()
758 "Search messages from sender of the current article.
759 This is effectively a shortcut for calling `nnmairix-search' with
760 f:current_from."
761 (interactive)
762 (let* ((server
763 (nnmairix-backend-to-server gnus-current-select-method))
764 from)
765 (if server
766 (if (gnus-buffer-live-p gnus-article-buffer)
767 (progn
768 (save-excursion
769 (set-buffer gnus-article-buffer)
770 (gnus-summary-toggle-header 1)
771 (setq from (cadr (gnus-extract-address-components
772 (gnus-fetch-field "From"))))
773 (nnmairix-search (concat "f:" from) server -1)))
774 (message "No article buffer."))
775 (error "No nnmairix server found for back end %s:%s"
776 (symbol-name (car gnus-current-select-method))
777 (nth 1 gnus-current-select-method)))))
780 (defun nnmairix-purge-old-groups (&optional dontask server)
781 "Delete mairix search groups which are no longer used.
783 You may want to call this from time to time if you are creating
784 and deleting lots of nnmairix groups. If DONTASK is t, do not ask
785 before deleting a group on the back end. SERVER specifies nnmairix server."
786 (interactive)
787 (let ((server (or server
788 (gnus-server-to-method (car (nnmairix-get-server))))))
789 (if (nnmairix-open-server (nth 1 server))
790 (when (nnmairix-call-backend
791 "request-list" nnmairix-backend-server)
792 (let (cur qualgroup folder)
793 (save-excursion
794 (set-buffer nntp-server-buffer)
795 (goto-char (point-min))
796 (while (re-search-forward nnmairix-group-regexp (point-max) t)
797 (setq cur (match-string 0)
798 qualgroup (gnus-group-prefixed-name
799 (match-string 1) server))
800 (when (not (and (gnus-group-entry qualgroup)
801 (string= cur
802 (gnus-group-get-parameter
803 qualgroup 'folder))))
804 (when (or dontask
805 (y-or-n-p
806 (concat "Delete group " cur
807 " on server " nnmairix-backend-server "? ")))
808 (nnmairix-call-backend
809 "request-delete-group" cur t nnmairix-backend-server)))))))
810 (message "Couldn't open server %s" (nth 1 server)))))
813 (defun nnmairix-update-database (&optional servers)
814 "Call mairix for updating the database for SERVERS.
816 If SERVERS is nil, do update for all nnmairix servers. Mairix
817 will be called asynchronously unless
818 `nnmairix-mairix-synchronous-update' is t. Mairix will be called
819 with `nnmairix-mairix-update-options'."
820 (interactive)
821 (let ((servers (or servers
822 (nnmairix-get-nnmairix-servers)))
823 args cur commandsplit)
824 (while servers
825 (setq cur (car (pop servers)))
826 (nnmairix-open-server
827 (nth 1 (gnus-server-to-method cur)))
828 (setq commandsplit (split-string nnmairix-mairix-command))
829 (nnheader-message 7 "Updating mairix database for %s..." cur)
830 (if nnmairix-mairix-synchronous-update
831 (progn
832 (setq args (append (list (car commandsplit) nil
833 (get-buffer nnmairix-mairix-output-buffer)
834 nil)))
835 (if (> (length commandsplit) 1)
836 (setq args (append args (cdr commandsplit) nnmairix-mairix-update-options))
837 (setq args (append args nnmairix-mairix-update-options)))
838 (apply 'call-process args)
839 (nnheader-message 7 "Updating mairix database for %s... done" cur))
840 (progn
841 (setq args (append (list cur (get-buffer nnmairix-mairix-output-buffer)
842 (car commandsplit))))
843 (if (> (length commandsplit) 1)
844 (setq args (append args (cdr commandsplit) nnmairix-mairix-update-options))
845 (setq args (append args nnmairix-mairix-update-options)))
846 (set-process-sentinel (apply 'start-process args)
847 'nnmairix-sentinel-mairix-update-finished))))))
849 (autoload 'gnus-registry-fetch-group "gnus-registry")
851 (defun nnmairix-goto-original-article (&optional no-registry)
852 "Jump to the original group and display article.
853 The original group of the article is first determined with the
854 registry (if enabled). If the registry is not enabled or did not
855 find the article or the prefix NO-REGISTRY is non-nil, this
856 function will try to determine the original group form the path
857 of the mail file. The path is obtained through another mairix
858 search in raw mode."
859 (interactive "P")
860 (when (not (eq (car gnus-current-select-method) 'nnmairix))
861 (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
862 (if (eq (car method) 'nnmairix)
863 (nnmairix-open-server (nth 1 method))
864 (error "Not in a nnmairix group"))))
865 (when (not (gnus-buffer-live-p gnus-article-buffer))
866 (error "No article buffer available"))
867 (let ((server (nth 1 gnus-current-select-method))
868 mid rval group allgroups)
869 ;; get message id
870 (save-excursion
871 (set-buffer gnus-article-buffer)
872 (gnus-summary-toggle-header 1)
873 (setq mid (message-fetch-field "Message-ID"))
874 ;; first check the registry (if available)
875 (when (and (boundp 'gnus-registry-install)
876 gnus-registry-install
877 (not no-registry))
878 (setq group (gnus-registry-fetch-group mid)))
879 (while (string-match "[<>]" mid)
880 (setq mid (replace-match "" t t mid)))
881 (unless group
882 ;; registry was not available or did not find article
883 ;; so we search again with mairix in raw mode to get filename
884 (nnmairix-open-server server)
885 (setq rval
886 (nnmairix-call-mairix-binary-raw
887 (split-string nnmairix-mairix-command)
888 (list (concat "m:" mid))))
889 (if (zerop rval)
890 ;; determine original group(s) from filename
891 (save-excursion
892 (set-buffer nnmairix-mairix-output-buffer)
893 (goto-char (point-min))
894 (while (looking-at "/")
895 (push (nnmairix-determine-original-group)
896 allgroups)
897 (forward-line 1))
898 (if (> (length allgroups) 1)
899 (setq group
900 (completing-read
901 "Message exists in more than one group. Choose: "
902 allgroups nil t))
903 (setq group (car allgroups))))
904 (error "Mairix could not find original article. See buffer %s for details"
905 nnmairix-mairix-output-buffer))))
906 (if group
907 ;; show article in summary buffer
908 (nnmairix-show-original-article group mid)
909 (message "Couldn't find original article"))))
911 (defun nnmairix-determine-original-group ()
912 "Try to determine to original group from the file path."
913 (let (path filename serverbase group maildirflag allgroups)
914 (re-search-forward "^\\(.*\\)/\\(.*?\\)$")
915 (setq path (expand-file-name (match-string 1)))
916 (setq filename (match-string 2))
917 ;; when we deal with maildir, remove cur/new/tmp from path
918 (setq maildirflag (string-match ".+\\..+\\..+" filename))
919 (when maildirflag
920 (setq path
921 (replace-regexp-in-string
922 ".*\\(/cur\\|/new\\|/tmp\\)$" "" path t t 1)))
923 ;; we first check nnml and nnmaildir servers
924 (setq
925 group
926 (catch 'found
927 (dolist (cur gnus-opened-servers)
928 (when (or (and (not maildirflag)
929 (eq (caar cur) 'nnml))
930 (and maildirflag
931 (eq (caar cur) 'nnmaildir)))
932 ;; get base path from server
933 (if maildirflag
934 (setq serverbase (cadr (assoc 'directory (car cur))))
935 (setq serverbase (cadr (assoc 'nnml-directory (car cur))))
936 (when (not serverbase)
937 (setq serverbase nnml-directory)))
938 (setq serverbase (file-name-as-directory
939 (expand-file-name serverbase)))
940 (when (string-match (concat serverbase "\\(.*\\)") path)
941 ;; looks good - rest of the path should be the group
942 (setq group (match-string 1 path))
943 (when (string-match "/$" group)
944 (setq group (replace-match "" t t group)))
945 (when (not maildirflag)
946 ;; for nnml: convert slashes to dots
947 (while (string-match "/" group)
948 (setq group (replace-match "." t t group))))
949 (setq group (gnus-group-prefixed-name group (car cur)))
950 ;; check whether this group actually exists
951 (when (gnus-group-entry group)
952 (throw 'found group)))))))
953 (unless group
954 ;; we haven't found it yet --> look for nnimap groups
955 ;; assume last element of the path is the group
956 (string-match "^.*/\\.?\\(.*\\)$" path)
957 (setq group (match-string 1 path))
958 ;; convert dots to slashes (nested group)
959 (while (string-match "\\." group)
960 (setq group (replace-match "/" t t group)))
961 (dolist (cur gnus-opened-servers)
962 (when (eq (caar cur) 'nnimap)
963 (when (gnus-group-entry
964 (gnus-group-prefixed-name group (car cur)))
965 (push
966 (gnus-group-prefixed-name group (car cur))
967 allgroups))))
968 (if (> (length allgroups) 1)
969 (setq group (completing-read
970 "Group %s exists on more than one IMAP server. Choose: "
971 allgroups nil t))
972 (setq group (car allgroups))))
973 group))
976 ;;; ==== Helper functions
978 (defun nnmairix-request-group-with-article-number-correction (folder qualgroup)
979 "Request FOLDER on backend for nnmairix QUALGROUP and article number correction."
980 (save-excursion
981 (nnmairix-call-backend
982 "request-group" folder nnmairix-backend-server fast)
983 (set-buffer nnmairix-mairix-output-buffer)
984 (goto-char (point-min))
985 (re-search-forward "^Matched.*messages")
986 (nnheader-message 7 (match-string 0))
987 (set-buffer nntp-server-buffer)
988 (goto-char (point-min))
989 (let ((status (read (current-buffer)))
990 (total (read (current-buffer)))
991 (low (read (current-buffer)))
992 (high (read (current-buffer)))
993 (corr (gnus-group-get-parameter qualgroup 'numcorr t)))
994 (if (= status 211)
995 (progn
996 ;; Article number correction
997 (if (and corr
998 (> (+ (car (cddr corr)) high) 0))
999 (progn
1000 (when (car corr) ;Group has changed
1001 (setq corr
1002 (list nil
1003 (car (cddr corr))
1004 (+ (car (cddr corr)) high)))
1005 (gnus-group-set-parameter
1006 qualgroup 'numcorr corr))
1007 (setq low (+ low (cadr corr))
1008 high (+ high (cadr corr))))
1009 (when (member nnmairix-backend
1010 nnmairix-delete-and-create-on-change)
1011 (gnus-group-set-parameter
1012 qualgroup 'numcorr (list nil 0 high))))
1013 (erase-buffer)
1014 (insert (format "%d %d %d %d %s" status total low high group))
1016 (progn
1017 (nnheader-report
1018 'nnmairix "Error calling back end on group %s" folder)
1019 nil)))))
1021 (defun nnmairix-call-mairix-binary (command folder query threads)
1022 "Call mairix binary with COMMAND, using FOLDER and QUERY.
1023 If THREADS is non-nil, enable full threads."
1024 (let ((args (cons (car command) '(nil t nil))))
1025 (save-excursion
1026 (set-buffer
1027 (get-buffer-create nnmairix-mairix-output-buffer))
1028 (erase-buffer)
1029 (when (> (length command) 1)
1030 (setq args (append args (cdr command))))
1031 (when threads
1032 (setq args (append args '("-t"))))
1033 (apply 'call-process
1034 (append args (list "-o" folder) query)))))
1036 (defun nnmairix-call-mairix-binary-raw (command query)
1037 "Call mairix binary with COMMAND and QUERY in raw mode."
1038 (let ((args (cons (car command) '(nil t nil))))
1039 (save-excursion
1040 (set-buffer
1041 (get-buffer-create nnmairix-mairix-output-buffer))
1042 (erase-buffer)
1043 (when (> (length command) 1)
1044 (setq args (append args (cdr command))))
1045 (setq args (append args '("-r")))
1046 (apply 'call-process
1047 (append args query)))))
1049 (defun nnmairix-get-server ()
1050 "If there exists just one nnmairix server, return its value.
1051 Otherwise, ask user for server."
1052 (let ((openedserver (nnmairix-get-nnmairix-servers)))
1053 (when (not openedserver)
1054 (error "No opened nnmairix server found"))
1055 (if (> (length openedserver) 1)
1056 (progn
1057 (while
1058 (equal '("")
1059 (setq nnmairix-last-server
1060 (list (completing-read "Server: " openedserver nil 1
1061 (or nnmairix-last-server
1062 "nnmairix:"))))))
1063 nnmairix-last-server)
1064 (car openedserver))))
1066 (defun nnmairix-get-nnmairix-servers (&optional all)
1067 "Return available nnmairix servers.
1068 If ALL is t, return also the unopened/failed ones."
1069 (let ((alist gnus-opened-servers)
1070 server openedserver)
1071 (while alist
1072 (setq server (pop alist))
1073 (when (and server
1074 (or all
1075 (eq (cadr server) 'ok))
1076 (eq (caar server) 'nnmairix)
1077 (not (member (car server) gnus-ephemeral-servers)))
1078 (setq server
1079 (concat (symbol-name (caar server)) ":" (nth 1 (car server))))
1080 (push (list server) openedserver)))
1081 openedserver))
1084 (defun nnmairix-get-valid-servers ()
1085 "Return list of valid backend servers for nnmairix groups."
1086 (let ((alist gnus-opened-servers)
1087 (mairixservers (nnmairix-get-nnmairix-servers t))
1088 server mserver openedserver occ cur)
1089 ;; Get list of all nnmairix backends (i.e. backends which are
1090 ;; already occupied)
1091 (dolist (cur mairixservers)
1092 (push
1093 (concat
1094 (symbol-name
1095 (cadr (assoc 'nnmairix-backend
1096 (gnus-server-to-method (car cur)))))
1098 (cadr (assoc 'nnmairix-backend-server
1099 (gnus-server-to-method (car cur)))))
1100 occ))
1101 (while alist
1102 (setq server (pop alist))
1103 (setq mserver (gnus-method-to-server (car server)))
1104 ;; If this is the native server, convert it to the real server
1105 ;; name to avoid confusion
1106 (when (string= mserver "native")
1107 (setq mserver (format "%s:%s"
1108 (caar server)
1109 (nth 1 (car server)))))
1110 (when (and server
1111 (eq (cadr server) 'ok)
1112 (member (caar server) nnmairix-valid-backends)
1113 (not (member (car server) gnus-ephemeral-servers))
1114 (not (member (gnus-method-to-server (car server)) occ)))
1115 (push
1116 (list mserver)
1117 openedserver)))
1118 openedserver))
1120 (defun nnmairix-call-backend (func &rest args)
1121 "Call a function FUNC on backend with ARGS."
1122 (apply (intern (format "%s-%s" (symbol-name nnmairix-backend) func)) args))
1124 (defun nnmairix-get-backend-folder (group &optional server)
1125 "Return back end GROUP from nnmairix group on SERVER."
1126 (let* ((qualgroup (if server
1127 (gnus-group-prefixed-name group (list 'nnmairix server))
1128 group))
1129 (folder (gnus-group-get-parameter qualgroup 'folder)))
1130 folder))
1132 (defun nnmairix-get-numcorr (group &optional server)
1133 "Return values for article number correction nnmairix GROUP on SERVER."
1134 (let* ((qualgroup (if server
1135 (gnus-group-prefixed-name group (list 'nnmairix server))
1136 group))
1137 (corr (gnus-group-get-parameter qualgroup 'numcorr t)))
1138 corr))
1141 (defun nnmairix-rename-files-consecutively (path)
1142 "Rename all nnml mail files in PATH so that they have consecutive numbers.
1143 This should correct problems of wrong article counts when using
1144 nnmairix with nnml backends."
1145 (let* ((files
1146 (sort
1147 (mapcar 'string-to-number
1148 (directory-files path nil "[0-9]+" t))
1149 '<))
1150 (lastplusone (car files))
1151 (path (file-name-as-directory path)))
1152 (dolist (cur files)
1153 (when (not (= cur lastplusone))
1154 (rename-file (concat path
1155 (number-to-string cur))
1156 (concat path
1157 (number-to-string lastplusone)))
1158 (setq cur lastplusone))
1159 (setq lastplusone (1+ cur)))))
1161 (defun nnmairix-replace-group-and-numbers (articles backendgroup mairixgroup numc)
1162 "Replace folder names in Xref header and correct article numbers.
1163 Do this for all ARTICLES on BACKENDGROUP. Replace using
1164 MAIRIXGROUP. NUMC contains values for article number correction."
1165 (let ((buf (get-buffer-create " *nnmairix buffer*"))
1166 (corr (not (zerop numc)))
1167 (name (buffer-name nntp-server-buffer))
1168 header cur xref)
1169 (save-excursion
1170 (set-buffer buf)
1171 (erase-buffer)
1172 (set-buffer nntp-server-buffer)
1173 (goto-char (point-min))
1174 (nnheader-message 7 "nnmairix: Rewriting headers...")
1175 (mapc
1176 (lambda (article)
1177 (when (or (looking-at (number-to-string article))
1178 (nnheader-find-nov-line article))
1179 (setq cur (nnheader-parse-nov))
1180 (when corr
1181 (setq article (+ (mail-header-number cur) numc))
1182 (mail-header-set-number cur article))
1183 (setq xref (mail-header-xref cur))
1184 (when (and (stringp xref)
1185 (string-match (format "[ \t]%s:[0-9]+" backendgroup) xref))
1186 (setq xref (replace-match (format " %s:%d" mairixgroup article) t nil xref))
1187 (mail-header-set-xref cur xref))
1188 (set-buffer buf)
1189 (nnheader-insert-nov cur)
1190 (set-buffer nntp-server-buffer)
1191 (when (not (eobp))
1192 (forward-line 1))))
1193 articles)
1194 (nnheader-message 7 "nnmairix: Rewriting headers... done")
1195 (kill-buffer nntp-server-buffer)
1196 (set-buffer buf)
1197 (rename-buffer name)
1198 (setq nntp-server-buffer buf))))
1200 (defun nnmairix-backend-to-server (server)
1201 "Return nnmairix server most probably responsible for back end SERVER.
1202 User will be asked if this cannot be determined. Result is saved in
1203 parameter 'indexed-servers of corresponding default search
1204 group."
1205 (let ((allservers (nnmairix-get-nnmairix-servers))
1206 mairixserver found defaultgroup)
1207 (if (> (length allservers) 1)
1208 (progn
1209 ;; If there is more than one nnmairix server, we go through them
1210 (while (and allservers (not found))
1211 (setq mairixserver (gnus-server-to-method (car (pop allservers))))
1212 ;; First we look if SERVER is the backend of current nnmairix server
1213 (setq found (and (eq (cadr (assoc 'nnmairix-backend mairixserver))
1214 (car server))
1215 (string= (cadr (assoc 'nnmairix-backend-server mairixserver))
1216 (nth 1 server))))
1217 ;; If that's not the case, we look at 'indexed-servers
1218 ;; variable in default search group
1219 (when (not found)
1220 (setq defaultgroup (cadr (assoc 'nnmairix-default-group mairixserver)))
1221 (setq found (member (gnus-method-to-server server)
1222 (gnus-group-get-parameter
1223 (gnus-group-prefixed-name defaultgroup
1224 mairixserver)
1225 'indexed-servers t)))))
1226 ;; If still not found, we ask user
1227 (when (not found)
1228 (setq mairixserver
1229 (gnus-server-to-method
1230 (completing-read
1231 (format "Cannot determine which nnmairix server indexes %s. Please specify: "
1232 (gnus-method-to-server server))
1233 (nnmairix-get-nnmairix-servers) nil nil "nnmairix:")))
1234 ;; Save result in parameter of default search group so that
1235 ;; we don't have to ask again
1236 (setq defaultgroup (gnus-group-prefixed-name
1237 (cadr (assoc 'nnmairix-default-group mairixserver)) mairixserver))
1238 (gnus-group-set-parameter
1239 defaultgroup
1240 'indexed-servers
1241 (append (gnus-group-get-parameter defaultgroup 'indexed-servers t)
1242 (list (gnus-method-to-server server)))))
1243 mairixserver)
1244 ;; If there is just one (or none) nnmairix server:
1245 (gnus-server-to-method (caar allservers)))))
1247 (defun nnmairix-update-and-clear-marks (group &optional method)
1248 "Update group and clear all marks from GROUP using METHOD."
1249 (when method
1250 (setq group (gnus-group-prefixed-name group method)))
1251 (let ((method (or method
1252 (gnus-find-method-for-group group)))
1253 (folder (gnus-group-get-parameter group 'folder))
1254 (corr (gnus-group-get-parameter group 'numcorr t))
1255 info)
1256 (if (eq (nth 0 method) 'nnmairix)
1257 (save-excursion
1258 (nnmairix-open-server (nth 1 method))
1259 (set-buffer gnus-group-buffer)
1260 (setq info (gnus-get-info group))
1261 ;; Clear active and info
1262 (gnus-set-active group nil)
1263 (gnus-info-clear-data info)
1264 ;; Delete and re-create group if needed
1265 (when (member nnmairix-backend nnmairix-delete-and-create-on-change)
1266 (if (string-match nnmairix-group-regexp folder)
1267 (progn
1268 (nnmairix-call-backend "open-server"
1269 nnmairix-backend-server)
1270 (nnmairix-call-backend "request-delete-group"
1271 folder t nnmairix-backend-server)
1272 (nnmairix-call-backend "request-create-group"
1273 folder nnmairix-backend-server)
1274 ;; set flag that group has changed for article number correction
1275 (when corr
1276 (setcar corr t)
1277 (gnus-group-set-parameter group 'numcorr corr)))
1278 (error "Nnmairix-update-and-clear-marks - delete/create with\
1279 non-mairix group!! - check folder parameter")))
1280 (when (gnus-group-jump-to-group group)
1281 (gnus-group-get-new-news-this-group)))
1282 (error "Nnmairix-update-and-clear-marks - Called with non-nnmairix group"))))
1285 (defun nnmairix-sentinel-mairix-update-finished (proc status)
1286 "Sentinel for mairix update process PROC with STATUS."
1287 (if (equal status "finished\n")
1288 (nnheader-message 7 "Updating mairix database for %s... done" proc)
1289 (error "There was an error updating the mairix database for server %s. \
1290 See %s for details" proc nnmairix-mairix-output-buffer)))
1292 (defun nnmairix-create-message-line-for-search ()
1293 "Create message line for interactive query in minibuffer."
1294 (mapconcat
1295 (function
1296 (lambda (cur)
1297 (format "%c=%s" (car cur) (nth 3 cur))))
1298 nnmairix-interactive-query-parameters ","))
1300 (defun nnmairix-replace-illegal-chars (header)
1301 "Replace illegal characters in HEADER for mairix query."
1302 (when header
1303 (if (> emacs-major-version 20)
1304 (while (string-match "[^-.@/,& [:alnum:]]" header)
1305 (setq header (replace-match "" t t header)))
1306 (while (string-match "[[]{}:<>]" header)
1307 (setq header (replace-match "" t t header))))
1308 (while (string-match "[-& ]" header)
1309 (setq header (replace-match "," t t header)))
1310 header))
1312 (defun nnmairix-show-original-article (group mid)
1313 "Switch to GROUP and display Article with message-id MID."
1314 (when (string-match "Summary" (buffer-name (current-buffer)))
1315 (gnus-summary-exit))
1316 (pop-to-buffer gnus-group-buffer)
1317 (gnus-group-jump-to-group group)
1318 (gnus-summary-read-group group 1 t)
1319 (gnus-summary-refer-article mid)
1320 (gnus-summary-limit-to-headers (format "message-id: <%s>" mid))
1321 (gnus-summary-select-article)
1322 ;; Force redisplay
1323 (gnus-summary-show-article)
1324 (nnheader-message 5 "Switched to group %s." group))
1327 ;; ==== Widget stuff
1329 (defvar nnmairix-widgets)
1330 (defvar nnmairix-widgets-values nil)
1332 (defun nnmairix-widget-search-from-this-article ()
1333 "Create mairix query based on current article using graphical widgets."
1334 (interactive)
1335 (nnmairix-widget-search
1336 (nnmairix-widget-get-values)))
1339 (defun nnmairix-widget-get-values ()
1340 "Create values for editable fields from current article."
1341 (if (not (gnus-buffer-live-p gnus-article-buffer))
1342 (error "No article buffer available")
1343 (save-excursion
1344 (gnus-summary-toggle-header 1)
1345 (set-buffer gnus-article-buffer)
1346 (mapcar
1347 (function
1348 (lambda (field)
1349 (list (car (cddr field))
1350 (if (car field)
1351 (nnmairix-replace-illegal-chars
1352 (gnus-fetch-field (car field)))
1353 nil))))
1354 nnmairix-widget-fields-list))))
1357 (defun nnmairix-widget-search (&optional mvalues)
1358 "Create mairix query interactively using graphical widgets.
1359 MVALUES may contain values from current article."
1360 (interactive)
1361 ;; Select window for mairix customization
1362 (funcall nnmairix-widget-select-window-function)
1363 ;; generate widgets
1364 (nnmairix-widget-create-query mvalues)
1365 ;; generate Buttons
1366 (widget-create 'push-button
1367 :notify
1368 (if mvalues
1369 (lambda (&rest ignore)
1370 (nnmairix-widget-send-query nnmairix-widgets
1372 (lambda (&rest ignore)
1373 (nnmairix-widget-send-query nnmairix-widgets
1374 nil)))
1375 "Send Query")
1376 (widget-insert " ")
1377 (widget-create 'push-button
1378 :notify
1379 (if mvalues
1380 (lambda (&rest ignore)
1381 (nnmairix-widget-create-group nnmairix-widgets
1383 (lambda (&rest ignore)
1384 (nnmairix-widget-create-group nnmairix-widgets
1385 nil)))
1386 "Create permanent group")
1387 (widget-insert " ")
1388 (widget-create 'push-button
1389 :notify (lambda (&rest ignore)
1390 (kill-buffer nnmairix-customize-query-buffer))
1391 "Cancel")
1392 (use-local-map widget-keymap)
1393 (widget-setup)
1394 (goto-char (point-min)))
1396 (defun nnmairix-widget-send-query (widgets &optional withvalues)
1397 "Send query from WIDGETS to mairix binary.
1398 If WITHVALUES is t, query is based on current article."
1399 (nnmairix-search
1400 (nnmairix-widget-make-query-from-widgets widgets)
1401 (if withvalues
1402 (gnus-method-to-server
1403 (nnmairix-backend-to-server gnus-current-select-method))
1404 (car (nnmairix-get-server)))
1405 (if (widget-value (cadr (assoc "Threads" widgets)))
1407 -1))
1408 (kill-buffer nnmairix-customize-query-buffer))
1410 (defun nnmairix-widget-create-group (widgets &optional withvalues)
1411 "Create nnmairix group based on current widget values WIDGETS.
1412 If WITHVALUES is t, query is based on current article."
1413 (let ((group (read-string "Name of the group: ")))
1414 (when (not (zerop (length group)))
1415 (nnmairix-create-search-group
1416 (if withvalues
1417 (gnus-method-to-server
1418 (nnmairix-backend-to-server gnus-current-select-method))
1419 (car (nnmairix-get-server)))
1420 group
1421 (nnmairix-widget-make-query-from-widgets widgets)
1422 (widget-value (cadr (assoc "Threads" widgets))))))
1423 (kill-buffer nnmairix-customize-query-buffer))
1426 (defun nnmairix-widget-make-query-from-widgets (widgets)
1427 "Create mairix query from widget values WIDGETS."
1428 (let (query temp flag)
1429 ;; first we do the editable fields
1430 (dolist (cur nnmairix-widget-fields-list)
1431 ;; See if checkbox is checked
1432 (when (widget-value
1433 (cadr (assoc (concat "c" (car (cddr cur))) widgets)))
1434 ;; create query for the field
1435 (push
1436 (concat
1437 (nth 1 cur)
1439 (nnmairix-replace-illegal-chars
1440 (widget-value
1441 (cadr (assoc (concat "e" (car (cddr cur))) widgets)))))
1442 query)))
1443 ;; Flags
1444 (when (member 'flags nnmairix-widget-other)
1445 (setq flag
1446 (mapconcat
1447 (function
1448 (lambda (flag)
1449 (setq temp
1450 (widget-value (cadr (assoc (car flag) nnmairix-widgets))))
1451 (if (string= "yes" temp)
1452 (cadr flag)
1453 (if (string= "no" temp)
1454 (concat "-" (cadr flag))))))
1455 '(("seen" "s") ("replied" "r") ("flagged" "f")) ""))
1456 (when (not (zerop (length flag)))
1457 (push (concat "F:" flag) query)))
1458 ;; return query string
1459 (mapconcat 'identity query " ")))
1462 (defun nnmairix-widget-create-query (&optional values)
1463 "Create widgets for creating mairix queries.
1464 Fill in VALUES if based on an article."
1465 (let (allwidgets)
1466 (when (get-buffer nnmairix-customize-query-buffer)
1467 (kill-buffer nnmairix-customize-query-buffer))
1468 (switch-to-buffer nnmairix-customize-query-buffer)
1469 (kill-all-local-variables)
1470 (erase-buffer)
1471 (widget-insert "Specify your query for Mairix (check boxes for activating fields):\n\n")
1472 (widget-insert "(Whitespaces will be converted to ',' (i.e. AND). Use '/' for OR.)\n\n")
1473 ; (make-local-variable 'nnmairix-widgets)
1474 (setq nnmairix-widgets (nnmairix-widget-build-editable-fields values))
1475 (when (member 'flags nnmairix-widget-other)
1476 (widget-insert "\nFlags:\n Seen: ")
1477 (nnmairix-widget-add "seen"
1478 'menu-choice
1479 :value "ignore"
1480 '(item "yes") '(item "no") '(item "ignore"))
1481 (widget-insert " Replied: ")
1482 (nnmairix-widget-add "replied"
1483 'menu-choice
1484 :value "ignore"
1485 '(item "yes") '(item "no") '(item "ignore"))
1486 (widget-insert " Ticked: ")
1487 (nnmairix-widget-add "flagged"
1488 'menu-choice
1489 :value "ignore"
1490 '(item "yes") '(item "no") '(item "ignore")))
1491 (when (member 'threads nnmairix-widget-other)
1492 (widget-insert "\n")
1493 (nnmairix-widget-add "Threads" 'checkbox nil))
1494 (widget-insert " Show full threads\n\n")))
1497 (defun nnmairix-widget-build-editable-fields (values)
1498 "Build editable field widgets in `nnmairix-widget-fields-list'.
1499 VALUES may contain values for editable fields from current article."
1500 ;; how can this be done less ugly?
1501 (let ((ret))
1502 (mapc
1503 (function
1504 (lambda (field)
1505 (setq field (car (cddr field)))
1506 (setq ret
1507 (nconc
1508 (list
1509 (list
1510 (concat "c" field)
1511 (widget-create 'checkbox
1512 :tag field
1513 :notify (lambda (widget &rest ignore)
1514 (nnmairix-widget-toggle-activate widget))
1515 nil)))
1516 (list
1517 (list
1518 (concat "e" field)
1519 (widget-create 'editable-field
1520 :size 60
1521 :format (concat " " field ":"
1522 (make-string (- 11 (length field)) ?\ )
1523 "%v")
1524 :value (or (cadr (assoc field values)) ""))))
1525 ret))
1526 (widget-insert "\n")
1527 ;; Deactivate editable field
1528 (widget-apply (cadr (nth 1 ret)) :deactivate)))
1529 nnmairix-widget-fields-list)
1530 ret))
1532 (defun nnmairix-widget-add (name &rest args)
1533 "Add a widget NAME with optional ARGS."
1534 (push
1535 (list name
1536 (apply 'widget-create args))
1537 nnmairix-widgets))
1539 (defun nnmairix-widget-toggle-activate (widget)
1540 "Toggle activation status of WIDGET dependent on corresponding checkbox value."
1541 (let ((field (widget-get widget :tag)))
1542 (if (widget-value widget)
1543 (widget-apply
1544 (cadr (assoc (concat "e" field) nnmairix-widgets))
1545 :activate)
1546 (widget-apply
1547 (cadr (assoc (concat "e" field) nnmairix-widgets))
1548 :deactivate)))
1549 (widget-setup))
1551 (provide 'nnmairix)
1553 ;; arch-tag: bb187498-b229-4a55-8c07-6d3f80713e94
1554 ;;; nnmairix.el ends here