Advertisement
MestreLion

eudora - launcher for Eudora Mail in wine

Aug 21st, 2011
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 16.17 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # eudora - Launcher for Qualcomm's Eudora Mail 7 under Wine
  4. #
  5. #    Copyright (C) 2011 Rodrigo Silva (MestreLion) <[email protected]>
  6. #
  7. #    This program is free software: you can redistribute it and/or modify
  8. #    it under the terms of the GNU General Public License as published by
  9. #    the Free Software Foundation, either version 3 of the License, or
  10. #    (at your option) any later version.
  11. #
  12. #    This program is distributed in the hope that it will be useful,
  13. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #    GNU General Public License for more details.
  16. #
  17. #    You should have received a copy of the GNU General Public License
  18. #    along with this program.  If not, see <http://www.gnu.org/licenses/gpl>.
  19. #
  20. #    IMPORTANT NOTE: The above copyright notice and GPL licence are for this
  21. #    launcher and associated native files only! Eudora Mail itself is
  22. #    propietary software, copyright of Qualcomm Inc. Wine and its tools
  23. #    are free software under a different copyright and license. See
  24. #    <http://www.eudora.com> and <http://www.winehq.org>
  25. #
  26. # Huge thanks for all the gurus and friends in irc://irc.freenet.org/#bash
  27. # and the contributors of http://mywiki.wooledge.org/ - Best bash source ever!
  28. #
  29. # TODO: use Simple MAPI for attachments
  30. # TODO: window size (start /MAX, /M, /R)
  31.  
  32. #######################################  Functions
  33.  
  34. manual()
  35. {
  36. cat << MANUALPAGE
  37. eudora - launcher for Qualcomm's Eudora Mail under wine
  38.  
  39. Usage
  40.  
  41. $SELF [OPTIONS] [ MAILTO-URI | ADDRESS(ES)... | FILE(S)... ]
  42.  
  43. $SELF [OPTIONS] --raw [ARGUMENTS...]
  44.  
  45. $SELF { --help | --manual | --version }
  46.  
  47.  
  48. Description
  49.  
  50. Eudora is a Mail Client for windows. This launcher provides a basic interface
  51. for native enviroment to use Eudora running under Wine compatability layer.
  52.  
  53. It is not recommended for the user to directly invoke this eudora launcher
  54. with arguments. Its main purpose is to be registered in a native desktop
  55. environment as default email client / mailto handler, so Eudora can be
  56. integrated and used as a native email client by tools like xdg-email, Nautilus'
  57. "Send to -> Email", Simple Scan, Web browser "mailto:" links, etc.
  58.  
  59. After eudora is set as default email client, it is strongly encouraged that
  60. xdg-email tool is used for launching eudora, by both direct user invocation
  61. and scripting. It has features like URI encoding, exit codes for missing files,
  62. and its command-line options are much more user-friendly.
  63.  
  64.  
  65. Options
  66.  
  67. Eudora and email options:
  68.  
  69. MAILTO-URI
  70.     A mailto uri in the format "mailto:[ADDRESS][?OPTION[&OPTION(S)...]]",
  71.     according to RFC2368. Valid OPTIONS are "to=ADDRESS", "cc=ADDRESS",
  72.     "bcc=ADDRESS", "subject=SUBJECT", "body=BODY". Aditionaly, "attach=FILE",
  73.     while not part of RFC2368, is also accepted.
  74.  
  75.     Some mailto uri examples:
  76.     mailto:[email protected]?subject=hello&body=Email%20test
  77.     mailto:attach=/dir/file1&attach=/dir/file2
  78.  
  79.     Since mailto uri is not parsed (except for attachments, see note below),
  80.     and must be properly encoded, it is highly recommended that xdg-email
  81.     utility is used to compose the mailto uri and invoke eudora.
  82.  
  83.     If any valid attachment is present in the uri, all other fields are ignored
  84.     See Attachments section for details and limitations on attachment handling.
  85.  
  86. ADDRESS(ES)...
  87.     A valid (list of) email address(es). If any character other than letters,
  88.     digits or @.-\/ are present in the address, it must be properly url-encoded.
  89.    
  90.     As with mailto uri, it is highly recommended that xdg-email utility is used
  91.     to compose the mailto uri and invoke eudora.
  92.    
  93. FILE(S)...
  94.     A (list of) file to be sent as attachment. If any valid file is given, any
  95.     mailto uri or address(es) are ignored. See Attachments section for details
  96.     and limitations on attachment handling. Files are required to be present
  97.     after eudora returns. Test for Eudora.exe process if deleting temporary
  98.     files is required.
  99.  
  100. Settings and control options:
  101.  
  102. --wineprefix DIR
  103.     Path to wine's virtual windows environment where Eudora Mail is installed.
  104.  
  105. --exefolder DIR
  106.    Unix path where Eudora is installed
  107.  
  108. --datafolder DIR
  109.    Unix path to Eudora's data Folder (User settings, emails and mailboxes)
  110.  
  111. --debug
  112.     Turns on shell attribute x (set -x), to print commands and their arguments
  113.     as they are executed, and redirects all output that otherwise would be
  114.     silenced to CONFIG/eudora.log (see Configuration Files section).
  115.    
  116. --raw
  117.     Relay all command-line arguments (except --raw itself and --debug) directly
  118.     to Eudora.exe windows executable. No parsing of addresses, mailto: URI, file
  119.     handling or translation from unix paths to windows paths is performed.
  120.     For testing purposes only.
  121.  
  122. Above options take precence over environment variables or settings in the
  123. configuration file. See Enviroment Variables and Configuration Files for details.
  124.  
  125. Generic options:
  126.  
  127. --help
  128.     Show command synopsis.
  129.  
  130. --manual
  131.     Show this manualpage.
  132.  
  133. --version
  134.     Show the launcher version information.
  135.  
  136.  
  137. Attachments
  138.  
  139. Eudora.exe accepts as command line arguments either a mailto uri OR a list of
  140. filenames to be attached, but not both. Furthermore, any "attach" field of a
  141. mailto uri is silently ignored, since it is not part of original RFC2368
  142. specification. Thus, it is currently not possible to launch Eudora.exe with both
  143. attachments and other (to, cc, bcc, subject, body) fields.
  144.  
  145. So, when an "attach" field is present is in the mailto uri, file is tested and,
  146. if valid, it is passed as command line argument and the mailto uri argument is
  147. discarted. Any other valid files passed directly in command line also makes
  148. eudora (this launcher) silently ignore and discart any mailto uri before
  149. launching Eudora.exe
  150.  
  151. Valid files are the ones that satisfy ALL following conditions:
  152.  
  153. - File must exist and not be a directory
  154.  
  155. - Full path must contain a target mapped to a wine drive (C:, D:, Z: etc)
  156.   or contain a target of one of wine's user folders (My Documents, Desktop,
  157.   My Pictures, etc - usually mapped via winecfg's Desktop Integration to native
  158.   user folders \$HOME, $\HOME/Desktop, \$HOME/Pictures etc)
  159.  
  160. - Due to a bug in Eudora.exe not properly handling escaping characters, file
  161.   name and path, when translated to a windows syntax path, must not cointain any
  162.   spaces
  163.  
  164. All non-valid files, either from mailto uri or command line, are silently
  165. ignored.
  166.  
  167. All file names and paths, in both mailto uri or command line argument, must use
  168. Unix syntax (/path/to/file). Files attached via mailto uri may start with a
  169. "file://" prefix (that is stripped before testing) and must be url encoded.
  170. Url decode and translation to windows path syntax (C:\path\to\file) is performed
  171. by eudora. Relative paths are accepted and properly translated.
  172.  
  173.  
  174. Environment Variables
  175.  
  176. eudora honours the following environment variables, which takes precedence over
  177. settings in the cofiguration file but may be overriden by respective command
  178. line options.
  179.  
  180. WINEPREFIX
  181.     Path to wine's virtual windows environment where Eudora Mail is installed.
  182.     See wine documentation for more details. Default is "\$HOME/.wine"
  183.  
  184. EUDORA_EXEPATH
  185.     The full (unix) path where Eudora is installed (where Eudora.exe is located)
  186.     Default is "\$WINEPREFIX/dosdevices/c:/Program Files/Qualcomm/Eudora"
  187.  
  188. EUDORA_DATAFILE
  189.     The full (unix) path to Eudora's Data Folder, which stores user's mailboxes
  190.     and settings. Default is:
  191.     "\$WINEPREFIX/dosdevices/c:/users/\$USER/Application Data/Qualcomm/Eudora"
  192.  
  193. EUDORA_DEBUG
  194.     If set to any value, will trigger Debug mode. See --debug in Options section
  195.  
  196. EUDORA_RAW
  197.     If set to any value, will trigger Raw mode. See --raw in Options section
  198.  
  199. wine, used internally to launch Eudora, is also affected by several other
  200. environment variables, such as WINEPATH and WINEDEBUG, and can affect eudora.
  201.  
  202.  
  203. Configuration files
  204.  
  205. All configuration files are stored in \$HOME/.eudora ($HOME/eudora
  206. for this user). The following are used:
  207.  
  208. eudora.log
  209.     Dump of execution commands when debug mode is activated
  210.    
  211. eudora.conf
  212.     Contains default settings for environment variables, in VAR=value format.
  213.     This file will be sourced by this launcher, so #comments are allowed but
  214.     extra caution must be taken with its syntax. These settings may be
  215.     overriden by environment variables or respective command line options
  216.  
  217.  
  218. Exit Codes
  219.  
  220. An exit code of 0 indicates success while a non-zero exit code indicates
  221. failure. The following failure codes can be returned:
  222.  
  223. 1   Something bad happened
  224.  
  225.  
  226. Examples
  227.  
  228. $SELF mailto:[email protected]
  229.  
  230. $SELF mailto:[email protected][email protected]&subject=Eudora+Lives&body=..kind+of
  231.  
  232. $SELF /tmp/somefile.txt
  233.  
  234. $SELF mailto:?attach=/tmp/AttachMe&attach=file:///tmp/AndMe' ../test/MeToo.txt
  235.  
  236. $SELF /tmp/OkToAttach.jpg "/tmp/no spaces allowed.mp3" /tmp/SorryNoDirsEither/
  237.  
  238. $SELF mailto:[email protected]?body=i+will+be+ignored&attach=/tmp/file
  239.  
  240.  
  241. Written by
  242.  
  243. Rodigo Silva (MestreLion) <[email protected]>
  244.  
  245.  
  246. Licenses and Copyright
  247.  
  248. Copyright (C) 2011 Rodigo Silva (MestreLion) <[email protected]>.
  249. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
  250. This is free software: you are free to change and redistribute it.
  251. There is NO WARRANTY, to the extent permitted by law.
  252.  
  253. IMPORTANT NOTE: The above copyright notice and GPL licence are for this launcher
  254. and associated native files only! Eudora Mail itself is propietary software,
  255. copyright of Qualcomm Inc. Wine and its tools are free software under different
  256. copyright and license. See <http://www.eudora.com> and <http://www.winehq.org>
  257.  
  258. MANUALPAGE
  259. }
  260.  
  261. usage()
  262. {
  263. cat << USAGE
  264. Usage: $SELF [OPTIONS] [ MAILTO-URI | ADDRESS(ES)... | FILE(S)... ]
  265.        $SELF [OPTIONS] --raw [ARGUMENTS...]
  266.        $SELF { --help | --manual | --version }
  267.  
  268. Launches Eudora Mail under Wine
  269.  
  270. Options:
  271. --wineprefix DIR  Unix path to wine's windows environment where Eudora Mail is
  272. --exepath DIR     Unix path where Eudora.exe is located
  273. --datafolder DIR  Unix path to Eudora's data Folder
  274. --debug           Turns on debug mode
  275. --raw             Relay all command-line arguments unparsed to Eudora.exe
  276.  
  277. Use "$SELF --manual" for additional info
  278. USAGE
  279. }
  280.  
  281. version() {
  282. cat << VERSION
  283. $SELF 1.0
  284.  
  285. Copyright (C) 2011 Rodrigo Silva (MestreLion) <[email protected]>
  286. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
  287. This is free software: you are free to change and redistribute it.
  288. There is NO WARRANTY, to the extent permitted by law.
  289.  
  290. Eudora Mail is a copyright of Qualcomm Inc. See <http://www.eudora.com>
  291. For wine copyright and license, see <http://www.winehq.com>
  292.  
  293. Launcher written by Rodrigo Silva (MestreLion) <[email protected]>
  294. VERSION
  295. }
  296.  
  297. debug() {
  298.     exec 3>>"$HOME/.config/eudora/eudora.log" 1>&3 2>&3
  299.     set -x
  300. }
  301.  
  302. fix_crash() {
  303.     if [[ -e "$EUDORA_DATAFOLDER/OWNER.LOK" ]] && \
  304.        ! { ps -A | grep -qi Eudora.exe ; }; then
  305.    
  306.         if zenity --question --no-wrap --title="Eudora crash fix" \
  307.                   --text="Looks like Eudora has crashed last time. Would you "\
  308.                          "like me to try to fix that?\n\n(that will close all "\
  309.                          "your opened mailboxes tough)"
  310.         then
  311.             : # awk -f $EUDORA_DATAFOLDER '[Open Windows]'
  312.         fi
  313.     fi
  314. }
  315.  
  316.  
  317. read_settings() {
  318.     settings="$1"
  319.     if [[ ! -f "$settings" ]]; then
  320.         mkdir -p "${settings%/*}/"
  321.         cat > "$settings" <<- _READSETTINGS
  322.         # Eudora settings file
  323.         WINEPREFIX="${WINEPREFIX:-"$HOME/.wine"}"
  324.         EUDORA_EXEPATH="${EUDORA_EXEPATH:-"$WINEPREFIX/dosdevices/c:/Program Files/Qualcomm/Eudora"}"
  325.         EUDORA_DATAFOLDER="${EUDORA_DATAFOLDER:-"$WINEPREFIX/dosdevices/c:/users/$USER/Application Data/Qualcomm/Eudora"}"
  326.         EUDORA_RAW="$EUDORA_RAW"
  327.         EUDORA_DEBUG="$EUDORA_DEBUG"
  328.         _READSETTINGS
  329.     fi
  330.     source "$settings"
  331. }
  332.  
  333.  
  334. # search for an alternate path for a unix file,
  335. # based on wine's user profile dirs (Desktop, My Pictures, etc)
  336. userprofile_search() {
  337.  
  338.     local unixfile="$1"
  339.     local dir
  340.     local key
  341.     local list
  342.  
  343.     # create profile map only once 
  344.     if [[ ${#profilemap[@]} = 0 ]]; then
  345.  
  346.         # Loop wine's user folder, for symlinks to filesystem dirs
  347.         # (so files in ~/Desktop, ~/Music, etc are correctly translated
  348.         # by winepath even if wine has no "Z:\ => /" mapping)
  349.         # (reference: cmd switches qksautc)
  350.         dir=$( wine winepath -u \
  351.                "$(wine cmd /c echo '%USERPROFILE%'|cut -d$'\r' -f1)" )
  352.         if [[ -d "$dir" ]]; then
  353.             list=("$dir"/*)
  354.             for dir in "${list[@]}"; do
  355.                 key=$(readlink -s "$dir")
  356.                 [[ -h "$dir" ]] && profilemap[${key%/}]="${dir%/}"
  357.             done
  358.         fi
  359.         [[ "$EUDORA_DEBUG" ]] && declare -p profilemap
  360.     fi
  361.  
  362.     # winepath with no options nicely convert from relative to absolute
  363.     # without canonicalizing it, but give weird results if file already absolute
  364.     [[ "$unixfile" = /* ]] || unixfile=$(winepath "$1")
  365.    
  366.     # loop profile map and try to get a match
  367.     while IFS= read -rd '' dir; do
  368.         if [[ "${unixfile%/*}/" = "$dir"/* ]]; then
  369.            
  370.             result="${profilemap[$dir]}${unixfile#$dir}"
  371.             return
  372.         fi
  373.     done < <(printf '%s\0' "${!profilemap[@]}" | sort -z --reverse)
  374.    
  375.     result=""
  376. }
  377.  
  378. # Translate file from unix path to windows path
  379. translate_file() {
  380.  
  381.     local unixfile="$1"
  382.     local winfile=""
  383.  
  384.     # File must exist and not be a directory
  385.     if [[ -e "$unixfile" && ! -d "$unixfile" ]]; then
  386.    
  387.         # Get the windows path
  388.         winfile=$(wine winepath -w "$unixfile")
  389.        
  390.         # Check if file was not succesfully mapped
  391.         if [[ "$winfile" = \\\\\?\\unix\\* ]]; then
  392.            
  393.             # Try to map to a user folder
  394.             userprofile_search "$unixfile"
  395.             [[ "$result" ]] && winfile=$(wine winepath -w "$result")           
  396.         fi
  397.        
  398.         # Lame eudora does not support filenames with espaces (not even quoted)
  399.         [[ "$winfile" = *[[:blank:]]* ]] && winfile=""     
  400.     fi
  401.    
  402.     result="$winfile"
  403. }
  404.  
  405. handle_mailto() {
  406.    
  407.     local url="${1#mailto:}"
  408.     local address=""
  409.     local querystring=""
  410.     local field
  411.     local value
  412.     local userfile
  413.     local attach
  414.     local options
  415.    
  416.     if [[ "$url" = *\?* ]]; then
  417.         IFS="?" read -r address querystring <<< "$url"
  418.     else
  419.         address=""
  420.         querystring="$url"
  421.     fi
  422.    
  423.     while IFS="=" read -r field value; do
  424.         case "$field" in
  425.         attach)
  426.             userfile=$(echo "$value"|sed 's/^file:\/\///;s/+/ /g;s/%/\\x/g')
  427.             files+=( "$(echo -e "$userfile")" )
  428.             attach=1
  429.         ;;
  430.         *) options+="&${field}=${value}" ;;
  431.         esac
  432.     done <<< "${querystring//&/$'\n'}"
  433.    
  434.     # Eudora can handle either mailto: or attachments, but not both
  435.     if [[ "$attach" ]]; then result=""; else result="$1"; fi
  436.    
  437. }
  438.  
  439.  
  440. ####################################### Main
  441.  
  442.  
  443. read_settings "$HOME/.config/eudora/eudora.conf"
  444.  
  445. unset profilemap; declare -A profilemap
  446. unset args      ; declare -a args
  447. unset output    ; declare -a output
  448. unset result    ; declare result
  449. unset mailto    ; declare mailto
  450. unset options   ; declare options
  451. unset file      ; declare file
  452.  
  453.  
  454. export WINEPREFIX
  455.  
  456.  
  457. exec 3> /dev/null # to avoid harcoding /dev/null everywhere. For tools' stderr.
  458.  
  459. SELF="${0##*/}" # buitin $(basename $0)
  460.  
  461. while [[ $# -gt 0 ]]; do
  462.    
  463.     arg="$1"
  464.     shift
  465.    
  466.     case "$arg" in
  467.    
  468.     --help   ) usage   ; exit ;;
  469.     --manual ) manual  ; exit ;;
  470.     --version) version ; exit ;;
  471.    
  472.     --debug  ) EUDORA_DEBUG=1 ;;
  473.     --raw    ) EUDORA_RAW=1   ;;
  474.    
  475.     --wineprefix) WINEPREFIX="$1"        ; shift ;;
  476.     --exepath   ) EUDORA_EXEPATH="$1"    ; shift ;;
  477.     --datafolder) EUDORA_DATAFOLDER="$1" ; shift ;;
  478.    
  479.    
  480.     mailto:*) args+=( "$arg" ); mailto="$arg";;
  481.    
  482.     *@*)
  483.         args+=( "$arg" )
  484.         if [[ "${mailto}" ]] ; then
  485.             options="${options}to=${arg}&"
  486.         else
  487.             mailto="mailto:${arg}?"
  488.         fi
  489.         ;;
  490.  
  491.     *) files+=( "$arg" ); args+=( "$arg" );;
  492.    
  493.     esac
  494. done
  495.  
  496. [[ "$EUDORA_DEBUG" ]] && debug
  497.  
  498. if [[ "$EUDORA_RAW" ]]; then
  499.     output=( "${args[@]}" )
  500. else
  501.     [[ "$mailto" ]] && handle_mailto "$mailto"
  502.    
  503.     for file in "${files[@]}"; do
  504.         translate_file "$file"
  505.         [[ "$result" ]] && output+=( "$result" )
  506.     done
  507.    
  508.     [[ "${#output[@]}" -gt 0 ]] || output=( "$mailto" )
  509. fi
  510.  
  511. [[ "$EUDORA_DEBUG" ]] && declare -p output
  512.  
  513. cd "$WINEPREFIX/dosdevices/c:/Program Files/Qualcomm/Eudora" || exit 1
  514.  
  515. wine "C:\\windows\\command\\start.exe" /MAX ./Eudora.exe "${output[@]}" 2>&3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement