Revision: miles@gnu.org--gnu-2004/gnus--devo--0--patch-21
[gnus] / lisp / legacy-gnus-agent.el
1 (require 'gnus-start)
2 (require 'gnus-util)
3 (require 'gnus-range)
4 (require 'gnus-agent)
5
6 ; Oort Gnus v0.08 - This release updated agent to no longer use
7 ;                   history file and to support a compressed alist.
8
9 (defun gnus-agent-convert-to-compressed-agentview (converting-to)
10   "Iterates over all agentview files to ensure that they have been
11 converted to the compressed format."
12
13   (let ((search-in (list gnus-agent-directory))
14         here
15         members
16         member
17         converted-something)
18     (while (setq here (pop search-in))
19       (setq members (directory-files here t))
20       (while (setq member (pop members))
21         (cond ((string-match "/\\.\\.?$" member)
22                nil)
23               ((file-directory-p member)
24                (push member search-in))
25               ((equal (file-name-nondirectory member) ".agentview")
26                (setq converted-something 
27                      (or (gnus-agent-convert-agentview member)
28                          converted-something))))))
29
30     (if converted-something
31         (gnus-message 4 "Successfully converted Gnus %s offline (agent) files to %s" gnus-newsrc-file-version converting-to))))
32
33 (defun gnus-agent-convert-agentview (file)
34   "Load FILE and do a `read' there."
35   (with-temp-buffer
36       (nnheader-insert-file-contents file)
37       (goto-char (point-min))
38       (let ((inhibit-quit t)
39             (alist (read (current-buffer)))
40             (version (condition-case nil (read (current-buffer))
41                        (end-of-file 0)))
42             changed-version
43             history-file)
44
45         (cond
46          ((= version 0)
47           (let (entry
48                 (gnus-command-method nil))
49             (mm-disable-multibyte) ;; everything is binary
50             (erase-buffer)
51             (insert "\n")
52             (let ((file (concat (file-name-directory file) "/history")))
53               (when (file-exists-p file)
54                 (nnheader-insert-file-contents file)
55                 (setq history-file file)))
56
57             (goto-char (point-min))
58             (while (not (eobp))
59               (if (and (looking-at
60                         "[^\t\n]+\t\\([0-9]+\\)\t\\([^ \n]+\\) \\([0-9]+\\)")
61                        (string= (gnus-agent-article-name ".agentview" (match-string 2))
62                                 file)
63                        (setq entry (assoc (string-to-number (match-string 3)) alist)))
64                   (setcdr entry (string-to-number (match-string 1))))
65               (forward-line 1))
66             (setq changed-version t)))
67          ((= version 1)
68           (setq changed-version t)))
69
70         (when changed-version
71           (erase-buffer)
72           (let ((compressed nil))
73             (mapcar (lambda (pair)
74                       (let* ((article-id (car pair))
75                              (day-of-download (cdr pair))
76                              (comp-list (assq day-of-download compressed)))
77                         (if comp-list
78                             (setcdr comp-list
79                                     (cons article-id (cdr comp-list)))
80                           (setq compressed
81                                 (cons (list day-of-download article-id)
82                                       compressed)))
83                         nil)) alist)
84             (mapcar (lambda (comp-list)
85                       (setcdr comp-list
86                               (gnus-compress-sequence
87                                (nreverse (cdr comp-list)))))
88                     compressed)
89             (princ compressed (current-buffer)))
90           (insert "\n2\n")
91           (write-file file)
92           (when history-file
93             (delete-file history-file))
94           t))))
95
96 ;; End of Oort Gnus v0.08 updates
97
98 ;; No Gnus v0.3 - This release provides a mechanism for upgrading gnus
99 ;;                from previous versions.  Therefore, the previous
100 ;;                hacks to handle a gnus-agent-expire-days that
101 ;;                specifies a list of values can be removed.
102
103 (defun gnus-agent-unlist-expire-days (converting-to)
104   (when (listp gnus-agent-expire-days)
105     (let (buffer)
106       (unwind-protect
107           (save-window-excursion
108             (setq buffer (gnus-get-buffer-create " *Gnus agent upgrade*"))
109             (set-buffer buffer)
110             (erase-buffer)
111             (insert "The definition of gnus-agent-expire-days has been changed.\nYou currently have it set to the list:\n  ")
112             (let (print-level print-length) (pp gnus-agent-expire-days (current-buffer)))
113
114             (insert "\nIn order to use version '" converting-to "' of gnus, you will need to set\n")
115             (insert "gnus-agent-expire-days to an integer. If you still wish to set different\n")
116             (insert "expiration days to individual groups, you must instead set the\n")
117             (insert "'agent-days-until-old group and/or topic parameter.\n")
118             (insert "\n")
119             (insert "If you would like, gnus can iterate over every group comparing its name to the\n")
120             (insert "regular expressions that you currently have in gnus-agent-expire-days.  When\n")
121             (insert "gnus finds a match, it will update that group's 'agent-days-until-old group\n")
122             (insert "parameter to the value associated with the regular expression.\n")
123             (insert "\n")
124             (insert "Whether gnus assigns group parameters, or not, gnus will terminate with an\n")
125             (insert "ERROR as soon as this function completes.  The reason is that you must\n")
126             (insert "manually edit your configuration to either not set gnus-agent-expire-days or\n")
127             (insert "to set it to an integer before gnus can be used.\n")
128             (insert "\n")
129             (insert "Once you have successfully edited gnus-agent-expire-days, gnus will be able to\n")
130             (insert "execute past this function.\n")
131             (insert "\n")
132             (insert "Should gnus use gnus-agent-expire-days to assign\n")
133             (insert "agent-days-until-old parameters to individual groups? (Y/N)")
134
135             (switch-to-buffer buffer)
136             (beep)
137             (beep)
138
139             (let ((echo-keystrokes 0)
140                   c)
141               (while (progn (setq c (read-char-exclusive))
142                             (cond ((or (eq c ?y) (eq c ?Y))
143                                          (save-excursion
144                                            (let ((groups (gnus-group-listed-groups)))
145                                              (while groups
146                                                (let* ((group (pop groups))
147                                                       (days gnus-agent-expire-days)
148                                                       (day (catch 'found
149                                                              (while days
150                                                                (when (eq 0 (string-match
151                                                                             (caar days)
152                                                                             group))
153                                                                  (throw 'found (cadar days)))
154                                                                (setq days (cdr days)))
155                                                              nil)))
156                                                  (when day
157                                                    (gnus-group-set-parameter group 'agent-days-until-old
158                                                                              day))))))
159                                    nil
160                                    )
161                                   ((or (eq c ?n) (eq c ?N))
162                                    nil)
163                                   (t
164                                    t))))))
165         (kill-buffer buffer))
166       (error "Change gnus-agent-expire-days to an integer for gnus to start."))))
167
168 ;; The gnus-agent-unlist-expire-days has its own conversion prompt.
169 ;; Therefore, hide the default prompt.
170 (gnus-convert-mark-converter-prompt 'gnus-agent-unlist-expire-days t)
171
172 (defun gnus-agent-unhook-expire-days (converting-to)
173   "Remove every lambda from gnus-group-prepare-hook that mention the
174 symbol gnus-agent-do-once in their definition.  This should NOT be
175 necessary as gnus-agent.el no longer adds them.  However, it is
176 possible that the hook was persistently saved."
177     (let ((h t)) ; iterate from bgn of hook
178       (while h
179         (let ((func (progn (when (eq h t)
180                              ;; init h to list of functions
181                              (setq h (cond ((listp gnus-group-prepare-hook)
182                                             gnus-group-prepare-hook)
183                                            ((boundp 'gnus-group-prepare-hook)
184                                             (list gnus-group-prepare-hook)))))
185                            (pop h))))
186
187           (when (cond ((eq (type-of func) 'compiled-function)
188                        ;; Search def. of compiled function for gnus-agent-do-once string
189                        (let* (definition 
190                                print-level 
191                                print-length
192                                (standard-output
193                                 (lambda (char)
194                                   (setq definition (cons char definition)))))
195                          (princ func) ; populates definition with reversed list of characters
196                          (let* ((i (length definition))
197                                 (s (make-string i 0)))
198                            (while definition
199                              (aset s (setq i (1- i)) (pop definition)))
200
201                            (string-match "\\bgnus-agent-do-once\\b" s))))
202                       ((listp func)
203                        (eq (cadr (nth 2 func)) 'gnus-agent-do-once) ; handles eval'd lambda
204                        ))
205
206             (remove-hook 'gnus-group-prepare-hook func)
207             ;; I don't what remove-hook is going to actually do to the
208             ;; hook list so start over from the beginning.
209             (setq h t))))))
210
211 ;; gnus-agent-unhook-expire-days is safe in that it does not modify
212 ;; the .newsrc.eld file.
213 (gnus-convert-mark-converter-prompt 'gnus-agent-unhook-expire-days t)
214
215 ;;; arch-tag: 845c7b8a-88f7-4468-b8d7-94e8fc72cf1a