Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AddressBook to SquirrelMail script Apps
I wrote a shell script to convert home and work email addresses from AddressBook into SquirrelMail's abook format. It's my first real shell script and could likely be improved upon (note the TODO). I'd appreciate any comments and especially hope someone else finds this useful. It's not extensively tested, and you should back up any existing abook files before running it (again note the TODO).

It's free to use and modify to your heart's content (like removing the need to run as root, so individual users can run it for themselves -- I have it cron'd for my wife and I). If you make significant changes, all I ask is that you please post them in the comments for others to use and learn from.

The script requires the contacts command line utility and of course SquirrelMail. The two grep lines following the sudo lines are split for width, but should be on the same line as the preceding sudo.
#!/bin/sh

#   to be run as root (manually or with cron)
#   pass one or more user's names as arguments
#
#   Requires:
#     /usr/local/bin/contacts @ http://gnufoo.org/contacts/
#     SquirrelMail @ http://www.squirrelmail.org/
#
#   TODO: make option to merge with existing abook instead of overwriting

# Change the following to your SquirrelMail data directory
sm_data_path="/var/squirrelmail/data/"
E_NOARGS=65
valid_users=0
invalid_users=0

if [ "$(/usr/bin/whoami)" != "root" ] ; then
  echo "`basename $0`: You must be root to run this command." >&2
  exit 1
fi 

if [ ! -n "$1" ]; then
  echo "Usage: `basename $0` username1 username2 etc."
  exit $E_NOARGS
fi

for arg in "$@"; do
  if nireport . /users name | sed 's/[^[:alnum:]]//g' | grep "^$arg$" ; then
    # valid user- perform abook update
    let "valid_users+=1"
    sudo -u $arg /usr/local/bin/contacts -HsS -f "%fn-%ln-home|%fn|%ln|%he|" | 
/usr/bin/grep -v "||" > /tmp/${arg}.abook
    sudo -u $arg /usr/local/bin/contacts -HsS -f "%fn-%ln-work|%fn|%ln|%we|" | 
/usr/bin/grep -v "||" >> /tmp/${arg}.abook
    /bin/mv /tmp/${arg}.abook ${sm_data_path}
    /usr/sbin/chown www:www ${sm_data_path}${arg}.abook
  else
    # not a valid user
    echo "`basename $0`: $user is not a valid user" >&2
    let "invalid_users+=1"
  fi
done

echo "${valid_users} valid user's were processed."
echo "${invalid_users} invalid user's were skipped."

exit 0
[robg adds: I haven't tested this one...]
    •    
  • Currently 2.33 / 5
  You rated: 2 / 5 (6 votes cast)
 
[13,569 views]  

An AddressBook to SquirrelMail script | 3 comments | Create New Account
Click here to return to the 'An AddressBook to SquirrelMail script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AddressBook to SquirrelMail script
Authored by: susfour on Aug 03, '04 12:50:19PM

Another solution to keep from having to sync your addressbooks is just to let both addressbook and squirrelmail use an LDAP server for their addresses.



[ Reply to This | # ]
LDAP
Authored by: jecwobble on Aug 03, '04 01:50:22PM

I thought about that. I'm running the client verions of OS X on my home computer. I've toyed with setting up LDAP for other reasons than this, but just haven't had the time or strong inclination. Your suggestion might change that...

Thanks.



[ Reply to This | # ]
An AddressBook to SquirrelMail script
Authored by: jwibi on Aug 05, '04 08:56:06AM

only one minor comment: the new version of "contacts" is no longer in /usr/local/bin but rather in /usr/bin.

if you make this adjustment in the script, it works like a charm! thx a lot - this saved me a lot of re-typing... ;-)

(LDAP would be great but setting it up on a Mac OS X client looks like a major project...)



[ Reply to This | # ]