-- Eudora-GPG-Lib version 0.008 -- Richard Chang -- Read the Notes file. Use at your own risk. -- If you make money with this, give me some. Otherwise free. -- -- Version .008 -- 1. Fixed ParseToField to handle empty To field, etc. -- Read a Unix text file and return the contents as a string with -- linefeeds replaced with carriage returns -- on ReadUnixFile(filename) set CR to ASCII character 13 set LF to ASCII character 10 try tell application "Finder" if file filename exists then if size of file filename > 0 then set ifile to (open for access file filename) set AMesg to (read ifile) close access ifile else return "" end if else tell me to display dialog "File not found: " & filename buttons {"OK"} default button "OK" return "" end if end tell on error tell me to display dialog "Could not read file: " & filename buttons {"OK"} default button "OK" return "" end try -- Replace Unix linefeeds with Mac carriage returns set NewMesg to "" repeat with i from 1 to length of AMesg set NextChar to character i of AMesg if NextChar is equal to LF then set NewMesg to NewMesg & CR else set NewMesg to NewMesg & NextChar end if end repeat return NewMesg end ReadUnixFile -- Write some text to a Unix text file. Carriage returns are replaced with -- linefeeds prior to writing. -- on WriteUnixFile(filename, SomeText) set CR to ASCII character 13 set LF to ASCII character 10 -- replace Mac carriage returns with Unix linefeeds set NewMesg to "" repeat with i from 1 to length of SomeText set NextChar to character i of SomeText if NextChar is equal to CR then set NewMesg to NewMesg & LF else set NewMesg to NewMesg & NextChar end if end repeat try tell application "Finder" set ofile to (open for access file filename with write permission) --set group privileges of file filename to none --set everyones privileges of file filename to none write NewMesg to ofile close access ofile end tell on error display dialog "Could not write to file: " & filename end try end WriteUnixFile -- Make a unique temporary folder in the user's "Temporary Items" folder -- in /tmp// (which is not readable to other users). -- Returns a pair of values { Mac path as string, single quotedUnix path } -- on MakeTempFolder() -- Get user's Temporary Items directory Mac-style -- The parent directory of "Temporary Items" has permission og-rwx tell application "Finder" to set TempFolder to (path to the temporary items folder) -- Create a new directory in user's Temporary Items Folder set i to 0 set found to false tell application "Finder" repeat until found set FolderName to "temp" & i if (folder FolderName of TempFolder) exists then set i to i + 1 else set found to true set NewFolder to make folder in TempFolder with properties {name:FolderName} set group privileges of NewFolder to none set everyones privileges of NewFolder to none end if end repeat end tell set theFolder to (NewFolder as string) set UnixDir to POSIX path of theFolder set QuotedUnixDir to "'" & UnixDir & "'" --display dialog theFolder --display dialog QuotedUnixDir do shell script "chmod og-rwx " & QuotedUnixDir -- for good measure return {theFolder, QuotedUnixDir} end MakeTempFolder -- Clean up temporary files securely (overwrite) and safely (be paranoid about deleting stuff). -- Parameters: -- aFolder is a MacOS folder as string where the files live. -- SomeFiles is a string with file names separated by spaces (to be used with rm) -- on CleanUp(aFolder, SomeFiles) tell application "Finder" -- pick a command to overwrite and delete files if file "sw:bin:shred" of startup disk exists then set RM to "/sw/bin/shred -uz" else if file "usr:bin:shred" of startup disk exists then set RM to "/usr/bin/shred -uz" else set RM to "/bin/rm -P" end if set UnixDir to POSIX path of aFolder set QuotedUnixDir to "'" & UnixDir & "'" if (folder aFolder exists) then do shell script "cd " & QuotedUnixDir & " ; " & RM & " " & SomeFiles & "; /bin/rmdir " & QuotedUnixDir end if -- tell Finder to check that we removed this folder! update folder aFolder end tell end CleanUp -- Parse To: field of message and extract the first recipient -- on ParseToField(SomeText) -- skip over initial quoted string (e.g., "John Q. Smith" ) set state to 1 set i to 4 -- skip over the "To: " prefix repeat while state is not 4 set i to i + 1 -- fell off the end of the string? if i > length of SomeText then -- display dialog "Could not parse email address" return "" end if set char to character i of SomeText -- AppleScript doesn't have a case/switch construct! -- state 1 = initial state, skips over spaces if state is 1 then if char is "\"" then set state to 2 else if char is not " " then set state to 4 end if -- state 2 = seen a " else if state is 2 then if char is "\"" then -- found closing " set state to 5 else if char is "\\" then -- take care of escapes set state to 3 end if -- state 3 = gobble up escaped char else if state is 3 then set state to 2 -- state 5 = exit after advancing 1 char else if state is 5 then set state to 4 else display dialog "Internal error: bad state = " & state return "" end if end repeat -- find @ symbol set AtIndex to 0 repeat while i ² length of SomeText if (character i of SomeText) is "@" then set AtIndex to i exit repeat end if set i to i + 1 end repeat -- display dialog "AtIndex = " & AtIndex if AtIndex = 0 then return "" -- find end of email address set i to AtIndex + 1 repeat while i ² length of SomeText set char to character i of SomeText if (char is ",") or (char is " ") or (char is ">") or (char is ";") or (char is "\"") then exit repeat end if set i to i + 1 end repeat set AddrStop to i - 1 -- display dialog "AddrStop = " & AddrStop -- find beginning of email address set i to AtIndex - 1 repeat while i ³ 1 set char to character i of SomeText if (char is ",") or (char is " ") or (char is "<") or (char is ";") or (char is "\"") then exit repeat end if set i to i - 1 end repeat set AddrStart to i + 1 -- display dialog "AddrStart = " & AddrStart -- construct substring. return (get text AddrStart thru AddrStop of SomeText) end ParseToField -- Get the email address of person to receive encrypted message. -- Bug user until a string of length >= 4 is entered -- on GetRecipient() set CR to ASCII character 13 tell application "Eudora" to set ToField to field "To:" of message 0 set recipient to ParseToField(ToField) set response to display dialog "Encrypt to:" default answer recipient buttons {"Cancel", "OK"} default button "OK" set recipient to (text returned of response) as string repeat if length of recipient ³ 6 then exit repeat set response to display dialog "Please enter an email address." & CR & Â "Encrypt to:" default answer recipient buttons {"Cancel", "OK"} default button "OK" set recipient to (text returned of response) as string end repeat return recipient end GetRecipient -- Write some text to a Unix text file. Carriage returns are replaced with -- linefeeds prior to writing. -- Long lines are split: -- 1. Last space before limit replaced with a line feed. -- 2. Quote prefixes (e.g. ">>") are copied to split lines. -- 3. Really long words are left unchanged. -- Returns Boolean value true if any lines were wrapped. on WrapUnixFile(filename, SomeText, WrapLength) set CR to ASCII character 13 set LF to ASCII character 10 set SP to ASCII character 32 -- Initialize set WasLong to false set NewMesg to "" set NewLine to "" set CharCount to 0 set FindPrefix to true set Prefix to "" set LastSpace to 0 repeat with i from 1 to length of SomeText set NextChar to character i of SomeText set CharCount to CharCount + 1 if FindPrefix then if (NextChar is ">") then set Prefix to Prefix & ">" else set FindPrefix to false end if end if if NextChar is CR then set NewMesg to NewMesg & NewLine & LF set NewLine to "" set CharCount to 0 set FindPrefix to true set Prefix to "" set LastSpace to 0 else set NewLine to NewLine & NextChar if NextChar is SP then set LastSpace to CharCount end if if CharCount > WrapLength and LastSpace > 0 then set WasLong to true if LastSpace > 1 then set NewMesg to NewMesg & (get text 1 thru (LastSpace - 1) of NewLine) & LF else set NewMesg to NewMesg & LF end if if LastSpace < CharCount then set NewLine to Prefix & (get text (LastSpace + 1) thru CharCount of NewLine) else set NewLine to Prefix end if set CharCount to (length of Prefix) + CharCount - LastSpace set LastSpace to 0 end if end if end repeat -- in case last line has no CR set NewMesg to NewMesg & NewLine try tell application "Finder" set ofile to (open for access file filename with write permission) --set group privileges of file filename to none --set everyones privileges of file filename to none write NewMesg to ofile close access ofile end tell on error display dialog "Could not write to file: " & filename end try return WasLong end WrapUnixFile