[Evolution] Sharing mail between Mozilla & Evolution



I want to share my mail between Mozilla and Evolution.  In fact, I'm
currently doing just that.  My question is: is this /safe/?

I saw the FAQ question ``Can I read mail from a mailbox file created by
some other application?''  However, I have two problems with this:

 1. Even after following the directions, it didn't work quite as I
    expected.  (I didn't see the messages I expected to see).

 2. Even if it *did* work, it would be undesirable.  I want to share
    /all/ my mail...over 250 MB worth, spread over 30 directories
    and over 230 files.  Repeating the stes of FAQ #11 over 230 times
    is not my idea of a "good thing".

I previously looked for an answer to this question on the list archives (http://lists.ximian.com/archives/public/evolution/), and saw a message from ted cypress com on August 7 that contained the beginnings of a shell script. So in my infinite wisdom (or lack thereof), I enhanced it into a shell script that would take all of my Mozilla mail and create a corresponding Evolution mail directory structure, using symlinks to connect the ``mbox'' files.

The script is attached, if anyone is interested.

However, while this works as I'd expect, and works reasonably well, I don't know if this is "safe". For all I know, Evolution stores additional information in the ``mbox'' file, which Mozilla won't like. Or vice versa.

Is there any way to know if what I'm doing will work? Or am I guaranteeing that I'll lose my mail doing this?

Thanks,
 - Jon
#!/bin/sh
# 
# Convert Mozilla Mail into Evolution mail.
# See print_help for (minimal) documentation.
#
# Copyright (c) 2001 Jonathan Pryor
# 
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions are met:
# 
#   1. Redistributions of source code must retain the above copyright notice, 
#      this list of conditions and the following disclaimer.
#   2. Redistributions in binary form must reproduce the above copyright 
#      notice, this list of conditions and the following disclaimer in the 
#      documentation and/or other materials provided with the distribution.
#   3. The name of the author may not be used to endorse or promote products 
#      derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 
# NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


print_help ()
  {
  cat <<EOF
Mozilla Mail -> Evolution Mail conversion utility

Usage: $1 [OPTIONS] <mozilla mail directory> <evolution mail directory>

Where OPTIONS can contain:
  -c, --copy      Copy the Mozilla mail files into the Evolution mail
                  directory structure.

  -m, --move      Move the Mozilla mail files into the Evolution mail
                  directory structure.

  -s, --sym-link  Create symbolic links to the Mozilla mail files, allowing
                  sharing of mail between Mozilla and Evolution.
                  This is the default.

  -h, --help      Display this message and exit.

  --              Disable option parsing (allowing specification of
                  directories that have the same name as one of these
                  options).
EOF
  }


#
# Mail structures:
# The descriptions of how Mozilla and Evolution maintain their respective mail
# files/folders is required in order to understand how Mozilla's mail
# directory structure is converted into Evolution's directory structure.
#
# Structure of Mozilla mail:
#   - A mail folder is a file on disk
#   - If the folder has subdirectories, there is a directory of the same name 
#     with ``.sbd'' appended, and the subdirecties are normal files.  
#     (this recurses.)
#   - Normally mail files have no extension; .msf files are not "normal" mail 
#     files.
#
# Structure of Evolution mail:
#   - Each mail folder is two things:
#       (1) a directory on disk of the same name
#       (2) an ``mbox'' file which contains the mail.
#         This assumes that you're using the ``mbox'' mail format, and not any
#         of the other supported Evolution formats.
#   - Mail folder subdirectories are located in the ``subfolders'' directory 
#     of the mail directory.  (this recurses.)
#   - Each folder also contains a "folder-metadata.xml" file, which is used by
#     Evolution for...something.  I'm not exactly sure what it's for, but the
#     contents of it seem fairly regular.  See ``create_metadata''.
#


# Converts a string into an XML-compatible string
#   $1: string to convert
to_xml ()
  {
  local s="$1"
  # ``&'' is a special character in sed's ``replacement'' expression.  Thus, to
  # replace the characters with their escaped equivalents, we have to escape
  # the ``&''...
  s=`echo "$s" | sed "s/&/\\&amp;/g"`
  s=`echo "$s" | sed "s/</\\&lt;/g"`
  s=`echo "$s" | sed "s/>/\\&gt;/g"`
  echo "$s"
  }

# Creates the text of ``folder-metadata.xml'' for a given folder.
#   $1: the description of the folder
create_metadata ()
  {
  local name=`to_xml "$1"`
  cat <<EOF
<?xml version="1.0"?>
<efolder>
        <type>mail</type>
        <description>$name folder</description>
</efolder>
EOF
  }

# It's a mail file if it contains no file extension
# Since it's difficult to tell what the file extension is, it's a file that
# doesn't end with: ``.sbd'', ``.msf'', ``.dat''
#   $1: the filename to check
# Return true(0) if it's a mail file, false(1) otherwise.
is_mail_file ()
  {
  # the file extensions we don't like.
  local badexts="sbd msf dat"

  # BUG? In certain circumstances, a mail directory exists, but there are no
  # files within the directory.  In this case (an empty directory), 
  # ``for i in * ; do echo $i ; done'' returns "*", which isn't an actual file
  # on the filesystem.
  # Thus, we special-case "*" and return false in that case.
  if [ "$1" = "*" ] ; then
    return 1
  fi

  for b in $badexts ; do
    # If the filename ends in one of our "bad extensions", it's not a mail
    # file.
    if echo "$1" | grep "\\.$b$" > /dev/null ; then
      return 1
    fi
  done

  return 0
  }

# Is the file-name a mail directory?
#   $1: The filename to test
# Return true(0) if it's a mail directory, false(1) otherwise.
is_mail_dir ()
  {
  # if filename contains ".sbd", it's valid.
  if echo "$1" | grep \\.sbd$ > /dev/null ; then
    return 0
  fi
  return 1
  }


# The copy/move/sym-link operation to perform on the Mozilla mail files
# The default is to perform a sym-link.
mail_operation="ln -sf"


# Convert a Mozilla mail file into a Evolution mail folder
#   $1: The Mozilla mail file to convert.  
#       If its name is the empty string, then an empty mail file is assumed
#   $2: The name of the Evolution Directory
#   $3: The destination root directory
convert_moz_file_to_evo_dir ()
  {
  local mf=$1
  local ed=$2
  local dest=$3
  mkdir -p "$dest/$ed"
  if [ "$mf" = "" ] ; then
    create_metadata Unknown > "$dest/$ed/folder-metadata.xml"
    touch "$dest/$ed/mbox"
  else
    # force the link, as previously the file may have been `touch`ed, so this
    # time we have a valid data
    create_metadata "$ed" > "$dest/$ed/folder-metadata.xml"
    # ln -sf "$i" "$dest/$ed/mbox"
    $mail_operation "$mf" "$dest/$ed/mbox"
  fi
  }

# Convert a Mozilla mail folder (and subfolders) into an Evolution mail folder.
#   $1: the Mozilla mail folder to start at
#   $2: Where to create the root of the resulting Evolution mail folders.
convert_moz_dir_to_evo ()
  {
  local src=$1
  local dest=$2
  # echo "** {begin src=$src"
  # echo "** {begin dest=$dest"
  mkdir -p "$dest"
  for i in "$src"/* ; do
    # echo "** examining file: $i"
    local f=`basename "$i"`
    # echo "** examining basename: $f"
    if is_mail_file "$f" ; then
      # echo "** mail file: $f"
      # link to the mozilla file
      convert_moz_file_to_evo_dir "$i" "$f" "$dest"
    elif is_mail_dir "$f" ; then
      # echo "** mail folder: $i"
      # strip off ``.sbd''
      f=`echo "$f" | sed s/\.sbd$//`
      # if the file doesn't exist...
      if [ ! -e "$dest/$f" ] ; then
        # create it.
        convert_moz_file_to_evo_dir "" "$f" "$dest"
      fi
      # convert subdirectories
      convert_moz_dir_to_evo "$i" "$dest/$f/subfolders"
    fi
  done
  # echo "** end dest=$dest }"
  # echo "** end src=$src }"
  }


# The mozilla source and evolution destination directories
source=
dest=

# Set the source/destination
# The first time this is called, the source is set.  The second time, the
# destination is set.
# Nothing happens on subsequent invocations of this function.
#   $1: What the source/destination should be set to.
set_source_or_dest ()
  {
  if [ -z "$source" ] ; then
    source="$1"
  elif [ -z "$dest" ] ; then
    dest="$1"
  fi
  }

# Parse the program options.
# Returns true (0) if execution should continue (we have all required
# arguments), false (1) otherwise.
parse_options ()
  {
  local parse=true

  for option do
    if [ "$parse" = "true" ] ; then
      # echo "** argument=\"$option\""
      case "$option" in
        -\? | /\? | -h | -he | -hel | -help | --h | --he | --hel | --help)
          return 1
          ;;
        --)
          parse=false
          ;;
        -c | --copy)
          mail_operation="cp"
          ;;
        -m | --move)
          mail_operation="mv"
          ;;
        -s | --sym-link)
          mail_operation="ln -sf"
          ;;
        *)
          set_source_or_dest "$option"
          ;;
      esac
    else
      set_source_or_dest "$option"
    fi
  done

  # Did we get enough program arguments?
  if [ -n "$source" -a -n "$dest" ] ; then
    return 0
  fi

  cat <<EOF
Insufficient number of arguments.

EOF
  return 1
  }

# If we can proceed...convert the mail files.
if parse_options "$@" ; then
  # echo mail_operation="\"$mail_operation\""
  # echo source="\"$source\""
  # echo dest="\"$dest\""
  convert_moz_dir_to_evo "$source" "$dest"
else
  print_help `basename "$0"`
fi



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]