mm-bodies.el (mm-decode-content-transfer-encoding): Allow leading whitespace in base6...
[emacs.git] / lisp / gnus / gnus-srvr.el
blob9bf2d37a3e49a5ae187ae004c6d5877f31ba3899
1 ;;; gnus-srvr.el --- virtual server support for Gnus
3 ;; Copyright (C) 1995-2011 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (eval-when-compile (require 'cl))
29 (require 'gnus)
30 (require 'gnus-start)
31 (require 'gnus-spec)
32 (require 'gnus-group)
33 (require 'gnus-int)
34 (require 'gnus-range)
36 (autoload 'gnus-group-make-nnir-group "nnir")
38 (defcustom gnus-server-mode-hook nil
39 "Hook run in `gnus-server-mode' buffers."
40 :group 'gnus-server
41 :type 'hook)
43 (defcustom gnus-server-exit-hook nil
44 "Hook run when exiting the server buffer."
45 :group 'gnus-server
46 :type 'hook)
48 (defcustom gnus-server-line-format " {%(%h:%w%)} %s%a\n"
49 "Format of server lines.
50 It works along the same lines as a normal formatting string,
51 with some simple extensions.
53 The following specs are understood:
55 %h back end
56 %n name
57 %w address
58 %s status
59 %a agent covered
61 General format specifiers can also be used.
62 See Info node `(gnus)Formatting Variables'."
63 :link '(custom-manual "(gnus)Formatting Variables")
64 :group 'gnus-server-visual
65 :type 'string)
67 (defcustom gnus-server-mode-line-format "Gnus: %%b"
68 "The format specification for the server mode line."
69 :group 'gnus-server-visual
70 :type 'string)
72 (defcustom gnus-server-browse-in-group-buffer nil
73 "Whether server browsing should take place in the group buffer.
74 If nil, a faster, but more primitive, buffer is used instead."
75 :version "22.1"
76 :group 'gnus-server-visual
77 :type 'boolean)
79 ;;; Internal variables.
81 (defvar gnus-inserted-opened-servers nil)
83 (defvar gnus-server-line-format-alist
84 `((?h gnus-tmp-how ?s)
85 (?n gnus-tmp-name ?s)
86 (?w gnus-tmp-where ?s)
87 (?s gnus-tmp-status ?s)
88 (?a gnus-tmp-agent ?s)))
90 (defvar gnus-server-mode-line-format-alist
91 `((?S gnus-tmp-news-server ?s)
92 (?M gnus-tmp-news-method ?s)
93 (?u gnus-tmp-user-defined ?s)))
95 (defvar gnus-server-line-format-spec nil)
96 (defvar gnus-server-mode-line-format-spec nil)
97 (defvar gnus-server-killed-servers nil)
99 (defvar gnus-server-mode-map)
101 (defvar gnus-server-menu-hook nil
102 "*Hook run after the creation of the server mode menu.")
104 (defun gnus-server-make-menu-bar ()
105 (gnus-turn-off-edit-menu 'server)
106 (unless (boundp 'gnus-server-server-menu)
107 (easy-menu-define
108 gnus-server-server-menu gnus-server-mode-map ""
109 '("Server"
110 ["Add..." gnus-server-add-server t]
111 ["Browse" gnus-server-read-server t]
112 ["Scan" gnus-server-scan-server t]
113 ["List" gnus-server-list-servers t]
114 ["Kill" gnus-server-kill-server t]
115 ["Yank" gnus-server-yank-server t]
116 ["Copy" gnus-server-copy-server t]
117 ["Show" gnus-server-show-server t]
118 ["Edit" gnus-server-edit-server t]
119 ["Regenerate" gnus-server-regenerate-server t]
120 ["Compact" gnus-server-compact-server t]
121 ["Exit" gnus-server-exit t]))
123 (easy-menu-define
124 gnus-server-connections-menu gnus-server-mode-map ""
125 '("Connections"
126 ["Open" gnus-server-open-server t]
127 ["Close" gnus-server-close-server t]
128 ["Offline" gnus-server-offline-server t]
129 ["Deny" gnus-server-deny-server t]
130 "---"
131 ["Open All" gnus-server-open-all-servers t]
132 ["Close All" gnus-server-close-all-servers t]
133 ["Reset All" gnus-server-remove-denials t]))
135 (gnus-run-hooks 'gnus-server-menu-hook)))
137 (defvar gnus-server-mode-map nil)
138 (put 'gnus-server-mode 'mode-class 'special)
140 (unless gnus-server-mode-map
141 (setq gnus-server-mode-map (make-sparse-keymap))
142 (suppress-keymap gnus-server-mode-map)
144 (gnus-define-keys gnus-server-mode-map
145 " " gnus-server-read-server-in-server-buffer
146 "\r" gnus-server-read-server
147 gnus-mouse-2 gnus-server-pick-server
148 "q" gnus-server-exit
149 "l" gnus-server-list-servers
150 "k" gnus-server-kill-server
151 "y" gnus-server-yank-server
152 "c" gnus-server-copy-server
153 "a" gnus-server-add-server
154 "e" gnus-server-edit-server
155 "S" gnus-server-show-server
156 "s" gnus-server-scan-server
158 "O" gnus-server-open-server
159 "\M-o" gnus-server-open-all-servers
160 "C" gnus-server-close-server
161 "\M-c" gnus-server-close-all-servers
162 "D" gnus-server-deny-server
163 "L" gnus-server-offline-server
164 "R" gnus-server-remove-denials
166 "n" next-line
167 "p" previous-line
169 "g" gnus-server-regenerate-server
171 "G" gnus-group-make-nnir-group
173 "z" gnus-server-compact-server
175 "\C-c\C-i" gnus-info-find-node
176 "\C-c\C-b" gnus-bug))
178 (defface gnus-server-agent
179 '((((class color) (background light)) (:foreground "PaleTurquoise" :bold t))
180 (((class color) (background dark)) (:foreground "PaleTurquoise" :bold t))
181 (t (:bold t)))
182 "Face used for displaying AGENTIZED servers"
183 :group 'gnus-server-visual)
184 ;; backward-compatibility alias
185 (put 'gnus-server-agent-face 'face-alias 'gnus-server-agent)
186 (put 'gnus-server-agent-face 'obsolete-face "22.1")
188 (defface gnus-server-opened
189 '((((class color) (background light)) (:foreground "Green3" :bold t))
190 (((class color) (background dark)) (:foreground "Green1" :bold t))
191 (t (:bold t)))
192 "Face used for displaying OPENED servers"
193 :group 'gnus-server-visual)
194 ;; backward-compatibility alias
195 (put 'gnus-server-opened-face 'face-alias 'gnus-server-opened)
196 (put 'gnus-server-opened-face 'obsolete-face "22.1")
198 (defface gnus-server-closed
199 '((((class color) (background light)) (:foreground "Steel Blue" :italic t))
200 (((class color) (background dark))
201 (:foreground "LightBlue" :italic t))
202 (t (:italic t)))
203 "Face used for displaying CLOSED servers"
204 :group 'gnus-server-visual)
205 ;; backward-compatibility alias
206 (put 'gnus-server-closed-face 'face-alias 'gnus-server-closed)
207 (put 'gnus-server-closed-face 'obsolete-face "22.1")
209 (defface gnus-server-denied
210 '((((class color) (background light)) (:foreground "Red" :bold t))
211 (((class color) (background dark)) (:foreground "Pink" :bold t))
212 (t (:inverse-video t :bold t)))
213 "Face used for displaying DENIED servers"
214 :group 'gnus-server-visual)
215 ;; backward-compatibility alias
216 (put 'gnus-server-denied-face 'face-alias 'gnus-server-denied)
217 (put 'gnus-server-denied-face 'obsolete-face "22.1")
219 (defface gnus-server-offline
220 '((((class color) (background light)) (:foreground "Orange" :bold t))
221 (((class color) (background dark)) (:foreground "Yellow" :bold t))
222 (t (:inverse-video t :bold t)))
223 "Face used for displaying OFFLINE servers"
224 :group 'gnus-server-visual)
225 ;; backward-compatibility alias
226 (put 'gnus-server-offline-face 'face-alias 'gnus-server-offline)
227 (put 'gnus-server-offline-face 'obsolete-face "22.1")
229 (defvar gnus-server-font-lock-keywords
230 '(("(\\(agent\\))" 1 'gnus-server-agent)
231 ("(\\(opened\\))" 1 'gnus-server-opened)
232 ("(\\(closed\\))" 1 'gnus-server-closed)
233 ("(\\(offline\\))" 1 'gnus-server-offline)
234 ("(\\(denied\\))" 1 'gnus-server-denied)))
236 (defun gnus-server-mode ()
237 "Major mode for listing and editing servers.
239 All normal editing commands are switched off.
240 \\<gnus-server-mode-map>
241 For more in-depth information on this mode, read the manual
242 \(`\\[gnus-info-find-node]').
244 The following commands are available:
246 \\{gnus-server-mode-map}"
247 (interactive)
248 (when (gnus-visual-p 'server-menu 'menu)
249 (gnus-server-make-menu-bar))
250 (kill-all-local-variables)
251 (gnus-simplify-mode-line)
252 (setq major-mode 'gnus-server-mode)
253 (setq mode-name "Server")
254 (gnus-set-default-directory)
255 (setq mode-line-process nil)
256 (use-local-map gnus-server-mode-map)
257 (buffer-disable-undo)
258 (setq truncate-lines t)
259 (setq buffer-read-only t)
260 (if (featurep 'xemacs)
261 (put 'gnus-server-mode 'font-lock-defaults '(gnus-server-font-lock-keywords t))
262 (set (make-local-variable 'font-lock-defaults)
263 '(gnus-server-font-lock-keywords t)))
264 (gnus-run-mode-hooks 'gnus-server-mode-hook))
266 (defun gnus-server-insert-server-line (gnus-tmp-name method)
267 (let* ((gnus-tmp-how (car method))
268 (gnus-tmp-where (nth 1 method))
269 (elem (assoc method gnus-opened-servers))
270 (gnus-tmp-status
271 (cond
272 ((eq (nth 1 elem) 'denied) "(denied)")
273 ((eq (nth 1 elem) 'offline) "(offline)")
275 (condition-case nil
276 (if (or (gnus-server-opened method)
277 (eq (nth 1 elem) 'ok))
278 "(opened)"
279 "(closed)")
280 ((error) "(error)")))))
281 (gnus-tmp-agent (if (and gnus-agent
282 (gnus-agent-method-p method))
283 " (agent)"
284 "")))
285 (beginning-of-line)
286 (gnus-add-text-properties
287 (point)
288 (prog1 (1+ (point))
289 ;; Insert the text.
290 (eval gnus-server-line-format-spec))
291 (list 'gnus-server (intern gnus-tmp-name)
292 'gnus-named-server (intern (gnus-method-to-server method t))))))
294 (defun gnus-enter-server-buffer ()
295 "Set up the server buffer."
296 (gnus-server-setup-buffer)
297 (gnus-configure-windows 'server)
298 ;; Usually `gnus-configure-windows' will finish with the
299 ;; `gnus-server-buffer' selected as the current buffer, but not always (I
300 ;; bumped into it when starting from a dedicated *Group* frame, and
301 ;; gnus-configure-windows opened *Server* into its own dedicated frame).
302 (with-current-buffer (get-buffer gnus-server-buffer)
303 (gnus-server-prepare)))
305 (defun gnus-server-setup-buffer ()
306 "Initialize the server buffer."
307 (unless (get-buffer gnus-server-buffer)
308 (with-current-buffer (gnus-get-buffer-create gnus-server-buffer)
309 (gnus-server-mode))))
311 (defun gnus-server-prepare ()
312 (gnus-set-format 'server-mode)
313 (gnus-set-format 'server t)
314 (let ((alist gnus-server-alist)
315 (buffer-read-only nil)
316 done server op-ser)
317 (erase-buffer)
318 (setq gnus-inserted-opened-servers nil)
319 ;; First we do the real list of servers.
320 (while alist
321 (unless (member (cdar alist) done)
322 (push (cdar alist) done)
323 (setq server (pop alist))
324 (when (and server (car server) (cdr server))
325 (gnus-server-insert-server-line (car server) (cdr server))))
326 (when (member (cdar alist) done)
327 (pop alist)))
328 ;; Then we insert the list of servers that have been opened in
329 ;; this session.
330 (dolist (open gnus-opened-servers)
331 (when (and (not (member (car open) done))
332 ;; Just ignore ephemeral servers.
333 (not (member (car open) gnus-ephemeral-servers)))
334 (push (car open) done)
335 (gnus-server-insert-server-line
336 (setq op-ser (format "%s:%s" (caar open) (nth 1 (car open))))
337 (car open))
338 (push (list op-ser (car open)) gnus-inserted-opened-servers))))
339 (goto-char (point-min))
340 (gnus-server-position-point))
342 (defun gnus-server-server-name ()
343 (let ((server (get-text-property (point-at-bol) 'gnus-server)))
344 (and server (symbol-name server))))
346 (defun gnus-server-named-server ()
347 "Return a server name that matches one of the names returned by
348 `gnus-method-to-server'."
349 (let ((server (get-text-property (point-at-bol) 'gnus-named-server)))
350 (and server (symbol-name server))))
352 (defalias 'gnus-server-position-point 'gnus-goto-colon)
354 (defconst gnus-server-edit-buffer "*Gnus edit server*")
356 (defun gnus-server-update-server (server)
357 (with-current-buffer gnus-server-buffer
358 (let* ((buffer-read-only nil)
359 (entry (assoc server gnus-server-alist))
360 (oentry (assoc (gnus-server-to-method server)
361 gnus-opened-servers)))
362 (when entry
363 (gnus-dribble-enter
364 (concat "(gnus-server-set-info \"" server "\" '"
365 (gnus-prin1-to-string (cdr entry)) ")\n")))
366 (when (or entry oentry)
367 ;; Buffer may be narrowed.
368 (save-restriction
369 (widen)
370 (when (gnus-server-goto-server server)
371 (gnus-delete-line))
372 (if entry
373 (gnus-server-insert-server-line (car entry) (cdr entry))
374 (gnus-server-insert-server-line
375 (format "%s:%s" (caar oentry) (nth 1 (car oentry)))
376 (car oentry)))
377 (gnus-server-position-point))))))
379 (defun gnus-server-set-info (server info)
380 ;; Enter a select method into the virtual server alist.
381 (when (and server info)
382 (gnus-dribble-enter
383 (concat "(gnus-server-set-info \"" server "\" '"
384 (gnus-prin1-to-string info) ")"))
385 (let* ((server (nth 1 info))
386 (entry (assoc server gnus-server-alist))
387 (cached (assoc server gnus-server-method-cache)))
388 (if cached
389 (setq gnus-server-method-cache
390 (delq cached gnus-server-method-cache)))
391 (if entry
392 (progn
393 ;; Remove the server from `gnus-opened-servers' since
394 ;; it has never been opened with the new `info' yet.
395 (gnus-opened-servers-remove (cdr entry))
396 ;; Don't make a new Lisp object.
397 (setcar (cdr entry) (car info))
398 (setcdr (cdr entry) (cdr info)))
399 (setq gnus-server-alist
400 (nconc gnus-server-alist (list (cons server info))))))))
402 ;;; Interactive server functions.
404 (defun gnus-server-kill-server (server)
405 "Kill the server on the current line."
406 (interactive (list (gnus-server-server-name)))
407 (unless (gnus-server-goto-server server)
408 (if server (error "No such server: %s" server)
409 (error "No server on the current line")))
410 (unless (assoc server gnus-server-alist)
411 (error "Read-only server %s" server))
412 (gnus-dribble-touch)
413 (let ((buffer-read-only nil))
414 (gnus-delete-line))
415 (push (assoc server gnus-server-alist) gnus-server-killed-servers)
416 (setq gnus-server-alist (delq (car gnus-server-killed-servers)
417 gnus-server-alist))
418 (let ((groups (gnus-groups-from-server server)))
419 (when (and groups
420 (gnus-yes-or-no-p
421 (format "Kill all %s groups from this server? "
422 (length groups))))
423 (dolist (group groups)
424 (setq gnus-newsrc-alist
425 (delq (assoc group gnus-newsrc-alist)
426 gnus-newsrc-alist))
427 (when gnus-group-change-level-function
428 (funcall gnus-group-change-level-function
429 group gnus-level-killed 3)))))
430 (gnus-server-position-point))
432 (defun gnus-server-yank-server ()
433 "Yank the previously killed server."
434 (interactive)
435 (unless gnus-server-killed-servers
436 (error "No killed servers to be yanked"))
437 (let ((alist gnus-server-alist)
438 (server (gnus-server-server-name))
439 (killed (car gnus-server-killed-servers)))
440 (if (not server)
441 (setq gnus-server-alist (nconc gnus-server-alist (list killed)))
442 (if (string= server (caar gnus-server-alist))
443 (push killed gnus-server-alist)
444 (while (and (cdr alist)
445 (not (string= server (caadr alist))))
446 (setq alist (cdr alist)))
447 (if alist
448 (setcdr alist (cons killed (cdr alist)))
449 (setq gnus-server-alist (list killed)))))
450 (gnus-server-update-server (car killed))
451 (setq gnus-server-killed-servers (cdr gnus-server-killed-servers))
452 (gnus-server-position-point)))
454 (defun gnus-server-exit ()
455 "Return to the group buffer."
456 (interactive)
457 (gnus-run-hooks 'gnus-server-exit-hook)
458 (gnus-kill-buffer (current-buffer))
459 (gnus-configure-windows 'group t))
461 (defun gnus-server-list-servers ()
462 "List all available servers."
463 (interactive)
464 (let ((cur (gnus-server-server-name)))
465 (gnus-server-prepare)
466 (if cur (gnus-server-goto-server cur)
467 (goto-char (point-max))
468 (forward-line -1))
469 (gnus-server-position-point)))
471 (defun gnus-server-set-status (method status)
472 "Make METHOD have STATUS."
473 (let ((entry (assoc method gnus-opened-servers)))
474 (if entry
475 (setcar (cdr entry) status)
476 (push (list method status) gnus-opened-servers))))
478 (defun gnus-opened-servers-remove (method)
479 "Remove METHOD from the list of opened servers."
480 (setq gnus-opened-servers (delq (assoc method gnus-opened-servers)
481 gnus-opened-servers)))
483 (defun gnus-server-open-server (server)
484 "Force an open of SERVER."
485 (interactive (list (gnus-server-server-name)))
486 (let ((method (gnus-server-to-method server)))
487 (unless method
488 (error "No such server: %s" server))
489 (gnus-server-set-status method 'ok)
490 (prog1
491 (or (gnus-open-server method)
492 (progn (message "Couldn't open %s" server) nil))
493 (gnus-server-update-server server)
494 (gnus-server-position-point))))
496 (defun gnus-server-open-all-servers ()
497 "Open all servers."
498 (interactive)
499 (dolist (server gnus-inserted-opened-servers)
500 (gnus-server-open-server (car server))))
502 (defun gnus-server-close-server (server)
503 "Close SERVER."
504 (interactive (list (gnus-server-server-name)))
505 (let ((method (gnus-server-to-method server)))
506 (unless method
507 (error "No such server: %s" server))
508 (gnus-server-set-status method 'closed)
509 (prog1
510 (gnus-close-server method)
511 (gnus-server-update-server server)
512 (gnus-server-position-point))))
514 (defun gnus-server-offline-server (server)
515 "Set SERVER to offline."
516 (interactive (list (gnus-server-server-name)))
517 (let ((method (gnus-server-to-method server)))
518 (unless method
519 (error "No such server: %s" server))
520 (prog1
521 (gnus-close-server method)
522 (gnus-server-set-status method 'offline)
523 (gnus-server-update-server server)
524 (gnus-server-position-point))))
526 (defun gnus-server-close-all-servers ()
527 "Close all servers."
528 (interactive)
529 (dolist (server gnus-inserted-opened-servers)
530 (gnus-server-close-server (car server)))
531 (dolist (server gnus-server-alist)
532 (gnus-server-close-server (car server))))
534 (defun gnus-server-deny-server (server)
535 "Make sure SERVER will never be attempted opened."
536 (interactive (list (gnus-server-server-name)))
537 (let ((method (gnus-server-to-method server)))
538 (unless method
539 (error "No such server: %s" server))
540 (gnus-server-set-status method 'denied))
541 (gnus-server-update-server server)
542 (gnus-server-position-point)
545 (defun gnus-server-remove-denials ()
546 "Make all denied servers into closed servers."
547 (interactive)
548 (dolist (server gnus-opened-servers)
549 (when (eq (nth 1 server) 'denied)
550 (setcar (nthcdr 1 server) 'closed)))
551 (gnus-server-list-servers))
553 (defun gnus-server-copy-server (from to)
554 "Copy a server definiton to a new name."
555 (interactive
556 (list
557 (or (gnus-server-server-name)
558 (error "No server on the current line"))
559 (read-string "Copy to: ")))
560 (unless from
561 (error "No server on current line"))
562 (unless (and to (not (string= to "")))
563 (error "No name to copy to"))
564 (when (assoc to gnus-server-alist)
565 (error "%s already exists" to))
566 (unless (gnus-server-to-method from)
567 (error "%s: no such server" from))
568 (let ((to-entry (cons from (gnus-copy-sequence
569 (gnus-server-to-method from)))))
570 (setcar to-entry to)
571 (setcar (nthcdr 2 to-entry) to)
572 (push to-entry gnus-server-killed-servers)
573 (gnus-server-yank-server)))
575 (defun gnus-server-add-server (how where)
576 (interactive
577 (list (intern (gnus-completing-read "Server method"
578 (mapcar 'car gnus-valid-select-methods)
580 (read-string "Server name: ")))
581 (when (assq where gnus-server-alist)
582 (error "Server with that name already defined"))
583 (push (list where how where) gnus-server-killed-servers)
584 (gnus-server-yank-server))
586 (defun gnus-server-goto-server (server)
587 "Jump to a server line."
588 (interactive
589 (list (gnus-completing-read "Goto server" (mapcar 'car gnus-server-alist) t)))
590 (let ((to (text-property-any (point-min) (point-max)
591 'gnus-server (intern server))))
592 (when to
593 (goto-char to)
594 (gnus-server-position-point))))
596 (defun gnus-server-edit-server (server)
597 "Edit the server on the current line."
598 (interactive (list (gnus-server-server-name)))
599 (unless server
600 (error "No server on current line"))
601 (unless (assoc server gnus-server-alist)
602 (error "This server can't be edited"))
603 (let ((info (cdr (assoc server gnus-server-alist))))
604 (gnus-close-server info)
605 (gnus-edit-form
606 info "Editing the server."
607 `(lambda (form)
608 (gnus-server-set-info ,server form)
609 (gnus-server-list-servers)
610 (gnus-server-position-point))
611 'edit-server)))
613 (defun gnus-server-show-server (server)
614 "Show the definition of the server on the current line."
615 (interactive (list (gnus-server-server-name)))
616 (unless server
617 (error "No server on current line"))
618 (let ((info (gnus-server-to-method server)))
619 (gnus-edit-form
620 info "Showing the server."
621 `(lambda (form)
622 (gnus-server-position-point))
623 'edit-server)))
625 (defun gnus-server-scan-server (server)
626 "Request a scan from the current server."
627 (interactive (list (gnus-server-server-name)))
628 (let ((method (gnus-server-to-method server)))
629 (if (not (gnus-get-function method 'request-scan))
630 (error "Server %s can't scan" (car method))
631 (gnus-message 3 "Scanning %s..." server)
632 (gnus-request-scan nil method)
633 (gnus-message 3 "Scanning %s...done" server))))
635 (defun gnus-server-read-server-in-server-buffer (server)
636 "Browse a server in server buffer."
637 (interactive (list (gnus-server-server-name)))
638 (let (gnus-server-browse-in-group-buffer)
639 (gnus-server-read-server server)))
641 (defun gnus-server-read-server (server)
642 "Browse a server."
643 (interactive (list (gnus-server-server-name)))
644 (let ((buf (current-buffer)))
645 (prog1
646 (gnus-browse-foreign-server server buf)
647 (with-current-buffer buf
648 (gnus-server-update-server (gnus-server-server-name))
649 (gnus-server-position-point)))))
651 (defun gnus-server-pick-server (e)
652 (interactive "e")
653 (mouse-set-point e)
654 (gnus-server-read-server (gnus-server-server-name)))
658 ;;; Browse Server Mode
661 (defvar gnus-browse-menu-hook nil
662 "*Hook run after the creation of the browse mode menu.")
664 (defcustom gnus-browse-subscribe-newsgroup-method
665 'gnus-subscribe-alphabetically
666 "Function(s) called when subscribing groups in the Browse Server Buffer
667 A few pre-made functions are supplied: `gnus-subscribe-randomly'
668 inserts new groups at the beginning of the list of groups;
669 `gnus-subscribe-alphabetically' inserts new groups in strict
670 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
671 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
672 for your decision; `gnus-subscribe-killed' kills all new groups;
673 `gnus-subscribe-zombies' will make all new groups into zombies;
674 `gnus-subscribe-topics' will enter groups into the topics that
675 claim them."
676 :version "24.1"
677 :group 'gnus-server
678 :type '(radio (function-item gnus-subscribe-randomly)
679 (function-item gnus-subscribe-alphabetically)
680 (function-item gnus-subscribe-hierarchically)
681 (function-item gnus-subscribe-interactively)
682 (function-item gnus-subscribe-killed)
683 (function-item gnus-subscribe-zombies)
684 (function-item gnus-subscribe-topics)
685 function
686 (repeat function)))
688 (defvar gnus-browse-mode-hook nil)
689 (defvar gnus-browse-mode-map nil)
690 (put 'gnus-browse-mode 'mode-class 'special)
692 (unless gnus-browse-mode-map
693 (setq gnus-browse-mode-map (make-keymap))
694 (suppress-keymap gnus-browse-mode-map)
696 (gnus-define-keys
697 gnus-browse-mode-map
698 " " gnus-browse-read-group
699 "=" gnus-browse-select-group
700 "n" gnus-browse-next-group
701 "p" gnus-browse-prev-group
702 "\177" gnus-browse-prev-group
703 [delete] gnus-browse-prev-group
704 "N" gnus-browse-next-group
705 "P" gnus-browse-prev-group
706 "\M-n" gnus-browse-next-group
707 "\M-p" gnus-browse-prev-group
708 "\r" gnus-browse-select-group
709 "u" gnus-browse-unsubscribe-current-group
710 "l" gnus-browse-exit
711 "L" gnus-browse-exit
712 "q" gnus-browse-exit
713 "Q" gnus-browse-exit
714 "d" gnus-browse-describe-group
715 "\C-c\C-c" gnus-browse-exit
716 "?" gnus-browse-describe-briefly
718 "\C-c\C-i" gnus-info-find-node
719 "\C-c\C-b" gnus-bug))
721 (defun gnus-browse-make-menu-bar ()
722 (gnus-turn-off-edit-menu 'browse)
723 (unless (boundp 'gnus-browse-menu)
724 (easy-menu-define
725 gnus-browse-menu gnus-browse-mode-map ""
726 '("Browse"
727 ["Subscribe" gnus-browse-unsubscribe-current-group t]
728 ["Read" gnus-browse-read-group t]
729 ["Select" gnus-browse-select-group t]
730 ["Describe" gnus-browse-describe-group t]
731 ["Next" gnus-browse-next-group t]
732 ["Prev" gnus-browse-prev-group t]
733 ["Exit" gnus-browse-exit t]))
734 (gnus-run-hooks 'gnus-browse-menu-hook)))
736 (defvar gnus-browse-current-method nil)
737 (defvar gnus-browse-return-buffer nil)
739 (defvar gnus-browse-buffer "*Gnus Browse Server*")
741 (defun gnus-browse-foreign-server (server &optional return-buffer)
742 "Browse the server SERVER."
743 (setq gnus-browse-current-method (gnus-server-to-method server))
744 (setq gnus-browse-return-buffer return-buffer)
745 (let* ((method gnus-browse-current-method)
746 (orig-select-method gnus-select-method)
747 (gnus-select-method method)
748 groups group)
749 (gnus-message 5 "Connecting to %s..." (nth 1 method))
750 (cond
751 ((not (gnus-check-server method))
752 (gnus-message
753 1 "Unable to contact server %s: %s" (nth 1 method)
754 (gnus-status-message method))
755 nil)
756 ((not
757 (prog2
758 (gnus-message 6 "Reading active file...")
759 (gnus-request-list method)
760 (gnus-message 6 "Reading active file...done")))
761 (gnus-message
762 1 "Couldn't request list: %s" (gnus-status-message method))
763 nil)
765 (with-current-buffer nntp-server-buffer
766 (let ((cur (current-buffer)))
767 (goto-char (point-min))
768 (unless (or (null gnus-ignored-newsgroups)
769 (string= gnus-ignored-newsgroups ""))
770 (delete-matching-lines gnus-ignored-newsgroups))
771 ;; We treat NNTP as a special case to avoid problems with
772 ;; garbage group names like `"foo' that appear in some badly
773 ;; managed active files. -jh.
774 (if (eq (car method) 'nntp)
775 (while (not (eobp))
776 (ignore-errors
777 (push (cons
778 (mm-string-as-unibyte
779 (buffer-substring
780 (point)
781 (progn
782 (skip-chars-forward "^ \t")
783 (point))))
784 (let ((last (read cur)))
785 (cons (read cur) last)))
786 groups))
787 (forward-line))
788 (while (not (eobp))
789 (ignore-errors
790 (push (cons
791 (mm-string-as-unibyte
792 (if (eq (char-after) ?\")
793 (read cur)
794 (let ((p (point)) (name ""))
795 (skip-chars-forward "^ \t\\\\")
796 (setq name (buffer-substring p (point)))
797 (while (eq (char-after) ?\\)
798 (setq p (1+ (point)))
799 (forward-char 2)
800 (skip-chars-forward "^ \t\\\\")
801 (setq name (concat name (buffer-substring
802 p (point)))))
803 name)))
804 (let ((last (read cur)))
805 (cons (read cur) last)))
806 groups))
807 (forward-line)))))
808 (setq groups (sort groups
809 (lambda (l1 l2)
810 (string< (car l1) (car l2)))))
811 (if gnus-server-browse-in-group-buffer
812 (let* ((gnus-select-method orig-select-method)
813 (gnus-group-listed-groups
814 (mapcar (lambda (group)
815 (let ((name
816 (gnus-group-prefixed-name
817 (car group) method)))
818 (gnus-set-active name (cdr group))
819 name))
820 groups)))
821 (gnus-configure-windows 'group)
822 (funcall gnus-group-prepare-function
823 gnus-level-killed 'ignore 1 'ignore))
824 (gnus-get-buffer-create gnus-browse-buffer)
825 (gnus-configure-windows 'browse)
826 (buffer-disable-undo)
827 (let ((buffer-read-only nil))
828 (erase-buffer))
829 (gnus-browse-mode)
830 (setq mode-line-buffer-identification
831 (list
832 (format
833 "Gnus: %%b {%s:%s}" (car method) (cadr method))))
834 (let ((buffer-read-only nil)
835 name
836 (prefix (let ((gnus-select-method orig-select-method))
837 (gnus-group-prefixed-name "" method))))
838 (while (setq group (pop groups))
839 (gnus-add-text-properties
840 (point)
841 (prog1 (1+ (point))
842 (insert
843 (format "%c%7d: %s\n"
844 (let ((level
845 (if (string= prefix "")
846 (gnus-group-level (setq name (car group)))
847 (gnus-group-level
848 (concat prefix (setq name (car group)))))))
849 (cond
850 ((<= level gnus-level-subscribed) ? )
851 ((<= level gnus-level-unsubscribed) ?U)
852 ((= level gnus-level-zombie) ?Z)
853 (t ?K)))
854 (max 0 (- (1+ (cddr group)) (cadr group)))
855 ;; Don't decode if name is ASCII
856 (if (and (fboundp 'detect-coding-string)
857 (eq (detect-coding-string name t) 'undecided))
858 name
859 (mm-decode-coding-string
860 name
861 (inline (gnus-group-name-charset method name)))))))
862 (list 'gnus-group name)
864 (switch-to-buffer (current-buffer)))
865 (goto-char (point-min))
866 (gnus-group-position-point)
867 (gnus-message 5 "Connecting to %s...done" (nth 1 method))
868 t))))
870 (defun gnus-browse-mode ()
871 "Major mode for browsing a foreign server.
873 All normal editing commands are switched off.
875 \\<gnus-browse-mode-map>
876 The only things you can do in this buffer is
878 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
879 The group will be inserted into the group buffer upon exit from this
880 buffer.
882 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
884 3) `\\[gnus-browse-exit]' to return to the group buffer."
885 (interactive)
886 (kill-all-local-variables)
887 (when (gnus-visual-p 'browse-menu 'menu)
888 (gnus-browse-make-menu-bar))
889 (gnus-simplify-mode-line)
890 (setq major-mode 'gnus-browse-mode)
891 (setq mode-name "Browse Server")
892 (setq mode-line-process nil)
893 (use-local-map gnus-browse-mode-map)
894 (buffer-disable-undo)
895 (setq truncate-lines t)
896 (gnus-set-default-directory)
897 (setq buffer-read-only t)
898 (gnus-run-mode-hooks 'gnus-browse-mode-hook))
900 (defun gnus-browse-read-group (&optional no-article number)
901 "Enter the group at the current line.
902 If NUMBER, fetch this number of articles."
903 (interactive "P")
904 (let ((group (gnus-browse-group-name)))
905 (if (or (not (gnus-get-info group))
906 (gnus-ephemeral-group-p group))
907 (unless (gnus-group-read-ephemeral-group
908 group gnus-browse-current-method nil
909 (cons (current-buffer) 'browse)
910 nil nil nil number)
911 (error "Couldn't enter %s" group))
912 (unless (gnus-group-read-group nil no-article group)
913 (error "Couldn't enter %s" group)))))
915 (defun gnus-browse-select-group (&optional number)
916 "Select the current group.
917 If NUMBER, fetch this number of articles."
918 (interactive "P")
919 (gnus-browse-read-group 'no number))
921 (defun gnus-browse-next-group (n)
922 "Go to the next group."
923 (interactive "p")
924 (prog1
925 (forward-line n)
926 (gnus-group-position-point)))
928 (defun gnus-browse-prev-group (n)
929 "Go to the next group."
930 (interactive "p")
931 (gnus-browse-next-group (- n)))
933 (defun gnus-browse-unsubscribe-current-group (arg)
934 "(Un)subscribe to the next ARG groups.
935 The variable `gnus-browse-subscribe-newsgroup-method' determines
936 how new groups will be entered into the group buffer."
937 (interactive "p")
938 (when (eobp)
939 (error "No group at current line"))
940 (let ((ward (if (< arg 0) -1 1))
941 (arg (abs arg)))
942 (while (and (> arg 0)
943 (not (eobp))
944 (gnus-browse-unsubscribe-group)
945 (zerop (gnus-browse-next-group ward)))
946 (decf arg))
947 (gnus-group-position-point)
948 (when (/= 0 arg)
949 (gnus-message 7 "No more newsgroups"))
950 arg))
952 (defun gnus-browse-group-name ()
953 (save-excursion
954 (beginning-of-line)
955 (let ((name (get-text-property (point) 'gnus-group)))
956 (when (re-search-forward ": \\(.*\\)$" (point-at-eol) t)
957 (concat (gnus-method-to-server-name gnus-browse-current-method) ":"
958 (or name
959 (match-string-no-properties 1)))))))
961 (defun gnus-browse-describe-group (group)
962 "Describe the current group."
963 (interactive (list (gnus-browse-group-name)))
964 (gnus-group-describe-group nil group))
966 (defun gnus-browse-unsubscribe-group ()
967 "Toggle subscription of the current group in the browse buffer."
968 (let ((sub nil)
969 (buffer-read-only nil)
970 group)
971 (save-excursion
972 (beginning-of-line)
973 ;; If this group it killed, then we want to subscribe it.
974 (unless (eq (char-after) ? )
975 (setq sub t))
976 (setq group (gnus-browse-group-name))
977 (when (gnus-server-equal gnus-browse-current-method "native")
978 (setq group (gnus-group-real-name group)))
979 (if sub
980 (progn
981 ;; Make sure the group has been properly removed before we
982 ;; subscribe to it.
983 (if (gnus-ephemeral-group-p group)
984 (gnus-kill-ephemeral-group group))
985 (let ((entry (gnus-group-entry group)))
986 (if entry
987 ;; Just change the subscription level if it is an
988 ;; unsubscribed group.
989 (gnus-group-change-level entry
990 gnus-level-default-subscribed)
991 ;; If it is a killed group or a zombie, feed it to the
992 ;; mechanism for new group subscription.
993 (gnus-call-subscribe-functions
994 gnus-browse-subscribe-newsgroup-method
995 group)
996 (gnus-request-update-group-status group 'subscribe)))
997 (delete-char 1)
998 (insert (let ((lvl (gnus-group-level group)))
999 (cond
1000 ((< lvl gnus-level-unsubscribed) ? )
1001 ((< lvl gnus-level-zombie) ?U)
1002 ((< lvl gnus-level-killed) ?Z)
1003 (t ?K)))))
1004 (gnus-group-change-level
1005 group gnus-level-unsubscribed gnus-level-default-subscribed)
1006 (delete-char 1)
1007 (insert ?U)))
1010 (defun gnus-browse-exit ()
1011 "Quit browsing and return to the group buffer."
1012 (interactive)
1013 (when (eq major-mode 'gnus-browse-mode)
1014 (gnus-kill-buffer (current-buffer)))
1015 ;; Insert the newly subscribed groups in the group buffer.
1016 (with-current-buffer gnus-group-buffer
1017 (gnus-group-list-groups nil))
1018 (if gnus-browse-return-buffer
1019 (gnus-configure-windows 'server 'force)
1020 (gnus-configure-windows 'group 'force)))
1022 (defun gnus-browse-describe-briefly ()
1023 "Give a one line description of the group mode commands."
1024 (interactive)
1025 (gnus-message 6 "%s"
1026 (substitute-command-keys "\\<gnus-browse-mode-map>\\[gnus-group-next-group]:Forward \\[gnus-group-prev-group]:Backward \\[gnus-browse-exit]:Exit \\[gnus-info-find-node]:Run Info \\[gnus-browse-describe-briefly]:This help")))
1028 (defun gnus-server-regenerate-server ()
1029 "Issue a command to the server to regenerate all its data structures."
1030 (interactive)
1031 (let ((server (gnus-server-server-name)))
1032 (unless server
1033 (error "No server on the current line"))
1034 (condition-case ()
1035 (gnus-get-function (gnus-server-to-method server)
1036 'request-regenerate)
1037 (error
1038 (error "This back end doesn't support regeneration")))
1039 (gnus-message 5 "Requesting regeneration of %s..." server)
1040 (unless (gnus-open-server server)
1041 (error "Couldn't open server"))
1042 (if (gnus-request-regenerate server)
1043 (gnus-message 5 "Requesting regeneration of %s...done" server)
1044 (gnus-message 5 "Couldn't regenerate %s" server))))
1048 ;;; Server compaction. -- dvl
1051 ;; #### FIXME: this function currently fails to update the Group buffer's
1052 ;; #### appearance.
1053 (defun gnus-server-compact-server ()
1054 "Issue a command to the server to compact all its groups.
1056 Note: currently only implemented in nnml."
1057 (interactive)
1058 (let ((server (gnus-server-server-name)))
1059 (unless server
1060 (error "No server on the current line"))
1061 (condition-case ()
1062 (gnus-get-function (gnus-server-to-method server)
1063 'request-compact)
1064 (error
1065 (error "This back end doesn't support compaction")))
1066 (gnus-message 5 "\
1067 Requesting compaction of %s... (this may take a long time)"
1068 server)
1069 (unless (gnus-open-server server)
1070 (error "Couldn't open server"))
1071 (if (not (gnus-request-compact server))
1072 (gnus-message 5 "Couldn't compact %s" server)
1073 (gnus-message 5 "Requesting compaction of %s...done" server)
1074 ;; Invalidate the original article buffer which might be out of date.
1075 ;; #### NOTE: Yes, this might be a bit rude, but since compaction
1076 ;; #### will not happen very often, I think this is acceptable.
1077 (let ((original (get-buffer gnus-original-article-buffer)))
1078 (and original (gnus-kill-buffer original))))))
1080 (provide 'gnus-srvr)
1082 ;;; gnus-srvr.el ends here