home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Lib / table / tb_getdl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  6.5 KB  |  329 lines

  1. /* tb_getdl.c: expand a distribution list */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Lib/table/RCS/tb_getdl.c,v 6.0 1991/12/18 20:24:28 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Lib/table/RCS/tb_getdl.c,v 6.0 1991/12/18 20:24:28 jpo Rel $
  9.  *
  10.  * $Log: tb_getdl.c,v $
  11.  * Revision 6.0  1991/12/18  20:24:28  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include    "util.h"
  19. #include    "chan.h"
  20. #include    "table.h"
  21. #include    "dl.h"
  22. #include    "adr.h"
  23. #include    "retcode.h"
  24. #include    <sys/stat.h>
  25.  
  26. ADDR        *tb_getModerator();
  27. static Table    *List = NULLTBL;
  28. static Name *getNames();
  29. static Name *getNamesFromFile();
  30. Name *new_Name();
  31. extern    char    *compress ();
  32. /* 
  33. This routine returns the values:
  34.     NOTOK    - Table or routine erroe.
  35.     OK    - List found in Table
  36.     DONE    - temporary error ?
  37. */
  38. char    *namefile;
  39.  
  40. extern char    *list_tbl;
  41.  
  42. tb_getdl (key, plist,complain)
  43. char    *key;
  44. dl    **plist;
  45. int    complain;
  46. {
  47.     char    buf[BUFSIZ],
  48.         *ix,
  49.         *start,
  50.         mod[MAXPATHLENGTH],
  51.         *str;
  52.     int    retval = OK;
  53.     ADDR    *adr;
  54.  
  55.     namefile = NULLCP;
  56.     *plist = NULL;
  57.     PP_DBG(("tb_getdl (%s)", key));
  58.  
  59.     if (List == NULLTBL) 
  60.         if ((List = tb_nm2struct(list_tbl)) == NULLTBL) {
  61.             PP_LOG(LLOG_EXCEPTIONS, ("tb_getdl (no table)"));
  62.             return DONE;
  63.         }
  64.     
  65.     if (tb_k2val (List, key, buf, TRUE) == NOTOK) {
  66.         if (complain == OK) {
  67.             PP_LOG(LLOG_EXCEPTIONS,
  68.                    ("Unable to find entry for %s in list table %s",
  69.                 key, list_tbl));
  70.             return DONE;
  71.         } 
  72.         return NOTOK;
  73.     }
  74.  
  75.     *plist = (dl *) calloc(1, sizeof(dl));
  76.     (*plist) -> dl_listname = strdup(key);
  77.  
  78.     start = &(buf[0]);
  79.     if ((adr = tb_getModerator(key)) == NULLADDR) {
  80.         PP_LOG(LLOG_EXCEPTIONS,
  81.                ("Unable to get moderator for dl '%s'",key));
  82.         retval = DONE;
  83.     } else {
  84.         (*plist) -> dl_moderator = strdup(adr->ad_value);
  85.         adr_free(adr);
  86.     }
  87.     if ((ix = index(start, ',')) == NULLCP) {
  88.         PP_LOG(LLOG_EXCEPTIONS,
  89.                ("tb_getdl syntax error for '%s' : listname:[uids],...", key));
  90.         retval = DONE;
  91.     } else {
  92.         *ix = '\0';
  93.         (void) compress(start,mod);
  94.         if (mod[0] != '\0')
  95.             (*plist)->dl_uids = getNames(mod, &retval);
  96.         else
  97.             (*plist)->dl_uids = NULL;
  98.     }
  99.  
  100.     start = ix+1;
  101.     if (retval == OK) {
  102.         str = start;
  103.         ix = index(str, ',');
  104.         if (ix != NULL)
  105.             *ix++ = '\0';
  106.         (*plist) -> dl_desc = ix;
  107.         (*plist) -> dl_list = getNames(str, &retval);
  108.         (*plist) -> dl_file = namefile;
  109.     }
  110.  
  111.     return retval;
  112. }
  113.  
  114. Name *new_Name(str, file)
  115. char    *str;
  116. char    *file;
  117. {
  118.     Name    *ret = (Name *) calloc(1, sizeof(Name));
  119.  
  120.     ret -> name = strdup(str);
  121.     if (file)
  122.         ret -> file = strdup(file);
  123.     return ret;
  124. }
  125.  
  126. static Name *getNames(str, pretval)
  127. char    *str;
  128. int    *pretval;
  129. {
  130.     Name    *head = NULL,
  131.         *tail = NULL,
  132.         *temp = NULL;
  133.     char    *ix, *start;
  134.  
  135.     while ((ix = index(str,'|')) != NULL) {
  136.         *ix = NULL;
  137.         start = str;
  138.         while (isspace(*start)) start++;
  139.  
  140.         if (strncmp(start,"file=",strlen("file=")) == 0)
  141.             temp = getNamesFromFile(start, pretval);
  142.         else
  143.             temp = new_Name(start, NULLCP);
  144.         if (head == NULL)
  145.             head = tail = temp;
  146.         else 
  147.             tail -> next = temp;
  148.         while (tail -> next != NULL)
  149.             tail = tail->next;
  150.         str = ix + 1;
  151.     }
  152.  
  153.     start = str;
  154.     while (isspace(*start)) start++;
  155.  
  156.     if (strcmp(start,"") != 0) {
  157.         if (strncmp(start,"file=",strlen("file=")) == 0)
  158.             temp = getNamesFromFile(start,pretval);
  159.         else
  160.             temp = new_Name(start,NULLCP);
  161.         if (head == NULL)
  162.             head = tail = temp;
  163.         else 
  164.             tail -> next = temp;
  165.     }
  166.     return head;
  167. }
  168.  
  169. extern char    *loc_dist_prefix;
  170.  
  171. ADDR    *tb_getModerator(list)
  172. char    *list;
  173. {
  174.     char    moderator[LINESIZE],
  175.         *ix;
  176.     ADDR    *adr;
  177.     RP_Buf    rp;
  178.     extern char    *getpostmaster();
  179.  
  180.     if ((ix = index(list, '@')) != NULL)
  181.         *ix = '\0';
  182.     
  183.     if (strncmp(list, loc_dist_prefix, strlen(loc_dist_prefix)) == 0) {
  184.         /* attempt to strip of loc_dist_prefix to find moderator */
  185.         (void) sprintf(moderator, 
  186.                    "%s-request",
  187.                    (list+strlen(loc_dist_prefix)));
  188.         adr = adr_new (moderator, AD_822_TYPE, 0);
  189.         adr -> ad_resp = NO;
  190. #ifdef UKORDER
  191.         if (!rp_isbad(ad_parse(adr, &rp, CH_UK_PREF))) {
  192. #else
  193.         if (!rp_isbad(ad_parse(adr, &rp, CH_USA_PREF))) {
  194. #endif
  195.             if (ix != NULL) *ix = '@';
  196.             return adr;
  197.         }
  198.         adr_free(adr);
  199.     }
  200.         
  201.     (void) sprintf(moderator,"%s-request",list);
  202.     adr = adr_new (moderator, AD_822_TYPE, 0);
  203.     adr -> ad_resp = NO;
  204. #ifdef UKORDER
  205.     if (!rp_isbad(ad_parse(adr, &rp, CH_UK_PREF))) 
  206. #else
  207.     if (!rp_isbad(ad_parse(adr, &rp, CH_USA_PREF))) 
  208. #endif
  209.         return adr;
  210.     adr_free(adr);
  211.     
  212.     adr = adr_new ((ix = getpostmaster(AD_822_TYPE)),
  213.                AD_822_TYPE, 0);
  214.     adr -> ad_resp = NO;
  215. #ifdef UKORDER
  216.     if (rp_isbad(ad_parse(adr, &rp, CH_UK_PREF))) {
  217. #else
  218.     if (rp_isbad(ad_parse(adr, &rp, CH_UK_PREF))) {
  219. #endif
  220.         PP_OPER(NULLCP,
  221.                ("Failed to parse postmaster '%s' [%s]",
  222.             ix, adr->ad_parse_message));
  223.         adr_free(adr);
  224.         return NULLADDR;
  225.     }
  226.     return adr;
  227. }
  228.  
  229. static Name *getNamesFromFile(file,pretval)
  230. char    *file;
  231. int    *pretval;
  232. {
  233.     Name    *head = NULL,
  234.         *tail = NULL,
  235.         *temp;
  236.     char    entry[LINESIZE],
  237.         fullname[MAXPATHLENGTH],
  238.         *ix;
  239.     struct stat statbuf;
  240.     extern char    *tbldfldir;
  241.     FILE    *fd;
  242.     int    len;
  243.  
  244.     if ((ix = index(file,'=')) == NULLCP) {
  245.         PP_LOG(LLOG_EXCEPTIONS,
  246.                ("error in parsing '%s'",file));
  247.         return NULL;
  248.     }
  249.     ix++;
  250.     (void) compress(ix,ix);
  251.  
  252.     if (ix[0] != '/') 
  253.         (void) sprintf(fullname, "%s/%s", tbldfldir, ix);
  254.     else
  255.         (void) sprintf(fullname, "%s", ix);
  256.  
  257.     if (namefile == NULLCP) namefile = strdup(fullname);
  258.     
  259.     
  260.     if (stat (fullname, &statbuf) != OK) {
  261.         PP_OPER(fullname, ("unable to stat file"));
  262.         *pretval = DONE;
  263.         return NULL;
  264.     }
  265.  
  266.     if ((statbuf.st_mode & S_IFMT) != S_IFREG) {
  267.         PP_OPER(fullname, ("Not a regular file"));
  268.         *pretval = DONE;
  269.         return NULL;
  270.     }
  271.  
  272.     if ((fd = fopen(fullname,"r")) == NULL) {
  273.         PP_OPER(fullname, ("Can't open file"));
  274.         *pretval = DONE;
  275.         return NULL;
  276.     }
  277.  
  278.     while (fgets(entry, LINESIZE, fd) != NULL) {
  279.         (void) compress(entry, entry);
  280.         if (entry[0] == '\n' || entry[0] == '#' || entry[0] == '\0')
  281.             continue;
  282.  
  283.         len = strlen(entry);
  284.         if (entry[len-1] == '\n') entry[len-1] = '\0';
  285.         temp = new_Name(entry, fullname);
  286.         if (head == NULL)
  287.             head = tail = temp;
  288.         else {
  289.             tail->next = temp;
  290.             tail = temp;
  291.         }
  292.     }
  293.     (void) fclose(fd);
  294.     return head;
  295. }
  296.  
  297. name_free(list)
  298. Name    *list;
  299. {
  300.     if (list == NULL)
  301.         return;
  302.     if (list -> next) 
  303.         name_free(list -> next);
  304.     if (list -> name)
  305.         free(list -> name);
  306.     if (list -> file)
  307.         free(list->file);
  308.  
  309.     free((char *) list);
  310. }
  311.             
  312. dl_free(list)
  313. dl    *list;
  314. {
  315.     if (list == NULLDL)
  316.         return;
  317.     if (list -> dl_listname)
  318.         free(list -> dl_listname);
  319.     if (list -> dl_moderator)
  320.         free(list -> dl_moderator);
  321.     if (list -> dl_uids)
  322.         name_free(list -> dl_uids);
  323.     if (list -> dl_list)
  324.         name_free(list -> dl_list);
  325.     if (list -> dl_file)
  326.         free(list -> dl_file);
  327.     free((char *) list);
  328. }
  329.