(*

Reply to digest
Michael Schürig 1997, Public Domain

Required Scripting Additions:

"Jon's Commands" by Jon Pugh, freely available from
<http://iw.cts.com/~jonpugh/Default.html> (and info-mac)

"Kamprath's Text Utilities" by Michael F. Kamprath, freely available from
<http://www.leonardo.net/kamprath/claireware.html> (and info-mac).

Usage: Select the digested message you want to reply to including its headers.
"From:" field in the selected text is used for address (private reply to author).
Hold down the control key to include attribution with date, time and author;
otherwise only author is used.

Latest changes/modifications:
Markus Frischknecht (mailto:mfrischknecht@access.ch) 20/7/1999:
 - added use of personalities

This script is public domain. Use and change at will, but if you release a
modified version please include the information above.


*)

property nl : ASCII character 10

set AddDate to ("control" is in (keys pressed)) -- OSA Menu uses "option" for itself


-- set up double tell mechanism

tell application "Finder"
	try
		-- MacOS 8.0 and 8.1, but also works for Sys 7.5 and 7.6
		set EU to (process 1 whose creator type is "CSOm") as «class psn »
	on error
		-- Eudora is not running, so abort
		beep
	end try
end tell


tell application "Eudora Pro 3.1.3" to tell EU
	try
		
		set qmsg to selected text
		
		set subj to my extractField("Subject", qmsg)
		
		set who to my extractField("From", qmsg)
		
		if AddDate then
			set when to my extractField("Date", qmsg)
			set attrib to "On " & when & ", " & who & " wrote:" & return
		else
			-- set attrib to "" -- empty text, not used (control key wasn't down)
			-- or just use name of author:
			set attrib to who & " wrote:" & return
		end if
		
		-- for replying to Author only:
		set replyTo to my extractField("From", qmsg)
		
		-- for replying to whole list:
		-- set replyTo to field "reply-to" of message ""
		-- set replyTo to text 11 thru -1 of replyTo -- remove leading "reply-to: "
		
		
		try
			-- in Pro version: get personality of current msg, use on new msg
			set msgPersonality to personality of message "" -- works fine
			set personalityPresent to true
		on error
			-- no personalities present in this version of Eudora
			set personalityPresent to false
		end try
		
		-- now create new message and edit it
		set msg to make new message at end of mailbox "Out"
		
		if personalityPresent then
			set personality of msg to msgPersonality -- apply same personality as orig. msg
		end if
		
		set field "To" of msg to replyTo
		
		set SubjQuoteStr to get setting 7020 -- use re quote from Eudora settings!
		set field "Subject" of msg to (SubjQuoteStr & subj)
		
		
		set msgBody to my makeBody(qmsg)
		
		set field "" of msg to (attrib & msgBody)
		
	on error errMsg
		if length of errMsg > 255 then
			set errMsg to text 1 thru 255 of errMsg
		end if
		display dialog errMsg buttons {"OK"} default button 1
	end try
end tell

on extractField(fldnm, msg)
	set fld to find text after (fldnm & ":") in the text msg stopping at return
	if fld is "" then error "No " & fldnm & " found in the selected text."
	repeat while first character of fld is " "
		set fld to text 2 thru -1 of fld
	end repeat
	return fld
end extractField

on makeBody(msg)
	-- remove headers (they're separated by a blank line)
	set off to offset of return & return in msg
	
	-- until end of msg: offset -1
	set msg to text (off + 1) thru -1 of msg
	
	-- add quote chars
	set msg to replace string return in the text msg with string (nl & "> ")
	set msg to replace string (nl & "> >") in the text msg with string (nl & ">>")
	set msg to replace string nl in the text msg with string return
	set msg to text (2) thru -1 of msg -- get rid of first empty line (1 CR)
	
	-- MF:
	-- if selection ended with CR, an additional quote prefix is now at end of msg;
	-- delete, just CR wanted
	
	set msglength to length of msg
	set last2char to text (msglength - 1) thru -1 of msg
	
	if last2char is equal to "> " then
		set msg to text (1) thru (msglength - 2) of msg & return
	end if
	-- end of new
	
	return msg
end makeBody