legacy-gnus-agent.el 11 KB

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