3 # This script takes a list of .forward files and generates a list of colon
4 # delimited fields for import into a ldap directory. The fields represent
5 # the user and their email forwarding.
7 # A sample invokation..
9 # find -name ".foward" -maxdepth 2 | mkforwardlist | sort | less
10 # Then correct any invalid forward files if possible. After that stash the
11 # output in a file, remove the invalid lines and import it.
13 # It also understand .qmail type files
15 import re, time, getopt, os, sys, pwd, stat;
17 AddressSplit = re.compile("<(.*)>");
20 File = sys.stdin.readline().strip()
24 # Attempt to determine the UID
26 User = pwd.getpwuid(os.stat(File)[stat.ST_UID])[0];
28 print "Invalid0", File;
31 # Read the first two non comment non empty lines
32 Forward = open(File,"r");
35 Line2 = Forward.readline().strip()
38 if Line2[0] == '#' or Line2[0] == '\n':
45 # If we got more than one line or no lines at all it is invalid
46 if Line == None or Line == "" or Line2 != "":
47 print "Invalid1", File;
50 # Abort for funky things like pipes or directions to mailboxes
51 if Line[0] == '/' or Line[0] == '|' or Line[0] == '.' or Line[-1] == '/' or \
53 print "Invalid2", File;
56 # Split off the address part
57 Address = AddressSplit.match(Line);
59 # Or parse a qmail adddress..
62 Address = Address[1:];
65 print "Invalid3", File;
68 print User + ":",Address;