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