LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Blogs > catkin
User Name
Password

Notices

Rate this Entry

Script to prepare a Thunderbird PAB export for gnokii to write to Nokia series 40

Posted 05-19-2011 at 03:47 AM by catkin
Updated 06-26-2011 at 09:40 AM by catkin (Version 0.2 of the script)

Hello

Here's a script to prepare a Thunderbird Address Book LDIF export for gnokii --writephonebook to write to a Nokia Series 40. The usage is something like:
Code:
cat /tmp/TB.PAB.ldif \
  | prepare_ldif_for_gnokii.awk \
  1>/tmp/ldif.prepared \
  2>/tmp/prepare_ldif_for_gnokii.log
cat /tmp/ldif.prepared \
  | gnokii --writephonebook --overwrite --memory-type ME --ldif \
  2>/tmp/gnokii.stderr
The script:
Code:
#!/usr/bin/awk -f

# Prepares a .ldif file in preparation for gnokii to load to a Nokia Series 40 phone

# Change history
# * Version 0.0 17may11 Charles
#   First published version, developed and tested using Thunderbird export
# * Version 0.1 23may11 Charles
#   Changed tabs to spaces
# * Version 0.2 23jun11 Charles
#   Fixed base64 wrapping lines by adding --wrap=0 option
#   Added ^mobile: to telephone numbers to have non-numerics stripped

BEGIN {
    FS = ":"
    max_description_len = 116 # gnokii limit at 12may11
}

# Note cn for use in any log message
/^cn:/ {
    if ( $0 ~ /^cn::/ )
    {
        "echo " substr( $0, 6 ) " | base64 --decode" | getline cn
    }
    else
    {
        cn = substr( $0, 5 )
    }
}

# Remove non-numerics from phone numbers, keeping any +
/^facsimiletelephonenumber: / || /^homePhone: / || /^mobile: / || /^telephoneNumber: / {
    $2 = substr( $2, 2 ) # Remove leading space
    if ( substr( $2, 1, 1 ) == "+" )
    {
        plus = "+"
        $2 = substr( $2, 2 )
    }
    else plus = ""
    gsub( /[^0-9]/, "", $2 )
    print $1 ": " plus $2
    next
}

# Truncate any long "description"s
/^description:/ {
    base64 = 0
    truncated = 0
    if ( $0 ~ /^description::/ )
    {
        #print "Found base-64 description. $0: " $0 > "/dev/stderr"
        base64 = 1
        "echo " substr( $0, 14 ) " | base64 --decode" | getline value
        if ( value ~ /^base64: invalid input/ )
        {
            print "WARNING: " cn ": description/note is invalid base-64; deleted (input line " NR ")" > "/dev/stderr"
            next
        }
    }
    else value = $2
    if ( length( value ) > max_description_len )
    {
        truncated = 1
        print "WARNING: " cn ": description/note longer than " max_description_len "; truncated (input line " NR ")" > "/dev/stderr"
        value = substr( value, 1, max_description_len )
    }
    if ( base64 )
    {
        gsub( "'", "'\"'\"'", value )
        "echo '" value "' | base64 --wrap=0" | getline value
        print "description:: " value
    }
    else
    {
        print "description: " value
    }
    next
}

{ print }
Best

Charles
Posted in Uncategorized
Views 1166 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 12:05 PM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration