*** 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 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-start)
32 (require 'gnus-int)
33 (require 'gnus-range)
34
35 ;;;
36 ;;; Moving by comparing Message-ID's.
37 ;;;
38
39 ;;;###autoload
40 (defun gnus-change-server (from-server to-server)
41   "Move from FROM-SERVER to TO-SERVER.
42 Update the .newsrc.eld file to reflect the change of nntp server."
43   (interactive
44    (list gnus-select-method (gnus-read-method "Move to method: ")))
45
46   ;; First start Gnus.
47   (let ((gnus-activate-level 0)
48         (nnmail-spool-file nil))
49     (gnus))
50
51   (save-excursion
52     ;; Go through all groups and translate.
53     (let ((newsrc gnus-newsrc-alist)
54           (nntp-nov-gap nil)
55           info)
56       (while (setq info (pop newsrc))
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                    (setq type (gnus-retrieve-headers
94                                (gnus-uncompress-range
95                                 (gnus-active group))
96                                group from-server)))
97           ;; Make it easier to map marks.
98           (let ((mark-lists (gnus-info-marks info))
99                 ms type m)
100             (while mark-lists
101               (setq type (caar mark-lists)
102                     ms (gnus-uncompress-range (cdr (pop mark-lists))))
103               (while ms
104                 (if (setq m (assq (car ms) marks))
105                     (setcdr m (cons type (cdr m)))
106                   (push (list (car ms) type) marks))
107                 (pop ms))))
108           ;; Convert.
109           (when (eq type 'headers)
110             (nnvirtual-convert-headers))
111           ;; Go through the headers and map away.
112           (set-buffer nntp-server-buffer)
113           (goto-char (point-min))
114           (while (looking-at
115                   "^[0-9]+\t[^\t]*\t[^\t]*\t[^\t]*\t\\([^\t]*\\)\t")
116             (setq to-article
117                   (gnus-gethash
118                    (buffer-substring (match-beginning 1) (match-end 1))
119                    hashtb))
120             ;; Add this article to the list of read articles.
121             (push to-article to-reads)
122             ;; See if there are any marks and then add them.
123             (when (setq mark (assq (read (current-buffer)) marks))
124               (setq marks (delq mark marks))
125               (setcar mark to-article)
126               (push mark to-marks))
127             (forward-line 1))
128           ;; Now we know what the read articles are and what the
129           ;; article marks are.  We transform the information
130           ;; into the Gnus info format.
131           (setq to-reads
132                 (gnus-range-add
133                  (gnus-compress-sequence (and to-reads (sort to-reads '<)) t)
134                  (cons 1 (1- (car to-active)))))
135           (gnus-info-set-read info to-reads)
136           ;; Do the marks.  I'm sure y'all understand what's
137           ;; going on down below, so I won't bother with any
138           ;; further comments.  <duck>
139           (let ((mlists gnus-article-mark-lists)
140                 lists ms a)
141             (while mlists
142               (push (list (cdr (pop mlists))) lists))
143             (while (setq ms (pop marks))
144               (setq article (pop ms))
145               (while ms
146                 (setcdr (setq a (assq (pop ms) lists))
147                         (cons article (cdr a)))))
148             (setq a lists)
149             (while a
150               (setcdr (car a) (gnus-compress-sequence
151                                (and (cdar a) (sort (cdar a) '<))))
152               (pop a))
153             (gnus-info-set-marks info lists t)))))
154     (gnus-message 7 "Translating %s...done" group)))
155
156 (defun gnus-group-move-group-to-server (info from-server to-server)
157   "Move the group on the current line from FROM-SERVER to TO-SERVER."
158   (interactive
159    (let ((info (gnus-get-info (gnus-group-group-name))))
160      (list info (gnus-find-method-for-group (gnus-info-group info))
161            (gnus-read-method (format "Move group %s to method: "
162                                      (gnus-info-group info))))))
163   (save-excursion
164     (gnus-move-group-to-server info from-server to-server)
165     ;; We have to update the group info to point use the right server.
166     (gnus-info-set-method info to-server t)
167     ;; We also have to change the name of the group and stuff.
168     (let* ((group (gnus-info-group info))
169            (new-name (gnus-group-prefixed-name
170                       (gnus-group-real-name group) to-server)))
171       (gnus-info-set-group info new-name)
172       (gnus-sethash new-name (gnus-gethash group gnus-newsrc-hashtb)
173                     gnus-newsrc-hashtb)
174       (gnus-sethash group nil gnus-newsrc-hashtb))))
175
176 (provide 'gnus-move)
177
178 ;;; gnus-move.el ends here