Revision: miles@gnu.org--gnu-2005/gnus--devo--0--patch-151
[gnus] / lisp / gnus-move.el
1 ;;; gnus-move.el --- commands for moving Gnus from one server to another
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
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 2, or (at your option)
13 ;; any later version.
14
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.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-start)
33 (require 'gnus-int)
34 (require 'gnus-range)
35
36 ;;;
37 ;;; Moving by comparing Message-ID's.
38 ;;;
39
40 ;;;###autoload
41 (defun gnus-change-server (from-server to-server)
42   "Move from FROM-SERVER to TO-SERVER.
43 Update the .newsrc.eld file to reflect the change of nntp server."
44   (interactive
45    (list gnus-select-method (gnus-read-method "Move to method: ")))
46
47   ;; First start Gnus.
48   (let ((gnus-activate-level 0)
49         (mail-sources nil)
50         (nnmail-spool-file nil))
51     (gnus))
52
53   (save-excursion
54     ;; Go through all groups and translate.
55     (let ((nntp-nov-gap nil))
56       (dolist (info gnus-newsrc-alist)
57         (when (gnus-group-native-p (gnus-info-group info))
58           (gnus-move-group-to-server info from-server to-server))))))
59
60 (defun gnus-move-group-to-server (info from-server to-server)
61   "Move group INFO from FROM-SERVER to TO-SERVER."
62   (let ((group (gnus-info-group info))
63         to-active hashtb type mark marks
64         to-article to-reads to-marks article
65         act-articles)
66     (gnus-message 7 "Translating %s..." group)
67     (when (gnus-request-group group nil to-server)
68       (setq to-active (gnus-parse-active)
69             hashtb (gnus-make-hashtable 1024)
70             act-articles (gnus-uncompress-range to-active))
71       ;; Fetch the headers from the `to-server'.
72       (when (and to-active
73                  act-articles
74                  (setq type (gnus-retrieve-headers
75                              act-articles
76                              group to-server)))
77         ;; Convert HEAD headers.  I don't care.
78         (when (eq type 'headers)
79           (nnvirtual-convert-headers))
80         ;; Create a mapping from Message-ID to article number.
81         (set-buffer nntp-server-buffer)
82         (goto-char (point-min))
83         (while (looking-at
84                 "^[0-9]+\t[^\t]*\t[^\t]*\t[^\t]*\t\\([^\t]*\\)\t")
85           (gnus-sethash
86            (buffer-substring (match-beginning 1) (match-end 1))
87            (read (current-buffer))
88            hashtb)
89           (forward-line 1))
90         ;; Then we read the headers from the `from-server'.
91         (when (and (gnus-request-group group nil from-server)
92                    (gnus-active group)
93                    (gnus-uncompress-range
94                     (gnus-active group))
95                    (setq type (gnus-retrieve-headers
96                                (gnus-uncompress-range
97                                 (gnus-active group))
98                                group from-server)))
99           ;; Make it easier to map marks.
100           (let ((mark-lists (gnus-info-marks info))
101                 ms type m)
102             (while mark-lists
103               (setq type (caar mark-lists)
104                     ms (gnus-uncompress-range (cdr (pop mark-lists))))
105               (while ms
106                 (if (setq m (assq (car ms) marks))
107                     (setcdr m (cons type (cdr m)))
108                   (push (list (car ms) type) marks))
109                 (pop ms))))
110           ;; Convert.
111           (when (eq type 'headers)
112             (nnvirtual-convert-headers))
113           ;; Go through the headers and map away.
114           (set-buffer nntp-server-buffer)
115           (goto-char (point-min))
116           (while (looking-at
117                   "^[0-9]+\t[^\t]*\t[^\t]*\t[^\t]*\t\\([^\t]*\\)\t")
118             (when (setq to-article
119                         (gnus-gethash
120                          (buffer-substring (match-beginning 1) (match-end 1))
121                          hashtb))
122               ;; Add this article to the list of read articles.
123               (push to-article to-reads)
124               ;; See if there are any marks and then add them.
125               (when (setq mark (assq (read (current-buffer)) marks))
126                 (setq marks (delq mark marks))
127                 (setcar mark to-article)
128                 (push mark to-marks))
129               (forward-line 1)))
130           ;; Now we know what the read articles are and what the
131           ;; article marks are.  We transform the information
132           ;; into the Gnus info format.
133           (setq to-reads
134                 (gnus-range-add
135                  (gnus-compress-sequence
136                   (and (setq to-reads (delq nil to-reads))
137                        (sort to-reads '<))
138                   t)
139                  (cons 1 (1- (car to-active)))))
140           (gnus-info-set-read info to-reads)
141           ;; Do the marks.  I'm sure y'all understand what's
142           ;; going on down below, so I won't bother with any
143           ;; further comments.  <duck>
144           (let ((mlists gnus-article-mark-lists)
145                 lists ms a)
146             (while mlists
147               (push (list (cdr (pop mlists))) lists))
148             (while (setq ms (pop marks))
149               (setq article (pop ms))
150               (while ms
151                 (setcdr (setq a (assq (pop ms) lists))
152                         (cons article (cdr a)))))
153             (setq a lists)
154             (while a
155               (setcdr (car a) (gnus-compress-sequence
156                                (and (cdar a) (sort (cdar a) '<))))
157               (pop a))
158             (gnus-info-set-marks info lists t)))))
159     (gnus-message 7 "Translating %s...done" group)))
160
161 (defun gnus-group-move-group-to-server (info from-server to-server)
162   "Move the group on the current line from FROM-SERVER to TO-SERVER."
163   (interactive
164    (let ((info (gnus-get-info (gnus-group-group-name))))
165      (list info (gnus-find-method-for-group (gnus-info-group info))
166            (gnus-read-method (format "Move group %s to method: "
167                                      (gnus-info-group info))))))
168   (save-excursion
169     (gnus-move-group-to-server info from-server to-server)
170     ;; We have to update the group info to point use the right server.
171     (gnus-info-set-method info to-server t)
172     ;; We also have to change the name of the group and stuff.
173     (let* ((group (gnus-info-group info))
174            (new-name (gnus-group-prefixed-name
175                       (gnus-group-real-name group) to-server)))
176       (gnus-info-set-group info new-name)
177       (gnus-sethash new-name (gnus-group-entry group) gnus-newsrc-hashtb)
178       (gnus-sethash group nil gnus-newsrc-hashtb))))
179
180 (provide 'gnus-move)
181
182 ;;; arch-tag: 503742b8-7d66-4d79-bb31-4a698070707b
183 ;;; gnus-move.el ends here