#!/usr/bin/perl
# Copyright © 2004 Jamie Zawinski <jwz@jwz.org>
#
# Permission to use, copy, modify, distribute, and sell this software
# and its documentation for any purpose is hereby granted without fee,
# provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear in
# supporting documentation.  No representations are made about the
# suitability of this software for any purpose.  It is provided "as
# is" without express or implied warranty.
#
# Created:  3-Mar-2004 by Jamie Zawinski, Anonymous, and Jacob Post.
#
##########################################################################
#
# This program, originally by Jamie Zawinski for parsing mork files to
# extract URLs from the history, has been repurposed by Clinton
# Gormley to extract vCards from the Thunderbird default personal
# addressbook (abook.mab), in a suitable format for importing into
# Evolution or for storing on the iPod.
#
# It looks for abook.mab in the directories specified below in
# ABOOK_PATHS.  If there are more paths that should be added by
# default, please let me know.
#
# BUG : At the moment, there is no way of removing deleted addresses.
# Thunderbird continues to store them in the address book, and I can't
# see any way to find the deleted flag.  There is a key called
# ns:addrbk:db:table:kind:deleted, but deleted records don't seem to
# have this set.  So if you figure this one out, please let me know...
#
# If you would also like to include the Collected addresses, you can
# specify the history.mab file on the command line.
# 
# I have done nothing with encoding in this program - on my system it
# just works. If it doesn't for you, please send me the details.
#
# vCards should have a maximum line length of 75 chars. I have set the
# line length lower in case there are characters which (when you
# recode) take up more than one byte, so you have a degree of
# flexibility with this. You may well run into problems if you are
# using many non ASCII characters.
#
# The changes I have made to Jamie Zawinski's script enjoy the same
# copyright as his original script (see above).
#
# The part of this script that parses the addressbook worked well for
# me, but Jamie says that it is unreliable, and has given up trying to
# improve it.  But until the Mozilla vcard project gets underway
# (http://vcard.mozdev.org/) and uses Mozilla's own mork libraries,
# you probably won't find anything better.
# Mileage may vary... :)
# 
# Hope this helps!
#
# Clinton Gormley (perl@traveljury.com)
#
##########################################################################
#
# Just added support for parsing mobile phones (cell phones) entries in
# address books from Thunderbird.
# My work was so lame hacking this file that I couldn't help myself
# being in the credits too. :-P
# Of course, my lines of program are GPL licensed too.
#
# Sebastian Cruz (crux@lugmen.org.ar)
#
##########################################################################
#
# Contributed to the gtkpod project by Clinton Gormley
# (perl@traveljury.com). This code can be used either under the
# license mentioned above or under the terms of the GNU General Pulic
# License as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The Ugly Truth Laid Bare:
#
#   In Netscape Naviga wocense as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
##########################################################################
#
# And Now, The 