Indent.
[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., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, 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 ((newsrc gnus-newsrc-alist)
56           (nntp-nov-gap nil)
57           info)
58       (while (setq info (pop newsrc))
59         (when (gnus-group-native-p (gnus-info-group info))
60           (gnus-move-group-to-server info from-server to-server))))))
61
62 (defun gnus-move-group-to-server (info from-server to-server)
63   "Move group INFO from FROM-SERVER to TO-SERVER."
64   (let ((group (gnus-info-group info))
65         to-active hashtb type mark marks
66         to-article to-reads to-marks article
67         act-articles)
68     (gnus-message 7 "Translating %s..." group)
69     (when (gnus-request-group group nil to-server)
70       (setq to-active (gnus-parse-active)
71             hashtb (gnus-make-hashtable 1024)
72             act-articles (gnus-uncompress-range to-active))
73       ;; Fetch the headers from the `to-server'.
74       (when (and to-active
75                  act-articles
76                  (setq type (gnus-retrieve-headers
77                              act-articles
78                              group to-server)))
79         ;; Convert HEAD headers.  I don't care.
80         (when (eq type 'headers)
81           (nnvirtual-convert-headers))
82         ;; Create a mapping from Message-ID to article number.
83         (set-buffer nntp-server-buffer)
84         (goto-char (point-min))
85         (while (looking-at
86                 "^[0-9]+\t[^\t]*\t[^\t]*\t[^\t]*\t\\([^\t]*\\)\t")
87           (gnus-sethash
88            (buffer-substring (match-beginning 1) (match-end 1))
89            (read (current-buffer))
90            hashtb)
91           (forward-line 1))
92         ;; Then we read the headers from the `from-server'.
93         (when (and (gnus-request-group group nil from-server)
94                    (gnus-active group)
95                    (gnus-uncompress-range
96                     (gnus-active group))
97                    (setq type (gnus-retrieve-headers
98                                (gnus-uncompress-range
99                                 (gnus-active group))
100                                group from-server)))
101           ;; Make it easier to map marks.
102           (let ((mark-lists (gnus-info-marks info))
103                 ms type m)
104             (while mark-lists
105               (setq type (caar mark-lists)
106                     ms (gnus-uncompress-range (cdr (pop mark-lists))))
107               (while ms
108                 (if (setq m (assq (car ms) marks))
109                     (setcdr m (cons type (cdr m)))
110                   (push (list (car ms) type) marks))
111                 (pop ms))))
112           ;; Convert.
113           (when (eq type 'headers)
114             (nnvirtual-convert-headers))
115           ;; Go through the headers and map away.
116           (set-buffer nntp-server-buffer)
117           (goto-char (point-min))
118           (while (looking-at
119                   "^[0-9]+\t[^\t]*\t[^\t]*\t[^\t]*\t\\([^\t]*\\)\t")
120             (when (setq to-article
121                         (gnus-gethash
122                          (buffer-substring (match-beginning 1) (match-end 1))
123                          hashtb))
124               ;; Add this article to the list of read articles.
125               (push to-article to-reads)
126               ;; See if there are any marks and then add them.
127               (when (setq mark (assq (read (current-buffer)) marks))
128                 (setq marks (delq mark marks))
129                 (setcar mark to-article)
130                 (push mark to-marks))
131               (forward-line 1)))
132           ;; Now we know what the read articles are and what the
133           ;; article marks are.  We transform the information
134           ;; into the Gnus info format.
135           (setq to-reads
136                 (gnus-range-add
137                  (gnus-compress-sequence
138                   (and (setq to-reads (delq nil to-reads))
139                        (sort to-reads '<))
140                   t)
141                  (cons 1 (1- (car to-active)))))
142           (gnus-info-set-read info to-reads)
143           ;; Do the marks.  I'm sure y'all understand what's
144           ;; going on down below, so I won't bother with any
145           ;; further comments.  <duck>
146           (let ((mlists gnus-article-mark-lists)
147                 lists ms a)
148             (while mlists
149               (push (list (cdr (pop mlists))) lists))
150             (while (setq ms (pop marks))
151               (setq article (pop ms))
152               (while ms
153                 (setcdr (setq a (assq (pop ms) lists))
154                         (cons article (cdr a)))))
155             (setq a lists)
156             (while a
157               (setcdr (car a) (gnus-compress-sequence
158                                (and (cdar a) (sort (cdar a) '<))))
159               (pop a))
160             (gnus-info-set-marks info lists t)))))
161     (gnus-message 7 "Translating %s...done" group)))
162
163 (defun gnus-group-move-group-to-server (info from-server to-server)
164   "Move the group on the current line from FROM-SERVER to TO-SERVER."
165   (interactive
166    (let ((info (gnus-get-info (gnus-group-group-name))))
167      (list info (gnus-find-method-for-group (gnus-info-group info))
168            (gnus-read-method (format "Move group %s to method: "
169                                      (gnus-info-group info))))))
170   (save-excursion
171     (gnus-move-group-to-server info from-server to-server)
172     ;; We have to update the group info to point use the right server.
173     (gnus-info-set-method info to-server t)
174     ;; We also have to change the name of the group and stuff.
175     (let* ((group (gnus-info-group info))
176            (new-name (gnus-group-prefixed-name
177                       (gnus-group-real-name group) to-server)))
178       (gnus-info-set-group info new-name)
179       (gnus-sethash new-name (gnus-gethash group gnus-newsrc-hashtb)
180                     gnus-newsrc-hashtb)
181       (gnus-sethash group nil gnus-newsrc-hashtb))))
182
183 (provide 'gnus-move)
184
185 ;;; gnus-move.el ends here