#!/bin/python
"""
build an LDIF address book from
my table of people I've sent mail to

"""
_version = "$Id: toAddrBk.py,v 1.2 2002/02/10 04:57:14 connolly Exp $" #@@ check styleguide to see if _version is right

import sys
from string import split, strip

# LDAP client API for Python
# What is Python-LDAP?
#

# Python-LDAP provides a module for accessing LDAP servers from
# Python. It wraps an underlying LDAP C library that provides an RFC
# 1823 API, such as OpenLDAP.
# Mailing list
#
# Discussion about the use and future of Python-LDAP occurs in the
# python-ldap-dev mailing list. You can subscribe or unsubscribe to
# this list or browse the list archive.
#
# Id: index.shtml,v 1.5 2001/05/16 01:15:36 leonard Exp
# http://python-ldap.sourceforge.net/

# [ns97] Netscape Directory Server
# Administrator's Guide
# Last updated December 1997.
#
# Chapter 2
# LDAP Data Interchange Format
# http://developer.netscape.com/docs/manuals/directory/admin30/ldif.htm

import ldif

def testMakeAddrbookEntry():
    """aped field names from [ns97]
    cool... test succeeded: evolution
    read this in and created an addressbook entry"""

    dn = "cn=connolly,ou=W3C" #@@ not sure about this...

    # clearly this is analagous to an RDF KB:
    # prop : [val, val, ...]
    # perhaps wrap it in a makeStatement API?
    entry = {"cn": ["Dan Connolly"],
             "mail": ["connolly@w3.org"],
             }
    rep = ldif.CreateLDIF(dn, entry)
    print rep

def recipsToN3(inf, outf):
    seen = {}

    outf.write("@prefix dc: <http://purl.org/dc/elements/1.1/>.\n")
    outf.write("@prefix c: <http://www.w3.org/2000/10/swap/pim/contact#>.\n")
    outf.write("@prefix e: <http://www.w3.org/2000/04/maillog2rdf/email#>.\n")
    
    while 1:
        ln = inf.readline()
        if not ln: break
        
        # as produce by @@shell-one-liner from recips.py output...
        # e.g.
        # 1998-04 <353353A6.19D113A7@w3.org> yamachan@w3.org 
        # 1998-04 <3533C41B.5A5D0DEB@w3.org> w3ctemp@w3.org ron fink

        fields = split(strip(ln), None, 3)
        #print "debug: fields:", fields
        
        if fields[3:]:
            when, mid, addr, phrase = fields

            if phrase[0] == '"': phrase = phrase[1:]
            if phrase[-1] == '"': phrase = phrase[:-1]
            if '"' in phrase:
                #@@report limitation
                continue
            
            outf.write(
                """<mid:%s> dc:date "%s";
                     e:to [ c:mailbox <mailto:%s>; c:fullName "%s" ].\n""" % \
                (mid[1:-1], when, addr, phrase) )
        

def recipsToLDIF(inf, outf):
    while 1:
        ln = inf.readline()
        if not ln: break
        
        # as produce by @@shell-one-liner from recips.py output...
        # e.g.
        # 1998-04 yamachan@w3.org 
        # 1998-04 w3ctemp@w3.org ron fink

        # @@TODO: handle some anomalies
        # cn: Martin J. =?iso-8859-1?Q?D=FCrst?=
        # cn: Lynda Templin (by way of Alan Kotok <kotok@w3.org>)
        # cn: 'cmsmcq@acm.org'

        fields = split(strip(ln), None, 2)
        #print "debug: fields:", fields
        
        if fields[2:]:
            when, addr, phrase = fields

            if '@' in phrase: # bogus use of phrase
                continue
            
            mbox, domain = split(addr, "@")
            dn = "cn=%s,ou=%s" % (mbox, domain) #@@ not sure about this...
            
            entry = {"cn": [phrase],
                     "mail": [addr],
                     "description": ["recipient message in %s" % (when,)] #@@TODO: n3-ize this
                     }

            rep = ldif.CreateLDIF(dn, entry)
            outf.write(rep)
        else:
            #print >>sys.stderr, "debug: no phrase; skipping ", ln
            pass

                 
        
if __name__ == '__main__':
    #recipsToN3(sys.stdin, sys.stdout)
    recipsToLDIF(sys.stdin, sys.stdout)
    

# $Log: toAddrBk.py,v $
# Revision 1.2  2002/02/10 04:57:14  connolly
# ldif output
#
# Revision 1.1  2002/01/04 19:31:29  connolly
# as used for evolution import today
#
