legacy-gnus-agent.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. ;;; gnus-agent.el --- Legacy unplugged support for Gnus
  2. ;; Copyright (C) 2004-2015 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
  126. (gnus-format-message
  127. "\nIn order to use version `%s' of gnus, you will need to set\n"
  128. converting-to))
  129. (insert "gnus-agent-expire-days to an integer. If you still wish to set different\n")
  130. (insert "expiration days to individual groups, you must instead set the\n")
  131. (insert (gnus-format-message
  132. "`agent-days-until-old' group and/or topic parameter.\n"))
  133. (insert "\n")
  134. (insert "If you would like, gnus can iterate over every group comparing its name to the\n")
  135. (insert "regular expressions that you currently have in gnus-agent-expire-days. When\n")
  136. (insert (gnus-format-message
  137. "gnus finds a match, it will update that group's `agent-days-until-old' group\n"))
  138. (insert "parameter to the value associated with the regular expression.\n")
  139. (insert "\n")
  140. (insert "Whether gnus assigns group parameters, or not, gnus will terminate with an\n")
  141. (insert "ERROR as soon as this function completes. The reason is that you must\n")
  142. (insert "manually edit your configuration to either not set gnus-agent-expire-days or\n")
  143. (insert "to set it to an integer before gnus can be used.\n")
  144. (insert "\n")
  145. (insert "Once you have successfully edited gnus-agent-expire-days, gnus will be able to\n")
  146. (insert "execute past this function.\n")
  147. (insert "\n")
  148. (insert "Should gnus use gnus-agent-expire-days to assign\n")
  149. (insert "agent-days-until-old parameters to individual groups? (Y/N)")
  150. (switch-to-buffer buffer)
  151. (beep)
  152. (beep)
  153. (let ((echo-keystrokes 0)
  154. c)
  155. (while (progn (setq c (read-char-exclusive))
  156. (cond ((or (eq c ?y) (eq c ?Y))
  157. (save-excursion
  158. (let ((groups (gnus-group-listed-groups)))
  159. (while groups
  160. (let* ((group (pop groups))
  161. (days gnus-agent-expire-days)
  162. (day (catch 'found
  163. (while days
  164. (when (eq 0 (string-match
  165. (caar days)
  166. group))
  167. (throw 'found (cadr (car days))))
  168. (setq days (cdr days)))
  169. nil)))
  170. (when day
  171. (gnus-group-set-parameter group 'agent-days-until-old
  172. day))))))
  173. nil
  174. )
  175. ((or (eq c ?n) (eq c ?N))
  176. nil)
  177. (t
  178. t))))))
  179. (kill-buffer buffer))
  180. (error "Change gnus-agent-expire-days to an integer for gnus to start"))))
  181. ;; The gnus-agent-unlist-expire-days has its own conversion prompt.
  182. ;; Therefore, hide the default prompt.
  183. (gnus-convert-mark-converter-prompt 'gnus-agent-unlist-expire-days t)
  184. (defun gnus-agent-unhook-expire-days (converting-to)
  185. "Remove every lambda from `gnus-group-prepare-hook' that mention the
  186. symbol `gnus-agent-do-once' in their definition. This should NOT be
  187. necessary as gnus-agent.el no longer adds them. However, it is
  188. possible that the hook was persistently saved."
  189. (let ((h t)) ; Iterate from bgn of hook.
  190. (while h
  191. (let ((func (progn (when (eq h t)
  192. ;; Init h to list of functions.
  193. (setq h (cond ((listp gnus-group-prepare-hook)
  194. gnus-group-prepare-hook)
  195. ((boundp 'gnus-group-prepare-hook)
  196. (list gnus-group-prepare-hook)))))
  197. (pop h))))
  198. (when (cond ((byte-code-function-p func)
  199. ;; Search def. of compiled function for
  200. ;; gnus-agent-do-once string.
  201. (let* (definition
  202. print-level
  203. print-length
  204. (standard-output
  205. (lambda (char)
  206. (setq definition (cons char definition)))))
  207. (princ func) ; Populates definition with reversed list
  208. ; of characters.
  209. (let* ((i (length definition))
  210. (s (make-string i 0)))
  211. (while definition
  212. (aset s (setq i (1- i)) (pop definition)))
  213. (string-match "\\bgnus-agent-do-once\\b" s))))
  214. ((listp func)
  215. (eq (cadr (nth 2 func)) 'gnus-agent-do-once) ; Handles eval'd lambda.
  216. ))
  217. (remove-hook 'gnus-group-prepare-hook func)
  218. ;; I don't what remove-hook is going to actually do to the
  219. ;; hook list so start over from the beginning.
  220. (setq h t))))))
  221. ;; gnus-agent-unhook-expire-days is safe in that it does not modify
  222. ;; the .newsrc.eld file.
  223. (gnus-convert-mark-converter-prompt 'gnus-agent-unhook-expire-days t)
  224. (provide 'legacy-gnus-agent)
  225. ;;; legacy-gnus-agent.el ends here