Test 13.
[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.