home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / a2.0bemacs-src.lha / Emacs-19.25 / lisp / gnus-uu.el < prev    next >
Encoding:
Text File  |  1994-05-03  |  103.5 KB  |  2,928 lines

  1. ;;; gnus-uu.el --- extract, view or save (uu)encoded files from gnus
  2. ;; Copyright (C) 1985, 1986, 1987, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. ;; Author: Lars Ingebrigtsen <larsi@ifi.uio.no>
  5. ;; Created: 2 Oct 1993
  6. ;; Version: v2.7.2
  7. ;; Last Modified: 1994/05/03
  8. ;; Keyword: news
  9.  
  10. ;; This file is part of GNU Emacs.
  11.  
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. ;; GNU General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  24. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; All gnus-uu commands start with `C-c C-v'.
  29. ;;
  30. ;; Short user manual for this package:
  31. ;;
  32. ;; Type `C-c C-v C-v' to decode and view all articles of the current
  33. ;; series. The defaults should be reasonable for most systems.
  34. ;;
  35. ;; Type `C-c C-v C-i' to toggle interactive mode. When using
  36. ;; interactive mode, gnus-uu will which display a buffer that will let
  37. ;; you see the suggested commands to be executed.
  38. ;;
  39. ;; To post an uuencoded file, type `C-c C-v p', which will enter you
  40. ;; into a buffer analogous to the one you will get when typing `a'. Do
  41. ;; an `M-x describe-mode' in this buffer to get a description of what
  42. ;; this buffer lets you do.
  43. ;;
  44. ;; Read the documentation of the `gnus-uu' dummy function for a more
  45. ;; complete description of what this package does and how you can
  46. ;; customize it to fit your needs.
  47. ;; 
  48. ;;
  49. ;;
  50. ;; History
  51. ;;
  52. ;; v1.0: First version released Oct 2 1992.
  53. ;;
  54. ;; v1.1: Changed `C-c C-r' to `C-c C-e' and `C-c C-p' to `C-c C-k'.
  55. ;; Changed (setq gnus-exit-group-hook) to (add-hook).  Removed
  56. ;; checking for "Re:" for finding parts.
  57. ;;
  58. ;; v2.2: Fixed handling of currupted archives. Changed uudecoding to
  59. ;; an asynchronous process to avoid loading tons of data into emacs
  60. ;; buffers. No longer reads articles emacs already have aboard.  Fixed
  61. ;; a firmer support for shar files. Made regexp searches for files
  62. ;; more convenient. Added `C-c C-l' for editing uucode begin
  63. ;; lines. Added multi-system decoder entry point. Added interactive
  64. ;; view mode. Added function for decoding and saving all uuencoded
  65. ;; articles in the current newsgroup.
  66. ;;
  67. ;; v2.3: After suggestions I have changed all the gnus-uu key bindings
  68. ;; to avoid hogging all the user keys (C-c LETTER). Also added
  69. ;; (provide) and fixed some saving stuff. First posted version to
  70. ;; gnu.emacs.sources.
  71. ;;
  72. ;; v2.4: Fixed some more in the save-all category. Automatic fixing of
  73. ;; uucode "begin" lines: names on the form of "dir/file" are
  74. ;; translated into "dir-file". Added a function for fixing stripped
  75. ;; uucode articles. Added binhex save.
  76. ;;
  77. ;; v2.5: First version copyrighted by FSF. Changed lots of
  78. ;; documentation strings.
  79. ;;
  80. ;; v2.5.1: Added uuencode/posting code to post binary files. 
  81. ;;
  82. ;; v2.6: Thread support. gnus-uu is now able to decode uuencoded files
  83. ;; posted in threads. gnus-uu can also post in threads. I don't know
  84. ;; if this ability is of much use - I've never seen anyone post
  85. ;; uuencoded files in threads.
  86. ;;
  87. ;; v2.7: gnus-uu is now able to decode (and view/save) multiple
  88. ;; encoded files in one big gulp. Also added pseudo-mime support
  89. ;; (users can use metamail to view files), posting uuencoded/mime
  90. ;; files and various other bits and pieces.
  91. ;;
  92. ;; v2.7.1: New functions for decoding/saving threads bound to `C-c
  93. ;; C-v C-j'. Handy to save entire threads, not very useful for
  94. ;; decoding, as nobody posts encoded files in threads...
  95. ;;
  96. ;; V2.7.2: New functions for digesting and forwarding articles added
  97. ;; on the suggestion of Per Abrahamsen. Also added a function for
  98. ;; marking threads. 
  99.  
  100. ;;; Code: 
  101.  
  102. (require 'gnus)
  103. (require 'gnuspost)
  104.  
  105. ;; Binding of keys to the gnus-uu functions.
  106.  
  107. (defvar gnus-uu-ctl-map nil)
  108. (define-prefix-command 'gnus-uu-ctl-map)
  109. (define-key gnus-summary-mode-map "\C-c\C-v" gnus-uu-ctl-map)
  110.  
  111. (define-key gnus-uu-ctl-map "\C-v" 'gnus-uu-decode-and-view)
  112. (define-key gnus-uu-ctl-map "v" 'gnus-uu-decode-and-save)
  113. (define-key gnus-uu-ctl-map "\C-s" 'gnus-uu-shar-and-view)
  114. (define-key gnus-uu-ctl-map "s" 'gnus-uu-shar-and-save)
  115. (define-key gnus-uu-ctl-map "\C-m" 'gnus-uu-multi-decode-and-view)
  116. (define-key gnus-uu-ctl-map "m" 'gnus-uu-multi-decode-and-save)
  117.  
  118. (define-key gnus-uu-ctl-map "\C-b" 'gnus-uu-decode-and-show-in-buffer)
  119.  
  120. (define-key gnus-summary-mode-map "#" 'gnus-uu-mark-article)
  121. (define-key gnus-summary-mode-map "\M-#" 'gnus-uu-unmark-article)
  122. (define-key gnus-uu-ctl-map "\C-u" 'gnus-uu-unmark-all-articles)
  123. (define-key gnus-uu-ctl-map "\C-r" 'gnus-uu-mark-by-regexp)
  124. (define-key gnus-uu-ctl-map "r" 'gnus-uu-mark-by-regexp)
  125. (define-key gnus-uu-ctl-map "t" 'gnus-uu-mark-thread)
  126.  
  127. (define-key gnus-uu-ctl-map "\M-\C-v" 'gnus-uu-marked-decode-and-view)
  128. (define-key gnus-uu-ctl-map "\M-v" 'gnus-uu-marked-decode-and-save)
  129. (define-key gnus-uu-ctl-map "\M-\C-s" 'gnus-uu-marked-shar-and-view)
  130. (define-key gnus-uu-ctl-map "\M-s" 'gnus-uu-marked-shar-and-save)
  131. (define-key gnus-uu-ctl-map "\M-\C-m" 'gnus-uu-marked-multi-decode-and-view)
  132. (define-key gnus-uu-ctl-map "\M-m" 'gnus-uu-marked-multi-decode-and-save)
  133.  
  134. (define-key gnus-uu-ctl-map "f" 'gnus-uu-digest-and-forward)
  135. (define-key gnus-uu-ctl-map "\M-f" 'gnus-uu-marked-digest-and-forward)
  136.  
  137. (define-key gnus-uu-ctl-map "\C-i" 'gnus-uu-toggle-interactive-view)
  138. (define-key gnus-uu-ctl-map "\C-t" 'gnus-uu-toggle-any-variable)
  139.  
  140. (define-key gnus-uu-ctl-map "\C-l" 'gnus-uu-edit-begin-line)
  141.  
  142. (define-key gnus-uu-ctl-map "a" 'gnus-uu-decode-and-save-all-unread-articles)
  143. (define-key gnus-uu-ctl-map "w" 'gnus-uu-decode-and-save-all-articles)
  144. (define-key gnus-uu-ctl-map "\C-a" 'gnus-uu-decode-and-view-all-unread-articles)
  145. (define-key gnus-uu-ctl-map "\C-w" 'gnus-uu-decode-and-view-all-articles)
  146.  
  147. (define-key gnus-uu-ctl-map "\C-h" 'gnus-uu-threaded-decode-and-view)
  148. (define-key gnus-uu-ctl-map "h" 'gnus-uu-threaded-decode-and-save)
  149. (define-key gnus-uu-ctl-map "\C-j" 'gnus-uu-threaded-multi-decode-and-view)
  150. (define-key gnus-uu-ctl-map "j" 'gnus-uu-threaded-multi-decode-and-save)
  151.  
  152. (define-key gnus-uu-ctl-map "p" 'gnus-uu-post-news)
  153.  
  154. ;; Dummy function gnus-uu
  155.  
  156. (defun gnus-uu ()
  157.   "gnus-uu is a package for uudecoding and viewing articles.
  158.  
  159.  
  160. Keymap overview:
  161.  
  162. By default, all gnus-uu keystrokes begin with `C-c C-v'. 
  163.  
  164. There four decoding commands categories:
  165. All commands for viewing are `C-c C-v C-LETTER'.
  166. All commands for saving are `C-c C-v LETTER'.
  167. All commands for marked viewing are `C-c C-v C-M-LETTER'.
  168. All commands for marked saving are `C-c C-v M-LETTER'.
  169.  
  170. \\<gnus-summary-mode-map>\\[gnus-uu-decode-and-view]\tDecode and view articles
  171. \\[gnus-uu-decode-and-save]\tDecode and save articles
  172. \\[gnus-uu-shar-and-view]\tUnshar and view articles
  173. \\[gnus-uu-shar-and-save]\tUnshar and save articles
  174. \\[gnus-uu-multi-decode-and-view]\tChoose a decoding method, decode and view articles
  175. \\[gnus-uu-multi-decode-and-save]\tChoose a decoding method, decode and save articles
  176.  
  177. \\[gnus-uu-threaded-multi-decode-and-view]\tDecode a thread and view
  178. \\[gnus-uu-threaded-multi-decode-and-save]\tDecode a thread and save
  179.  
  180. \\[gnus-uu-decode-and-show-in-buffer]\tDecode the current article and view the result in a buffer
  181. \\[gnus-uu-edit-begin-line]\tEdit the 'begin' line of an uuencoded article
  182.  
  183. \\[gnus-uu-decode-and-save-all-unread-articles]\tDecode and save all unread articles
  184. \\[gnus-uu-decode-and-save-all-articles]\tDecode and save all articles
  185. \\[gnus-uu-decode-and-view-all-unread-articles]\tDecode and view all unread articles
  186. \\[gnus-uu-decode-and-view-all-articles]\tDecode and view all articles
  187.  
  188. \\[gnus-uu-digest-and-forward]\tDigest and forward a series of articles
  189. \\[gnus-uu-marked-digest-and-forward]\tDigest and forward all marked articles
  190.  
  191. \\[gnus-uu-mark-article]\tMark the current article for decoding
  192. \\[gnus-uu-unmark-article]\tUnmark the current article
  193. \\[gnus-uu-unmark-all-articles]\tUnmark all articles
  194. \\[gnus-uu-mark-by-regexp]\tMark articles for decoding by regexp
  195. \\[gnus-uu-mark-thread]\tMark articles in this thread
  196. \\[gnus-uu-marked-decode-and-view]\tDecode and view marked articles
  197. \\[gnus-uu-marked-decode-and-save]\tDecode and save marked articles
  198. \\[gnus-uu-marked-shar-and-view]\tUnshar and view marked articles
  199. \\[gnus-uu-marked-shar-and-save]\tUnshar and save marked articles
  200. \\[gnus-uu-marked-multi-decode-and-view]\tChoose decoding method, decode and view marked articles
  201. \\[gnus-uu-marked-multi-decode-and-save]\tChoose decoding method, decode and save marked articles
  202.  
  203. \\[gnus-uu-toggle-asynchronous]\tToggle asynchronous viewing mode
  204. \\[gnus-uu-toggle-query]\tToggle whether to ask before viewing a file
  205. \\[gnus-uu-toggle-always-ask]\tToggle whether to ask to save a file after viewing
  206. \\[gnus-uu-toggle-kill-carriage-return]\tToggle whether to strip trailing carriage returns
  207. \\[gnus-uu-toggle-interactive-view]\tToggle whether to use interactive viewing mode
  208. \\[gnus-uu-toggle-correct-stripped-articles]\tToggle whether to 'correct' articles
  209. \\[gnus-uu-toggle-view-with-metamail]\tToggle whether to use metamail for viewing 
  210. \\[gnus-uu-toggle-any-variable]\tToggle any of the things above
  211.  
  212. \\[gnus-uu-post-news]\tPost an uuencoded article
  213.  
  214. Function description:
  215.  
  216. `gnus-uu-decode-and-view' will try to find all articles in the same
  217. series, uudecode them and view the resulting file(s).
  218.  
  219. gnus-uu guesses what articles are in the series according to the
  220. following simplish rule: The subjects must be (nearly) identical,
  221. except for the last two numbers of the line. (Spaces are largely
  222. ignored, however.)
  223.  
  224. For example: If you choose a subject called 
  225.   \"cat.gif (2/3)\"
  226. gnus-uu will find all the articles that matches
  227.   \"^cat.gif ([0-9]+/[0-9]+).*$\".  
  228.  
  229. Subjects that are nonstandard, like 
  230.   \"cat.gif (2/3) Part 6 of a series\", 
  231. will not be properly recognized by any of the automatic viewing
  232. commands, and you have to mark the articles manually with '#'.
  233.  
  234. `gnus-uu-decode-and-save' will do the same as
  235. `gnus-uu-decode-and-view', except that it will not display the
  236. resulting file, but save it instead.
  237.  
  238. `gnus-uu-shar-and-view' and `gnus-uu-shar-and-save' are the \"shar\"
  239. equivalents to the uudecode functions. Instead of feeding the articles
  240. to uudecode, they are run through /bin/sh. Most shar files can be
  241. viewed and/or saved with the normal uudecode commands, which is much
  242. safer, as no foreign code is run.
  243.  
  244. Instead of having windows popping up automatically, it can be handy to
  245. view files interactivly, especially when viewing archives. Use
  246. `gnus-uu-toggle-interactive-mode' to toggle interactive mode.
  247.  
  248. `gnus-uu-mark-article' marks an article for later
  249. decoding/unsharing/saving/viewing. The files will be decoded in the
  250. sequence they were marked. To decode the files after you've marked the
  251. articles you are interested in, type the corresponding key strokes as
  252. the normal decoding commands, but put a `M-' in the last
  253. keystroke. For instance, to perform a standard uudecode and view, you
  254. would type `C-c C-v C-v'. To perform a marked uudecode and view, say
  255. `C-v C-v M-C-v'. All the other view and save commands are handled the
  256. same way; marked uudecode and save is then `C-c C-v M-v'.
  257.  
  258. `gnus-uu-unmark-article' will remove the mark from a previosly marked
  259. article.
  260.  
  261. `gnus-uu-unmark-all-articles' will remove the mark from all marked
  262. articles.
  263.  
  264. `gnus-uu-mark-by-regexp' will prompt for a regular expression and mark
  265. all articles matching that regular expression.
  266.  
  267. `gnus-uu-mark-thread' will mark all articles downward in the current
  268. thread.
  269.  
  270. There's an additional way to reach the decoding functions to make
  271. future expansions easier: `gnus-uu-multi-decode-and-view' and the
  272. corresponding save, marked view and marked save functions. You will be
  273. prompted for a decoding method, like uudecode, shar, binhex or plain
  274. save. Note that methods like binhex and save doesn't have view modes;
  275. even if you issue a view command (`C-c C-v C-m' and \"binhex\"),
  276. gnus-uu will just save the resulting binhex file.
  277.  
  278. `gnus-uu-decode-and-show-in-buffer' will decode the current article
  279. and display the results in an emacs buffer. This might be useful if
  280. there's jsut some text in the current article that has been uuencoded
  281. by some perverse poster.
  282.  
  283. `gnus-uu-decode-and-save-all-articles' looks at all the articles in
  284. the current newsgroup and tries to uudecode everything it can
  285. find. The user will be prompted for a directory where the resulting
  286. files (if any) will be
  287. saved. `gnus-uu-decode-and-save-unread-articles' does only checks
  288. unread articles. 
  289.  
  290. `gnus-uu-decode-and-view-all-articles' does the same as the function
  291. above, only viewing files instead of saving them. 
  292.  
  293. `gnus-uu-edit-begin-line' lets you edit the begin line of an uuencoded
  294. file in the current article. Useful to change a corrupted begin line.
  295.  
  296.  
  297. When using the view commands, `gnus-uu-decode-and-view' for instance,
  298. gnus-uu will (normally, see below) try to view the file according to
  299. the rules given in `gnus-uu-default-view-rules' and
  300. `gnus-uu-user-view-rules'. If it recognizes the file, it will display
  301. it immediately. If the file is some sort of archive, gnus-uu will
  302. attempt to unpack the archive and see if any of the files in the
  303. archive can be viewed. For instance, if you have a gzipped tar file
  304. \"pics.tar.gz\" containing the files \"pic1.jpg\" and \"pic2.gif\",
  305. gnus-uu will uncompress and detar the main file, and then view the two
  306. pictures. This unpacking process is recursive, so if the archive
  307. contains archives of archives, it'll all be unpacked.
  308.  
  309. If the view command doesn't recognise the file type, or can't view it
  310. because you don't have the viewer, or can't view *any* of the files in
  311. the archive, the user will be asked if she wishes to have the file
  312. saved somewhere. Note that if the decoded file is an archive, and
  313. gnus-uu manages to view some of the files in the archive, it won't
  314. tell the user that there were some files that were unviewable. Try
  315. interactive view for a different approach.
  316.  
  317.  
  318. Note that gnus-uu adds a function to `gnus-exit-group-hook' to clear
  319. the list of marked articles and check for any generated files that
  320. might have escaped deletion if the user typed `C-g' during viewing.
  321.  
  322.  
  323. `gnus-uu-toggle-asynchronous' toggles the `gnus-uu-asynchronous'
  324. variable.
  325.  
  326. `gnus-uu-toggle-query' toggles the `gnus-uu-ask-before-view'
  327. variable.
  328.  
  329. `gnus-uu-toggle-always-ask' toggles the `gnus-uu-view-and-save'
  330. variable.
  331.  
  332. `gnus-uu-toggle-kill-carriage-return' toggles the
  333. `gnus-uu-kill-carriage-return' variable.
  334.  
  335. `gnus-uu-toggle-interactive-view' toggles interactive mode. If it is
  336. turned on, gnus-uu won't view files immediately, but will give you a
  337. buffer with the default commands and files and let you edit the
  338. commands and execute them at leisure.
  339.  
  340. `gnus-uu-toggle-correct-stripped-articles' toggles whether to check
  341. and correct uuencoded articles that may have had trailing spaces
  342. stripped by mailers.
  343.  
  344. `gnus-uu-toggle-view-with-metamail' toggles whether to skip the
  345. gnus-uu viewing methods and just guess at an content-type based on the
  346. file name suffix and feed it to metamail.
  347.  
  348. `gnus-uu-toggle-any-variable' is an interface to the toggle commands
  349. listed above.
  350.  
  351.  
  352. Customization
  353.  
  354.    Rule Variables
  355.  
  356.    gnus-uu uses \"rule\" variables to decide how to view a file. All
  357.    these variables are of the form
  358.   
  359.       (list '(regexp1 command2)
  360.             '(regexp2 command2)
  361.             ...)
  362.  
  363.    `gnus-uu-user-view-rules'
  364.      This variable is consulted first when viewing files. If you wish
  365.      to use, for instance, sox to convert an .au sound file, you could
  366.      say something like:
  367.  
  368.        (setq gnus-uu-user-view-rules
  369.          (list '(\"\\\\.au$\" \"sox %s -t .aiff > /dev/audio\")))
  370.  
  371.    `gnus-uu-user-view-rules-end'
  372.      This variable is consulted if gnus-uu couldn't make any matches
  373.      from the user and default view rules.
  374.  
  375.    `gnus-uu-user-interactive-view-rules'
  376.      This is the variable used instead of `gnus-uu-user-view-rules'
  377.      when in interactive mode.
  378.  
  379.    `gnus-uu-user-interactive-view-rules-end'
  380.      This variable is used instead of `gnus-uu-user-view-rules-end'
  381.      when in interactive mode.
  382.  
  383.    `gnus-uu-user-archive-rules`
  384.      This variable can be used to say what comamnds should be used to
  385.      unpack archives.
  386.  
  387.    
  388.    Other Variables
  389.  
  390.    `gnus-uu-tmp-dir'
  391.      Where gnus-uu does its work.
  392.  
  393.    `gnus-uu-do-not-unpack-archives'
  394.      Non-nil means that gnus-uu won't peek inside archives looking for
  395.      files to dispay.
  396.  
  397.    `gnus-uu-view-and-save'
  398.      Non-nil means that the user will always be asked to save a file
  399.      after viewing it.
  400.  
  401.    `gnus-uu-asynchronous' 
  402.      Non-nil means that files will be viewed asynchronously.  This can
  403.      be useful if you're viewing long .mod files, for instance, which
  404.      often takes several minutes. Note, however, that since gnus-uu
  405.      doesn't ask, and if you are viewing an archive with lots of
  406.      viewable files, you'll get them all up more or less at once,
  407.      which can be confusing, to say the least. To get gnus-uu to ask
  408.      you before viewing a file, set the `gnus-uu-ask-before-view' 
  409.      variable.
  410.  
  411.    `gnus-uu-ask-before-view'
  412.      Non-nil means that gnus-uu will ask you before viewing each file
  413.  
  414.    `gnus-uu-ignore-default-view-rules'
  415.      Non-nil means that gnus-uu will ignore the default viewing rules.
  416.  
  417.    `gnus-uu-ignore-default-archive-rules'
  418.      Non-nil means that gnus-uu will ignore the default archive
  419.      unpacking commands.
  420.  
  421.    `gnus-uu-kill-carriage-return'
  422.      Non-nil means that gnus-uu will strip all carriage returns from
  423.      articles.
  424.  
  425.    `gnus-uu-unmark-articles-not-decoded'
  426.      Non-nil means that gnus-uu will mark articles that were
  427.      unsuccessfully decoded as unread.
  428.  
  429.    `gnus-uu-output-window-height'
  430.      This variable says how tall the output buffer window is to be
  431.      when using interactive view mode.
  432.  
  433.    `gnus-uu-correct-stripped-uucode'
  434.      Non-nil means that gnus-uu will *try* to fix uuencoded files that
  435.      have had traling spaces deleted.
  436.  
  437.    `gnus-uu-use-interactive-view'
  438.      Non-nil means that gnus-uu will use interactive viewing mode.
  439.  
  440.    `gnus-uu-view-with-metamail'
  441.      Non-nil means that gnus-uu will ignore the viewing commands
  442.      defined by the rule variables and just fudge a MIME content type
  443.      based on the file name. The result will be fed to metamail for
  444.      viewing.
  445.  
  446.    `gnus-uu-save-in-digest'
  447.      Non-nil means that gnus-uu, when asked to save without decoding,
  448.      will save in digests.  If this variable is nil, gnus-uu will just
  449.      save everything in a file without any embellishments. The
  450.      digesting almost conforms to RFC1153 - no easy way to specify any
  451.      meaningful volume and issue numbers were found, so I simply
  452.      dropped them.
  453.  
  454.    `gnus-uu-post-include-before-composing'
  455.      Non-nil means that gnus-uu will ask for a file to encode before
  456.      you compose the article.  If this variable is t, you can either
  457.      include an encoded file with \\<gnus-uu-post-reply-mode-map>\\[gnus-uu-post-insert-binary-in-article] or have one included for you when you 
  458.      post the article.
  459.  
  460.    `gnus-uu-post-length'
  461.      Maximum length of an article.  The encoded file will be split
  462.      into how many articles it takes to post the entire file.
  463.  
  464.    `gnus-uu-post-threaded'
  465.      Non-nil means that gnus-uu will post the encoded file in a
  466.      thread.  This may not be smart, as no other decoder I have seen
  467.      are able to follow threads when collecting uuencoded
  468.      articles. (Well, I have seen one package that does that -
  469.      gnus-uu, but somehow, I don't think that counts...) Default is
  470.      nil.
  471.  
  472.    `gnus-uu-post-separate-description'
  473.      Non-nil means that the description will be posted in a separate
  474.      article.  The first article will typically be numbered (0/x). If
  475.      this variable is nil, the description the user enters will be
  476.      included at the beginning of the first article, which will be
  477.      numbered (1/x). Default is t.
  478. "
  479.   (interactive)
  480.   )
  481.  
  482. ;; Default viewing action rules
  483.  
  484. (defvar gnus-uu-default-view-rules 
  485.   (list 
  486.    '("\\.\\(jpe?g\\|gif\\|tiff?\\|p[pgb]m\\|xwd\\|xbm\\|pcx\\)$" "xv")
  487.    '("\\.tga$" "tgatoppm %s | xv -")
  488.    '("\\.te?xt$\\|\\.doc$\\|read.*me" "xterm -e less")
  489.    '("\\.\\(wav\\|aiff\\|hcom\\|u[blw]\\|s[bfw]\\|voc\\|smp\\)$" 
  490.      "sox -v .5 %s -t .au -u - > /dev/audio")
  491.    '("\\.au$" "cat %s > /dev/audio")
  492.    '("\\.mod$" "str32")
  493.    '("\\.ps$" "ghostview")
  494.    '("\\.dvi$" "xdvi")
  495.    '("\\.[1-6]$" "xterm -e man -l")
  496.    '("\\.html$" "xmosaic")
  497.    '("\\.mpe?g$" "mpeg_play")
  498.    '("\\.fli$" "xflick")
  499.    '("\\.flc$" "xanim")
  500.    '("\\.\\(tar\\|arj\\|zip\\|zoo\\|arc\\|gz\\|Z\\|lzh\\|ar\\|lha\\)$" 
  501.      "gnus-uu-archive"))
  502.  
  503.   "Default actions to be taken when the user asks to view a file.  
  504. To change the behaviour, you can either edit this variable or set
  505. `gnus-uu-user-view-rules' to something useful.
  506.  
  507. For example:
  508.  
  509. To make gnus-uu use 'xli' to display JPEG and GIF files, put the
  510. following in your .emacs file
  511.  
  512.   (setq gnus-uu-user-view-rules (list '(\"jpg$\\\\|gif$\" \"xli\")))
  513.  
  514. Both these variables are lists of lists with two string elements. The
  515. first string is a regular expression. If the file name matches this
  516. regular expression, the command in the second string is executed with
  517. the file as an argument.
  518.  
  519. If the command string contains \"%s\", the file name will be inserted
  520. at that point in the command string. If there's no \"%s\" in the
  521. command string, the file name will be appended to the command string
  522. before executing.
  523.  
  524. There are several user variables to tailor the behaviour of gnus-uu to
  525. your needs. First we have `gnus-uu-user-view-rules', which is the
  526. variable gnus-uu first consults when trying to decide how to view a
  527. file. If this variable contains no matches, gnus-uu examines the
  528. default rule vaiable provided in this package. If gnus-uu finds no
  529. match here, it uses `gnus-uu-user-view-rules-end' to try to make a
  530. match.
  531.  
  532. Unless, of course, you are using the interactive view mode. Then
  533. `gnus-uu-user-interactive-view-rules' and
  534. `gnus-uu-user-interactive-view-rules-end' will be used instead.")
  535.  
  536. (defvar gnus-uu-user-view-rules nil 
  537.   "Variable detailing what actions are to be taken to view a file.
  538. See the documentation on the `gnus-uu-default-view-rules' variable for 
  539. details.")
  540.  
  541. (defvar gnus-uu-user-view-rules-end nil
  542.   "Variable saying what actions are to be taken if no rule matched the file name.
  543. See the documentation on the `gnus-uu-default-view-rules' variable for 
  544. details.")
  545.  
  546. (defvar gnus-uu-user-interactive-view-rules nil
  547.   "Variable detailing what actions are to be taken to view a file when using interactive mode.
  548. See the documentation on the `gnus-uu-default-view-rules' variable for 
  549. details.")
  550.  
  551. (defvar gnus-uu-user-interactive-view-rules-end nil
  552.   "Variable saying what actions are to be taken if no rule matched the file name when using interactive mode.
  553. See the documentation on the `gnus-uu-default-view-rules' variable for 
  554. details.")
  555.  
  556. (defvar gnus-uu-default-interactive-view-rules-begin
  557.   (list
  558.    '("\\.te?xt$\\|\\.doc$\\|read.*me\\|\\.c?$\\|\\.h$\\|\\.bat$\\|\\.asm$\\|makefile" "cat %s | sed s/\r//g")
  559.    '("\\.pas$" "cat %s | sed s/\r//g")
  560.    ))
  561.  
  562. (defvar gnus-uu-default-interactive-view-rules-end
  563.   (list
  564.    '(".*" "file")))
  565.  
  566. ;; Default unpacking commands
  567.  
  568. (defvar gnus-uu-default-archive-rules 
  569.   (list '("\\.tar$" "tar xf")
  570.     '("\\.zip$" "unzip -o")
  571.     '("\\.ar$" "ar x")
  572.     '("\\.arj$" "unarj x")
  573.     '("\\.zoo$" "zoo -e")
  574.     '("\\.\\(lzh\\|lha\\)$" "lha x")
  575.     '("\\.Z$" "uncompress")
  576.     '("\\.gz$" "gunzip")
  577.     '("\\.arc$" "arc -x"))
  578.   )
  579.  
  580. (defvar gnus-uu-destructive-archivers 
  581.   (list "uncompress" "gunzip"))
  582.  
  583. (defvar gnus-uu-user-archive-rules nil
  584.   "A list that can be set to override the default archive unpacking commands.
  585. To use, for instance, 'untar' to unpack tar files and 'zip -x' to
  586. unpack zip files, say the following:
  587.   (setq gnus-uu-user-archive-rules 
  588.     (list '(\"\\\\.tar$\" \"untar\")
  589.           '(\"\\\\.zip$\" \"zip -x\")))"
  590.   )
  591.  
  592. ;; Pseudo-MIME support
  593.  
  594. (defconst gnus-uu-ext-to-mime-list
  595.   (list '("\\.gif$" "image/gif")
  596.     '("\\.jpe?g$" "image/jpeg")
  597.     '("\\.tiff?$" "image/tiff")
  598.     '("\\.xwd$" "image/xwd")
  599.     '("\\.pbm$" "image/pbm")
  600.     '("\\.pgm$" "image/pgm")
  601.     '("\\.ppm$" "image/ppm")
  602.     '("\\.xbm$" "image/xbm")
  603.     '("\\.pcx$" "image/pcx")
  604.     '("\\.tga$" "image/tga")
  605.     '("\\.ps$" "image/postscript")
  606.     '("\\.fli$" "video/xflick")
  607.     '("\\.wav$" "audio/wav")
  608.     '("\\.aiff$" "audio/aiff")
  609.     '("\\.hcom$" "audio/hcom")
  610.     '("\\.voc$" "audio/voc")
  611.     '("\\.smp$" "audio/smp")
  612.     '("\\.mod$" "audio/mod")
  613.     '("\\.dvi$" "image/dvi")
  614.     '("\\.mpe?g$" "video/mpeg")
  615.     '("\\.au$" "audio/basic")
  616.     '("\\.\\(te?xt\\|doc\\|c\\|h\\)$" "text/plain")
  617.     '("read.*me" "text/plain")
  618.     '("\\.html$" "text/html")
  619.     '("\\..*$" "unknown/unknown")))
  620.  
  621. ;; Various variables users may set 
  622.  
  623. (defvar gnus-uu-tmp-dir "/tmp/" 
  624.   "Variable saying where gnus-uu is to do its work.
  625. Default is \"/tmp/\".")
  626.  
  627. (defvar gnus-uu-do-not-unpack-archives nil 
  628.   "Non-nil means that gnus-uu won't peek inside archives looking for files to dispay. 
  629. Default is nil.")
  630.  
  631. (defvar gnus-uu-view-and-save nil 
  632.   "Non-nil means that the user will always be asked to save a file after viewing it.
  633. If the variable is nil, the suer will only be asked to save if the
  634. viewing is unsuccessful. Default is nil.")
  635.  
  636. (defvar gnus-uu-asynchronous nil
  637.   "Non-nil means that files will be viewed asynchronously.
  638. Default is nil.")
  639.  
  640. (defvar gnus-uu-ask-before-view nil
  641.   "Non-nil means that gnus-uu will ask you before viewing each file. 
  642. Especially useful when `gnus-uu-asynchronous' is set. Default is
  643. nil.")
  644.  
  645. (defvar gnus-uu-ignore-default-view-rules nil
  646.   "Non-nil means that gnus-uu will ignore the default viewing rules.
  647. Only the user viewing rules will be consulted. Default is nil.")
  648.  
  649. (defvar gnus-uu-ignore-default-archive-rules nil 
  650.   "Non-nil means that gnus-uu will ignore the default archive unpacking commands.  
  651. Only the user unpacking commands will be consulted. Default is nil.")
  652.  
  653. (defvar gnus-uu-kill-carriage-return t
  654.   "Non-nil means that gnus-uu will strip all carriage returns from articles.
  655. Default is t.")
  656.  
  657. (defvar gnus-uu-view-with-metamail nil
  658.   "Non-nil means that files will be viewed with metamail.
  659. The gnus-uu viewing functions will be ignored and gnus-uu will try
  660. to guess at a content-type based on file name suffixes. Default
  661. it nil.")
  662.  
  663. (defvar gnus-uu-unmark-articles-not-decoded nil
  664.   "Non-nil means that gnus-uu will mark articles that were unsuccessfully decoded as unread. 
  665. Default is nil.")
  666.  
  667. (defvar gnus-uu-output-window-height 20 
  668.   "This variable says how tall the output buffer window is to be when using interactive view mode. 
  669. Change it at your convenience. Default is 20.")
  670.  
  671. (defvar gnus-uu-correct-stripped-uucode nil
  672.   "Non-nil means that gnus-uu will *try* to fix uuencoded files that have had traling spaces deleted. 
  673. Default is nil.")
  674.  
  675. (defvar gnus-uu-use-interactive-view nil
  676.   "Non-nil means that gnus-uu will use interactive viewing mode.
  677. Gnus-uu will create a special buffer where the user may choose
  678. interactively which files to view and how. Default is nil.")
  679.  
  680. (defvar gnus-uu-save-in-digest nil
  681.   "Non-nil means that gnus-uu, when asked to save without decoding, will save in digests.
  682. If this variable is nil, gnus-uu will just save everything in a 
  683. file without any embellishments. The digesting almost conforms to RFC1153 -
  684. no easy way to specify any meaningful volume and issue numbers were found, 
  685. so I simply dropped them.")
  686.  
  687.  
  688. ;; Internal variables
  689.  
  690. (defconst gnus-uu-begin-string "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
  691. (defconst gnus-uu-end-string "^end[ \t]*$")
  692.  
  693. (defconst gnus-uu-body-line "^M")
  694. (let ((i 61))
  695.   (while (> (setq i (1- i)) 0)
  696.     (setq gnus-uu-body-line (concat gnus-uu-body-line "[^a-z]")))
  697.   (setq gnus-uu-body-line (concat gnus-uu-body-line ".?$")))
  698.  
  699. ;"^M.............................................................?$"
  700.  
  701. (defconst gnus-uu-shar-begin-string "^#! */bin/sh")
  702.  
  703. (defvar gnus-uu-shar-file-name nil)
  704. (defconst gnus-uu-shar-name-marker "begin [0-7][0-7][0-7][ \t]+\\(\\(\\w\\|\\.\\)*\\b\\)")
  705. (defvar gnus-uu-shar-directory nil)
  706.  
  707. (defvar gnus-uu-file-name nil)
  708. (defconst gnus-uu-uudecode-process nil)
  709.  
  710. (defvar gnus-uu-interactive-file-list nil)
  711. (defvar gnus-uu-marked-article-list nil)
  712. (defvar gnus-uu-generated-file-list nil)
  713. (defvar gnus-uu-work-dir nil)
  714.  
  715. (defconst gnus-uu-interactive-buffer-name "*gnus-uu interactive*")
  716. (defconst gnus-uu-output-buffer-name "*Gnus UU Output*")
  717. (defconst gnus-uu-result-buffer "*Gnus UU Result Buffer*")
  718.  
  719. (defconst gnus-uu-error-during-unarching nil)
  720.  
  721. ;; Interactive functions
  722.  
  723. ;; UUdecode and view
  724.  
  725. (defun gnus-uu-decode-and-view ()
  726.   "UUdecodes and 'views' (if possible) the resulting file.
  727. 'Viewing' can be any action at all, as defined in the
  728. `gnus-uu-file-action-list' variable. Running 'xv' on gifs and 'cat
  729. >/dev/audio' on au files are popular actions. If the file can't be
  730. viewed, the user is asked if she would like to save the file instead."
  731.   (interactive) 
  732.   (gnus-uu-decode-and-view-or-save t nil))
  733.  
  734. (defun gnus-uu-decode-and-save ()
  735.   "Decodes and saves the resulting file."
  736.   (interactive)
  737.   (gnus-uu-decode-and-view-or-save nil nil))
  738.  
  739. (defun gnus-uu-marked-decode-and-view ()
  740.   "Decodes and views articles marked.
  741. The marked equivalent to `gnus-uu-decode-and-view'."
  742.   (interactive)
  743.   (gnus-uu-decode-and-view-or-save t t))
  744.  
  745. (defun gnus-uu-marked-decode-and-save ()
  746.   "Decodes and saves articles marked.
  747. The marked equivalent to `gnus-uu-decode-and-save'."
  748.   (interactive)
  749.   (gnus-uu-decode-and-view-or-save nil t))
  750.       
  751.  
  752. ;; Unshar and view
  753.  
  754. (defun gnus-uu-shar-and-view ()
  755.   "Unshars and views articles.
  756. The shar equivalent of `gnus-uu-decode-and-view'."
  757.   (interactive)
  758.   (gnus-uu-unshar-and-view-or-save t nil))
  759.  
  760. (defun gnus-uu-shar-and-save ()
  761.   "Unshars and saves files.
  762. The shar equivalent to `gnus-uu-decode-and-save'."
  763.   (interactive)
  764.   (gnus-uu-unshar-and-view-or-save nil nil))
  765.  
  766. (defun gnus-uu-marked-shar-and-view ()
  767.   "Unshars and views articles marked.
  768. The marked equivalent to `gnus-uu-shar-and-view'."
  769.   (interactive)
  770.   (gnus-uu-unshar-and-view-or-save t t))
  771.  
  772. (defun gnus-uu-marked-shar-and-save ()
  773.   "Unshars and saves articles marked.
  774. The marked equivalent to `gnus-uu-shar-and-save'."
  775.   (interactive)
  776.   (gnus-uu-unshar-and-view-or-save nil t))
  777.  
  778. ;; Threaded decode
  779.  
  780. (defun gnus-uu-threaded-decode-and-view ()
  781.   "Decodes and saves the resulting file."
  782.   (interactive)
  783.   (gnus-uu-threaded-decode-and-view-or-save t))
  784.  
  785. (defun gnus-uu-threaded-decode-and-save ()
  786.   "Decodes and saves the resulting file."
  787.   (interactive)
  788.   (gnus-uu-threaded-decode-and-view-or-save nil))
  789.  
  790. (defun gnus-uu-threaded-multi-decode-and-view ()
  791.   "Decodes and saves the resulting file."
  792.   (interactive)
  793.   (gnus-uu-threaded-multi-decode-and-view-or-save t))
  794.  
  795. (defun gnus-uu-threaded-multi-decode-and-save ()
  796.   "Decodes and saves the resulting file."
  797.   (interactive)
  798.   (gnus-uu-threaded-multi-decode-and-view-or-save nil))
  799.  
  800. (defun gnus-uu-threaded-decode-and-view-or-save (&optional view)
  801.   (gnus-uu-unmark-all-articles)
  802.   (gnus-uu-mark-thread)
  803.   (gnus-uu-decode-and-view-or-save view t))
  804.  
  805. (defun gnus-uu-threaded-multi-decode-and-view-or-save (view)
  806.   (let (type)
  807.     (message "Decode type: [u]udecode, (s)har, s(a)ve, (b)inhex: ")
  808.     (setq type (read-char))
  809.     (if (not (or (= type ?u) (= type ?s) (= type ?b) (= type ?a)))
  810.     (error "No such decoding method '%c'" type))
  811.     
  812.     (gnus-uu-unmark-all-articles)
  813.     (gnus-uu-mark-thread)
  814.  
  815.     (if (= type ?\r) (setq type ?u))
  816.     (cond ((= type ?u) (gnus-uu-decode-and-view-or-save view t))
  817.       ((= type ?s) (gnus-uu-unshar-and-view-or-save view t))
  818.       ((= type ?b) (gnus-uu-binhex-and-save view t))
  819.       ((= type ?a) (gnus-uu-save-articles view t)))))
  820.     
  821.  
  822. ;; Toggle commands      
  823.  
  824. (defun gnus-uu-toggle-asynchronous ()
  825.   "This function toggles asynchronous viewing."
  826.   (interactive)
  827.   (if (setq gnus-uu-asynchronous (not gnus-uu-asynchronous))
  828.       (message "gnus-uu will now view files asynchronously")
  829.     (message "gnus-uu will now view files synchronously")))
  830.  
  831. (defun gnus-uu-toggle-query ()
  832.   "This function toggles whether to ask before viewing or not."
  833.   (interactive)
  834.   (if (setq gnus-uu-ask-before-view (not gnus-uu-ask-before-view))
  835.       (message "gnus-uu will now ask before viewing")
  836.     (message "gnus-uu will now view without asking first")))
  837.  
  838. (defun gnus-uu-toggle-always-ask ()
  839.   "This function toggles whether to always ask to save a file after viewing."
  840.   (interactive)
  841.   (if (setq gnus-uu-view-and-save (not gnus-uu-view-and-save))
  842.       (message "gnus-uu will now ask to save the file after viewing")
  843.     (message "gnus-uu will now not ask to save after successful viewing")))
  844.  
  845. (defun gnus-uu-toggle-interactive-view ()
  846.   "This function toggles whether to use interactive view."
  847.   (interactive)
  848.   (if (setq gnus-uu-use-interactive-view (not gnus-uu-use-interactive-view))
  849.       (message "gnus-uu will now use interactive view")
  850.     (message "gnus-uu will now use non-interactive view")))
  851.  
  852. (defun gnus-uu-toggle-unmark-undecoded ()
  853.   "This function toggles whether to unmark articles not decoded."
  854.   (interactive)
  855.   (if (setq gnus-uu-unmark-articles-not-decoded 
  856.         (not gnus-uu-unmark-articles-not-decoded))
  857.       (message "gnus-uu will now unmark articles not decoded")
  858.     (message "gnus-uu will now not unmark articles not decoded")))
  859.  
  860. (defun gnus-uu-toggle-kill-carriage-return ()
  861.   "This function toggles the stripping of carriage returns from the articles."
  862.   (interactive)
  863.   (if (setq gnus-uu-kill-carriage-return (not gnus-uu-kill-carriage-return))
  864.       (message "gnus-uu will now strip carriage returns")
  865.     (message "gnus-uu won't strip carriage returns")))
  866.  
  867. (defun gnus-uu-toggle-view-with-metamail ()
  868.   "This function toggles whether to view files with metamail."
  869.   (interactive)
  870.   (if (setq gnus-uu-view-with-metamail (not gnus-uu-view-with-metamail))
  871.       (message "gnus-uu will now view with metamail")
  872.     (message "gnus-uu will now view with the gnus-uu viewing functions")))
  873.  
  874. (defun gnus-uu-toggle-correct-stripped-uucode ()
  875.   "This function toggles whether to correct stripped uucode."
  876.   (interactive)
  877.   (if (setq gnus-uu-correct-stripped-uucode 
  878.         (not gnus-uu-correct-stripped-uucode))
  879.       (message "gnus-uu will now correct stripped uucode")
  880.     (message "gnus-uu won't check and correct stripped uucode")))
  881.  
  882. (defun gnus-uu-toggle-any-variable ()
  883.   "This function ask what variable the user wants to toggle."
  884.   (interactive)
  885.   (let (rep)
  886.     (message "(a)sync, (q)uery, (p)ask, (k)ill CR, (i)nteract, (u)nmark, (c)orrect, (m)eta")
  887.     (setq rep (read-char))
  888.     (if (= rep ?a)
  889.     (gnus-uu-toggle-asynchronous))
  890.     (if (= rep ?q)
  891.     (gnus-uu-toggle-query))
  892.     (if (= rep ?p)
  893.     (gnus-uu-toggle-always-ask))
  894.     (if (= rep ?k)
  895.     (gnus-uu-toggle-kill-carriage-return))
  896.     (if (= rep ?u)
  897.     (gnus-uu-toggle-unmark-undecoded))
  898.     (if (= rep ?c)
  899.     (gnus-uu-toggle-correct-stripped-uucode))
  900.     (if (= rep ?m)
  901.     (gnus-uu-toggle-view-with-metamail))
  902.     (if (= rep ?i)
  903.     (gnus-uu-toggle-interactive-view))))
  904.  
  905.  
  906. ;; Misc interactive functions
  907.  
  908. (defun gnus-uu-decode-and-show-in-buffer ()
  909.   "Uudecodes the current article and displays the result in a buffer.
  910. Might be useful if someone has, for instance, some text uuencoded in
  911. their sigs. (Stranger things have happened.)"
  912.   (interactive)
  913.   (gnus-uu-initialize)
  914.   (let ((uu-buffer (get-buffer-create gnus-uu-output-buffer-name))
  915.     file-name)
  916.     (save-excursion
  917.       (and 
  918.        (gnus-summary-select-article)
  919.        (gnus-uu-grab-articles (list gnus-current-article) 
  920.                   'gnus-uu-uustrip-article-as)
  921.        (setq file-name (concat gnus-uu-work-dir gnus-uu-file-name))
  922.        (progn
  923.      (save-excursion
  924.        (set-buffer uu-buffer)
  925.        (erase-buffer)
  926.        (insert-file-contents file-name))
  927.      (set-window-buffer (get-buffer-window gnus-article-buffer) 
  928.                 uu-buffer)
  929.      (message "Showing file %s in buffer" file-name)
  930.      (delete-file file-name))))))
  931.  
  932. (defun gnus-uu-edit-begin-line ()
  933.   "Edit the begin line of the current article."
  934.   (interactive)
  935.   (let ((buffer-read-only nil)
  936.     begin b)
  937.     (save-excursion
  938.       (gnus-summary-select-article)
  939.       (set-buffer gnus-article-buffer)
  940.       (goto-line 1)
  941.       (if (not (re-search-forward "begin " nil t))
  942.       (error "No begin line in the current article")
  943.     (beginning-of-line)
  944.     (setq b (point))
  945.     (end-of-line)
  946.     (setq begin (buffer-substring b (point)))
  947.     (setq begin (read-string "" begin))
  948.     (setq buffer-read-only nil)
  949.     (delete-region b (point))
  950.     (insert-string begin)))))
  951.  
  952.  
  953. ;; Multi functions
  954.  
  955. (defun gnus-uu-multi-decode-and-view ()
  956.   "Choose a method of decoding and then decode and view.
  957. This function lets the user decide what method to use for decoding.
  958. Other than that, it's equivalent to the other decode-and-view
  959. functions."
  960.   (interactive)
  961.   (gnus-uu-multi-decode-and-view-or-save t nil))
  962.  
  963. (defun gnus-uu-multi-decode-and-save ()
  964.   "Choose a method of decoding and then decode and save.
  965. This function lets the user decide what method to use for decoding.
  966. Other than that, it's equivalent to the other decode-and-save 
  967. functions."
  968.   (interactive)
  969.   (gnus-uu-multi-decode-and-view-or-save nil nil))
  970.  
  971. (defun gnus-uu-marked-multi-decode-and-view ()
  972.   "Choose a method of decoding and then decode and view the marked articles.
  973. This function lets the user decide what method to use for decoding.
  974. Other than that, it's equivalent to the other marked decode-and-view 
  975. functions."
  976.   (interactive)
  977.   (gnus-uu-multi-decode-and-view-or-save t t))
  978.  
  979. (defun gnus-uu-marked-multi-decode-and-save ()
  980.   "Choose a method of decoding and then decode and save the marked articles.
  981. This function lets the user decide what method to use for decoding.
  982. Other than that, it's equivalent to the other marked decode-and-save 
  983. functions."
  984.   (interactive)
  985.   (gnus-uu-multi-decode-and-view-or-save t t))
  986.  
  987. (defun gnus-uu-multi-decode-and-view-or-save (view marked)
  988.   (let (type)
  989.     (message "[u]udecode, (s)har, s(a)ve, (b)inhex: ")
  990.     (setq type (read-char))
  991.     (if (= type ?\r) (setq type ?u))
  992.     (cond ((= type ?u) (gnus-uu-decode-and-view-or-save view marked))
  993.       ((= type ?s) (gnus-uu-unshar-and-view-or-save view marked))
  994.       ((= type ?b) (gnus-uu-binhex-and-save view marked))
  995.       ((= type ?a) (gnus-uu-save-articles view marked))
  996.       (t (error "Unknown decode method '%c'." type)))))
  997.  
  998.  
  999. ;; "All articles" commands
  1000.  
  1001. (defconst gnus-uu-rest-of-articles nil)
  1002. (defvar gnus-uu-current-save-dir nil)
  1003.  
  1004. (defun gnus-uu-decode-and-view-all-articles (&optional unread)
  1005.   "Try to decode all articles and view the result."
  1006.   (interactive)
  1007.   (if (not (setq gnus-uu-marked-article-list 
  1008.          (nreverse (gnus-uu-get-list-of-articles 
  1009.                 "^." nil unread t))))
  1010.       (error "No%s articles to be decoded" (if unread " unread" "")))
  1011.   (gnus-uu-decode-and-view-or-save t t))
  1012.  
  1013. (defun gnus-uu-decode-and-view-all-unread-articles (&optional unread)
  1014.   "Try to decode all unread articles and view the result."
  1015.   (interactive)
  1016.   (gnus-uu-decode-and-view-all-articles t))
  1017.  
  1018. (defun gnus-uu-decode-and-save-all-unread-articles ()
  1019.   "Try to decode all unread articles and saves the result.
  1020. This function reads all unread articles in the current group and sees
  1021. whether it can uudecode the articles. The user will be prompted for an
  1022. directory to put the resulting (if any) files."
  1023.   (interactive)
  1024.   (gnus-uu-decode-and-save-articles t t))
  1025.  
  1026. (defun gnus-uu-decode-and-save-all-articles ()
  1027.   "Try to decode all articles and saves the result.
  1028. Does the same as `gnus-uu-decode-and-save-all-unread-articles', except
  1029. that it grabs all articles visible, unread or not."
  1030.   (interactive)
  1031.   (gnus-uu-decode-and-save-articles nil t))
  1032.  
  1033. (defun gnus-uu-decode-and-save-articles (&optional unread unmark)
  1034.   (let ((gnus-uu-unmark-articles-not-decoded t)
  1035.     dir)
  1036.     (if (not (setq gnus-uu-marked-article-list 
  1037.            (nreverse (gnus-uu-get-list-of-articles 
  1038.                   "^." nil unread t))))
  1039.     (error "No%s articles to be decoded." (if unread " unread" ""))
  1040.       (setq dir (gnus-uu-read-directory "Where do you want the files? "))
  1041.       (gnus-uu-decode-and-view-or-save nil t dir)
  1042.       (message "Saved."))))
  1043.  
  1044.  
  1045. ;; Work functions
  1046.  
  1047. ; All the interactive uudecode/view/save/marked functions are interfaces
  1048. ; to this function, which does the rest.
  1049. (defun gnus-uu-decode-and-view-or-save (view marked &optional save-dir)
  1050.   (gnus-uu-initialize)
  1051.   (let (decoded)
  1052.     (save-excursion
  1053.       (if (gnus-uu-decode-and-strip nil marked)
  1054.       (progn
  1055.         (setq decoded t)
  1056.         (if view 
  1057.         (gnus-uu-view-directory gnus-uu-work-dir 
  1058.                     gnus-uu-use-interactive-view)
  1059.           (gnus-uu-save-directory gnus-uu-work-dir save-dir save-dir)
  1060.           (gnus-uu-check-for-generated-files)))))
  1061.  
  1062.     (gnus-uu-summary-next-subject)
  1063.  
  1064.     (if gnus-uu-error-during-unarching 
  1065.     (gnus-uu-clean-up)
  1066.       (if (and gnus-uu-use-interactive-view view decoded)
  1067.       (gnus-uu-do-interactive)))
  1068.  
  1069.     (if (or (not view) (not gnus-uu-use-interactive-view) (not decoded))
  1070.     (gnus-uu-clean-up))))
  1071.  
  1072. ; Unshars and views/saves marked/unmarked articles.
  1073. (defun gnus-uu-unshar-and-view-or-save (view marked)
  1074.   (gnus-uu-initialize)
  1075.   (let (tar-file files decoded)
  1076.     (save-excursion
  1077.       (setq gnus-uu-shar-directory 
  1078.         (make-temp-name (concat gnus-uu-tmp-dir "gnusuush")))
  1079.       (make-directory gnus-uu-shar-directory)
  1080.       (gnus-uu-add-file gnus-uu-shar-directory)
  1081.       (if (gnus-uu-decode-and-strip t marked)
  1082.       (progn
  1083.         (setq decoded t)
  1084.         (setq files (directory-files gnus-uu-shar-directory t))
  1085.         (setq gnus-uu-generated-file-list
  1086.           (append files gnus-uu-generated-file-list))
  1087.         (if (> (length files) 3)
  1088.         (progn 
  1089.           (setq tar-file 
  1090.             (concat
  1091.              (make-temp-name (concat gnus-uu-tmp-dir "gnusuuar"))
  1092.              ".tar"))
  1093.           (gnus-uu-add-file tar-file)
  1094.           (call-process 
  1095.            "sh" nil 
  1096.            (get-buffer-create gnus-uu-output-buffer-name) nil "-c" 
  1097.            (format "cd %s ; tar cf %s * ; cd .. ; rm -r %s" 
  1098.                gnus-uu-shar-directory tar-file
  1099.                gnus-uu-shar-directory))
  1100.           (if view
  1101.               (gnus-uu-view-file tar-file)
  1102.             (gnus-uu-save-file tar-file)))
  1103.           (if view
  1104.           (gnus-uu-view-file (elt files 2))
  1105.         (gnus-uu-save-file (elt files 2)))))))
  1106.  
  1107.     (gnus-uu-summary-next-subject)
  1108.  
  1109.     (if (and gnus-uu-use-interactive-view view decoded)
  1110.     (gnus-uu-do-interactive))
  1111.  
  1112.     (if (or (not gnus-uu-use-interactive-view) (not decoded))
  1113.     (gnus-uu-clean-up))))
  1114.  
  1115.  
  1116. ;; Functions for saving and possibly digesting articles without
  1117. ;; any decoding.
  1118.  
  1119. (defconst gnus-uu-saved-article-name nil)
  1120.  
  1121. ; VIEW isn't used, but is here anyway, to provide similar interface to
  1122. ; the other related functions.  If MARKED is non-nil, the list of
  1123. ; marked articles is used.  If NO-SAVE is non-nil, the articles aren't
  1124. ; actually saved in a permanent location, but the collecting is done
  1125. ; and a temporary file with the result is returned.
  1126. (defun gnus-uu-save-articles (view marked &optional no-save)
  1127.   (let (list-of-articles)
  1128.     (save-excursion
  1129.       (gnus-uu-initialize)
  1130.       (if (not marked)
  1131.       (setq list-of-articles (gnus-uu-get-list-of-articles))
  1132.     (setq list-of-articles (reverse gnus-uu-marked-article-list))
  1133.     (setq gnus-uu-marked-article-list nil))
  1134.  
  1135.       (if (not list-of-articles)
  1136.       (error "No list of articles"))
  1137.  
  1138.       (setq gnus-uu-saved-article-name 
  1139.         (concat gnus-uu-work-dir 
  1140.             (if no-save
  1141.             gnus-newsgroup-name
  1142.               (read-file-name "Enter file name: " gnus-newsgroup-name
  1143.                       gnus-newsgroup-name))))
  1144.       (gnus-uu-add-file gnus-uu-saved-article-name)
  1145.       (if (and (gnus-uu-grab-articles list-of-articles 'gnus-uu-save-article t)
  1146.            (not no-save))
  1147.       (gnus-uu-save-file gnus-uu-saved-article-name)
  1148.     gnus-uu-saved-article-name))))
  1149.  
  1150. ; Function called by gnus-uu-grab-articles to treat each article.
  1151. (defun gnus-uu-save-article (buffer in-state)
  1152.   (if (not gnus-uu-save-in-digest)
  1153.       (save-excursion
  1154.     (set-buffer buffer)
  1155.     (write-region 1 (point-max) gnus-uu-saved-article-name t)
  1156.     (cond ((eq in-state 'first) (list gnus-uu-saved-article-name 'begin))
  1157.           ((eq in-state 'first-and-last) (list gnus-uu-saved-article-name 'begin 'end))
  1158.           ((eq in-state 'last) (list 'end))
  1159.           (t (list 'middle))))
  1160.     (let (beg subj name headers headline sorthead body end-string state)
  1161.       (string-match "/\\([^/]*\\)$" gnus-uu-saved-article-name)
  1162.       (setq name (substring gnus-uu-saved-article-name (match-beginning 1)
  1163.                 (match-end 1)))
  1164.       (if (or (eq in-state 'first) 
  1165.           (eq in-state 'first-and-last))
  1166.       (progn 
  1167.         (setq state (list 'begin))
  1168.         (save-excursion (set-buffer (get-buffer-create "*gnus-uu-body*"))
  1169.                 (erase-buffer))
  1170.         (save-excursion 
  1171.           (set-buffer (get-buffer-create "*gnus-uu-pre*"))
  1172.           (erase-buffer)
  1173.           (insert (format 
  1174.                "Date: %s\nFrom: %s\nSubject: %s Digest\n\nTopics:\n"
  1175.                (current-time-string) name name))))
  1176.     (if (not (eq in-state 'end))
  1177.         (setq state (list 'middle))))
  1178.       (save-excursion
  1179.     (set-buffer (get-buffer "*gnus-uu-body*"))
  1180.     (goto-char (setq beg (point-max)))
  1181.     (save-excursion
  1182.       (save-restriction
  1183.         (set-buffer buffer)
  1184.         (goto-char 1)
  1185.         (re-search-forward "\n\n")
  1186.         (setq body (buffer-substring (1- (point)) (point-max)))
  1187.         (narrow-to-region 1 (point))
  1188.         (setq headers (list "Date:" "From:" "To:" "Cc:" "Subject:"
  1189.                 "Message-ID:" "Keywords:" "Summary:"))
  1190.         (while headers
  1191.           (setq headline (car headers))
  1192.           (setq headers (cdr headers))
  1193.           (goto-char 1)
  1194.           (if (re-search-forward (concat "^" headline ".*$") nil t)
  1195.           (setq sorthead 
  1196.             (concat sorthead (buffer-substring 
  1197.                       (match-beginning 0)
  1198.                       (match-end 0)) "\n"))))
  1199.         (widen)))
  1200.     (insert sorthead)(goto-char (point-max))
  1201.     (insert body)(goto-char (point-max))
  1202.     (insert (concat "\n" (make-string 30 ?-) "\n\n"))
  1203.     (goto-char beg)
  1204.     (if (re-search-forward "^Subject: \\(.*\\)$" nil t)
  1205.         (progn
  1206.           (setq subj (buffer-substring (match-beginning 1) (match-end 1)))
  1207.           (save-excursion 
  1208.         (set-buffer (get-buffer "*gnus-uu-pre*"))
  1209.         (insert (format "   %s\n" subj))))))
  1210.       (if (or (eq in-state 'last)
  1211.           (eq in-state 'first-and-last))
  1212.       (progn
  1213.         (save-excursion
  1214.           (set-buffer (get-buffer "*gnus-uu-pre*"))
  1215.           (insert (format "\n\n%s\n\n" (make-string 70 ?-)))
  1216.           (write-region 1 (point-max) gnus-uu-saved-article-name))
  1217.         (save-excursion
  1218.           (set-buffer (get-buffer "*gnus-uu-body*"))
  1219.           (goto-char (point-max))
  1220.           (insert 
  1221.            (concat (setq end-string (format "End of %s Digest" name)) 
  1222.                "\n"))
  1223.           (insert (concat (make-string (length end-string) ?*) "\n"))
  1224.           (write-region 1 (point-max) gnus-uu-saved-article-name t))
  1225.         (kill-buffer (get-buffer "*gnus-uu-pre*"))
  1226.         (kill-buffer (get-buffer "*gnus-uu-body*"))
  1227.         (setq state (cons 'end state))))
  1228.       (if (memq 'begin state)
  1229.       (cons gnus-uu-saved-article-name state)
  1230.     state))))
  1231.  
  1232.  
  1233. ;; Digest and forward articles
  1234.  
  1235. (autoload 'gnus-mail-forward-using-mail "gnusmail"
  1236.       "Forward the current message to another user." t)
  1237. (autoload 'gnus-mail-forward-using-mhe "gnusmail"
  1238.       "Forward the current message to another user." t)
  1239.  
  1240. (defun gnus-uu-digest-and-forward (&optional marked)
  1241.   "Digests and forwards all articles in this series."
  1242.   (interactive)
  1243.   (let ((gnus-uu-save-in-digest t)
  1244.     file buf)
  1245.     (setq file (gnus-uu-save-articles nil marked t))
  1246.     (switch-to-buffer (setq buf (get-buffer-create "*gnus-uu-forward*")))
  1247.     (erase-buffer)
  1248.     (delete-other-windows)
  1249.     (erase-buffer)
  1250.     (insert-file file)
  1251.     (goto-char 1)
  1252.     (bury-buffer buf)
  1253.     (funcall gnus-mail-forward-method)))
  1254.  
  1255. (defun gnus-uu-marked-digest-and-forward (&optional marked)
  1256.   "Digests and forwards all marked articles."
  1257.   (interactive)
  1258.   (gnus-uu-digest-and-forward t))
  1259.  
  1260.  
  1261. ;; Binhex treatment - not very advanced. 
  1262.  
  1263. (defconst gnus-uu-binhex-body-line 
  1264.   "^[^:]...............................................................$")
  1265. (defconst gnus-uu-binhex-begin-line 
  1266.   "^:...............................................................$")
  1267. (defconst gnus-uu-binhex-end-line
  1268.   ":$")
  1269. (defvar gnus-uu-binhex-article-name nil)
  1270.  
  1271. ; This just concatenates and strips stuff from binhexed articles.
  1272. ; No actual unbinhexing takes place. VIEW is ignored.
  1273. (defun gnus-uu-binhex-and-save (view marked)
  1274.   (gnus-uu-initialize)
  1275.   (let (list-of-articles)
  1276.     (save-excursion
  1277.       (if (not marked)
  1278.       (setq list-of-articles (gnus-uu-get-list-of-articles))
  1279.     (setq list-of-articles (reverse gnus-uu-marked-article-list))
  1280.     (setq gnus-uu-marked-article-list nil))
  1281.       (if (not list-of-articles)
  1282.       (error "No list of articles"))
  1283.  
  1284.       (setq gnus-uu-binhex-article-name 
  1285.         (concat gnus-uu-work-dir 
  1286.             (read-file-name "Enter binhex file name: " 
  1287.                     gnus-newsgroup-name
  1288.                     gnus-newsgroup-name)))
  1289.       (gnus-uu-add-file gnus-uu-binhex-article-name)
  1290.       (if (gnus-uu-grab-articles list-of-articles 'gnus-uu-binhex-article t)
  1291.       (gnus-uu-save-file gnus-uu-binhex-article-name))))
  1292.   (gnus-uu-check-for-generated-files)
  1293.   (gnus-uu-summary-next-subject))
  1294.  
  1295. (defun gnus-uu-binhex-article (buffer in-state)
  1296.   (let (state start-char)
  1297.     (save-excursion
  1298.       (set-buffer buffer)
  1299.       (widen)
  1300.       (goto-char 1)
  1301.       (if (not (re-search-forward gnus-uu-binhex-begin-line nil t))
  1302.       (if (not (re-search-forward gnus-uu-binhex-body-line nil t))
  1303.           (setq state (list 'wrong-type))))
  1304.  
  1305.       (if (memq 'wrong-type state)
  1306.       ()
  1307.     (beginning-of-line)
  1308.     (setq start-char (point))
  1309.     (if (looking-at gnus-uu-binhex-begin-line)
  1310.         (progn
  1311.           (setq state (list 'begin))
  1312.           (write-region 1 1 gnus-uu-binhex-article-name))
  1313.       (setq state (list 'middle)))
  1314.     (goto-char (point-max))
  1315.     (re-search-backward (concat gnus-uu-binhex-body-line "\\|" 
  1316.                     gnus-uu-binhex-end-line) nil t)
  1317.     (if (looking-at gnus-uu-binhex-end-line)
  1318.         (setq state (if (memq 'begin state)
  1319.                 (cons 'end state)
  1320.               (list 'end))))
  1321.     (beginning-of-line)
  1322.     (forward-line 1)
  1323.     (if (file-exists-p gnus-uu-binhex-article-name)
  1324.         (append-to-file start-char (point) gnus-uu-binhex-article-name))))
  1325.     (if (memq 'begin state)
  1326.     (cons gnus-uu-binhex-article-name state)
  1327.       state)))
  1328.       
  1329.  
  1330. ;; Internal view commands
  1331.  
  1332. ; This function takes two parameters. The first is name of the file to
  1333. ; be viewed. `gnus-uu-view-file' will look for an action associated
  1334. ; with the file type of the file. If it finds an appropriate action,
  1335. ; the file will be attempted displayed.
  1336. ; The second parameter specifies if the user is to be asked whether to
  1337. ; save the file if viewing is unsuccessful. t means "do not ask."
  1338. ;
  1339. ; Note that the file given will be deleted by this function, one way
  1340. ; or another. If `gnus-uu-asynchronous' is set, it won't be deleted
  1341. ; right away, but sometime later. If the user is offered to save the
  1342. ; file, it'll be moved to wherever the user wants it.
  1343.  
  1344. ; `gnus-uu-view-file' returns t if viewing is successful.
  1345.  
  1346. (defun gnus-uu-view-file (file-name &optional dont-ask)
  1347.   (let (action did-view
  1348.            (didnt-want t)
  1349.            (do-view t))
  1350.  
  1351.     (setq action (gnus-uu-get-action file-name))
  1352.  
  1353. ; Do interactive view if that is wanted and it is not an archive
  1354.     (if (and gnus-uu-use-interactive-view 
  1355.          (not (string= (or action "") "gnus-uu-archive")))
  1356.     (gnus-uu-enter-interactive-file (or action "") file-name)
  1357.  
  1358.       (if action
  1359.       (progn
  1360.  
  1361.         (if gnus-uu-ask-before-view
  1362.         (setq didnt-want 
  1363.               (or (not (setq do-view
  1364.                      (y-or-n-p 
  1365.                       (format "Do you want to view %s? " 
  1366.                           file-name))))
  1367.               didnt-want)))
  1368.  
  1369.         (if do-view
  1370.         (setq did-view 
  1371.               (if gnus-uu-asynchronous
  1372.               (gnus-uu-call-asynchronous file-name action)
  1373.             (gnus-uu-call-synchronous file-name action)))))
  1374.  
  1375.     (if (and (not dont-ask) (not gnus-uu-use-interactive-view))
  1376.         (progn
  1377.           (if (and
  1378.            didnt-want
  1379.            (or (not action)
  1380.                (and (string= action "gnus-uu-archive") 
  1381.                 (not did-view))))
  1382.           (progn
  1383.             (message 
  1384.              (format "Could find no rule for %s" file-name))
  1385.             (sit-for 2)))
  1386.           (and (or (not did-view) gnus-uu-view-and-save)
  1387.            (y-or-n-p 
  1388.             (format "Do you want to save the file %s? "
  1389.                 file-name))
  1390.            (gnus-uu-save-file file-name)))))
  1391.  
  1392.       (if (and (file-exists-p file-name) 
  1393.            (not gnus-uu-use-interactive-view)
  1394.            (or 
  1395.         (not (and gnus-uu-asynchronous did-view))
  1396.         (string= action "gnus-uu-archive")))
  1397.       (delete-file file-name)))
  1398.  
  1399.     did-view))
  1400.  
  1401. (defun gnus-uu-get-action (file-name)
  1402.   (let (action)
  1403.     (setq action 
  1404.       (gnus-uu-choose-action 
  1405.        file-name
  1406.        (append 
  1407.         (if (and gnus-uu-use-interactive-view 
  1408.              gnus-uu-user-interactive-view-rules)
  1409.         gnus-uu-user-interactive-view-rules
  1410.           gnus-uu-user-view-rules)
  1411.         (if (or gnus-uu-ignore-default-view-rules 
  1412.             (not gnus-uu-use-interactive-view))
  1413.         ()
  1414.           gnus-uu-default-interactive-view-rules-begin)
  1415.         (if gnus-uu-ignore-default-view-rules 
  1416.         nil 
  1417.           gnus-uu-default-view-rules)
  1418.         (if gnus-uu-use-interactive-view
  1419.         (append gnus-uu-user-interactive-view-rules-end
  1420.             (if gnus-uu-ignore-default-view-rules
  1421.                 ()
  1422.               gnus-uu-default-interactive-view-rules-end))
  1423.           gnus-uu-user-view-rules-end))))
  1424.     (if (and (not (string= (or action "") "gnus-uu-archive")) 
  1425.          gnus-uu-view-with-metamail)
  1426.     (if (setq action 
  1427.           (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list))
  1428.         (setq action (format "metamail -d -b -c \"%s\"" action))))
  1429.     action))
  1430.  
  1431. ; `gnus-uu-call-synchronous' takes two parameters: The name of the
  1432. ; file to be displayed and the command to display it with. Returns t
  1433. ; on success and nil if the file couldn't be displayed.
  1434. (defun gnus-uu-call-synchronous (file-name action)
  1435.   (let (did-view command)
  1436.     (save-excursion
  1437.       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
  1438.       (erase-buffer)
  1439.       (setq command (gnus-uu-command action file-name))
  1440.       (message "Viewing with '%s'" command)
  1441.       (if (not (= 0 (call-process "sh" nil t nil "-c" command)))
  1442.       (progn
  1443.         (goto-char 1)
  1444.         (while (re-search-forward "\n" nil t)
  1445.           (replace-match " "))
  1446.         (message (concat "Error: " (buffer-substring 1 (point-max))))
  1447.         (sit-for 2))
  1448.     (message "")
  1449.     (setq did-view t)))
  1450.     did-view))
  1451.  
  1452. ; `gnus-uu-call-asyncronous' takes two parameters: The name of the
  1453. ; file to be displayed and the command to display it with. Since the
  1454. ; view command is executed asynchronously, it's kinda hard to decide
  1455. ; whether the command succeded or not, so this function always returns
  1456. ; t. It also adds "; rm -f file-name" to the end of the execution
  1457. ; string, so the file will be removed after viewing has ended.
  1458. (defun gnus-uu-call-asynchronous (file-name action)
  1459.   (let (command file tmp-file start)
  1460.     (while (string-match "/" file-name start)
  1461.       (setq start (1+ (match-beginning 0))))
  1462.     (setq file (substring file-name start))
  1463.     (setq tmp-file (concat gnus-uu-work-dir file))
  1464.     (if (string= tmp-file file-name)
  1465.     ()
  1466.       (rename-file file-name tmp-file t)
  1467.       (setq file-name tmp-file))
  1468.  
  1469.     (setq command (gnus-uu-command action file-name))
  1470.     (setq command (format "%s ; rm -f %s" command file-name))
  1471.     (message "Viewing with %s" command)
  1472.     (start-process "gnus-uu-view" nil "sh" "-c" command)
  1473.     t))
  1474.  
  1475. ; `gnus-uu-decode-and-strip' does all the main work. It finds out what
  1476. ; articles to grab, grabs them, strips the result and decodes. If any
  1477. ; of these operations fail, it returns nil, t otherwise.  If shar is
  1478. ; t, it will pass this on to `gnus-uu-grab-articles', which will
  1479. ; (probably) unshar the articles. If use-marked is non-nil, it won't
  1480. ; try to find articles, but use the marked list.
  1481. (defun gnus-uu-decode-and-strip (&optional shar use-marked)
  1482.   (let (list-of-articles)
  1483.     (save-excursion
  1484.  
  1485.       (if use-marked
  1486.       (if (not gnus-uu-marked-article-list)
  1487.           (message "No articles marked")
  1488.         (setq list-of-articles (reverse gnus-uu-marked-article-list))
  1489.         (gnus-uu-unmark-all-articles))
  1490.     (setq list-of-articles (gnus-uu-get-list-of-articles)))
  1491.       
  1492.       (and list-of-articles
  1493.        (gnus-uu-grab-articles 
  1494.         list-of-articles 
  1495.         (if shar 'gnus-uu-unshar-article 'gnus-uu-uustrip-article-as)
  1496.         t)))))
  1497.  
  1498. ; Takes a string and puts a \ in front of every special character;
  1499. ; ignores any leading "version numbers" thingies that they use in the
  1500. ; comp.binaries groups, and either replaces anything that looks like
  1501. ; "2/3" with "[0-9]+/[0-9]+" or, if it can't find something like that,
  1502. ; replaces the last two numbers with "[0-9]+". This, in my experience,
  1503. ; should get most postings of a series."
  1504. (defun gnus-uu-reginize-string (string)
  1505.   (let ((count 2)
  1506.     (vernum "v[0-9]+[a-z][0-9]+:")
  1507.     reg beg)
  1508.     (save-excursion
  1509.       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
  1510.       (erase-buffer)
  1511.       (insert (regexp-quote string))
  1512.       (setq beg 1)
  1513.  
  1514.       (setq case-fold-search nil)
  1515.       (goto-char 1)
  1516.       (if (looking-at vernum)
  1517.       (progn
  1518.         (replace-match vernum t t)
  1519.         (setq beg (length vernum))))
  1520.  
  1521.       (goto-char beg)
  1522.       (if (re-search-forward "[ \t]*[0-9]+/[0-9]+" nil t)
  1523.       (replace-match " [0-9]+/[0-9]+")
  1524.  
  1525.     (goto-char beg)
  1526.     (if (re-search-forward "[0-9]+[ \t]*of[ \t]*[0-9]+" nil t)
  1527.         (replace-match "[0-9]+ of [0-9]+")
  1528.  
  1529.       (end-of-line)
  1530.       (while (and (re-search-backward "[0-9]" nil t) (> count 0))
  1531.             (while (and 
  1532.             (looking-at "[0-9]") 
  1533.             (< 1 (goto-char (1- (point))))))
  1534.             (re-search-forward "[0-9]+" nil t)
  1535.         (replace-match "[0-9]+")
  1536.         (backward-char 5)
  1537.         (setq count (1- count)))))
  1538.  
  1539.       (goto-char beg)
  1540.       (while (re-search-forward "[ \t]+" nil t)
  1541.     (replace-match "[ \t]*" t t))
  1542.  
  1543.       (buffer-substring 1 (point-max)))))
  1544.  
  1545. ; Finds all articles that matches the regular expression given.
  1546. ; Returns the resulting list. SUBJECT is the regular expression to be
  1547. ; matched. If it is nil, the current article name will be used. If
  1548. ; MARK-ARTICLES is non-nil, articles found are marked. If ONLY-UNREAD
  1549. ; is non-nil, only unread articles are chose. If DO-NOT-TRANSLATE is
  1550. ; non-nil, article names are not equialized before sorting.
  1551. (defun gnus-uu-get-list-of-articles (&optional subject mark-articles only-unread do-not-translate)
  1552.   (let (beg end reg-subject list-of-subjects list-of-numbers art-num)
  1553.     (save-excursion
  1554.       
  1555. ; If the subject is not given, this function looks at the current subject
  1556. ; and takes that.
  1557.  
  1558.       (if subject
  1559.       (setq reg-subject subject)
  1560.     (end-of-line)
  1561.     (setq end (point))
  1562.     (beginning-of-line)
  1563.     (if (not (re-search-forward "\\] " end t))
  1564.         (progn (message "No valid subject chosen") (sit-for 2))
  1565.       (setq subject (buffer-substring (point) end))
  1566.       (setq reg-subject 
  1567.         (concat "\\[.*\\] " (gnus-uu-reginize-string subject)))))
  1568.  
  1569. ;      (message reg-subject)(sleep-for 2)
  1570.       
  1571.       (if reg-subject
  1572.       (progn
  1573.  
  1574. ; Collect all subjects matching reg-subject.
  1575.  
  1576.         (let ((case-fold-search t))
  1577.           (goto-char 1)
  1578.           (while (re-search-forward reg-subject nil t)
  1579.         (beginning-of-line)
  1580.         (setq beg (point))
  1581.         (if (or (not only-unread) (looking-at " \\|-"))
  1582.             (progn
  1583.               (end-of-line)
  1584.               (setq list-of-subjects (cons 
  1585.                           (buffer-substring beg (point))
  1586.                           list-of-subjects)))
  1587.           (end-of-line))))
  1588.  
  1589. ; Expand all numbers in all the subjects: (hi9 -> hi0009, etc).
  1590.  
  1591.         (setq list-of-subjects (gnus-uu-expand-numbers 
  1592.                     list-of-subjects
  1593.                     (not do-not-translate)))
  1594.  
  1595. ; Sort the subjects.
  1596.  
  1597.         (setq list-of-subjects (sort list-of-subjects 'gnus-uu-string<))
  1598.  
  1599. ; Get the article numbers from the sorted list of subjects.
  1600.  
  1601.         (while list-of-subjects 
  1602.           (setq art-num (gnus-uu-article-number (car list-of-subjects)))
  1603.           (if mark-articles (gnus-summary-mark-as-read art-num ?#))
  1604.           (setq list-of-numbers (cons art-num list-of-numbers))
  1605.           (setq list-of-subjects (cdr list-of-subjects)))
  1606.  
  1607.         (setq list-of-numbers (nreverse list-of-numbers))))
  1608.  
  1609.       list-of-numbers)))
  1610.  
  1611. ; Takes a list of strings and "expands" all numbers in all the
  1612. ; strings.  That is, this function makes all numbers equal length by
  1613. ; prepending lots of zeroes before each number. This is to ease later
  1614. ; sorting to find out what sequence the articles are supposed to be
  1615. ; decoded in. Returns the list of expanded strings.
  1616. (defun gnus-uu-expand-numbers (string-list &optional translate)
  1617.   (let (string out-list pos num)
  1618.     (save-excursion
  1619.       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
  1620.       (while string-list
  1621.     (erase-buffer)
  1622.     (setq string (car string-list))
  1623.     (setq string-list (cdr string-list))
  1624.     (insert string)
  1625.     (goto-char 1)
  1626.     (while (re-search-forward "[ \t]+" nil t)
  1627.       (replace-match " "))
  1628.     (goto-char 1)
  1629.     (if translate 
  1630.         (while (re-search-forward "[A-Za-z]" nil t)
  1631.           (replace-match "a" t t)))
  1632.  
  1633.     (goto-char 1)
  1634.     (if (not (search-forward "] " nil t))
  1635.         ()
  1636.       (while (re-search-forward "[0-9]+" nil t)
  1637.         (replace-match  
  1638.          (format "%06d" 
  1639.              (string-to-int (buffer-substring 
  1640.                      (match-beginning 0) (match-end 0))))))
  1641.       (setq string (buffer-substring 1 (point-max)))
  1642.       (setq out-list (cons string out-list)))))
  1643.     out-list))
  1644.  
  1645. ; Used in a sort for finding out what string is bigger, but ignoring
  1646. ; everything before the subject part.
  1647. (defun gnus-uu-string< (string1 string2) 
  1648.   (string< (substring string1 (string-match "\\] " string1))
  1649.        (substring string2 (string-match "\\] " string2))))
  1650.  
  1651.  
  1652. ;; gnus-uu-grab-article
  1653. ;
  1654. ; This is the general multi-article treatment function.  It takes a
  1655. ; list of articles to be grabbed and a function to apply to each
  1656. ; article. It puts the result in `gnus-uu-result-buffer'.
  1657. ;
  1658. ; The function to be called should take two parameters.  The first
  1659. ; parameter is the article buffer. The function should leave the
  1660. ; result, if any, in this buffer. This result is then appended on to
  1661. ; the `gnus-uu-result-buffer'. Most treatment functions will just
  1662. ; generate files...
  1663. ;
  1664. ; The second parameter is the state of the list of articles, and can
  1665. ; have four values: `first', `middle', `last' and `first-and-last'.
  1666. ;
  1667. ; The function should return a list. The list may contain the
  1668. ; following symbols:
  1669. ; `error' if an error occurred
  1670. ; `begin' if the beginning of an encoded file has been received
  1671. ;   If the list returned contains a `begin', the first element of
  1672. ;   the list *must* be a string with the file name of the decoded
  1673. ;   file.
  1674. ; `end' if the the end of an encoded file has been received
  1675. ; `middle' if the article was a body part of an encoded file
  1676. ; `wrong-type' if the article was not a part of an encoded file
  1677. ; `ok', which can be used everything is ok
  1678.  
  1679. (defvar gnus-uu-has-been-grabbed nil)
  1680.  
  1681. (defun gnus-uu-unmark-list-of-grabbed (&optional dont-unmark-last-article)
  1682.   (let (art)
  1683.     (if (not (and gnus-uu-has-been-grabbed
  1684.           gnus-uu-unmark-articles-not-decoded))
  1685.     ()
  1686.       (if dont-unmark-last-article
  1687.       (progn
  1688.         (setq art (car gnus-uu-has-been-grabbed))
  1689.         (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed))))
  1690.       (while gnus-uu-has-been-grabbed
  1691.     (gnus-summary-mark-as-unread (car gnus-uu-has-been-grabbed) t)
  1692.     (setq gnus-uu-has-been-grabbed (cdr gnus-uu-has-been-grabbed)))
  1693.       (if dont-unmark-last-article
  1694.       (setq gnus-uu-has-been-grabbed (list art))))))
  1695.  
  1696.  
  1697. ; This function takes a list of articles and a function to apply to
  1698. ; each article grabbed. The result of the function is appended on to
  1699. ; `gnus-uu-result-buffer'.
  1700. ; This function returns a list of files decoded if the grabbing and
  1701. ; the process-function has been successful and nil otherwise.
  1702. (defun gnus-uu-grab-articles (list-of-articles process-function &optional sloppy)
  1703.   (let ((result-buffer (get-buffer-create gnus-uu-result-buffer))
  1704.     (state 'first)
  1705.     (wrong-type t)
  1706.     has-been-begin has-been-end 
  1707.     article result-file result-files process-state)
  1708.  
  1709.     (save-excursion
  1710.       (set-buffer result-buffer)
  1711.       (erase-buffer))
  1712.     (setq gnus-uu-has-been-grabbed nil)
  1713.  
  1714.     (while (and list-of-articles 
  1715.         (not (memq 'error process-state))
  1716.         (or sloppy
  1717.             (not (memq 'end process-state))))
  1718.  
  1719.       (setq article (car list-of-articles))
  1720.       (setq list-of-articles (cdr list-of-articles))
  1721.       (setq gnus-uu-has-been-grabbed (cons article gnus-uu-has-been-grabbed))
  1722.  
  1723.       (if (eq list-of-articles ()) 
  1724.       (if (eq state 'first)
  1725.           (setq state 'first-and-last)
  1726.         (setq state 'last)))
  1727.  
  1728.       (message "Getting article %d" article)
  1729.       (if (not (= (or gnus-current-article 0) article))
  1730.       (gnus-summary-display-article article))
  1731.       (gnus-summary-mark-as-read article)
  1732.  
  1733.       (save-excursion (set-buffer gnus-article-buffer) (widen))
  1734.  
  1735.       (setq process-state (funcall process-function gnus-article-buffer state))
  1736.  
  1737. ;      (message "process-state er %s" process-state)(sleep-for 3)
  1738.  
  1739.       (if (or (memq 'begin process-state)
  1740.           (and (or (eq state 'first) (eq state 'first-and-last))
  1741.            (memq 'ok process-state)))
  1742.       (progn
  1743.         (if has-been-begin
  1744.         (if (file-exists-p result-file) (delete-file result-file)))
  1745.         (setq result-file (car process-state))
  1746.         (setq has-been-begin t)
  1747.         (setq has-been-end nil)))
  1748.  
  1749.       (if (memq 'end process-state)
  1750.       (progn
  1751.         (setq gnus-uu-has-been-grabbed nil)
  1752.         (setq result-files (cons result-file result-files))
  1753.         (setq has-been-end t)
  1754.         (setq has-been-begin nil)))
  1755.  
  1756.       (if (not (memq 'wrong-type process-state))
  1757.       (setq wrong-type nil)
  1758.     (if gnus-uu-unmark-articles-not-decoded
  1759.         (gnus-summary-mark-as-unread article t)))
  1760.  
  1761.       (if sloppy (setq wrong-type nil))
  1762.  
  1763.       (if (and (not has-been-begin)
  1764.            (not sloppy)
  1765.            (or (memq 'end process-state)
  1766.            (memq 'middle process-state)))
  1767.       (progn
  1768.         (setq process-state (list 'error))
  1769.         (message "No begin part at the beginning")
  1770.         (sleep-for 2))
  1771.     (setq state 'middle)))
  1772.  
  1773.     (if result-files
  1774.     ()
  1775.       (if (not has-been-begin)
  1776.       (message "Wrong type file")
  1777.     (if (memq 'error process-state)
  1778.         (setq result-files nil)
  1779.       (if (not (or (memq 'ok process-state) 
  1780.                (memq 'end process-state)))
  1781.           (progn
  1782.         (message "End of articles reached before end of file")
  1783.         (setq result-files nil))
  1784.         (gnus-uu-unmark-list-of-grabbed)))))
  1785.     result-files))
  1786.  
  1787. (defun gnus-uu-uudecode-sentinel (process event)
  1788.   (delete-process (get-process process)))
  1789.  
  1790. ; Uudecodes a file asynchronously.
  1791. (defun gnus-uu-uustrip-article-as (process-buffer in-state)
  1792.   (let ((state (list 'ok))
  1793.     (process-connection-type nil)
  1794.     start-char pst name-beg name-end)
  1795.     (save-excursion
  1796.       (set-buffer process-buffer)
  1797.       (let ((case-fold-search nil)
  1798.         (buffer-read-only nil))
  1799.  
  1800.     (goto-char 1)
  1801.  
  1802.     (if gnus-uu-kill-carriage-return
  1803.         (progn
  1804.           (while (search-forward "\r" nil t)
  1805.         (delete-backward-char 1))
  1806.           (goto-char 1)))
  1807.  
  1808.     (if (not (re-search-forward gnus-uu-begin-string nil t))
  1809.         (if (not (re-search-forward gnus-uu-body-line nil t))
  1810.         (setq state (list 'wrong-type))))
  1811.      
  1812.     (if (memq 'wrong-type state)
  1813.         ()
  1814.       (beginning-of-line)
  1815.       (setq start-char (point))
  1816.  
  1817.       (if (looking-at gnus-uu-begin-string)
  1818.           (progn 
  1819.         (setq name-end (match-end 1))
  1820.  
  1821.         ; Replace any slashes and spaces in file names before decoding
  1822.         (goto-char (setq name-beg (match-beginning 1)))
  1823.         (while (re-search-forward "/" name-end t)
  1824.           (replace-match "-"))
  1825.         (goto-char name-beg)
  1826.         (while (re-search-forward " " name-end t)
  1827.           (replace-match "_"))
  1828.  
  1829.         (setq gnus-uu-file-name (buffer-substring name-beg name-end))
  1830.         (and gnus-uu-uudecode-process
  1831.              (setq pst (process-status 
  1832.                 (or gnus-uu-uudecode-process "nevair")))
  1833.              (if (or (eq pst 'stop) (eq pst 'run))
  1834.              (progn
  1835.                (delete-process gnus-uu-uudecode-process)
  1836.                (gnus-uu-unmark-list-of-grabbed t))))
  1837.         (setq gnus-uu-uudecode-process
  1838.               (start-process 
  1839.                "*uudecode*" 
  1840.                (get-buffer-create gnus-uu-output-buffer-name)
  1841.                "sh" "-c" 
  1842.                (format "cd %s ; uudecode" gnus-uu-work-dir)))
  1843.         (set-process-sentinel 
  1844.          gnus-uu-uudecode-process 'gnus-uu-uudecode-sentinel)
  1845.         (setq state (list 'begin))
  1846.         (gnus-uu-add-file (concat gnus-uu-work-dir gnus-uu-file-name)))
  1847.         (setq state (list 'middle)))
  1848.     
  1849.       (goto-char (point-max))
  1850.  
  1851.       (re-search-backward 
  1852.        (concat gnus-uu-body-line "\\|" gnus-uu-end-string) nil t)
  1853.       (beginning-of-line)
  1854.  
  1855.       (if (looking-at gnus-uu-end-string)
  1856.           (setq state (cons 'end state)))
  1857.       (forward-line 1)
  1858.  
  1859.       (and gnus-uu-uudecode-process
  1860.            (setq pst (process-status 
  1861.               (or gnus-uu-uudecode-process "nevair")))
  1862.            (if (or (eq pst 'run) (eq pst 'stop))
  1863.            (progn
  1864.              (if gnus-uu-correct-stripped-uucode
  1865.              (progn
  1866.                (gnus-uu-check-correct-stripped-uucode 
  1867.                 start-char (point))
  1868.                (goto-char (point-max))
  1869.                (re-search-backward 
  1870.                 (concat gnus-uu-body-line "\\|" 
  1871.                     gnus-uu-end-string) 
  1872.                 nil t)
  1873.                (forward-line 1)))
  1874.              (condition-case err
  1875.              (process-send-region gnus-uu-uudecode-process 
  1876.                           start-char (point))
  1877.                (error 
  1878.             (progn 
  1879.               (message "gnus-uu: Couldn't uudecode")
  1880.               (sleep-for 2)
  1881.               (setq state (list 'wrong-type))
  1882.               (delete-process gnus-uu-uudecode-process)))))
  1883.          (setq state (list 'wrong-type))))
  1884.       (if (not gnus-uu-uudecode-process)
  1885.           (setq state (list 'wrong-type)))))
  1886.  
  1887.       (if (memq 'begin state)
  1888.       (cons (concat gnus-uu-work-dir gnus-uu-file-name) state)
  1889.     state))))
  1890.  
  1891. ; This function is used by `gnus-uu-grab-articles' to treat
  1892. ; a shared article.
  1893. (defun gnus-uu-unshar-article (process-buffer in-state)
  1894.   (let ((state (list 'ok))
  1895.     start-char)
  1896.     (save-excursion
  1897.      (set-buffer process-buffer)
  1898.      (goto-char 1)
  1899.      (if (not (re-search-forward gnus-uu-shar-begin-string nil t))
  1900.      (setq state (list 'wrong-type))
  1901.        (beginning-of-line)
  1902.        (setq start-char (point))
  1903.        (call-process-region 
  1904.     start-char (point-max) "sh" nil 
  1905.     (get-buffer-create gnus-uu-output-buffer-name) nil 
  1906.     "-c" (concat "cd " gnus-uu-shar-directory " ; sh"))))
  1907.     state))
  1908.  
  1909. ; Returns the name of what the shar file is going to unpack.
  1910. (defun gnus-uu-find-name-in-shar ()
  1911.   (let ((oldpoint (point))
  1912.     res)
  1913.     (goto-char 1)
  1914.     (if (re-search-forward gnus-uu-shar-name-marker nil t)
  1915.     (setq res (buffer-substring (match-beginning 1) (match-end 1))))
  1916.     (goto-char oldpoint)
  1917.     res))
  1918.  
  1919. ; Returns the article number of the given subject.
  1920. (defun gnus-uu-article-number (subject)
  1921.   (let (end)
  1922.     (string-match "[0-9]+[^0-9]" subject 1)
  1923.     (setq end (match-end 0))
  1924.     (string-to-int 
  1925.      (substring subject (string-match "[0-9]" subject 1) end)))) 
  1926.           
  1927. ; `gnus-uu-choose-action' chooses what action to perform given the name
  1928. ; and `gnus-uu-file-action-list'.  Returns either nil if no action is
  1929. ; found, or the name of the command to run if such a rule is found.
  1930. (defun gnus-uu-choose-action (file-name file-action-list)
  1931.   (let ((action-list (copy-sequence file-action-list))
  1932.     rule action)
  1933.     (while (not (or (eq action-list ()) action))
  1934.       (setq rule (car action-list))
  1935.       (setq action-list (cdr action-list))
  1936.       (if (string-match (car rule) file-name)
  1937.       (setq action (car (cdr rule)))))
  1938.     action))
  1939.  
  1940. (defun gnus-uu-save-directory (from-dir &optional default-dir ignore-existing)
  1941.   (let (dir file-name command files file)
  1942.     (setq files (directory-files from-dir t))
  1943.     (if default-dir
  1944.     (setq dir default-dir)
  1945.       (setq dir (gnus-uu-read-directory 
  1946.          (concat "Where do you want the file" 
  1947.              (if (< 3 (length files)) "s" "") "? "))))
  1948.  
  1949.     (while files
  1950.       (setq file (car files))
  1951.       (setq files (cdr files))
  1952.       (string-match "/[^/]*$" file)
  1953.       (setq file-name (substring file (1+ (match-beginning 0))))
  1954.       (if (string-match "^\\.\\.?$" file-name)
  1955.       ()
  1956.     (if (and (not ignore-existing) (file-exists-p (concat dir file-name)))
  1957.         (setq file-name
  1958.           (read-file-name "File exists. Enter a new name: " dir 
  1959.                   (concat dir file-name) nil file-name))
  1960.       (setq file-name (concat dir file-name)))
  1961.     (rename-file file file-name t)))))
  1962.  
  1963. ; Moves the file from the tmp directory to where the user wants it.
  1964. (defun gnus-uu-save-file (from-file-name &optional default-dir ignore-existing)
  1965.   (let (dir file-name command)
  1966.     (string-match "/[^/]*$" from-file-name)
  1967.     (setq file-name (substring from-file-name (1+ (match-beginning 0))))
  1968.     (if default-dir
  1969.     (setq dir default-dir)
  1970.       (setq dir (gnus-uu-read-directory "Where do you want the file? ")))
  1971.     (if (and (not ignore-existing) (file-exists-p (concat dir file-name)))
  1972.     (setq file-name
  1973.           (read-file-name "File exist. Enter a new name: " dir 
  1974.                   (concat dir file-name) nil file-name))
  1975.       (setq file-name (concat dir file-name)))
  1976.     (rename-file from-file-name file-name t)))
  1977.     
  1978. (defun gnus-uu-read-directory (prompt &optional default)
  1979.   (let (dir ok create)
  1980.     (while (not ok)
  1981.       (setq ok t)
  1982.       (setq dir (if default default
  1983.           (read-file-name prompt gnus-uu-current-save-dir 
  1984.                   gnus-uu-current-save-dir)))
  1985.       (while (string-match "/$" dir)
  1986.     (setq dir (substring dir 0 (match-beginning 0))))
  1987.       (if (file-exists-p dir)
  1988.       (if (not (file-directory-p dir))
  1989.           (progn
  1990.         (setq ok nil)
  1991.         (message "%s is a file" dir)
  1992.         (sit-for 2)))
  1993.     (setq create ?o)
  1994.     (while (not (or (= create ?y) (= create ?n)))
  1995.       (message "%s: No such directory. Do you want to create it? (y/n)" 
  1996.            dir)
  1997.       (setq create (read-char)))
  1998.     (if (= create ?y) (make-directory dir))))
  1999.     (setq gnus-uu-current-save-dir (concat dir "/"))))
  2000.  
  2001. ; Unpacks an archive and views all the files in it. Returns t if
  2002. ; viewing one or more files is successful.
  2003. (defun gnus-uu-treat-archive (file-path)
  2004.   (let ((did-unpack t)
  2005.     action command files file file-name dir)
  2006.     (setq action (gnus-uu-choose-action 
  2007.           file-path (append gnus-uu-user-archive-rules
  2008.                     (if gnus-uu-ignore-default-archive-rules
  2009.                     nil
  2010.                       gnus-uu-default-archive-rules))))
  2011.  
  2012.     (if (not action) (error "No unpackers for the file %s" file-path))
  2013.  
  2014.     (string-match "/[^/]*$" file-path)
  2015.     (setq file-name (substring file-path (1+ (match-beginning 0))))
  2016.     (setq dir (substring file-path 0 (match-beginning 0)))
  2017.  
  2018.     (if (gnus-uu-string-in-list action gnus-uu-destructive-archivers)
  2019.     (copy-file file-path (concat file-path "~") t))
  2020.  
  2021.     (setq command (format "cd %s ; %s" dir (gnus-uu-command action file-path)))
  2022.  
  2023.     (save-excursion
  2024.       (set-buffer (get-buffer-create gnus-uu-output-buffer-name))
  2025.       (erase-buffer))
  2026.  
  2027.     (message "Unpacking: %s..." (gnus-uu-command action file-path))
  2028.  
  2029.     (if (= 0 (call-process "sh" nil 
  2030.                (get-buffer-create gnus-uu-output-buffer-name)
  2031.                nil "-c" command))
  2032.     (message "")
  2033.       (message "Error during unpacking of archive")
  2034.       (sleep-for 2)
  2035.       (setq did-unpack nil))
  2036.  
  2037.     (if (gnus-uu-string-in-list action gnus-uu-destructive-archivers)
  2038.     (rename-file (concat file-path "~") file-path t))
  2039.  
  2040.     did-unpack))
  2041.  
  2042. ; Tries to view all the files in the given directory. Returns t if
  2043. ; viewing one or more files is successful.
  2044. (defun gnus-uu-view-directory (dir &optional dont-delete-files not-top)
  2045.   (let ((first t)
  2046.     files file did-view ignore-files)
  2047.     (setq files (directory-files dir t "[^/][^\\.][^\\.]?$"))
  2048.     (gnus-uu-add-file files)
  2049.     (setq ignore-files files)
  2050.     
  2051.     (while (gnus-uu-unpack-archives 
  2052.         files (if not-top (list ".")
  2053.             (if first () ignore-files)))
  2054.       (setq first nil)
  2055.       (gnus-uu-add-file 
  2056.        (setq files (directory-files dir t "[^/][^\\.][^\\.]?$"))))
  2057.       
  2058.     (while files
  2059.       (setq file (car files))
  2060.       (setq files (cdr files))
  2061.       (if (not (string= (or (gnus-uu-get-action file) "") "gnus-uu-archive"))
  2062.       (progn
  2063.         (set-file-modes file 448)
  2064.         (if (file-directory-p file)
  2065.         (setq did-view (or (gnus-uu-view-directory file 
  2066.                                dont-delete-files 
  2067.                                t) 
  2068.                    did-view))
  2069.           (setq did-view (or (gnus-uu-view-file file t) did-view)))))
  2070.       (if (and (not dont-delete-files) (file-exists-p file))
  2071.       (delete-file file)))
  2072.  
  2073.     (if (not dont-delete-files) (delete-directory dir))
  2074.     did-view))
  2075.  
  2076. (defun gnus-uu-unpack-archives (files &optional ignore)
  2077.   (let (path did-unpack)
  2078.     (while files
  2079.       (setq path (car files))
  2080.       (setq files (cdr files))
  2081.       (if (not (gnus-uu-string-in-list path ignore))
  2082.       (if (string= (or (gnus-uu-get-action 
  2083.                 (gnus-uu-name-from-path path)) "") 
  2084.                "gnus-uu-archive")
  2085.           (progn
  2086.         (setq did-unpack t)
  2087.         (setq gnus-uu-error-during-unarching
  2088.               (not (gnus-uu-treat-archive path)))
  2089.         (if ignore (delete-file path))))))
  2090.     did-unpack))
  2091.  
  2092.  
  2093. ;; Manual marking
  2094.  
  2095. (defun gnus-uu-enter-mark-in-list ()
  2096.   (let (article    beg)
  2097.     (save-excursion
  2098.       (beginning-of-line)
  2099.       (setq beg (point))
  2100.       (end-of-line)
  2101.       (setq article (gnus-uu-article-number 
  2102.              (buffer-substring beg (point))))
  2103.       (message "Adding article %d to list" article)
  2104.       (setq gnus-uu-marked-article-list 
  2105.         (cons article gnus-uu-marked-article-list))))) 
  2106.  
  2107. (defun gnus-uu-mark-article (&optional dont-move)
  2108.   "Marks the current article to be decoded later."
  2109.   (interactive)
  2110.   (gnus-uu-enter-mark-in-list)
  2111.   (gnus-summary-mark-as-read nil ?#)
  2112.   (gnus-summary-next-subject 1 nil))
  2113.  
  2114. (defun gnus-uu-unmark-article ()
  2115.   "Unmarks the current article."
  2116.   (interactive)
  2117.   (let ((in (copy-sequence gnus-uu-marked-article-list))
  2118.     out article beg found
  2119.     (old-point (point)))
  2120.     (beginning-of-line)
  2121.       (setq beg (point))
  2122.       (end-of-line)
  2123.       (setq article (gnus-uu-article-number (buffer-substring beg (point))))
  2124.       (message "Removing article %d" article)
  2125.       (while in 
  2126.     (if (not (= (car in) article))
  2127.         (setq out (cons (car in) out))
  2128.       (setq found t)
  2129.       (message "Removing article %d" article))
  2130.     (setq in (cdr in)))
  2131.       (if (not found) (message "Not a marked article."))
  2132.       (setq gnus-uu-marked-article-list (reverse out))
  2133.       (gnus-summary-mark-as-unread nil t)
  2134.       (gnus-summary-next-subject 1 nil)))
  2135.  
  2136. (defun gnus-uu-unmark-all-articles ()
  2137.   "Removes the mark from all articles marked for decoding."
  2138.   (interactive)
  2139.   (while gnus-uu-marked-article-list
  2140.     (gnus-summary-goto-subject (car gnus-uu-marked-article-list))
  2141.     (gnus-summary-mark-as-unread nil t)
  2142.     (setq gnus-uu-marked-article-list (cdr gnus-uu-marked-article-list))))
  2143.  
  2144. (defun gnus-uu-mark-by-regexp ()
  2145.   "Asks for a regular expression and marks all articles that match."
  2146.   (interactive)
  2147.   (let (exp)
  2148.     (setq exp (read-from-minibuffer "Mark (regexp): "))
  2149.     (setq gnus-uu-marked-article-list 
  2150.       (append gnus-uu-marked-article-list
  2151.           (reverse (gnus-uu-get-list-of-articles exp t))))
  2152.     (message "")))
  2153.       
  2154. (defun gnus-uu-mark-thread ()
  2155.   "Marks all articles downwards in this thread."
  2156.   (interactive)
  2157.   (beginning-of-line)
  2158.   (let (level)
  2159.     (if (not (search-forward ":" nil t))
  2160.     ()
  2161.       (setq level (current-column))
  2162.       (gnus-uu-enter-mark-in-list)
  2163.       (gnus-summary-mark-as-read nil ?#)
  2164.       (gnus-summary-search-forward)
  2165.       (while (< level (current-column))
  2166.     (gnus-uu-enter-mark-in-list)
  2167.     (gnus-summary-mark-as-read nil ?#)
  2168.     (gnus-summary-search-forward))
  2169.       (gnus-summary-search-backward))))
  2170.  
  2171.  
  2172. ;; Various stuff
  2173.  
  2174. (defun gnus-uu-string-in-list (string list)
  2175.   (while (and list
  2176.           (not (string= (car list) string))
  2177.           (setq list (cdr list))))
  2178.   list)
  2179.  
  2180. (defun gnus-uu-name-from-path (path)
  2181.   (string-match "/[^/]*$" path)
  2182.   (substring path (1+ (match-beginning 0))))
  2183.  
  2184. (defun gnus-uu-directory-files (dir)
  2185.   (let (files out file)
  2186.     (setq files (directory-files dir t))
  2187.     (while files
  2188.       (setq file (car files))
  2189.       (setq files (cdr files))
  2190.       (if (not (string-match "/\\.\\.?$" file))
  2191.       (setq out (cons file out))))
  2192.     (setq out (reverse out))
  2193.     (message "dir-files %s er %s" dir out)(sleep-for 2)
  2194.     out))
  2195.  
  2196. (defun gnus-uu-check-correct-stripped-uucode (start end)
  2197.   (let (found beg length short)
  2198.     (if (not gnus-uu-correct-stripped-uucode)
  2199.     ()
  2200.       (goto-char start)
  2201.  
  2202.       (if (re-search-forward " \\|`" end t)
  2203.       (progn
  2204.         (goto-char start)
  2205.         (while (not (eobp))
  2206.           (progn
  2207.         (if (looking-at "\n") (replace-match ""))
  2208.         (forward-line 1))))
  2209.         
  2210.     (while (not (eobp))
  2211.       (if (looking-at (concat gnus-uu-begin-string "\\|" 
  2212.                   gnus-uu-end-string))
  2213.           ()
  2214.         (if (not found)
  2215.         (progn
  2216.           (beginning-of-line)
  2217.           (setq beg (point))
  2218.           (end-of-line)
  2219.           (setq length (- (point) beg))))
  2220.         (setq found t)
  2221.         (beginning-of-line)
  2222.         (setq beg (point))
  2223.         (end-of-line)
  2224.         (if (not (= length (- (point) beg)))
  2225.         (insert (make-string (- length (- (point) beg)) ? ))))
  2226.       (forward-line 1))))))
  2227.  
  2228. (defun gnus-uu-initialize ()
  2229.   (gnus-uu-check-for-generated-files)
  2230.   (setq gnus-uu-tmp-dir (expand-file-name gnus-uu-tmp-dir))
  2231.   (if (string-match "[^/]$" gnus-uu-tmp-dir) 
  2232.       (setq gnus-uu-tmp-dir (concat gnus-uu-tmp-dir "/")))
  2233.   (if (not (file-directory-p gnus-uu-tmp-dir))
  2234.       (error "Temp directory %s doesn't exist" gnus-uu-tmp-dir)
  2235.     (if (not (file-writable-p gnus-uu-tmp-dir))
  2236.     (error "Temp directory %s can't be written to" gnus-uu-tmp-dir)))
  2237.   (setq gnus-uu-work-dir 
  2238.     (concat gnus-uu-tmp-dir (make-temp-name "gnus")))
  2239.   (gnus-uu-add-file gnus-uu-work-dir)
  2240.   (if (not (file-directory-p gnus-uu-work-dir)) 
  2241.       (make-directory gnus-uu-work-dir))
  2242.   (setq gnus-uu-work-dir (concat gnus-uu-work-dir "/"))
  2243.   (setq gnus-uu-error-during-unarching nil)
  2244.   (setq gnus-uu-interactive-file-list nil))
  2245.  
  2246. ; Kills the temporary uu buffers, kills any processes, etc.
  2247. (defun gnus-uu-clean-up ()
  2248.   (let (buf pst)
  2249.     (and gnus-uu-uudecode-process
  2250.      (setq pst (process-status (or gnus-uu-uudecode-process "nevair")))
  2251.      (if (or (eq pst 'stop) (eq pst 'run))
  2252.          (delete-process gnus-uu-uudecode-process)))
  2253.     (and (not gnus-uu-asynchronous) 
  2254.      (setq buf (get-buffer gnus-uu-output-buffer-name))
  2255.      (kill-buffer buf))
  2256.     (and (setq buf (get-buffer gnus-uu-result-buffer))
  2257.      (kill-buffer buf))))
  2258.  
  2259. ; `gnus-uu-check-for-generated-files' deletes any generated files that
  2260. ; hasn't been deleted, if, for instance, the user terminated decoding
  2261. ; with `C-g'.
  2262. (defun gnus-uu-check-for-generated-files ()
  2263.   (let (file dirs)
  2264.     (while gnus-uu-generated-file-list
  2265.       (setq file (car gnus-uu-generated-file-list))
  2266.       (setq gnus-uu-generated-file-list (cdr gnus-uu-generated-file-list))
  2267.       (if (not (string-match "/\\.[\\.]?$" file))
  2268.       (progn
  2269.         (if (file-directory-p file)
  2270.         (setq dirs (cons file dirs))
  2271.           (if (file-exists-p file)
  2272.           (delete-file file))))))
  2273.     (setq dirs (nreverse dirs))
  2274.     (while dirs
  2275.       (setq file (car dirs))
  2276.       (setq dirs (cdr dirs))
  2277.       (if (file-directory-p file)
  2278.       (delete-directory file)))))
  2279.  
  2280. ; Add a file (or a list of files) to be checked (and deleted if it/they
  2281. ; still exists upon exiting the newsgroup).
  2282. (defun gnus-uu-add-file (file)
  2283.   (if (stringp file)
  2284.       (setq gnus-uu-generated-file-list 
  2285.         (cons file gnus-uu-generated-file-list))
  2286.     (setq gnus-uu-generated-file-list 
  2287.       (append file gnus-uu-generated-file-list))))
  2288.  
  2289. ; Go to the next unread subject. If there is no further unread
  2290. ; subjects, go to the last subject in the buffer.
  2291. (defun gnus-uu-summary-next-subject ()
  2292.   (let (opi)
  2293.     (if (not (gnus-summary-search-forward t))
  2294.     (progn
  2295.       (goto-char 1)
  2296.       (sit-for 0)
  2297.       (goto-char (point-max))
  2298.       (forward-line -1)
  2299.       (beginning-of-line)
  2300.       (search-forward ":" nil t)))
  2301.  
  2302.     ; You may well find all this a bit puzzling - so do I, but I seem
  2303.     ; to have to do something like this to move to the next unread article,
  2304.     ; as `sit-for' seems to do some rather strange things here. Might
  2305.     ; be a bug in my head, probably.
  2306.     (setq opi (point))
  2307.     (sit-for 0)
  2308.     (goto-char opi)
  2309.     (gnus-summary-recenter)))
  2310.  
  2311. ; Inputs an action and a file and returns a full command, putting
  2312. ; ticks round the file name and escaping any ticks in the file name.
  2313. (defun gnus-uu-command (action file)
  2314.   (let ((ofile ""))
  2315.     (while (string-match "`\\|\"\\|\\$\\|\\\\" file)
  2316.       (progn
  2317.     (setq ofile
  2318.           (concat ofile (substring file 0 (match-beginning 0)) "\\"
  2319.               (substring file (match-beginning 0) (match-end 0))))
  2320.     (setq file (substring file (1+ (match-beginning 0))))))
  2321.     (setq ofile (concat "\"" ofile file "\""))
  2322.     (if (string-match "%s" action)
  2323.     (format action ofile)
  2324.       (concat action " " ofile))))
  2325.  
  2326.  
  2327. ;; Initializing
  2328. (add-hook 'gnus-exit-group-hook
  2329.       '(lambda ()
  2330.      (gnus-uu-clean-up)
  2331.      (setq gnus-uu-marked-article-list nil)
  2332.      (gnus-uu-check-for-generated-files)))
  2333.  
  2334.  
  2335. ;; Interactive exec mode
  2336.  
  2337. (defvar gnus-uu-output-window nil)
  2338. (defvar gnus-uu-mode-hook nil)
  2339. (defvar gnus-uu-mode-map nil)
  2340.  
  2341. (defun gnus-uu-do-interactive ()
  2342.   (let (int-buffer out-buf)
  2343.     (set-buffer 
  2344.      (setq int-buffer (get-buffer gnus-uu-interactive-buffer-name)))
  2345.     (switch-to-buffer-other-window int-buffer)
  2346.     (pop-to-buffer int-buffer)
  2347.     (setq gnus-uu-output-window 
  2348.       (split-window nil (- (window-height) gnus-uu-output-window-height)))
  2349.     (set-window-buffer gnus-uu-output-window
  2350.                (setq out-buf 
  2351.                  (get-buffer-create gnus-uu-output-buffer-name)))
  2352.     (save-excursion (set-buffer out-buf) (erase-buffer))
  2353.     (goto-char 1)
  2354.     (forward-line 3)
  2355.     (run-hooks 'gnus-uu-mode-hook)))
  2356.  
  2357. (defun gnus-uu-enter-interactive-file (action file)
  2358.   (let (command)
  2359.     (save-excursion
  2360.       (set-buffer (get-buffer-create gnus-uu-interactive-buffer-name))
  2361.       (if (not gnus-uu-interactive-file-list)
  2362.       (progn
  2363.         (erase-buffer)
  2364.         (gnus-uu-mode)
  2365.         (insert 
  2366.          "# Press return to execute a command.
  2367. # Press `C-c C-c' to exit interactive view.
  2368.  
  2369. ")))   
  2370.       (setq gnus-uu-interactive-file-list
  2371.         (cons file gnus-uu-interactive-file-list))
  2372.       (setq command (gnus-uu-command action file))
  2373.       (insert (format "%s\n" command)))))
  2374.  
  2375. (defun gnus-uu-interactive-execute ()
  2376.   "Executes the command on the current line in interactive mode."
  2377.   (interactive)
  2378.   (let (beg out-buf command)
  2379.     (beginning-of-line)
  2380.     (setq beg (point))
  2381.     (end-of-line)
  2382.     (setq command (buffer-substring beg (point)))
  2383.     (setq out-buf (get-buffer-create gnus-uu-output-buffer-name))
  2384.     (save-excursion
  2385.       (set-buffer out-buf)
  2386.       (erase-buffer)
  2387.       (insert (format "$ %s \n\n" command)))
  2388.     (message "Executing...")
  2389.     (if gnus-uu-asynchronous
  2390.     (start-process "gnus-uu-view" out-buf "sh" "-c" command)
  2391.       (call-process "sh" nil out-buf nil "-c" command)
  2392.       (message ""))
  2393.     (forward-line 1)
  2394.     (beginning-of-line)))
  2395.  
  2396. (defun gnus-uu-interactive-end ()
  2397.   "This function exits interactive view mode and returns to summary mode."
  2398.   (interactive)
  2399.   (let (buf)
  2400.     (delete-window gnus-uu-output-window)
  2401.     (gnus-uu-clean-up)
  2402.     (if (not gnus-uu-asynchronous) (gnus-uu-check-for-generated-files))
  2403.     (setq buf (get-buffer gnus-uu-interactive-buffer-name))
  2404.     (if gnus-article-buffer (switch-to-buffer gnus-article-buffer))
  2405.     (if buf (kill-buffer buf))
  2406.     (pop-to-buffer gnus-summary-buffer)))
  2407.  
  2408. (if gnus-uu-mode-map
  2409.     ()
  2410.   (setq gnus-uu-mode-map (make-sparse-keymap))
  2411.   (define-key gnus-uu-mode-map "\C-c\C-x" 'gnus-uu-interactive-execute)
  2412.   (define-key gnus-uu-mode-map "\C-c\C-v" 'gnus-uu-interactive-execute)
  2413.   (define-key gnus-uu-mode-map "\C-m" 'gnus-uu-interactive-execute)
  2414.   (define-key gnus-uu-mode-map "\C-c\C-c" 'gnus-uu-interactive-end)
  2415.   (define-key gnus-uu-mode-map "\C-cs" 
  2416.     'gnus-uu-interactive-save-current-file)
  2417.   (define-key gnus-uu-mode-map "\C-c\C-s"
  2418.     'gnus-uu-interactive-save-current-file-silent)
  2419.   (define-key gnus-uu-mode-map "\C-c\C-w" 'gnus-uu-interactive-save-all-files)
  2420.   (define-key gnus-uu-mode-map "\C-c\C-o" 'gnus-uu-interactive-save-original-file))
  2421.  
  2422. (defun gnus-uu-interactive-save-original-file ()
  2423.   "Saves the file from whence the file on the current line came from."
  2424.   (interactive)
  2425.   (let (file)
  2426.     (if (file-exists-p 
  2427.      (setq file (concat gnus-uu-work-dir
  2428.                 (or gnus-uu-file-name gnus-uu-shar-file-name))))
  2429.     (progn
  2430.       (gnus-uu-save-file file)
  2431.       (message "Saved file %s" 
  2432.            (or gnus-uu-file-name gnus-uu-shar-file-name)))
  2433.       (message "Already saved."))))
  2434.  
  2435. (defun gnus-uu-interactive-save-current-file-silent ()
  2436.   "Saves the file referred to on the current line in the current directory."
  2437.   (interactive)
  2438.   (gnus-uu-interactive-save-current-file t))
  2439.  
  2440. (defun gnus-uu-interactive-save-current-file (&optional dont-ask silent)
  2441.   "Saves the file referred to on the current line."
  2442.   (interactive)
  2443.   (let (files beg line file)
  2444.     (setq files (copy-sequence gnus-uu-interactive-file-list))
  2445.     (beginning-of-line)
  2446.     (setq beg (point))
  2447.     (end-of-line)
  2448.     (setq line (buffer-substring beg (point)))
  2449.     (while (and files
  2450.         (not (string-match 
  2451.               (concat "" (regexp-quote (setq file (car files))) "")
  2452.               line)))
  2453.       (setq files (cdr files)))
  2454.     (beginning-of-line)
  2455.     (forward-line 1)
  2456.     (if (not files)
  2457.     (if (not silent)
  2458.         (progn (message "Could not find file") (sit-for 2)))
  2459.       (gnus-uu-save-file file (if dont-ask gnus-uu-current-save-dir nil) silent)
  2460.       (delete-region beg (point)))))
  2461.  
  2462. (defun gnus-uu-interactive-save-all-files ()
  2463.   "Saves all files referred to in the interactive buffer."
  2464.   (interactive)
  2465.   (let (dir)
  2466.     (goto-char 1)
  2467.     (setq dir (gnus-uu-read-directory "Where do you want the files? "))
  2468.     (while (not (eobp))
  2469.       (gnus-uu-interactive-save-current-file t t))))
  2470.  
  2471. (defun gnus-uu-mode ()
  2472.   "Major mode for editing view commands in gnus-uu.
  2473.  
  2474. Commands:
  2475. \\<gnus-uu-mode-map>Return, C-c C-v, C-c C-x        Execute the current command
  2476. \\[gnus-uu-interactive-end]                         End interactive mode
  2477. \\[gnus-uu-interactive-save-current-file]                           Save the current file
  2478. \\[gnus-uu-interactive-save-current-file-silent]                         Save the current file without asking 
  2479.                                 where to put it
  2480. \\[gnus-uu-interactive-save-all-files]                         Save all files
  2481. \\[gnus-uu-interactive-save-original-file]                         Save the original file: If the files
  2482.                                 originated in an archive, the archive 
  2483.                                 file is saved.
  2484. "
  2485.   (interactive)
  2486.   (kill-all-local-variables)
  2487.   (use-local-map gnus-uu-mode-map)   
  2488.   (setq mode-name "gnus-uu")         
  2489.   (setq major-mode 'gnus-uu-mode)    
  2490. )
  2491.  
  2492.   (define-key gnus-uu-mode-map "\C-c\C-x" 'gnus-uu-interactive-execute)
  2493.   (define-key gnus-uu-mode-map "\C-c\C-v" 'gnus-uu-interactive-execute)
  2494.   (define-key gnus-uu-mode-map "\C-m" 'gnus-uu-interactive-execute)
  2495.   (define-key gnus-uu-mode-map "\C-c\C-c" 'gnus-uu-interactive-end)
  2496.   (define-key gnus-uu-mode-map "\C-cs" 
  2497.     'gnus-uu-interactive-save-current-file)
  2498.   (define-key gnus-uu-mode-map "\C-c\C-s"
  2499.     'gnus-uu-interactive-save-current-file-silent)
  2500.   (define-key gnus-uu-mode-map "\C-c\C-a" 'gnus-uu-interactive-save-all-files)
  2501.   (define-key gnus-uu-mode-map "\C-c\C-o" 'gnus-uu-interactive-save-original-file)
  2502.  
  2503.  
  2504. ;; Major mode for posting encoded articles.
  2505.  
  2506. (require 'sendmail)
  2507. (require 'rnews)
  2508.  
  2509. ; Any function that is to be used as and encoding method will take two
  2510. ; parameters: PATH-NAME and FILE-NAME. (E.g. "/home/gaga/spiral.jpg"
  2511. ; and "spiral.jpg", respectively.) The function should return nil if
  2512. ; the encoding wasn't successful.
  2513. (defvar gnus-uu-post-encode-method 'gnus-uu-post-encode-uuencode
  2514.   "Function used for encoding binary files.
  2515. There are three functions supplied with gnus-uu for encoding files:
  2516. `gnus-uu-post-encode-uuencode', which does straight uuencoding;
  2517. `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME 
  2518. headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with 
  2519. uuencode and adds MIME headers.")
  2520.  
  2521. (defvar gnus-uu-post-include-before-composing nil
  2522.   "Non-nil means that gnus-uu will ask for a file to encode before you compose the article.
  2523. If this variable is t, you can either include an encoded file with
  2524. \\<gnus-uu-post-reply-mode-map>\\[gnus-uu-post-insert-binary-in-article] or have one included for you when you post the article.")
  2525.  
  2526. (defvar gnus-uu-post-length 990
  2527.   "Maximum length of an article.
  2528. The encoded file will be split into how many articles it takes to
  2529. post the entire file.")
  2530.  
  2531. (defvar gnus-uu-post-threaded nil
  2532.   "Non-nil means that gnus-uu will post the encoded file in a thread.
  2533. This may not be smart, as no other decoder I have seen are able to
  2534. follow threads when collecting uuencoded articles. (Well, I have seen
  2535. one package that does that - gnus-uu, but somehow, I don't think that 
  2536. counts...) Default is nil.")
  2537.  
  2538. (defvar gnus-uu-post-separate-description t
  2539.   "Non-nil means that the description will be posted in a separate article.
  2540. The first article will typically be numbered (0/x). If this variable
  2541. is nil, the description the user enters will be included at the 
  2542. beginning of the first article, which will be numbered (1/x). Default 
  2543. is t.")
  2544.  
  2545. (defconst gnus-uu-post-binary-separator "--binary follows this line--")
  2546. (defvar gnus-uu-post-message-id nil)
  2547. (defvar gnus-uu-post-inserted-file-name nil)
  2548. (defvar gnus-uu-winconf-post-news nil)
  2549.  
  2550. ; The following map and mode was taken from rnewspost.el and edited
  2551. ; somewhat.
  2552. (defvar gnus-uu-post-reply-mode-map () "Mode map used by gnus-uu-post-reply.")
  2553. (or gnus-uu-post-reply-mode-map
  2554.     (progn
  2555.       (setq gnus-uu-post-reply-mode-map (make-keymap))
  2556.       (define-key gnus-uu-post-reply-mode-map "\C-c?" 'describe-mode)
  2557.       (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-d" 
  2558.     'news-reply-distribution)
  2559.       (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-k" 
  2560.     'news-reply-keywords)
  2561.       (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-n" 
  2562.     'news-reply-newsgroups)
  2563.       
  2564.       (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-f" 
  2565.     'news-reply-followup-to)
  2566.       (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-s" 'mail-subject)
  2567.       (define-key gnus-uu-post-reply-mode-map "\C-c\C-f\C-a" 
  2568.     'gnus-uu-post-reply-summary)
  2569.       (define-key gnus-uu-post-reply-mode-map "\C-c\C-r" 
  2570.     'news-caesar-buffer-body)
  2571.       (define-key gnus-uu-post-reply-mode-map "\C-c\C-w" 'news-reply-signature)
  2572.       (define-key gnus-uu-post-reply-mode-map "\C-c\C-y" 
  2573.     'news-reply-yank-original)
  2574.       (define-key gnus-uu-post-reply-mode-map "\C-c\C-q" 
  2575.     'mail-fill-yanked-message)
  2576.       (define-key gnus-uu-post-reply-mode-map "\C-c\C-c" 
  2577.     'gnus-uu-post-news-inews)
  2578.       (define-key gnus-uu-post-reply-mode-map "\C-c\C-s" 
  2579.     'gnus-uu-post-news-inews)
  2580.       (define-key gnus-uu-post-reply-mode-map "\C-c\C-i" 
  2581.     'gnus-uu-post-insert-binary-in-article)
  2582.       ))
  2583.  
  2584. ; This mode was taken from rnewspost.el and modified slightly.
  2585. (defun gnus-uu-post-reply-mode ()
  2586.   "Major mode for editing binary news to be posted on USENET.
  2587. First-time posters are asked to please read the articles in newsgroup:
  2588.                                                      news.announce.newusers .
  2589.  
  2590. Like news-reply-mode, which is like Text Mode, but with these
  2591. additional commands:
  2592.  
  2593. \\<gnus-uu-post-reply-mode-map>\\[gnus-uu-post-news-inews]  post the message.
  2594. C-c C-f     move to a header field (and create it if there isn't):
  2595.      C-c C-f C-n  move to Newsgroups:    C-c C-f C-s  move to Subj:
  2596.      C-c C-f C-f  move to Followup-To:      C-c C-f C-k  move to Keywords:
  2597.      C-c C-f C-d  move to Distribution:    C-c C-f C-a  move to Summary:
  2598. C-c C-y  news-reply-yank-original (insert current message, in NEWS).
  2599. C-c C-q  mail-fill-yanked-message (fill what was yanked).
  2600. C-c C-r  caesar rotate all letters by 13 places in the article's body (rot13).
  2601. \\[gnus-uu-post-insert-binary-in-article]  encode and include a file in this article.
  2602.  
  2603. This mode is almost identical to news-reply-mode, but has some
  2604. additional commands for treating encoded binary articles. In
  2605. particular, \\[gnus-uu-post-news-inews] will ask for a file to include, if
  2606. one hasn't been included already. It will post, first, the message
  2607. composed, and then it will post as many additional articles it takes
  2608. to post the entire encoded files.
  2609.  
  2610.    Relevant Variables
  2611.  
  2612.    `gnus-uu-post-encode-method' 
  2613.    There are three functions supplied with gnus-uu for encoding files:
  2614.    `gnus-uu-post-encode-uuencode', which does straight uuencoding;
  2615.    `gnus-uu-post-encode-mime', which encodes with base64 and adds MIME 
  2616.    headers; and `gnus-uu-post-encode-mime-uuencode', which encodes with 
  2617.    uuencode and adds MIME headers.
  2618.  
  2619.    `gnus-uu-post-include-before-composing'
  2620.    Non-nil means that gnus-uu will ask for a file to encode before you
  2621.    compose the article.  If this variable is t, you can either include
  2622.    an encoded file with `C-c C-i' or have one included for you when you 
  2623.    post the article.
  2624.  
  2625.    `gnus-uu-post-length'
  2626.    Maximum length of an article. The encoded file will be split into how 
  2627.    many articles it takes to post the entire file.
  2628.  
  2629.    `gnus-uu-post-separate-description'
  2630.    Non-nil means that the description will be posted in a separate 
  2631.    article. The first article will typically be numbered (0/x). If 
  2632.    this variable is nil, the description the user enters will be 
  2633.    included at the beginning of the first article, which will be 
  2634.    numbered (1/x). Default is t.
  2635.  
  2636.    `gnus-uu-post-threaded'
  2637.    Non-nil means that gnus-uu will post the encoded file in a thread.
  2638.    This may not be smart, as no other decoder I have seen are able to
  2639.    follow threads when collecting uuencoded articles. (Well, I have seen
  2640.    one package that does that - gnus-uu, but somehow, I don't think that 
  2641.    counts...) Default is nil.
  2642. "
  2643.   (interactive)
  2644.   ;; require...
  2645.   (or (fboundp 'mail-setup) (load "sendmail"))
  2646.   (kill-all-local-variables)
  2647.   (make-local-variable 'mail-reply-buffer)
  2648.   (setq mail-reply-buffer nil)
  2649.   (set-syntax-table text-mode-syntax-table)
  2650.   (use-local-map gnus-uu-post-reply-mode-map)
  2651.   (setq local-abbrev-table text-mode-abbrev-table)
  2652.   (setq major-mode 'gnus-uu-post-reply-mode)
  2653.   (setq mode-name "Gnus UU News")
  2654.   (make-local-variable 'paragraph-separate)
  2655.   (make-local-variable 'paragraph-start)
  2656.   (setq paragraph-start (concat "^" mail-header-separator "$\\|"
  2657.                 paragraph-start))
  2658.   (setq paragraph-separate (concat "^" mail-header-separator "$\\|"
  2659.                    paragraph-separate))
  2660.   (run-hooks 'text-mode-hook 'gnus-uu-post-reply-mode-hook))
  2661.  
  2662. (defun gnus-uu-post-news ()
  2663.   "Compose an article and post an encoded file."
  2664.   (interactive)
  2665.   (setq gnus-uu-post-inserted-file-name nil)
  2666.   (setq gnus-uu-winconf-post-news (current-window-configuration))
  2667.   (let (news-reply-mode)
  2668.     (fset 'news-reply-mode 'gnus-uu-post-reply-mode)
  2669.     (gnus-summary-post-news)
  2670.     (if gnus-uu-post-include-before-composing
  2671.     (save-excursion (setq gnus-uu-post-inserted-file-name 
  2672.                   (gnus-uu-post-insert-binary))))))
  2673.  
  2674. (defun gnus-uu-post-insert-binary-in-article ()
  2675.   "Inserts an encoded file in the buffer.
  2676. The user will be asked for a file name."
  2677.   (interactive)
  2678.   (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
  2679.       (error "Not in post-news buffer"))
  2680.   (save-excursion 
  2681.     (setq gnus-uu-post-inserted-file-name (gnus-uu-post-insert-binary))))
  2682.  
  2683. ; Encodes with uuencode and substitutes all spaces with backticks.
  2684. (defun gnus-uu-post-encode-uuencode (path file-name)
  2685.   (if (gnus-uu-post-encode-file "uuencode" path file-name)
  2686.       (progn
  2687.     (goto-char 1)
  2688.     (forward-line 1)
  2689.     (while (re-search-forward " " nil t)
  2690.       (replace-match "`"))
  2691.     t)))
  2692.  
  2693. ; Encodes with uuencode and adds MIME headers.
  2694. (defun gnus-uu-post-encode-mime-uuencode (path file-name)
  2695.   (if (gnus-uu-post-encode-uuencode path file-name)
  2696.       (progn
  2697.     (gnus-uu-post-make-mime file-name "x-uue")
  2698.     t)))
  2699.  
  2700. ; Encodes with base64 and adds MIME headers
  2701. (defun gnus-uu-post-encode-mime (path file-name)
  2702.   (if (gnus-uu-post-encode-file "mmencode" path file-name)
  2703.       (progn
  2704.     (gnus-uu-post-make-mime file-name "base64")
  2705.     t)))
  2706.  
  2707. ; Adds MIME headers.
  2708. (defun gnus-uu-post-make-mime (file-name encoding)
  2709.   (goto-char 1)
  2710.   (insert (format "Content-Type: %s; name=\"%s\"\n" 
  2711.           (gnus-uu-choose-action file-name gnus-uu-ext-to-mime-list) 
  2712.           file-name))
  2713.   (insert (format "Content-Transfer-Encoding: %s\n\n" encoding))
  2714.   (save-restriction
  2715.     (set-buffer gnus-post-news-buffer)
  2716.     (goto-char 1)
  2717.     (re-search-forward mail-header-separator)
  2718.     (beginning-of-line)
  2719.     (forward-line -1)
  2720.     (narrow-to-region 1 (point))
  2721.     (or (mail-fetch-field "mime-version")
  2722.     (progn
  2723.       (widen)
  2724.       (insert "MIME-Version: 1.0\n")))
  2725.     (widen)))
  2726.  
  2727. ; Encodes a file PATH with COMMAND, leaving the result in the
  2728. ; current buffer.
  2729. (defun gnus-uu-post-encode-file (command path file-name)
  2730.   (= 0 (call-process "sh" nil t nil "-c" 
  2731.              (format "%s %s %s" command path file-name))))
  2732.  
  2733. (defun gnus-uu-post-news-inews ()
  2734.   "Posts the composed news article and encoded file.
  2735. If no file has been included, the user will be asked for a file."
  2736.   (interactive)
  2737.   (if (not (eq (current-buffer) (get-buffer gnus-post-news-buffer)))
  2738.       (error "Not in post news buffer"))
  2739.  
  2740.   (let (file-name)
  2741.  
  2742.     (if gnus-uu-post-inserted-file-name
  2743.     (setq file-name gnus-uu-post-inserted-file-name)
  2744.       (setq file-name (gnus-uu-post-insert-binary)))
  2745.   
  2746.     (if gnus-uu-post-threaded
  2747.     (let ((gnus-required-headers 
  2748.            (if (memq 'Message-ID gnus-required-headers)
  2749.            gnus-required-headers
  2750.          (cons 'Message-ID gnus-required-headers)))
  2751.           gnus-inews-article-hook elem)
  2752.  
  2753.       (setq gnus-inews-article-hook (if (listp gnus-inews-article-hook)
  2754.                         gnus-inews-article-hook
  2755.                       (list gnus-inews-article-hook)))
  2756.       (setq gnus-inews-article-hook 
  2757.         (cons
  2758.          '(lambda ()
  2759.             (save-excursion
  2760.               (goto-char 1)
  2761.               (if (re-search-forward "^Message-ID: \\(.*\\)$" nil t)
  2762.               (setq gnus-uu-post-message-id 
  2763.                 (buffer-substring 
  2764.                  (match-beginning 1) (match-end 1)))
  2765.             (setq gnus-uu-post-message-id nil))))
  2766.          gnus-inews-article-hook))
  2767.       (gnus-uu-post-encoded file-name t))
  2768.       (gnus-uu-post-encoded file-name nil)))
  2769.   (setq gnus-uu-post-inserted-file-name nil)
  2770.   (and gnus-uu-winconf-post-news
  2771.        (set-window-configuration gnus-uu-winconf-post-news)))
  2772.       
  2773. ; Asks for a file to encode, encodes it and inserts the result in
  2774. ; the current buffer. Returns the file name the user gave.
  2775. (defun gnus-uu-post-insert-binary ()
  2776.   (let ((uuencode-buffer-name "*uuencode buffer*")
  2777.     file-path post-buf uubuf file-name)
  2778.  
  2779.     (setq file-path (read-file-name 
  2780.              "What file do you want to encode? "))
  2781.     (if (not (file-exists-p file-path))
  2782.     (error "%s: No such file" file-path))
  2783.  
  2784.     (goto-char (point-max))
  2785.     (insert (format "\n%s\n" gnus-uu-post-binary-separator))
  2786.     
  2787.     (if (string-match "^~/" file-path)
  2788.     (setq file-path (concat "$HOME" (substring file-path 1))))
  2789.     (if (string-match "/[^/]*$" file-path)
  2790.     (setq file-name (substring file-path (1+ (match-beginning 0))))
  2791.       (setq file-name file-path))
  2792.  
  2793.     (unwind-protect
  2794.     (if (save-excursion
  2795.           (set-buffer (setq uubuf 
  2796.                 (get-buffer-create uuencode-buffer-name)))
  2797.           (erase-buffer)
  2798.           (funcall gnus-uu-post-encode-method file-path file-name))
  2799.         (insert-buffer uubuf)
  2800.       (error "Encoding unsuccessful"))
  2801.       (kill-buffer uubuf))
  2802.     file-name))
  2803.  
  2804. ; Posts the article and all of the encoded file.
  2805. (defun gnus-uu-post-encoded (file-name &optional threaded)
  2806.   (let ((send-buffer-name "*uuencode send buffer*")
  2807.     (encoded-buffer-name "*encoded buffer*")
  2808.     (top-string "[ cut here %s (%s %d/%d) %s gnus-uu ]")
  2809.     (separator (concat mail-header-separator "\n\n"))
  2810.     file uubuf length parts header i end beg
  2811.     beg-line minlen buf post-buf whole-len beg-binary end-binary)
  2812.  
  2813.     (setq post-buf (current-buffer))
  2814.  
  2815.     (goto-char 1)
  2816.     (if (not (re-search-forward 
  2817.           (if gnus-uu-post-separate-description 
  2818.           gnus-uu-post-binary-separator 
  2819.         mail-header-separator) nil t))
  2820.     (error "Internal error: No binary/header separator"))
  2821.     (beginning-of-line)
  2822.     (forward-line 1)
  2823.     (setq beg-binary (point))
  2824.     (setq end-binary (point-max))
  2825.  
  2826.     (save-excursion 
  2827.       (set-buffer (setq uubuf (get-buffer-create encoded-buffer-name)))
  2828.       (erase-buffer)
  2829.       (insert-buffer-substring post-buf beg-binary end-binary)
  2830.       (goto-char 1)
  2831.       (setq length (count-lines 1 (point-max)))
  2832.       (setq parts (/ length gnus-uu-post-length))
  2833.       (if (not (< (% length gnus-uu-post-length) 4))
  2834.       (setq parts (1+ parts))))
  2835.  
  2836.     (if gnus-uu-post-separate-description
  2837.     (forward-line -1))
  2838.     (kill-region (point) (point-max))
  2839.  
  2840.     (goto-char 1)
  2841.     (search-forward mail-header-separator nil t)
  2842.     (beginning-of-line)
  2843.     (setq header (buffer-substring 1 (point)))
  2844.  
  2845.     (goto-char 1)
  2846.     (if (not gnus-uu-post-separate-description)
  2847.     ()
  2848.       (if (and (not threaded) (re-search-forward "^Subject: " nil t))
  2849.       (progn
  2850.         (end-of-line)
  2851.         (insert (format " (0/%d)" parts))))
  2852.       (gnus-inews-news))
  2853.  
  2854.     (save-excursion
  2855.       (setq i 1)
  2856.       (setq beg 1)
  2857.       (while (not (> i parts))
  2858.     (set-buffer (get-buffer-create send-buffer-name))
  2859.     (erase-buffer)
  2860.     (insert header)
  2861.     (if (and threaded gnus-uu-post-message-id)
  2862.         (insert (format "References: %s\n" gnus-uu-post-message-id)))
  2863.     (insert separator)
  2864.     (setq whole-len
  2865.           (- 62 (length (format top-string "" file-name i parts ""))))
  2866.     (if (> 1 (setq minlen (/ whole-len 2)))
  2867.         (setq minlen 1))
  2868.     (setq 
  2869.      beg-line 
  2870.      (format top-string
  2871.          (make-string minlen ?-) 
  2872.          file-name i parts
  2873.          (make-string 
  2874.           (if (= 0 (% whole-len 2)) (1- minlen) minlen) ?-)))
  2875.  
  2876.     (goto-char 1)
  2877.     (if (not (re-search-forward "^Subject: " nil t))
  2878.         ()
  2879.       (if (not threaded)
  2880.           (progn
  2881.         (end-of-line)
  2882.         (insert (format " (%d/%d)" i parts)))
  2883.         (if (or (and (= i 2) gnus-uu-post-separate-description)
  2884.             (and (= i 1) (not gnus-uu-post-separate-description)))
  2885.         (replace-match "Subject: Re: "))))
  2886.           
  2887.     (goto-char (point-max))
  2888.     (save-excursion
  2889.       (set-buffer uubuf)
  2890.       (goto-char beg)
  2891.       (if (= i parts)
  2892.           (goto-char (point-max))
  2893.         (forward-line gnus-uu-post-length))
  2894.       (setq end (point)))
  2895.     (insert-buffer-substring uubuf beg end)
  2896.     (insert beg-line)
  2897.     (insert "\n")
  2898.     (setq beg end)
  2899.     (setq i (1+ i))
  2900.     (goto-char 1)
  2901.     (re-search-forward mail-header-separator nil t)
  2902.     (beginning-of-line)
  2903.     (forward-line 2)
  2904.     (if (re-search-forward gnus-uu-post-binary-separator nil t)
  2905.         (progn 
  2906.           (replace-match "")
  2907.           (forward-line 1)))
  2908.     (insert beg-line)
  2909.     (insert "\n")
  2910.     (gnus-inews-news)))
  2911.  
  2912.     (and (setq buf (get-buffer send-buffer-name))
  2913.      (kill-buffer buf))
  2914.     (and (setq buf (get-buffer encoded-buffer-name))
  2915.      (kill-buffer buf))
  2916.  
  2917.     (if (not gnus-uu-post-separate-description)
  2918.     (progn
  2919.       (set-buffer-modified-p nil)
  2920.       (and (fboundp 'bury-buffer) (bury-buffer))))))
  2921.  
  2922. (provide 'gnus-uu)
  2923.  
  2924. ;; gnus-uu.el ends here
  2925.  
  2926.