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