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