Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / gnus / nnmairix.el
blob4eb4bde7d290c9309047bc2cea3b22be32d01851
1 ;;; nnmairix.el --- Mairix back end for Gnus, the Emacs newsreader
3 ;; Copyright (C) 2007-2014 Free Software Foundation, Inc.
5 ;; Author: David Engster <dengste@eml.cc>
6 ;; Keywords: mail searching
7 ;; Version: 0.6
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 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This is a back end for using the mairix search engine with
27 ;; Gnus. Mairix is a tool for searching words in locally stored
28 ;; mail. Mairix is very fast which allows using it efficiently for
29 ;; "smart folders", e.g. folders which are associated with search
30 ;; queries. Of course, you can also use this back end just for
31 ;; calling mairix with some search query.
33 ;; Mairix is written by Richard Curnow. More information can be found at
34 ;; http://www.rpcurnow.force9.co.uk/mairix/
36 ;; Commentary on the code: nnmairix sits between Gnus and the "real"
37 ;; back end which handles the mail (currently nnml, nnimap and
38 ;; nnmaildir were tested). I know this is all a bit hacky, but so far
39 ;; it works for me. This is the first back end I've written for Gnus,
40 ;; so I'd appreciate any comments, suggestions, bug reports (and, of
41 ;; course, patches) for improving nnmairix.
43 ;; nnmairix does not use an active file, since I wanted to contain the
44 ;; back end "inside Gnus" as much as possible without the need of an
45 ;; external file. It stores the query/folder information in the group
46 ;; parameters instead. This also implies that once you kill a mairix
47 ;; group, it's gone for good. I don't think that this is really
48 ;; problematic, since I don't see the need in unsubscribing and
49 ;; re-subscribing search groups
51 ;; Every mairix server is "responsible" for one mairix installation,
52 ;; i.e. you can have several mairix servers for different mairix
53 ;; configurations. Not that I think anyone will actually do this, but
54 ;; I thought it would be a "nice to have feature"...
56 ;; KNOWN BUGS:
57 ;; * Mairix does only support us-ascii characters.
59 ;; TODO/MISSING FEATURES:
60 ;; * Support of more back ends (nnmh, nnfolder, nnmbox...)?
61 ;; * Maybe use an active file instead of group parameters?
62 ;; * Maybe use "-a" when updating groups which are not newly created?
64 ;;; Changelog:
65 ;; 05/30/2008 - version 0.6
67 ;; * It is now possible to propagate marks from the nnmairix groups
68 ;; to the original messages (and for maildir also vice versa). See
69 ;; the docs for details on this feature - it's pretty delicate
70 ;; and currently needs a patched mairix binary to work smoothly.
72 ;; * Keep messages in nnmairix groups always read/unread
73 ;; (bound to 'G b r').
75 ;; * Recreate back end folder for nnmairix groups in case you
76 ;; somehow get wrong article counts (bound to 'G b d').
78 ;; * New group parameter 'allow-fast'. Toggling of parameter bound
79 ;; to 'G b a'. The default is nil, meaning that the group will
80 ;; always be updated with a mairix search, even when only entered.
82 ;; * More/Better use of the registry (if available). Can now also
83 ;; deal with duplicate messages in different groups.
85 ;; 02/06/2008 - version 0.5
87 ;; * New function: nnmairix-goto-original-article. Uses the
88 ;; registry or the mail file path for determining original group.
90 ;; * Deal with empty Xref header
92 ;; * Changed summary mode keybindings since the old ones were
93 ;; already taken
95 ;; (Thanks to Tassilo Horn and Ted Zlatanov for their help)
97 ;; 01/07/2008 - version 0.4
99 ;; * New/fixed doc strings and code cleanup.
101 ;; 11/18/2007 - version 0.3
103 ;; * Fixed bugs when dealing with nnml and native servers
105 ;; * Make variables customizable
107 ;; 10/10/2007 - version 0.2
109 ;; * Use nnml-directory/directory server variables for nnml and
110 ;; nnmaildir back ends as path for search folders. This way it
111 ;; becomes independent of 'base' setting in .mairixirc (but not for
112 ;; nnimap).
114 ;; * As a result: Changed nnmairix-backend-to-server so that user
115 ;; is asked when more than one nnmairix server exists and we do not
116 ;; know which one is responsible for current back end.
118 ;; * Rename files when using nnml back ends so that there are no
119 ;; holes in article numbers. This should fix all problems regarding
120 ;; wrong article counts with nnml.
122 ;; * More commands for creating queries (using widgets or the
123 ;; minibuffer).
125 ;; * Fixed bug in nnmairix-create-search-group-from-message
127 ;; * Changed copyright to FSF
129 ;; (Thanks to Georg C. F. Greve and Bastien for suggestions and
130 ;; ideas!)
132 ;; 10/03/2007 - version 0.1 - first release
135 ;;; Code:
137 (eval-when-compile (require 'cl)) ;For (pop (cdr ogroup)).
139 (require 'nnoo)
140 (require 'gnus-group)
141 (require 'gnus-sum)
142 (require 'message)
143 (require 'nnml)
144 (require 'widget)
146 (nnoo-declare nnmairix)
148 ;;; === Keymaps
150 (eval-when-compile
151 (when (featurep 'xemacs)
152 ;; The `kbd' macro requires that the `read-kbd-macro' macro is available.
153 (require 'edmacro)))
155 ;; Group mode
156 (defun nnmairix-group-mode-hook ()
157 "Nnmairix group mode keymap."
158 (define-key gnus-group-mode-map
159 (kbd "G b") (make-sparse-keymap))
160 (define-key gnus-group-mode-map
161 (kbd "G b g") 'nnmairix-create-search-group)
162 (define-key gnus-group-mode-map
163 (kbd "G b c") 'nnmairix-create-server-and-default-group)
164 (define-key gnus-group-mode-map
165 (kbd "G b q") 'nnmairix-group-change-query-this-group)
166 (define-key gnus-group-mode-map
167 (kbd "G b t") 'nnmairix-group-toggle-threads-this-group)
168 (define-key gnus-group-mode-map
169 (kbd "G b u") 'nnmairix-update-database)
170 (define-key gnus-group-mode-map
171 (kbd "G b s") 'nnmairix-search)
172 (define-key gnus-group-mode-map
173 (kbd "G b i") 'nnmairix-search-interactive)
174 (define-key gnus-group-mode-map
175 (kbd "G b m") 'nnmairix-widget-search)
176 (define-key gnus-group-mode-map
177 (kbd "G b p") 'nnmairix-group-toggle-propmarks-this-group)
178 (define-key gnus-group-mode-map
179 (kbd "G b r") 'nnmairix-group-toggle-readmarks-this-group)
180 (define-key gnus-group-mode-map
181 (kbd "G b d") 'nnmairix-group-delete-recreate-this-group)
182 (define-key gnus-group-mode-map
183 (kbd "G b a") 'nnmairix-group-toggle-allowfast-this-group)
184 (define-key gnus-group-mode-map
185 (kbd "G b o") 'nnmairix-propagate-marks))
187 ;; Summary mode
188 (defun nnmairix-summary-mode-hook ()
189 "Nnmairix summary mode keymap."
190 (define-key gnus-summary-mode-map
191 (kbd "G G t") 'nnmairix-search-thread-this-article)
192 (define-key gnus-summary-mode-map
193 (kbd "G G f") 'nnmairix-search-from-this-article)
194 (define-key gnus-summary-mode-map
195 (kbd "G G m") 'nnmairix-widget-search-from-this-article)
196 (define-key gnus-summary-mode-map
197 (kbd "G G g") 'nnmairix-create-search-group-from-message)
198 (define-key gnus-summary-mode-map
199 (kbd "G G o") 'nnmairix-goto-original-article)
200 (define-key gnus-summary-mode-map
201 (kbd "G G u") 'nnmairix-remove-tick-mark-original-article))
203 (add-hook 'gnus-group-mode-hook 'nnmairix-group-mode-hook)
204 (add-hook 'gnus-summary-mode-hook 'nnmairix-summary-mode-hook)
206 ;; ;;;###autoload
207 ;; (defun nnmairix-initialize (&optional force)
208 ;; (interactive "P")
209 ;; (if (not (or (file-readable-p "~/.mairixrc")
210 ;; force))
211 ;; (message "No file `~/.mairixrc', skipping nnmairix setup")
212 ;; (add-hook 'gnus-group-mode-hook 'nnmairix-group-mode-hook)
213 ;; (add-hook 'gnus-summary-mode-hook 'nnmairix-summary-mode-hook)))
215 ;; Customizable stuff
217 (defgroup nnmairix nil
218 "Back end for the Mairix mail search engine."
219 :group 'gnus)
221 (defcustom nnmairix-group-prefix "zz_mairix"
222 "Prefix for mairix search groups on back end server.
223 nnmairix will create these groups automatically on the back end
224 server for each nnmairix search group. The name on the back end
225 server will be this prefix plus a random number. You can delete
226 unused nnmairix groups on the back end using
227 `nnmairix-purge-old-groups'."
228 :version "23.1"
229 :type 'string
230 :group 'nnmairix)
232 (defcustom nnmairix-mairix-output-buffer "*mairix output*"
233 "Buffer used for mairix output."
234 :version "23.1"
235 :type 'string
236 :group 'nnmairix)
238 (defcustom nnmairix-customize-query-buffer "*mairix query*"
239 "Name of the buffer for customizing Mairix queries."
240 :version "23.1"
241 :type 'string
242 :group 'nnmairix)
244 (defcustom nnmairix-mairix-update-options '("-F" "-Q")
245 "Options when calling mairix for updating the database.
246 The default is '-F' and '-Q' for making updates faster. You
247 should call mairix without these options from time to
248 time (e.g. via cron job)."
249 :version "23.1"
250 :type '(repeat string)
251 :group 'nnmairix)
253 (defcustom nnmairix-mairix-search-options '("-Q")
254 "Options when calling mairix for searching.
255 The default is '-Q' for making searching faster."
256 :version "23.1"
257 :type '(repeat string)
258 :group 'nnmairix)
260 (defcustom nnmairix-mairix-synchronous-update nil
261 "Set this to t if you want Emacs to wait for mairix updating the database."
262 :version "23.1"
263 :type 'boolean
264 :group 'nnmairix)
266 (defcustom nnmairix-rename-files-for-nnml t
267 "Rename nnml mail files so that they are consecutively numbered.
268 When using nnml as back end, mairix might produce holes in the
269 article numbers which will produce wrong article counts by
270 Gnus. This option controls whether nnmairix should rename the
271 files consecutively."
272 :version "23.1"
273 :type 'boolean
274 :group 'nnmairix)
276 (defcustom nnmairix-widget-fields-list
277 '(("from" "f" "From") ("to" "t" "To") ("cc" "c" "Cc")
278 ("subject" "s" "Subject") ("to" "tc" "To or Cc")
279 ("from" "a" "Address") (nil "b" "Body") (nil "n" "Attachment")
280 ("Message-ID" "m" "Message ID") (nil "s" "Size") (nil "d" "Date"))
281 "Fields that should be editable during interactive query customization.
283 Header, corresponding mairix command and description for editable
284 fields in interactive query customization. The header specifies
285 which header contents should be inserted into the editable field
286 when creating a Mairix query based on the current message (can be
287 nil for disabling this)."
288 :version "23.1"
289 :type '(repeat (list
290 (choice :tag "Field"
291 (const :tag "none" nil)
292 (const :tag "From" "from")
293 (const :tag "To" "to")
294 (const :tag "Cc" "cc")
295 (const :tag "Subject" "subject")
296 (const :tag "Message ID" "Message-ID"))
297 (string :tag "Command")
298 (string :tag "Description")))
299 :group 'nnmairix)
301 (defcustom nnmairix-widget-select-window-function
302 (lambda () (select-window (get-largest-window)))
303 "Function for selecting the window for customizing the mairix query.
304 The default chooses the largest window in the current frame."
305 :version "23.1"
306 :type 'function
307 :group 'nnmairix)
309 (defcustom nnmairix-propagate-marks-upon-close t
310 "Flag if marks should be propagated upon closing a group.
311 The default of this variable is t. If set to 'ask, the
312 user will be asked if the flags should be propagated when the
313 group is closed. If set to nil, the user will have to manually
314 call 'nnmairix-propagate-marks'."
315 :version "23.1"
316 :type '(choice (const :tag "always" t)
317 (const :tag "ask" ask)
318 (const :tag "never" nil))
319 :group 'nnmairix)
321 (defcustom nnmairix-propagate-marks-to-nnmairix-groups nil
322 "Flag if marks from original articles should be seen in nnmairix groups.
323 The default is nil since it will only work if the articles are in
324 maildir format and NOT managed by the nnmaildir back end but
325 e.g. an IMAP server (which stores the marks in the maildir file
326 name). You may safely set this to t for testing - the worst that
327 can happen are wrong marks in nnmairix groups."
328 :version "23.1"
329 :type 'boolean
330 :group 'nnmairix)
332 (defcustom nnmairix-only-use-registry nil
333 "Use only the registry for determining original group(s).
334 If set to t, nnmairix will only use the registry for determining
335 the original group(s) of an article (which is also necessary for
336 propagating marks). If set to nil, it will also try to determine
337 the group from an additional mairix search which might be slow
338 when propagating lots of marks."
339 :version "23.1"
340 :type 'boolean
341 :group 'nnmairix)
343 (defcustom nnmairix-allowfast-default nil
344 "Whether fast entering should be the default for nnmairix groups.
345 You may set this to t to make entering the group faster, but note that
346 this might lead to problems, especially when used with marks propagation."
347 :version "23.1"
348 :type 'boolean
349 :group 'nnmairix)
351 ;; ==== Other variables
353 (defvar nnmairix-widget-other
354 '(threads flags)
355 "Other editable mairix commands when using customization widgets.
356 Currently there are 'threads and 'flags.")
358 (defvar nnmairix-interactive-query-parameters
359 '((?f "from" "f" "From") (?t "to" "t" "To") (?c "to" "tc" "To or Cc")
360 (?a "from" "a" "Address") (?s "subject" "s" "Subject") (?b nil "b" "Body")
361 (?d nil "d" "Date") (?n nil "n" "Attachment"))
362 "Things that should be editable during interactive query generation.
363 Every list element consists of the following entries: Keystroke,
364 message field (if any), mairix command and description.")
366 (defvar nnmairix-delete-and-create-on-change '(nnimap nnmaildir nnml)
367 "Controls on which back ends groups should be deleted and re-created.
368 This variable is a list of back ends where the search group
369 should be completely deleted and re-created when the query or
370 thread parameter changes. The default is to this for all
371 currently supported back ends. It usually also corrects the
372 problem of \"holes\" in the article numbers which often lead to a
373 wrong count of total articles shown by Gnus.")
375 ;;; === Server variables
377 (defvoo nnmairix-backend nil
378 "Back end where mairix stores its searches.")
380 (defvoo nnmairix-backend-server nil
381 "Name of the server where mairix stores its searches.")
383 (defvoo nnmairix-mairix-command "mairix"
384 "Command to call mairix for this nnmairix server.")
386 (defvoo nnmairix-hidden-folders nil
387 "Set this to t if the back end server uses hidden directories for
388 its maildir mail folders (e.g. the Dovecot IMAP server or mutt).")
390 (defvoo nnmairix-default-group nil
391 "Default search group. This is the group which is used for all
392 temporary searches, e.g. nnmairix-search.")
394 ;;; === Internal variables
396 (defconst nnmairix-group-regexp
397 (format "%s-\\(.*\\)-[0-9]+" nnmairix-group-prefix)
398 "Regexp for mairix groups on back end.")
400 (defconst nnmairix-valid-backends '(nnimap nnml nnmaildir)
401 "Back ends supported by nnmairix.
402 Other back ends might or might not work.")
404 (defvar nnmairix-last-server nil
405 "Last chosen server.")
407 (defvar nnmairix-current-server nil
408 "Current server.")
410 (defvar nnmairix-marks-cache nil
411 "Cache for marks which should be set upon closing current group.")
413 (defvar nnmairix-version-output nil
414 "Version string of mairix binary.")
416 ;;; === Gnus back end functions
418 (nnoo-define-basics nnmairix)
420 (gnus-declare-backend "nnmairix" 'mail 'address)
422 (deffoo nnmairix-open-server (server &optional definitions)
423 ;; just set server variables
424 (setq nnmairix-current-server server)
425 (nnoo-change-server 'nnmairix server definitions))
427 (deffoo nnmairix-request-group (group &optional server fast info)
428 ;; Call mairix and request group on back end server
429 (when server (nnmairix-open-server server))
430 (let* ((qualgroup (if server
431 (gnus-group-prefixed-name group (list 'nnmairix server))
432 group))
433 (folder (gnus-group-get-parameter qualgroup 'folder))
434 (allowfast (gnus-group-get-parameter qualgroup 'allow-fast))
435 (query (gnus-group-get-parameter qualgroup 'query t))
436 (threads (gnus-group-get-parameter qualgroup 'threads))
437 (backendmethod (gnus-server-to-method
438 (format "%s:%s" (symbol-name nnmairix-backend)
439 nnmairix-backend-server)))
440 rval mfolder folderpath args)
441 (cond
442 ((not folder)
443 ;; No folder parameter -> error
444 (nnheader-report 'nnmairix "Check folder parameter for group %s" group)
445 nil)
446 ((not query)
447 ;; No query -> return empty group
448 (with-current-buffer nntp-server-buffer
449 (erase-buffer)
450 (insert (concat "211 0 1 0 " group))
453 ;; For maildir++ folders: create a hidden directory (prepend dot)
454 (setq mfolder (if (and nnmairix-hidden-folders
455 (not (string-match "^\\." folder)))
456 (concat "." folder)
457 folder))
458 ;; For nnml and nnmaildir, precede mfolder with directory where mail
459 ;; is actually stored so that it's independent of 'base' setting
460 ;; in .mairixrc.
461 (when (eq nnmairix-backend 'nnml)
462 (setq folderpath (cadr (assoc 'nnml-directory backendmethod)))
463 ;; if nnml-directory is not explicitly set, use global value
464 (when (not folderpath)
465 (setq folderpath nnml-directory)))
466 (when (eq nnmairix-backend 'nnmaildir)
467 (setq folderpath
468 (cadr (assoc 'directory backendmethod))))
469 (when folderpath
470 (setq mfolder
471 (concat
472 (file-name-as-directory
473 (expand-file-name
474 folderpath))
475 mfolder)))
476 ;; If (not fast), call Mairix binary
477 ;; recreate underlying folder on the back end
478 (setq rval
479 (if (and fast allowfast)
481 (nnmairix-call-mairix-binary
482 (split-string nnmairix-mairix-command)
483 mfolder query threads)))
484 ;; Check return value
485 (cond
486 ((zerop rval) ; call was successful
487 (nnmairix-call-backend
488 "open-server" nnmairix-backend-server)
489 ;; If we're dealing with nnml, rename files
490 ;; consecutively and make new active file for this
491 ;; group
492 (when (eq nnmairix-backend 'nnml)
493 (when nnmairix-rename-files-for-nnml
494 (nnmairix-rename-files-consecutively mfolder))
495 (nnml-generate-nov-databases-directory mfolder nil t))
496 (nnmairix-call-backend
497 "request-scan" folder nnmairix-backend-server)
498 (if (and fast allowfast)
500 (nnmairix-request-group-with-article-number-correction
501 folder qualgroup)))
502 ((and (= rval 1)
503 (with-current-buffer nnmairix-mairix-output-buffer
504 (goto-char (point-min))
505 (looking-at "^Matched 0 messages")))
506 ;; No messages found -> return empty group
507 (nnheader-message 5 "Mairix: No matches found.")
508 (set-buffer nntp-server-buffer)
509 (erase-buffer)
510 (insert (concat "211 0 1 0 " group))
512 ;; Everything else is an error
514 (nnheader-report
515 'nnmairix "Error running mairix. See buffer %s for details"
516 nnmairix-mairix-output-buffer)
517 nil))))))
520 (deffoo nnmairix-request-create-group (group &optional server args)
521 (let ((qualgroup (if server (gnus-group-prefixed-name group (list 'nnmairix server))
522 group))
523 (exist t)
524 (count 0)
525 groupname info)
526 (when server (nnmairix-open-server server))
527 (gnus-group-add-parameter qualgroup '(query . nil))
528 (gnus-group-add-parameter qualgroup '(threads . nil))
529 (while exist
530 (setq count (1+ count))
531 (setq groupname (format "%s-%s-%s" nnmairix-group-prefix group
532 (number-to-string count)))
533 (setq exist (nnmairix-call-backend
534 "request-group" groupname nnmairix-backend-server)))
535 (nnmairix-call-backend
536 "request-create-group" groupname nnmairix-backend-server)
537 (gnus-group-add-parameter qualgroup '(folder . nil))
538 (when nnmairix-allowfast-default
539 (gnus-group-add-parameter qualgroup '(allow-fast . t)))
540 (gnus-group-set-parameter qualgroup 'folder groupname))
544 (deffoo nnmairix-retrieve-headers (articles group &optional server fetch-old)
545 (when server (nnmairix-open-server server))
546 (let* ((folder (nnmairix-get-backend-folder group server))
547 (corr (nnmairix-get-numcorr group server))
548 (numcorr 0)
549 rval)
550 (when (and corr
551 (not (zerop (cadr corr)))
552 (numberp (car articles)))
553 (setq numcorr (cadr corr))
554 (setq articles
555 (mapcar
556 (lambda (arg) (- arg numcorr))
557 articles)))
558 (setq rval
559 (if (eq nnmairix-backend 'nnimap)
560 (let ((gnus-nov-is-evil t))
561 (nnmairix-call-backend
562 "retrieve-headers" articles folder nnmairix-backend-server fetch-old))
563 (nnmairix-call-backend
564 "retrieve-headers" articles folder nnmairix-backend-server fetch-old)))
565 (nnmairix-replace-group-and-numbers articles folder group numcorr rval)
566 rval))
568 (deffoo nnmairix-request-article (article &optional group server to-buffer)
569 (when server (nnmairix-open-server server))
570 (let ((folder (nnmairix-get-backend-folder group server))
571 (corr (nnmairix-get-numcorr group server)))
572 (when (and
573 (numberp article)
574 corr
575 (not (zerop (cadr corr))))
576 (setq article (- article (cadr corr))))
577 (nnmairix-call-backend
578 "request-article" article folder nnmairix-backend-server to-buffer))
581 (deffoo nnmairix-request-list (&optional server)
582 (when server (nnmairix-open-server server))
583 (if (nnmairix-call-backend "request-list" nnmairix-backend-server)
584 (let (cpoint cur qualgroup folder)
585 (with-current-buffer nntp-server-buffer
586 (goto-char (point-min))
587 (setq cpoint (point))
588 (while (re-search-forward nnmairix-group-regexp (point-max) t)
589 (setq cur (match-string 1)
590 qualgroup (gnus-group-prefixed-name cur
591 (list 'nnmairix server)))
592 (if (and (gnus-group-entry qualgroup)
593 (string= (match-string 0)
594 (gnus-group-get-parameter qualgroup 'folder)))
595 (progn
596 (replace-match cur)
597 (delete-region cpoint (point-at-bol))
598 (forward-line)
599 (setq cpoint (point)))
600 (forward-line)))
601 (delete-region cpoint (point-max)))
603 nil))
605 ;; Silence byte-compiler.
606 (autoload 'gnus-registry-get-id-key "gnus-registry")
608 (deffoo nnmairix-request-set-mark (group actions &optional server)
609 (when server
610 (nnmairix-open-server server))
611 (let* ((qualgroup (gnus-group-prefixed-name group (list 'nnmairix nnmairix-current-server)))
612 (propmarks (gnus-group-get-parameter qualgroup 'propmarks))
613 (propto (gnus-group-get-parameter qualgroup 'propto t))
614 (corr (nnmairix-get-numcorr group server))
615 (folder (nnmairix-get-backend-folder group server)))
616 (save-excursion
617 (dolist (cur actions)
618 (let ((type (nth 1 cur))
619 (cmdmarks (nth 2 cur))
620 (range (gnus-uncompress-range (nth 0 cur)))
621 mid ogroup number method temp)
622 (when (and corr
623 (not (zerop (cadr corr))))
624 (setq range (mapcar (lambda (arg)
625 (- arg (cadr corr)))
626 range)))
627 (when propmarks
628 (nnheader-message 7 "nnmairix: Setting marks...")
629 (dolist (article range)
630 ;; get article (header) and extract message id
631 ;; we try to determine as many original articles as possible
632 (catch 'problem
633 (nnmairix-call-backend "open-server" nnmairix-backend-server)
634 (unless (gnus-request-head
635 article
636 (gnus-group-prefixed-name
637 folder
638 (list nnmairix-backend nnmairix-backend-server)))
639 (nnheader-message
640 3 "Unable to set mark: couldn't fetch article header for article number %d"
641 article)
642 (throw 'problem nil))
643 (set-buffer nntp-server-buffer)
644 (goto-char (point-min))
645 (let ((case-fold-search t))
646 (re-search-forward "^message-id:.*\\(<.+>\\)" nil t))
647 (setq mid (match-string 1))
648 (unless mid
649 (nnheader-message
650 3 "Unable to set mark: article number %d has no message-id header"
651 article)
652 (throw 'problem nil))
653 ;; get original group. First try registry, then file path
654 (setq ogroup
655 (nnmairix-determine-original-group-from-registry mid))
656 (unless (or ogroup
657 nnmairix-only-use-registry)
658 (setq ogroup
659 (nnmairix-determine-original-group-from-path
660 mid nnmairix-current-server)))
661 (unless ogroup
662 (nnheader-message
663 3 "Unable to set mark: couldn't find original group for %s" mid)
664 (throw 'problem nil))
665 ;; store original groups with mid's. We cannot get
666 ;; the article number immediately since this would
667 ;; generate problems with maildir (articles might
668 ;; get moved from /new to /cur and further marks
669 ;; could then not be set)
670 (dolist (cur ogroup)
671 (setq temp (assoc cur
672 nnmairix-marks-cache))
673 (if temp
674 (nconc temp (list (list mid type cmdmarks)))
675 (push (list cur (list mid type cmdmarks))
676 nnmairix-marks-cache)))))
677 (nnheader-message 7 "nnmairix: Setting marks... done")))))))
679 (deffoo nnmairix-close-group (group &optional server)
680 (when server
681 (nnmairix-open-server server))
682 (let* ((qualgroup (gnus-group-prefixed-name group (list 'nnmairix nnmairix-current-server)))
683 (propmarks (gnus-group-get-parameter qualgroup 'propmarks))
684 method)
685 (when (and propmarks
686 nnmairix-marks-cache)
687 (when (or (eq nnmairix-propagate-marks-upon-close t)
688 (and (eq nnmairix-propagate-marks-upon-close 'ask)
689 (y-or-n-p "Propagate marks to original articles? ")))
690 (with-current-buffer gnus-group-buffer
691 (nnmairix-propagate-marks)
692 ;; update mairix group
693 (gnus-group-jump-to-group qualgroup)
694 (gnus-group-get-new-news-this-group))))))
696 (autoload 'nnimap-request-update-info-internal "nnimap")
698 (deffoo nnmairix-request-marks (group info &optional server)
699 ;; propagate info from underlying IMAP folder to nnmairix group
700 ;; This is currently experimental and must be explicitly activated
701 ;; with nnmairix-propagate-marks-to-nnmairix-group
702 (when server
703 (nnmairix-open-server server))
704 (let* ((qualgroup (gnus-group-prefixed-name
705 group
706 (list 'nnmairix nnmairix-current-server)))
707 (readmarks (gnus-group-get-parameter qualgroup 'readmarks))
708 (propmarks (gnus-group-get-parameter qualgroup 'propmarks))
709 (folder (nnmairix-get-backend-folder group server))
710 (corr (nnmairix-get-numcorr group server))
711 (docorr (and corr (not (zerop (cadr corr)))))
712 (folderinfo `(,group 1 ((1 . 1))))
713 readrange marks)
714 (when (and propmarks
715 nnmairix-propagate-marks-to-nnmairix-groups)
716 ;; these groups are not subscribed, so we have to ask the back end directly
717 (if (eq nnmairix-backend 'nnimap)
718 (nnimap-request-update-info-internal folder folderinfo nnmairix-backend-server)
719 (nnmairix-call-backend "request-update-info" folder folderinfo nnmairix-backend-server))
720 ;; set range of read articles
721 (gnus-info-set-read
722 info
723 (if docorr
724 (nnmairix-map-range
725 `(lambda (x) (+ x ,(cadr corr)))
726 (gnus-info-read folderinfo))
727 (gnus-info-read folderinfo)))
728 ;; set other marks
729 (gnus-info-set-marks
730 info
731 (if docorr
732 (mapcar (lambda (cur)
733 (cons
734 (car cur)
735 (nnmairix-map-range
736 `(lambda (x) (+ x ,(cadr corr)))
737 (list (cadr cur)))))
738 (gnus-info-marks folderinfo))
739 (gnus-info-marks folderinfo))))
740 (when (eq readmarks 'unread)
741 (gnus-info-set-read info nil))
742 (when (eq readmarks 'read)
743 (gnus-info-set-read info (gnus-active qualgroup))))
746 (nnoo-define-skeleton nnmairix)
749 ;;; === Interactive functions
751 (defun nnmairix-create-search-group (server group query threads)
752 "Create on SERVER nnmairix search group GROUP with QUERY.
753 If THREADS is t, include whole threads from found messages. If
754 called interactively, user will be asked for parameters."
755 (interactive
756 (list
757 (gnus-server-to-method (car (nnmairix-get-server)))
758 (read-string "Group name: ")
759 (read-string "Query: ")
760 (y-or-n-p "Include threads? ")))
761 (when (and (stringp query)
762 (string-match "\\s-" query))
763 (setq query (split-string query)))
764 (when (not (listp query))
765 (setq query (list query)))
766 (when (and server group query)
767 (save-excursion
768 (let ((groupname (gnus-group-prefixed-name group server))
769 info)
770 (set-buffer gnus-group-buffer)
771 (gnus-group-make-group group server)
772 (gnus-group-set-parameter groupname 'query query)
773 (gnus-group-set-parameter groupname 'threads threads)
774 (nnmairix-update-and-clear-marks groupname)))))
776 (defun nnmairix-search-interactive ()
777 "Create mairix search interactively with the minibuffer."
778 (interactive)
779 (let ((char-header nnmairix-interactive-query-parameters)
780 header finished query achar)
781 (while (not finished)
782 (while (not achar)
783 (message "Query (%s): " (nnmairix-create-message-line-for-search))
784 (setq achar (read-char))
785 (when (not (assoc achar char-header))
786 (setq achar nil)))
787 (setq header (read-string
788 (concat "Match " (nth 3 (assoc achar char-header)) " on: ")))
789 (push (concat (nth 2 (assoc achar char-header)) ":" header) query)
790 (setq finished (not (y-or-n-p "Add another search query? "))
791 achar nil))
792 (nnmairix-search
793 (mapconcat 'identity query " ")
794 (car (nnmairix-get-server))
795 (y-or-n-p "Include whole threads? "))))
797 (defun nnmairix-create-search-group-from-message ()
798 "Interactively create search group with query based on current message."
799 (interactive)
800 (let ((char-header nnmairix-interactive-query-parameters)
801 (server (nnmairix-backend-to-server gnus-current-select-method))
802 query achar header finished group threads cq)
803 (when (or (not (gnus-buffer-live-p gnus-article-buffer))
804 (not (gnus-buffer-live-p gnus-summary-buffer)))
805 (error "No article or summary buffer"))
806 (when (not server)
807 (error "No nnmairix server found for back end %s:%s"
808 (symbol-name (car gnus-current-select-method))
809 (nth 1 gnus-current-select-method)))
810 (while (not finished)
811 (save-excursion
812 (gnus-summary-toggle-header 1)
813 (while (not achar)
814 (message "Query (%s): " (nnmairix-create-message-line-for-search))
815 (setq achar (read-char))
816 (when (not (assoc achar char-header))
817 (setq achar nil)))
818 (set-buffer gnus-article-buffer)
819 (setq header nil)
820 (when (setq cq (nth 1 (assoc achar char-header)))
821 (setq header
822 (nnmairix-replace-illegal-chars
823 (gnus-fetch-field (nth 1 (assoc achar char-header))))))
824 (setq header (read-string
825 (concat "Match " (nth 3 (assoc achar char-header)) " on: ")
826 header))
827 (push (concat (nth 2 (assoc achar char-header)) ":" header) query)
828 (setq finished (not (y-or-n-p "Add another search query? "))
829 achar nil)))
830 (setq threads (y-or-n-p "Include whole threads? "))
831 (setq group (read-string "Group name: "))
832 (set-buffer gnus-summary-buffer)
833 (message "Creating group %s on server %s with query %s." group
834 (gnus-method-to-server server) (mapconcat 'identity query " "))
835 (nnmairix-create-search-group server group query threads)))
837 (defun nnmairix-create-server-and-default-group ()
838 "Interactively create new nnmairix server with default search group.
839 All necessary information will be queried from the user."
840 (interactive)
841 (let* ((name (read-string "Name of the mairix server: "))
842 (server (gnus-completing-read "Back end server"
843 (nnmairix-get-valid-servers) t))
844 (mairix (read-string "Command to call mairix: " "mairix"))
845 (defaultgroup (read-string "Default search group: "))
846 (backend (symbol-name (car (gnus-server-to-method server))))
847 (servername (nth 1 (gnus-server-to-method server)))
848 (hidden (and (string-match "^nn\\(imap\\|maildir\\)$" backend)
849 (y-or-n-p
850 "Does the back end server work with maildir++ (i.e. hidden directories)? ")))
851 create)
853 (apply (intern (format "%s-%s" backend "open-server"))
854 (list servername))
856 (when (and hidden
857 (string-match "^\\." defaultgroup))
858 (setq defaultgroup (substring defaultgroup 1)))
859 ;; Create default search group
860 (gnus-group-make-group
861 defaultgroup (list 'nnmairix name (list 'nnmairix-backend (intern backend))
862 (list 'nnmairix-backend-server servername)
863 (list 'nnmairix-mairix-command mairix)
864 (list 'nnmairix-hidden-folders hidden)
865 (list 'nnmairix-default-group defaultgroup)))))
867 (defun nnmairix-group-change-query-this-group (&optional query)
868 "Set QUERY for group under cursor."
869 (interactive)
870 (let* ((group (gnus-group-group-name))
871 (method (gnus-find-method-for-group group))
872 (oldquery (gnus-group-get-parameter group 'query t)))
873 (if (eq (car method) 'nnmairix)
874 (progn
875 (when (listp oldquery)
876 (setq oldquery (mapconcat 'identity oldquery " ")))
877 (setq query (or query
878 (read-string "New query: " oldquery)))
879 (when (stringp query)
880 (setq query (split-string query)))
881 (when query
882 (gnus-group-set-parameter group 'query query)
883 (nnmairix-update-and-clear-marks group)))
884 (error "This is no nnmairix group"))))
887 (defun nnmairix-group-toggle-threads-this-group (&optional threads)
888 "Toggle threads parameter for this group.
889 If THREADS is a positive number, set threads parameter to t.
890 If THREADS is a negative number, set it to nil."
891 (interactive)
892 (let ((group (gnus-group-group-name)))
893 (when (nnmairix-group-toggle-parameter
894 group 'threads "Threads" threads)
895 (nnmairix-update-and-clear-marks group))))
897 (defun nnmairix-group-toggle-propmarks-this-group (&optional propmarks)
898 "Toggle marks propagation for this group.
899 If PROPMARKS is a positive number, set parameter to t.
900 If PROPMARKS is a negative number, set it to nil."
901 (interactive)
902 (unless (nnmairix-check-mairix-version "maildirpatch")
903 (error "You need a mairix binary with maildir patch to use this feature. See docs for details"))
904 (let ((group (gnus-group-group-name)))
905 (when (or (not (string= (gnus-group-short-name group)
906 (cadr (assoc 'nnmairix-default-group
907 (gnus-find-method-for-group group)))))
908 (y-or-n-p "You should not activate marks propagation for the default \
909 search group. Are you sure? "))
910 (nnmairix-group-toggle-parameter
911 group 'propmarks "Marks propagation" propmarks))))
913 (defun nnmairix-group-toggle-allowfast-this-group (&optional allowfast)
914 "Toggle fast entering for this group.
915 If ALLOWFAST is a positive number, set parameter to t.
916 If ALLOWFAST is a negative number, set it to nil."
917 (interactive)
918 (nnmairix-group-toggle-parameter
919 (gnus-group-group-name) 'allow-fast "Fast entering" allowfast))
922 (defun nnmairix-group-toggle-readmarks-this-group (&optional readmarks)
923 "Toggle read/unread marks for this group.
924 If READMARKS is a positive number, articles will always be read.
925 If READMARKS is a negative number, articles will always be unread.
926 If READMARKS is t or zero, marks will stay unchanged."
927 (interactive)
928 (let* ((group (gnus-group-group-name))
929 (method (gnus-find-method-for-group group))
930 (readmarks (or readmarks
931 (gnus-group-get-parameter group 'readmarks))))
932 (if (eq (car method) 'nnmairix)
933 (cond
934 ((or (and (numberp readmarks) (< readmarks 0))
935 (eq readmarks 'read))
936 (gnus-group-set-parameter group 'readmarks 'unread)
937 (nnheader-message 3 "Articles in %s always unread." group))
938 ((or (and (numberp readmarks) (> readmarks 0))
939 (not readmarks))
940 (gnus-group-set-parameter group 'readmarks 'read)
941 (nnheader-message 3 "Articles in %s always read." group))
943 (gnus-group-set-parameter group 'readmarks nil)
944 (nnheader-message 3 "Read marks in %s stay unchanged." group)))
945 (error "This is no nnmairix group"))))
948 (defun nnmairix-search (query &optional server threads)
949 "Sends QUERY to nnmairix backend SERVER, using default its search group.
951 Default search group is automatically entered and results are shown.
952 If THREADS is t, enable threads.
953 If THREADS is a negative number, disable threads.
954 Otherwise, leave threads parameter as it is."
955 (interactive (list (read-string "Query: ")))
956 (when (not server)
957 (setq server (car (nnmairix-get-server))))
958 (if (not server)
959 (error "No opened nnmairix server found")
960 (setq server (gnus-server-to-method server)))
961 (nnmairix-open-server (nth 1 server))
962 (let* ((qualgroup (gnus-group-prefixed-name nnmairix-default-group
963 (list 'nnmairix (nth 1 server)))))
964 (set-buffer gnus-group-buffer)
965 (when (stringp query)
966 (setq query (split-string query)))
967 (gnus-group-set-parameter qualgroup 'query query)
968 (if (symbolp threads)
969 (when (eq threads 't)
970 (gnus-group-set-parameter qualgroup 'threads t))
971 (when (< threads 0)
972 (gnus-group-set-parameter qualgroup 'threads nil)))
973 (nnmairix-update-and-clear-marks qualgroup)
974 (unless (equal (gnus-active qualgroup) '(1 . 0))
975 (gnus-group-read-group nil t qualgroup))))
977 (defun nnmairix-search-thread-this-article ()
978 "Search thread for the current article.
979 This is effectively a shortcut for calling `nnmairix-search'
980 with m:msgid of the current article and enabled threads."
981 (interactive)
982 (let* ((server
983 (nnmairix-backend-to-server gnus-current-select-method))
984 mid)
985 (if server
986 (if (gnus-buffer-live-p gnus-article-buffer)
987 (progn
988 (with-current-buffer gnus-article-buffer
989 (gnus-summary-toggle-header 1)
990 (setq mid (message-fetch-field "Message-ID")))
991 (while (string-match "[<>]" mid)
992 (setq mid (replace-match "" t t mid)))
993 (nnmairix-search (concat "m:" mid) server t))
994 (message "No article buffer."))
995 (error "No nnmairix server found for back end %s:%s"
996 (symbol-name (car gnus-current-select-method))
997 (nth 1 gnus-current-select-method)))))
999 (defun nnmairix-search-from-this-article ()
1000 "Search messages from sender of the current article.
1001 This is effectively a shortcut for calling `nnmairix-search' with
1002 f:current_from."
1003 (interactive)
1004 (let* ((server
1005 (nnmairix-backend-to-server gnus-current-select-method))
1006 from)
1007 (if server
1008 (if (gnus-buffer-live-p gnus-article-buffer)
1009 (progn
1010 (with-current-buffer gnus-article-buffer
1011 (gnus-summary-toggle-header 1)
1012 (setq from (cadr (gnus-extract-address-components
1013 (gnus-fetch-field "From"))))
1014 (nnmairix-search (concat "f:" from) server -1)))
1015 (message "No article buffer."))
1016 (error "No nnmairix server found for back end %s:%s"
1017 (symbol-name (car gnus-current-select-method))
1018 (nth 1 gnus-current-select-method)))))
1021 (defun nnmairix-purge-old-groups (&optional dontask server)
1022 "Delete mairix search groups which are no longer used.
1024 You may want to call this from time to time if you are creating
1025 and deleting lots of nnmairix groups. If DONTASK is t, do not ask
1026 before deleting a group on the back end. SERVER specifies nnmairix server."
1027 (interactive)
1028 (let ((server (or server
1029 (gnus-server-to-method (car (nnmairix-get-server))))))
1030 (if (nnmairix-open-server (nth 1 server))
1031 (when (nnmairix-call-backend
1032 "request-list" nnmairix-backend-server)
1033 (let (cur qualgroup folder)
1034 (with-current-buffer nntp-server-buffer
1035 (goto-char (point-min))
1036 (while (re-search-forward nnmairix-group-regexp (point-max) t)
1037 (setq cur (match-string 0)
1038 qualgroup (gnus-group-prefixed-name
1039 (match-string 1) server))
1040 (when (not (and (gnus-group-entry qualgroup)
1041 (string= cur
1042 (gnus-group-get-parameter
1043 qualgroup 'folder))))
1044 (when (or dontask
1045 (y-or-n-p
1046 (concat "Delete group " cur
1047 " on server " nnmairix-backend-server "? ")))
1048 (nnmairix-call-backend
1049 "request-delete-group" cur t nnmairix-backend-server)))))))
1050 (message "Couldn't open server %s" (nth 1 server)))))
1053 (defun nnmairix-update-database (&optional servers)
1054 "Call mairix for updating the database for SERVERS.
1056 If SERVERS is nil, do update for all nnmairix servers. Mairix
1057 will be called asynchronously unless
1058 `nnmairix-mairix-synchronous-update' is t. Mairix will be called
1059 with `nnmairix-mairix-update-options'."
1060 (interactive)
1061 (let ((servers (or servers
1062 (nnmairix-get-nnmairix-servers)))
1063 args cur commandsplit)
1064 (while servers
1065 (setq cur (car (pop servers)))
1066 (nnmairix-open-server
1067 (nth 1 (gnus-server-to-method cur)))
1068 (setq commandsplit (split-string nnmairix-mairix-command))
1069 (nnheader-message 7 "Updating mairix database for %s..." cur)
1070 (if nnmairix-mairix-synchronous-update
1071 (progn
1072 (setq args (append (list (car commandsplit) nil
1073 (get-buffer nnmairix-mairix-output-buffer)
1074 nil)))
1075 (if (> (length commandsplit) 1)
1076 (setq args (append args (cdr commandsplit) nnmairix-mairix-update-options))
1077 (setq args (append args nnmairix-mairix-update-options)))
1078 (apply 'call-process args)
1079 (nnheader-message 7 "Updating mairix database for %s... done" cur))
1080 (progn
1081 (setq args (append (list cur (get-buffer nnmairix-mairix-output-buffer)
1082 (car commandsplit))))
1083 (if (> (length commandsplit) 1)
1084 (setq args (append args (cdr commandsplit) nnmairix-mairix-update-options))
1085 (setq args (append args nnmairix-mairix-update-options)))
1086 (set-process-sentinel (apply 'start-process args)
1087 'nnmairix-sentinel-mairix-update-finished))))))
1089 (defun nnmairix-group-delete-recreate-this-group ()
1090 "Deletes and recreates group on the back end.
1091 You can use this function on nnmairix groups which continuously
1092 show wrong article counts."
1093 (interactive)
1094 (let* ((group (gnus-group-group-name))
1095 (method (gnus-find-method-for-group group)))
1096 (unless (eq (car method) 'nnmairix)
1097 (error "This is not a nnmairix group"))
1098 (when (y-or-n-p
1099 (format "Really recreate group %s on the back end? " group))
1100 (nnmairix-delete-recreate-group group)
1101 (gnus-group-get-new-news-this-group))))
1103 (defun nnmairix-propagate-marks (&optional server)
1104 "Propagate marks from nnmairix group to original articles.
1105 Unless SERVER is explicitly specified, will use the last opened
1106 nnmairix server. Only marks from current session will be set."
1107 (interactive)
1108 (if server
1109 (nnmairix-open-server server)
1110 (unless (eq (car gnus-current-select-method) 'nnmairix)
1111 (if nnmairix-current-server
1112 (nnmairix-open-server nnmairix-current-server)
1113 (error "No opened nnmairix server"))))
1114 (if nnmairix-marks-cache
1115 (let (number ogroup number-cache method mid-marks temp)
1116 ;; first we get the article numbers
1117 (catch 'problem
1118 (while (setq ogroup (pop nnmairix-marks-cache))
1119 (while (setq mid-marks (pop (cdr ogroup)))
1120 (setq number
1121 (cdr
1122 (gnus-request-head (car mid-marks) (car ogroup))))
1123 (unless number
1124 (nnheader-message
1125 3 "Unable to set mark: couldn't determine article number for %s in %s"
1126 (car mid-marks) (car ogroup))
1127 (throw 'problem nil))
1128 (setq temp (assoc (car ogroup) number-cache))
1129 (if temp
1130 (catch 'done
1131 (dolist (cur (cdr temp))
1132 (when (equal (cdr cur) (list (nth 1 mid-marks) (nth 2 mid-marks)))
1133 (nconc (car cur) (list number))
1134 (throw 'done nil)))
1135 (nconc temp (list (list (list number) (nth 1 mid-marks) (nth 2 mid-marks)))))
1136 (push (list (car ogroup) (list (list number) (nth 1 mid-marks) (nth 2 mid-marks)))
1137 number-cache)))))
1138 ;; now we set the marks
1139 (with-current-buffer gnus-group-buffer
1140 (nnheader-message 5 "nnmairix: Propagating marks...")
1141 (dolist (cur number-cache)
1142 (setq method (gnus-find-method-for-group (car cur)))
1143 (apply (intern (format "%s-%s"
1144 (symbol-name (car method))
1145 "request-set-mark"))
1146 (gnus-group-short-name (car cur))
1147 (cdr cur)
1148 (list (nth 1 method)))
1149 (gnus-group-jump-to-group (car cur))
1150 (gnus-group-get-new-news-this-group)))
1151 (nnheader-message 5 "nnmairix: Propagating marks... done"))
1152 (nnheader-message 3 "No marks to propagate.")))
1154 (defun nnmairix-update-groups (servername &optional skipdefault updatedb)
1155 "Update all search groups on SERVERNAME.
1156 If SKIPDEFAULT is t, the default search group will not be
1157 updated.
1158 If UPDATEDB is t, database for SERVERNAME will be updated first."
1159 (interactive (list (gnus-completing-read "Update groups on server"
1160 (nnmairix-get-nnmairix-servers))))
1161 (save-excursion
1162 (when (string-match ".*:\\(.*\\)" servername)
1163 (setq servername (match-string 1 servername)))
1164 (if (not (assoc (format "nnmairix:%s" servername)
1165 (nnmairix-get-nnmairix-servers)))
1166 (nnheader-message 3 "Server %s not opened" servername)
1167 (when updatedb
1168 (let ((nnmairix-mairix-synchronous-update t))
1169 (nnmairix-update-database
1170 (list (list (format "nnmairix:%s" servername))))))
1171 (let ((groups (nnmairix-get-groups-from-server servername))
1172 default)
1173 (when skipdefault
1174 (setq default
1175 (format "nnmairix+%s:%s"
1176 servername
1177 (cadr
1178 (assoc 'nnmairix-default-group
1179 (gnus-server-to-method
1180 (format "nnmairix:%s" servername)))))))
1181 (dolist (cur groups)
1182 (unless (and skipdefault
1183 (string= (car cur) default))
1184 (gnus-group-jump-to-group (car cur))
1185 (gnus-group-mark-group 1)))
1186 (gnus-group-get-new-news-this-group)))))
1188 (defun nnmairix-remove-tick-mark-original-article ()
1189 "Remove tick mark from original article.
1190 Marks propagation has to be enabled for this to work."
1191 (interactive)
1192 (unless (eq (car gnus-current-select-method) 'nnmairix)
1193 (error "Not in a nnmairix group"))
1194 (save-excursion
1195 (let ((mid (mail-header-message-id (gnus-summary-article-header)))
1196 groups cur)
1197 (when mid
1198 (setq groups (nnmairix-determine-original-group-from-registry mid))
1199 (unless (or groups
1200 nnmairix-only-use-registry)
1201 (setq groups
1202 (nnmairix-determine-original-group-from-path mid nnmairix-current-server)))
1203 (unless groups
1204 (error "Couldn't find original article"))
1205 (dolist (cur groups)
1206 (push `(,cur (,mid del (tick))) nnmairix-marks-cache))
1207 (nnheader-message 5 "Will remove tick mark for %s upon closing." mid)))))
1209 ;;; ==== Helper functions
1211 (defun nnmairix-request-group-with-article-number-correction (folder qualgroup)
1212 "Request FOLDER on back end for nnmairix QUALGROUP and article number correction."
1213 (save-excursion
1214 (nnmairix-call-backend "request-group" folder nnmairix-backend-server)
1215 (set-buffer nnmairix-mairix-output-buffer)
1216 (goto-char (point-min))
1217 (re-search-forward "^Matched.*messages")
1218 (nnheader-message 7 (match-string 0))
1219 (set-buffer nntp-server-buffer)
1220 (goto-char (point-min))
1221 (let ((status (read (current-buffer)))
1222 (total (read (current-buffer)))
1223 (low (read (current-buffer)))
1224 (high (read (current-buffer)))
1225 (corr (gnus-group-get-parameter qualgroup 'numcorr t)))
1226 (if (= status 211)
1227 (progn
1228 ;; Article number correction
1229 (if (and corr
1230 (> (+ (car (cddr corr)) high) 0))
1231 (progn
1232 (when (car corr) ;Group has changed
1233 (setq corr
1234 (list nil
1235 (car (cddr corr))
1236 (+ (car (cddr corr)) high)))
1237 (gnus-group-set-parameter
1238 qualgroup 'numcorr corr))
1239 (setq low (+ low (cadr corr))
1240 high (+ high (cadr corr))))
1241 (when (member nnmairix-backend
1242 nnmairix-delete-and-create-on-change)
1243 (gnus-group-set-parameter
1244 qualgroup 'numcorr (list nil 0 high))))
1245 (erase-buffer)
1246 (insert (format "%d %d %d %d %s" status total low high
1247 (gnus-group-real-name qualgroup)))
1249 (progn
1250 (nnheader-report
1251 'nnmairix "Error calling back end on group %s" folder)
1252 nil)))))
1254 (defun nnmairix-call-mairix-binary (command folder searchquery threads)
1255 "Call mairix binary with COMMAND, using FOLDER and SEARCHQUERY.
1256 If THREADS is non-nil, enable full threads."
1257 (let ((args (cons (car command) '(nil t nil))))
1258 (with-current-buffer
1259 (get-buffer-create nnmairix-mairix-output-buffer)
1260 (erase-buffer)
1261 (when (> (length command) 1)
1262 (setq args (append args (cdr command))))
1263 (when nnmairix-mairix-search-options
1264 (setq args (append args nnmairix-mairix-search-options)))
1265 ;; If we have a patched mairix binary, call it with "-c"
1266 (when (nnmairix-check-mairix-version "maildirpatch")
1267 (setq args (append args '("-c"))))
1268 (when threads
1269 (setq args (append args '("-t"))))
1270 (apply 'call-process
1271 (append args (list "-o" folder) searchquery)))))
1273 (defun nnmairix-call-mairix-binary-raw (command query)
1274 "Call mairix binary with COMMAND and QUERY in raw mode."
1275 (let ((args (cons (car command) '(nil t nil))))
1276 (with-current-buffer
1277 (get-buffer-create nnmairix-mairix-output-buffer)
1278 (erase-buffer)
1279 (when (> (length command) 1)
1280 (setq args (append args (cdr command))))
1281 (setq args (append args '("-r")))
1282 (apply 'call-process
1283 (append args query)))))
1285 (defun nnmairix-get-server ()
1286 "If there exists just one nnmairix server, return its value.
1287 Otherwise, ask user for server."
1288 (let ((openedserver (nnmairix-get-nnmairix-servers)))
1289 (when (not openedserver)
1290 (error "No opened nnmairix server found"))
1291 (if (> (length openedserver) 1)
1292 (progn
1293 (while
1294 (equal '("")
1295 (setq nnmairix-last-server
1296 (list (gnus-completing-read "Server" openedserver t
1297 (or nnmairix-last-server
1298 "nnmairix:"))))))
1299 nnmairix-last-server)
1300 (car openedserver))))
1302 (defun nnmairix-get-nnmairix-servers (&optional all)
1303 "Return available nnmairix servers.
1304 If ALL is t, return also the unopened/failed ones."
1305 (let ((alist gnus-opened-servers)
1306 server openedserver)
1307 (while alist
1308 (setq server (pop alist))
1309 (when (and server
1310 (or all
1311 (eq (cadr server) 'ok))
1312 (eq (caar server) 'nnmairix)
1313 (not (member (car server) gnus-ephemeral-servers)))
1314 (setq server
1315 (concat (symbol-name (caar server)) ":" (nth 1 (car server))))
1316 (push (list server) openedserver)))
1317 openedserver))
1319 (defun nnmairix-get-valid-servers ()
1320 "Return list of valid back end servers for nnmairix groups."
1321 (let ((alist gnus-opened-servers)
1322 (mairixservers (nnmairix-get-nnmairix-servers t))
1323 server mserver openedserver occ cur)
1324 ;; Get list of all nnmairix backends (i.e. backends which are
1325 ;; already occupied)
1326 (dolist (cur mairixservers)
1327 (push
1328 (concat
1329 (symbol-name
1330 (cadr (assoc 'nnmairix-backend
1331 (gnus-server-to-method (car cur)))))
1333 (cadr (assoc 'nnmairix-backend-server
1334 (gnus-server-to-method (car cur)))))
1335 occ))
1336 (while alist
1337 (setq server (pop alist))
1338 (setq mserver (gnus-method-to-server (car server)))
1339 ;; If this is the native server, convert it to the real server
1340 ;; name to avoid confusion
1341 (when (string= mserver "native")
1342 (setq mserver (format "%s:%s"
1343 (caar server)
1344 (nth 1 (car server)))))
1345 (when (and server
1346 (eq (cadr server) 'ok)
1347 (member (caar server) nnmairix-valid-backends)
1348 (not (member (car server) gnus-ephemeral-servers))
1349 (not (member (gnus-method-to-server (car server)) occ)))
1350 (push
1351 mserver
1352 openedserver)))
1353 openedserver))
1355 (defun nnmairix-get-groups-from-server (servername)
1356 "Return all groups for nnmairix server SERVERNAME."
1357 (let ((searchstring (format "nnmairix\\+%s:" servername))
1358 groups)
1359 (dolist (cur gnus-newsrc-alist)
1360 (when (string-match searchstring
1361 (car cur))
1362 (push (list (car cur)) groups)))
1363 groups))
1365 (defun nnmairix-call-backend (func &rest args)
1366 "Call a function FUNC on backend with ARGS."
1367 (apply (intern (format "%s-%s" (symbol-name nnmairix-backend) func)) args))
1369 (defun nnmairix-get-backend-folder (group &optional server)
1370 "Return back end GROUP from nnmairix group on SERVER."
1371 (let* ((qualgroup (if server
1372 (gnus-group-prefixed-name group (list 'nnmairix server))
1373 group))
1374 (folder (gnus-group-get-parameter qualgroup 'folder)))
1375 folder))
1377 (defun nnmairix-get-numcorr (group &optional server)
1378 "Return values for article number correction nnmairix GROUP on SERVER."
1379 (let* ((qualgroup (if server
1380 (gnus-group-prefixed-name group (list 'nnmairix server))
1381 group))
1382 (corr (gnus-group-get-parameter qualgroup 'numcorr t)))
1383 corr))
1386 (defun nnmairix-rename-files-consecutively (path)
1387 "Rename all nnml mail files in PATH so that they have consecutive numbers.
1388 This should correct problems of wrong article counts when using
1389 nnmairix with nnml backends."
1390 (let* ((files
1391 (sort
1392 (mapcar 'string-to-number
1393 (directory-files path nil "[0-9]+" t))
1394 '<))
1395 (lastplusone (car files))
1396 (path (file-name-as-directory path)))
1397 (dolist (cur files)
1398 (when (not (= cur lastplusone))
1399 (rename-file (concat path
1400 (number-to-string cur))
1401 (concat path
1402 (number-to-string lastplusone)))
1403 (setq cur lastplusone))
1404 (setq lastplusone (1+ cur)))))
1406 (defun nnmairix-replace-group-and-numbers (articles backendgroup mairixgroup numc type)
1407 "Replace folder names in Xref header and correct article numbers.
1408 Do this for all ARTICLES on BACKENDGROUP. Replace using
1409 MAIRIXGROUP. NUMC contains values for article number correction.
1410 TYPE is either 'nov or 'headers."
1411 (nnheader-message 7 "nnmairix: Rewriting headers...")
1412 (cond
1413 ((eq type 'nov)
1414 (let ((buf (get-buffer-create " *nnmairix buffer*"))
1415 (corr (not (zerop numc)))
1416 (name (buffer-name nntp-server-buffer))
1417 header cur xref)
1418 (with-current-buffer buf
1419 (erase-buffer)
1420 (set-buffer nntp-server-buffer)
1421 (goto-char (point-min))
1422 (mapc
1423 (lambda (article)
1424 (when (or (looking-at (number-to-string article))
1425 (nnheader-find-nov-line article))
1426 (setq cur (nnheader-parse-nov))
1427 (when corr
1428 (setq article (+ (mail-header-number cur) numc))
1429 (mail-header-set-number cur article))
1430 (setq xref (mail-header-xref cur))
1431 (when (and (stringp xref)
1432 (string-match (format "[ \t]%s:[0-9]+" backendgroup) xref))
1433 (setq xref (replace-match (format " %s:%d" mairixgroup article) t nil xref))
1434 (mail-header-set-xref cur xref))
1435 (set-buffer buf)
1436 (nnheader-insert-nov cur)
1437 (set-buffer nntp-server-buffer)
1438 (when (not (eobp))
1439 (forward-line 1))))
1440 articles)
1441 (kill-buffer nntp-server-buffer)
1442 (set-buffer buf)
1443 (rename-buffer name)
1444 (setq nntp-server-buffer buf))))
1445 ((and (eq type 'headers)
1446 (not (zerop numc)))
1447 (with-current-buffer nntp-server-buffer
1448 (save-excursion
1449 (goto-char (point-min))
1450 (while (re-search-forward "^[23][0-9]+ \\([0-9]+\\)" nil t)
1451 (replace-match (number-to-string
1452 (+ (string-to-number (match-string 1)) numc))
1453 t t nil 1))))))
1454 (nnheader-message 7 "nnmairix: Rewriting headers... done"))
1456 (defun nnmairix-backend-to-server (server)
1457 "Return nnmairix server most probably responsible for back end SERVER.
1458 User will be asked if this cannot be determined. Result is saved in
1459 parameter 'indexed-servers of corresponding default search
1460 group."
1461 (let ((allservers (nnmairix-get-nnmairix-servers))
1462 mairixserver found defaultgroup)
1463 (if (> (length allservers) 1)
1464 (progn
1465 ;; If there is more than one nnmairix server, we go through them
1466 (while (and allservers (not found))
1467 (setq mairixserver (gnus-server-to-method (car (pop allservers))))
1468 ;; First we look if SERVER is the backend of current nnmairix server
1469 (setq found (and (eq (cadr (assoc 'nnmairix-backend mairixserver))
1470 (car server))
1471 (string= (cadr (assoc 'nnmairix-backend-server mairixserver))
1472 (nth 1 server))))
1473 ;; If that's not the case, we look at 'indexed-servers
1474 ;; variable in default search group
1475 (when (not found)
1476 (setq defaultgroup (cadr (assoc 'nnmairix-default-group mairixserver)))
1477 (setq found (member (gnus-method-to-server server)
1478 (gnus-group-get-parameter
1479 (gnus-group-prefixed-name defaultgroup
1480 mairixserver)
1481 'indexed-servers t)))))
1482 ;; If still not found, we ask user
1483 (when (not found)
1484 (setq mairixserver
1485 (gnus-server-to-method
1486 (gnus-completing-read
1487 (format "Cannot determine which nnmairix server indexes %s. Please specify"
1488 (gnus-method-to-server server))
1489 (nnmairix-get-nnmairix-servers) nil "nnmairix:")))
1490 ;; Save result in parameter of default search group so that
1491 ;; we don't have to ask again
1492 (setq defaultgroup (gnus-group-prefixed-name
1493 (cadr (assoc 'nnmairix-default-group mairixserver)) mairixserver))
1494 (gnus-group-set-parameter
1495 defaultgroup
1496 'indexed-servers
1497 (append (gnus-group-get-parameter defaultgroup 'indexed-servers t)
1498 (list (gnus-method-to-server server)))))
1499 mairixserver)
1500 ;; If there is just one (or none) nnmairix server:
1501 (gnus-server-to-method (caar allservers)))))
1504 (defun nnmairix-delete-recreate-group (group)
1505 "Delete and recreate folder from GROUP on the back end."
1506 (when (member nnmairix-backend nnmairix-delete-and-create-on-change)
1507 (let ((folder (gnus-group-get-parameter group 'folder)))
1508 (if (string-match nnmairix-group-regexp folder)
1509 (progn
1510 (nnmairix-call-backend "open-server"
1511 nnmairix-backend-server)
1512 (nnmairix-call-backend "request-delete-group"
1513 folder t nnmairix-backend-server)
1514 (nnmairix-call-backend "request-create-group"
1515 folder nnmairix-backend-server))
1516 (error "`nnmairix-delete-recreate-group' called on \
1517 non-mairix group. Check folder parameter")))))
1519 (defun nnmairix-update-and-clear-marks (group &optional method)
1520 "Update group and clear all marks from GROUP using METHOD."
1521 (let ((method (or method
1522 (gnus-find-method-for-group group)))
1523 (corr (gnus-group-get-parameter group 'numcorr t))
1524 info)
1525 (unless (or (gnus-group-prefixed-p group)
1526 (not method))
1527 (setq group (gnus-group-prefixed-name group method)))
1528 (if (eq (nth 0 method) 'nnmairix)
1529 (save-excursion
1530 (nnmairix-open-server (nth 1 method))
1531 (set-buffer gnus-group-buffer)
1532 ;; (gnus-group-set-parameter group 'propmarks nil)
1533 (setq info (gnus-get-info group))
1534 ;; Clear active and info
1535 (gnus-set-active group nil)
1536 (gnus-info-clear-data info)
1537 ;; Delete and re-create group if needed
1538 (nnmairix-delete-recreate-group group)
1539 ;; set flag that group has changed for article number correction
1540 (when (member nnmairix-backend nnmairix-delete-and-create-on-change)
1541 (when corr
1542 (setcar corr t)
1543 (gnus-group-set-parameter group 'numcorr corr)))
1544 (gnus-group-jump-to-group group)
1545 (gnus-group-get-new-news-this-group))
1546 (error "`nnmairix-update-and-clear-marks' called with non-nnmairix group"))))
1548 (defun nnmairix-sentinel-mairix-update-finished (proc status)
1549 "Sentinel for mairix update process PROC with STATUS."
1550 (if (equal status "finished\n")
1551 (nnheader-message 7 "Updating mairix database for %s... done" proc)
1552 (error "There was an error updating the mairix database for server %s. \
1553 See %s for details" proc nnmairix-mairix-output-buffer)))
1555 (defun nnmairix-create-message-line-for-search ()
1556 "Create message line for interactive query in minibuffer."
1557 (mapconcat
1558 (function
1559 (lambda (cur)
1560 (format "%c=%s" (car cur) (nth 3 cur))))
1561 nnmairix-interactive-query-parameters ","))
1563 (defun nnmairix-replace-illegal-chars (header)
1564 "Replace illegal characters in HEADER for mairix query."
1565 (when header
1566 (while (string-match "[^-.@/,& [:alnum:]]" header)
1567 (setq header (replace-match "" t t header)))
1568 (while (string-match "[-& ]" header)
1569 (setq header (replace-match "," t t header)))
1570 header))
1572 (defun nnmairix-group-toggle-parameter (group parameter description &optional par)
1573 "Toggle on GROUP a certain PARAMETER.
1574 DESCRIPTION will be shown to the user with the activation
1575 status. If PAR is a positive number, the group parameter will be
1576 set to t and to nil otherwise."
1577 (let* ((method (gnus-find-method-for-group group))
1578 (par (or par
1579 (not (gnus-group-get-parameter group parameter)))))
1580 (if (eq (car method) 'nnmairix)
1581 (progn
1582 (when (numberp par)
1583 (setq par (> par 0)))
1584 (gnus-group-set-parameter group parameter par)
1585 (if par
1586 (message "%s activated for group %s" description group)
1587 (message "%s deactivated for group %s" description group))
1589 (error "This is no nnmairix group")
1590 nil)))
1592 ;; Search for original article helper functions
1594 (defun nnmairix-goto-original-article (&optional no-registry)
1595 "Jump to the original group and display article.
1596 The original group of the article is first determined with the
1597 registry (if enabled). If the registry is not enabled or did not
1598 find the article or the prefix NO-REGISTRY is non-nil, this
1599 function will try to determine the original group form the path
1600 of the mail file. The path is obtained through another mairix
1601 search in raw mode."
1602 (interactive "P")
1603 (when (not (eq (car gnus-current-select-method) 'nnmairix))
1604 (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
1605 (if (eq (car method) 'nnmairix)
1606 (nnmairix-open-server (nth 1 method))
1607 (error "Not in a nnmairix group"))))
1608 (when (not (gnus-buffer-live-p gnus-article-buffer))
1609 (error "No article buffer available"))
1610 (let ((server (nth 1 gnus-current-select-method))
1611 mid rval group allgroups)
1612 ;; get message id
1613 (with-current-buffer gnus-article-buffer
1614 (gnus-summary-toggle-header 1)
1615 (setq mid (message-fetch-field "Message-ID"))
1616 ;; first check the registry (if available)
1617 (unless no-registry
1618 (setq allgroups (nnmairix-determine-original-group-from-registry mid)))
1619 (unless (or allgroups
1620 nnmairix-only-use-registry)
1621 ;; registry was not available or did not find article
1622 ;; so we search again with mairix in raw mode to get filename
1623 (setq allgroups
1624 (nnmairix-determine-original-group-from-path mid server)))
1625 (if (> (length allgroups) 1)
1626 (setq group
1627 (gnus-completing-read
1628 "Message exists in more than one group. Choose"
1629 allgroups t))
1630 (setq group (car allgroups))))
1631 (if group
1632 ;; show article in summary buffer
1633 (nnmairix-show-original-article group mid)
1634 (nnheader-message 3 "Couldn't find original article"))))
1636 (defun nnmairix-determine-original-group-from-registry (mid)
1637 "Try to determine original group for message-id MID from the registry."
1638 (when (gnus-bound-and-true-p 'gnus-registry-enabled)
1639 (unless (string-match "^<" mid)
1640 (set mid (concat "<" mid)))
1641 (unless (string-match ">$" mid)
1642 (set mid (concat mid ">")))
1643 (gnus-registry-get-id-key mid 'group)))
1645 (defun nnmairix-determine-original-group-from-path (mid server)
1646 "Determine original group(s) for message-id MID from the file path.
1647 The file path is obtained through a mairix search for the id on
1648 SERVER."
1649 (nnmairix-open-server server)
1650 (while (string-match "[<>]" mid)
1651 (setq mid (replace-match "" t t mid)))
1652 ;; mairix somehow does not like '$' in message-id
1653 (when (string-match "\\$" mid)
1654 (setq mid (concat mid "=")))
1655 (while (string-match "\\$" mid)
1656 (setq mid (replace-match "=," t t mid)))
1657 (let (allgroups)
1658 (if (zerop (nnmairix-call-mairix-binary-raw
1659 (split-string nnmairix-mairix-command)
1660 (list (concat "m:" mid))))
1661 (with-current-buffer nnmairix-mairix-output-buffer
1662 (goto-char (point-min))
1663 (while (re-search-forward "^/.*$" nil t)
1664 (push (nnmairix-get-group-from-file-path (match-string 0))
1665 allgroups)
1666 (forward-line 1)))
1667 (error "Mairix could not find original article. See buffer %s for details"
1668 nnmairix-mairix-output-buffer))
1669 allgroups))
1671 (defun nnmairix-get-group-from-file-path (file)
1672 "Get group by parsing the message location FILE."
1673 (let (path filename serverbase group maildirflag allgroups)
1674 (string-match "^\\(.*\\)/\\(.*?\\)$" file)
1675 (setq path (expand-file-name (match-string 1 file)))
1676 (setq filename (match-string 2 file))
1677 ;; when we deal with maildir, remove cur/new/tmp from path
1678 (setq maildirflag (string-match ".+\\..+\\..+" filename))
1679 (when maildirflag
1680 (setq path
1681 (replace-regexp-in-string
1682 ".*\\(/cur\\|/new\\|/tmp\\)$" "" path t t 1)))
1683 ;; we first check nnml and nnmaildir servers
1684 (setq
1685 group
1686 (catch 'found
1687 (dolist (cur gnus-opened-servers)
1688 (when (or (and (not maildirflag)
1689 (eq (caar cur) 'nnml))
1690 (and maildirflag
1691 (eq (caar cur) 'nnmaildir)))
1692 ;; get base path from server
1693 (if maildirflag
1694 (setq serverbase (cadr (assoc 'directory (car cur))))
1695 (setq serverbase (cadr (assoc 'nnml-directory (car cur))))
1696 (unless serverbase
1697 (setq serverbase nnml-directory)))
1698 (setq serverbase (file-name-as-directory
1699 (expand-file-name serverbase)))
1700 (when (string-match (concat serverbase "\\(.*\\)") path)
1701 ;; looks good - rest of the path should be the group
1702 (setq group (match-string 1 path))
1703 (when (string-match "/$" group)
1704 (setq group (replace-match "" t t group)))
1705 (unless maildirflag
1706 ;; for nnml: convert slashes to dots
1707 (while (string-match "/" group)
1708 (setq group (replace-match "." t t group))))
1709 (setq group (gnus-group-prefixed-name group (car cur)))
1710 ;; check whether this group actually exists
1711 (when (gnus-group-entry group)
1712 (throw 'found group)))))))
1713 (unless group
1714 ;; we haven't found it yet --> look for nnimap groups. Assume
1715 ;; last element of the path is the group. This might fail since
1716 ;; IMAP servers may present groups to the client in arbitrary
1717 ;; ways...
1718 (string-match "^.*/\\.?\\(.*\\)$" path)
1719 (setq group (match-string 1 path))
1720 ;; convert dots to slashes (nested group)
1721 (while (string-match "\\." group)
1722 (setq group (replace-match "/" t t group)))
1723 (dolist (cur gnus-opened-servers)
1724 (when (eq (caar cur) 'nnimap)
1725 (when (gnus-group-entry
1726 (gnus-group-prefixed-name group (car cur)))
1727 (push
1728 (gnus-group-prefixed-name group (car cur))
1729 allgroups))))
1730 (if (> (length allgroups) 1)
1731 (setq group (gnus-completing-read
1732 "Group %s exists on more than one IMAP server. Choose"
1733 allgroups t))
1734 (setq group (car allgroups))))
1735 group))
1737 (defun nnmairix-show-original-article (group mid)
1738 "Switch to GROUP and display Article with message-id MID."
1739 (unless (string-match "^<" mid)
1740 (set mid (concat "<" mid)))
1741 (unless (string-match ">$" mid)
1742 (set mid (concat mid ">")))
1743 (when (string-match "Summary" (buffer-name (current-buffer)))
1744 (gnus-summary-exit))
1745 (pop-to-buffer gnus-group-buffer)
1746 (gnus-group-jump-to-group group)
1747 (gnus-summary-read-group group 1 t)
1748 (gnus-summary-refer-article mid)
1749 (gnus-summary-limit-to-headers (format "message-id: %s" mid))
1750 (gnus-summary-select-article)
1751 ;; Force redisplay
1752 (gnus-summary-show-article)
1753 (nnheader-message 5 "Switched to group %s." group))
1755 (defun nnmairix-map-range (func range)
1756 "Map function FUNC on all members of RANGE."
1757 (cond
1758 ((numberp range)
1759 (funcall func range))
1761 (mapcar (lambda (cur)
1762 (cond
1763 ((listp cur)
1764 (cons
1765 (funcall func (car cur))
1766 (funcall func (cdr cur))))
1767 ((numberp cur)
1768 (funcall func cur))))
1769 range))))
1771 (defun nnmairix-check-mairix-version (version &optional server)
1772 "Check mairix VERSION on SERVER.
1773 If VERSION is a number: specifies the minimum version.
1774 If VERSION is a string: must be contained in mairix version output."
1775 (unless server
1776 (setq server nnmairix-current-server))
1777 (let ((versionstring (cadr (assoc server nnmairix-version-output))))
1778 (unless versionstring
1779 ;; call "mairix -V" to get the version string
1780 (with-temp-buffer
1781 (setq versionstring
1782 (let* ((commandsplit (split-string nnmairix-mairix-command))
1783 (args (append (list (car commandsplit))
1784 `(nil t nil) (cdr commandsplit) '("-V"))))
1785 (apply 'call-process args)
1786 (goto-char (point-min))
1787 (re-search-forward "mairix.*")
1788 (match-string 0))))
1789 ;; save version string for current session
1790 (setq nnmairix-version-output
1791 (append nnmairix-version-output
1792 (list (list server versionstring)))))
1793 (cond
1794 ((stringp version)
1795 (string-match version versionstring))
1796 ((numberp version)
1797 (<= version (string-to-number
1798 (progn
1799 (string-match "mairix \\([0-9\\.]+\\)" versionstring)
1800 (match-string 1 versionstring))))))))
1802 ;; ==== Widget stuff
1804 (defvar nnmairix-widgets)
1805 (defvar nnmairix-widgets-values nil)
1807 (defun nnmairix-widget-search-from-this-article ()
1808 "Create mairix query based on current article using graphical widgets."
1809 (interactive)
1810 (nnmairix-widget-search
1811 (nnmairix-widget-get-values)))
1813 (defun nnmairix-widget-get-values ()
1814 "Create values for editable fields from current article."
1815 (if (not (gnus-buffer-live-p gnus-article-buffer))
1816 (error "No article buffer available")
1817 (save-excursion
1818 (gnus-summary-toggle-header 1)
1819 (set-buffer gnus-article-buffer)
1820 (mapcar
1821 (function
1822 (lambda (field)
1823 (list (car (cddr field))
1824 (if (car field)
1825 (nnmairix-replace-illegal-chars
1826 (gnus-fetch-field (car field)))
1827 nil))))
1828 nnmairix-widget-fields-list))))
1831 (defun nnmairix-widget-search (&optional mvalues)
1832 "Create mairix query interactively using graphical widgets.
1833 MVALUES may contain values from current article."
1834 (interactive)
1835 ;; Select window for mairix customization
1836 (funcall nnmairix-widget-select-window-function)
1837 ;; generate widgets
1838 (nnmairix-widget-create-query mvalues)
1839 ;; generate Buttons
1840 (widget-create 'push-button
1841 :notify
1842 (if mvalues
1843 (lambda (&rest ignore)
1844 (nnmairix-widget-send-query nnmairix-widgets
1846 (lambda (&rest ignore)
1847 (nnmairix-widget-send-query nnmairix-widgets
1848 nil)))
1849 "Send Query")
1850 (widget-insert " ")
1851 (widget-create 'push-button
1852 :notify
1853 (if mvalues
1854 (lambda (&rest ignore)
1855 (nnmairix-widget-create-group nnmairix-widgets
1857 (lambda (&rest ignore)
1858 (nnmairix-widget-create-group nnmairix-widgets
1859 nil)))
1860 "Create permanent group")
1861 (widget-insert " ")
1862 (widget-create 'push-button
1863 :notify (lambda (&rest ignore)
1864 (kill-buffer nnmairix-customize-query-buffer))
1865 "Cancel")
1866 (use-local-map widget-keymap)
1867 (widget-setup)
1868 (goto-char (point-min)))
1870 (defun nnmairix-widget-send-query (widgets &optional withvalues)
1871 "Send query from WIDGETS to mairix binary.
1872 If WITHVALUES is t, query is based on current article."
1873 (nnmairix-search
1874 (nnmairix-widget-make-query-from-widgets widgets)
1875 (if withvalues
1876 (gnus-method-to-server
1877 (nnmairix-backend-to-server gnus-current-select-method))
1878 (car (nnmairix-get-server)))
1879 (if (widget-value (cadr (assoc "Threads" widgets)))
1881 -1))
1882 (kill-buffer nnmairix-customize-query-buffer))
1884 (defun nnmairix-widget-create-group (widgets &optional withvalues)
1885 "Create nnmairix group based on current widget values WIDGETS.
1886 If WITHVALUES is t, query is based on current article."
1887 (let ((group (read-string "Name of the group: ")))
1888 (when (not (zerop (length group)))
1889 (nnmairix-create-search-group
1890 (if withvalues
1891 (gnus-method-to-server
1892 (nnmairix-backend-to-server gnus-current-select-method))
1893 (car (nnmairix-get-server)))
1894 group
1895 (nnmairix-widget-make-query-from-widgets widgets)
1896 (widget-value (cadr (assoc "Threads" widgets))))))
1897 (kill-buffer nnmairix-customize-query-buffer))
1900 (defun nnmairix-widget-make-query-from-widgets (widgets)
1901 "Create mairix query from widget values WIDGETS."
1902 (let (query temp flag)
1903 ;; first we do the editable fields
1904 (dolist (cur nnmairix-widget-fields-list)
1905 ;; See if checkbox is checked
1906 (when (widget-value
1907 (cadr (assoc (concat "c" (car (cddr cur))) widgets)))
1908 ;; create query for the field
1909 (push
1910 (concat
1911 (nth 1 cur)
1913 (nnmairix-replace-illegal-chars
1914 (widget-value
1915 (cadr (assoc (concat "e" (car (cddr cur))) widgets)))))
1916 query)))
1917 ;; Flags
1918 (when (member 'flags nnmairix-widget-other)
1919 (setq flag
1920 (mapconcat
1921 (function
1922 (lambda (flag)
1923 (setq temp
1924 (widget-value (cadr (assoc (car flag) nnmairix-widgets))))
1925 (if (string= "yes" temp)
1926 (cadr flag)
1927 (if (string= "no" temp)
1928 (concat "-" (cadr flag))))))
1929 '(("seen" "s") ("replied" "r") ("flagged" "f")) ""))
1930 (when (not (zerop (length flag)))
1931 (push (concat "F:" flag) query)))
1932 ;; return query string
1933 (mapconcat 'identity query " ")))
1936 (defun nnmairix-widget-create-query (&optional values)
1937 "Create widgets for creating mairix queries.
1938 Fill in VALUES if based on an article."
1939 (let (allwidgets)
1940 (when (get-buffer nnmairix-customize-query-buffer)
1941 (kill-buffer nnmairix-customize-query-buffer))
1942 (switch-to-buffer nnmairix-customize-query-buffer)
1943 (kill-all-local-variables)
1944 (erase-buffer)
1945 (widget-insert "Specify your query for Mairix (check boxes for activating fields):\n\n")
1946 (widget-insert "(Whitespaces will be converted to ',' (i.e. AND). Use '/' for OR.)\n\n")
1947 ; (make-local-variable 'nnmairix-widgets)
1948 (setq nnmairix-widgets (nnmairix-widget-build-editable-fields values))
1949 (when (member 'flags nnmairix-widget-other)
1950 (widget-insert "\nFlags:\n Seen: ")
1951 (nnmairix-widget-add "seen"
1952 'menu-choice
1953 :value "ignore"
1954 '(item "yes") '(item "no") '(item "ignore"))
1955 (widget-insert " Replied: ")
1956 (nnmairix-widget-add "replied"
1957 'menu-choice
1958 :value "ignore"
1959 '(item "yes") '(item "no") '(item "ignore"))
1960 (widget-insert " Ticked: ")
1961 (nnmairix-widget-add "flagged"
1962 'menu-choice
1963 :value "ignore"
1964 '(item "yes") '(item "no") '(item "ignore")))
1965 (when (member 'threads nnmairix-widget-other)
1966 (widget-insert "\n")
1967 (nnmairix-widget-add "Threads" 'checkbox nil))
1968 (widget-insert " Show full threads\n\n")))
1970 (defun nnmairix-widget-build-editable-fields (values)
1971 "Build editable field widgets in `nnmairix-widget-fields-list'.
1972 VALUES may contain values for editable fields from current article."
1973 ;; how can this be done less ugly?
1974 (let ((ret))
1975 (mapc
1976 (function
1977 (lambda (field)
1978 (setq field (car (cddr field)))
1979 (setq ret
1980 (nconc
1981 (list
1982 (list
1983 (concat "c" field)
1984 (widget-create 'checkbox
1985 :tag field
1986 :notify (lambda (widget &rest ignore)
1987 (nnmairix-widget-toggle-activate widget))
1988 nil)))
1989 (list
1990 (list
1991 (concat "e" field)
1992 (widget-create 'editable-field
1993 :size 60
1994 :format (concat " " field ":"
1995 (make-string (- 11 (length field)) ?\ )
1996 "%v")
1997 :value (or (cadr (assoc field values)) ""))))
1998 ret))
1999 (widget-insert "\n")
2000 ;; Deactivate editable field
2001 (widget-apply (cadr (nth 1 ret)) :deactivate)))
2002 nnmairix-widget-fields-list)
2003 ret))
2005 (defun nnmairix-widget-add (name &rest args)
2006 "Add a widget NAME with optional ARGS."
2007 (push
2008 (list name
2009 (apply 'widget-create args))
2010 nnmairix-widgets))
2012 (defun nnmairix-widget-toggle-activate (widget)
2013 "Toggle activation status of WIDGET depending on corresponding checkbox value."
2014 (let ((field (widget-get widget :tag)))
2015 (if (widget-value widget)
2016 (widget-apply
2017 (cadr (assoc (concat "e" field) nnmairix-widgets))
2018 :activate)
2019 (widget-apply
2020 (cadr (assoc (concat "e" field) nnmairix-widgets))
2021 :deactivate)))
2022 (widget-setup))
2024 (provide 'nnmairix)
2026 ;;; nnmairix.el ends here