Code Files

ddsu.awk

Generated on Tue Dec 05 17:39:05 Eastern Standard Time 2006 from ddsu.awk


# Program   : DDSU.AWK
# Purpose   : Create Department/Divison/Section/Unit distribution lists from USER.CSV
# Author    : Bob Jonkman <bjonkman@sobac.com>:
# Date      : 25 October 2004

# Modified  : 10 March 2006 - Added stats output to file StatsOut
#             13 March 2006 - Split output into ListsOut and MembersOut


# Create a new DS-GROUP entry  (to be in API format)
function addgroup(localGroupObject,description)
    {   print("DS-GROUP= \n")                                           > ListsOut
        print("  Operation= Add; \n")                                   > ListsOut
        print("  Visibility= "           GroupVIS     "; \n")           > ListsOut
        print("  Description= " txt2gwapi(QUOTE description QUOTE " generated on " strftime("%d %B %Y") " from data supplied by SAP. Contact your HR representative for changes.") "; \n") > ListsOut
        print("  Domain= "               GroupDOM     "; \n")           > ListsOut
        print("  Post-Office= "          GroupPO      "; \n")           > ListsOut
        print("  Object= "               localGroupObject  "; \n")      > ListsOut
        print("; \n")                                                   > ListsOut
        print("\n")                                                     > ListsOut
    }                


# Add a member to a group
function addmember(localGroupObject,DomainName,POName,ObjectName,ObjectType)
    {   print("DS-GROUP= \n")                                           > MembersOut
        print("  Operation= Modify; \n")                                > MembersOut
        print("  Domain= "               GroupDOM     "; \n")           > MembersOut
        print("  Post-Office= "          GroupPO      "; \n")           > MembersOut
        print("  Object= "               localGroupObject  "; \n")      > MembersOut
        print("  Member= \n")                                           > MembersOut
        print("    Operation= Add; \n")                                 > MembersOut
        print("    Object-Type= "        ObjectType  "; \n")            > MembersOut
        print("    Domain= "             DomainName  "; \n")            > MembersOut
        print("    Post-Office= "        POName      "; \n")            > MembersOut
        print("    Object= "             ObjectName  "; \n")            > MembersOut
        print("  ; \n")                                                 > MembersOut
        print("; \n")                                                   > MembersOut
        print("\n")                                                     > MembersOut

        stats[localGroupObject,ObjectType]++
    }                

# Function to parse fields omitting punctuation
function nopunct(string,     numpunct,numspace,numspaces,numamp)
    {   gsub(/&/," and ",string)            # Verbalize "&"

        gsub(/[^a-zA-Z0-9 ]/,"",string)     # Remove punctuation 

        while(gsub(/  /," ",string))        # Eliminate double spaces
            ;                               # the work is done by the gsub() in the condition
        return(string);
    }




BEGIN     {
        QUOTE   = "\""
        COMMA   = ","
        FS      = COMMA
        OFS     = COMMA
        ORS     = "" 
        SUBSEP  = COMMA
        FIELD_SEPARATOR     = COMMA
        FIELD_DELIMITER     = QUOTE
        RECORD_SEPARATOR    = "\n"
        
        IGNORECASE = 1;

        ListsOut    = "ddsulist.api"    # Output filename for creating the lists
        MembersOut  = "ddsumemb.api"    # Output filename for the members of groups
        PrefixIn    = "ddlist.csv"      # Input filename for list abbreviations
        StatsOut    = "ddsustats.csv"   # Output filename for stats, to be HTML-ized
        
        GroupPO     = "COTADM" ;        # Domain and 
        GroupDOM    = "COTPRI" ;        #  Post-Office in which the Groups are to be created
        GroupVIS    = "System" ;        # Visibility for all the Groups ("None" | "Post-Office" | "Domain" | "System")
        idomain     = "@sobac.com"     # for stats list, makes distribution lists addressable.

        TOPGroup    = "COT All Departments" # Name of the Top-Level 
        TOPObjectPrefix    = "COT";         # Top-Level prefix used for all Groups.

        NameLength    = 64 ;                # Maximum allowed object name length in RFC2821 and NDS == 64 (GW client == 80, GWAPI => 100)

        # Read in the Abbreviation Table ("Department - Division","Abbrev")
        while(getline < PrefixIn )
            PrefixList[tolower($1)] = $2 ;

        # Create the DDSU Statistics file
        print("Distribution List Name,Mailboxes,Sub-lists,Recipients,Department,Division,Section,Unit\n") > StatsOut

        # Output the API file headers
        print("WPC-API= 1.2; \n")           > ListsOut
        print("Header-Char= T50; \n")       > ListsOut
        print("Msg-Type= ADMIN; \n")        > ListsOut
        print("\n")                         > ListsOut

        print("WPC-API= 1.2; \n")           > MembersOut
        print("Header-Char= T50; \n")       > MembersOut
        print("Msg-Type= ADMIN; \n")        > MembersOut
        print("\n")                         > MembersOut

        # Create the top-level group
        addgroup(TOPGroup,TOPGroup)         # Use the full Groupname for the top-level group
        GroupNames[TOPGroup] = TOPGroup     # Count this group
    }



##### Read the headers #####

(FNR == 1)    {    numfields = parsecsv($0,headers);

# create reverse lookup for fieldnames, ie. fieldnumber["Department"]=1, fieldnumber["Division"]=2 &c.
        for (i=1; i <= numfields; i++)
            fieldnumber[headers[i]] = i;
    }

##### End of Read the Headers #####




(FNR > 1)    {  numberoffields = parsecsv($0,fields);
    
       if ((fields[fieldnumber["Domain"]] ~ /^COT*/ ) &&            # Only include CoT domains (not Internet or Zoo)
            (fields[fieldnumber["First-Name"]] != "" )    )         # Do not include generic IDs
       {
        
        Department = nopunct(fields[fieldnumber["Department"]]) ;
        if (tolower(Department) == "blank") Department = "" ;        # Make "blank" equal to a real blank


        Division   = nopunct(fields[fieldnumber["User-Def-2"]]) ;
        if (tolower(Division) == "blank") Division = "" ;

        Section    = nopunct(fields[fieldnumber["User-Def-7"]]) ;
        if (tolower(Section) == "blank") Section = "" ;

        Unit       = nopunct(fields[fieldnumber["User-Def-8"]]) ;
        if (tolower(Unit) == "blank") Unit = "";
        

        
        ParentObject = TOPGroup ;         # The Top-Level object name is the full groupname
        GroupObject = ParentObject ;

        stats[GroupObject,"Recipient"]++    # Count the recipients for this group

        if (Department != "")
        {
            if (PrefixList[tolower(Department)] == "")                              # If there is no Department prefix
            {
                GroupObjectPrefix = TOPObjectPrefix "-" Department ;                #   Then use the Department Name
                GroupObject = rtrim(substr(GroupObjectPrefix, 1, NameLength)); 
            }                                 
            else                                                                    # Else use the prefix
            {
                GroupObjectPrefix = TOPObjectPrefix "-" PrefixList[tolower(Department)] ;
                GroupObject = rtrim(substr(GroupObjectPrefix " " Department, 1, NameLength)) ;
            }


            if (!GroupNames[GroupObject])                                           # If the Department Group does not exist
            {   addgroup(GroupObject,fields[fieldnumber["Department"]]) ;           #    then add the Group    
                GroupNames[GroupObject]=GroupObject                                 #    count the Group
                addmember(ParentObject, GroupDOM, GroupPO, GroupObject, "Group");   #    and add it to its parent
            }

            stats[GroupObject,"Recipient"]++    # Count the recipients for this group
            fullname[GroupObject,"Department"] = fields[fieldnumber["Department"]]


            if (Division != "")
            {    ParentObject = GroupObject ;
                if (PrefixList[tolower(Department " - " Division)] == "")           # If there is no Divison prefix
                {    GroupObjectPrefix = GroupObjectPrefix "-" Division ;           #    then use the Divison name
                    GroupObject = rtrim(substr(GroupObjectPrefix, 1, NameLength));
                } 
                else                                                                # Else build up the prefix
                {    GroupObjectPrefix = GroupObjectPrefix "-" PrefixList[tolower(Department " - " Division)] ;
                    GroupObject = rtrim(substr(GroupObjectPrefix " " Division, 1, NameLength)) ;
                }
            
                if (!GroupNames[GroupObject])                                           # If the Group does not exist
                {    addgroup(GroupObject,fields[fieldnumber["User-Def-2"]]) ;          #    then add the Group
                    GroupNames[GroupObject]=GroupObject                                 #    count the Group
                    addmember(ParentObject, GroupDOM, GroupPO, GroupObject, "Group");   #    and add it to its parent
                }
        
                stats[GroupObject,"Recipient"]++    # Count the recipients for this group
                fullname[GroupObject,"Division"] = fields[fieldnumber["User-Def-2"]]

                if (Section != "")
                {    ParentObject = GroupObject ;
                    GroupObject = rtrim(substr(GroupObjectPrefix "-" Section , 1, NameLength)) ; 

                    if (!GroupNames[GroupObject])
                    {    addgroup(GroupObject,fields[fieldnumber["User-Def-7"]]) ;
                        GroupNames[GroupObject]=GroupObject        
                        addmember(ParentObject, GroupDOM, GroupPO, GroupObject, "Group");
                    }

                    stats[GroupObject,"Recipient"]++    # Count the recipients for this group
                    fullname[GroupObject,"Section"] = fields[fieldnumber["User-Def-7"]]


                    if (Unit != "")
                    {    ParentObject = GroupObject ;
                        GroupObject = rtrim(substr(rtrim(substr(ParentObject,1,NameLength-22)) "-" Unit , 1, NameLength)) ; # Guarantee a Unit length of 22 characters

                        if (!GroupNames[GroupObject])
                        {    addgroup(GroupObject,fields[fieldnumber["User-Def-8"]]) ;
                            GroupNames[GroupObject]=GroupObject        
                            addmember(ParentObject, GroupDOM, GroupPO, GroupObject, "Group");    #    and add it to its parent
                        }

                        stats[GroupObject,"Recipient"]++    # Count the recipients for this group
                        fullname[GroupObject,"Unit"] = fields[fieldnumber["User-Def-8"]]
                        

                    }
                }
            }
        }
        
        
        # Finally, add the member to the Group...
        addmember(GroupObject, fields[fieldnumber["Domain"]], fields[fieldnumber["Post-Office"]], fields[fieldnumber["Object"]], "User");

      }
    }


END    {    print("\n\n-END-\n")        > MembersOut
        print("\n\n-END-\n")        > ListsOut


        ORS = "\n"

        numGroupNames = asorti(GroupNames)  # Alphabetize the GroupNames

#        for(i in GroupNames) 
        for(i = 1; i <= numGroupNames; i++)
        {
            print(gwid2smtp(GroupNames[i]) idomain,stats[GroupNames[i],"User"],stats[GroupNames[i],"Group"],stats[GroupNames[i],"Recipient"],printcsv(fullname[GroupNames[i],"Department"]),printcsv(fullname[GroupNames[i],"Division"]),printcsv(fullname[GroupNames[i],"Section"]),printcsv(fullname[GroupNames[i],"Unit"])) > StatsOut
            groups++
            mailboxes += stats[GroupNames[i],"User"]
            sublists  += stats[GroupNames[i],"Group"]
        }

        print("=====")                          > StatsOut
        print("Total Mailboxes",mailboxes)      > StatsOut
#       print("Total Sublists",sublists)        > StatsOut
        print("Total Lists",groups)             > StatsOut
        print("")                               > StatsOut
        print("Date", QUOTE strftime("%a, %d %b %Y") QUOTE )  > StatsOut
    }

# EOF: DDSU.AWK


   

1 files processed.