home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume3 / uumail2 / opath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  5.6 KB  |  332 lines

  1. /*
  2.  * opath.c - get an optimal uucp path from the path database, using an
  3.  * RFC882-style address as input.  The '%' character is properly translated,
  4.  * and gateway-substitution is done to get mail onto other networks.
  5.  *
  6.  *
  7.  * Eric Roskos, Perkin-Elmer Corp. SDC
  8.  * Version 3.2 created 85/09/12 15:21:51
  9.  * $Log:    opath.c,v $
  10.  * Revision 3.7  85/11/14  20:22:16  sob
  11.  * Added #ifdef DEBUG to allow compiliation without DEBUG
  12.  * 
  13.  * Revision 3.6  85/11/08  03:05:22  sob
  14.  * release version
  15.  * 
  16.  * Revision 3.5  85/10/09  03:20:07  sob
  17.  * Added strindex call to make striping the printf stuff more pleasing.
  18.  * 
  19.  * Revision 3.4  85/09/30  02:49:12  sob
  20.  * Updated to use getpath revised by Stan Barber
  21.  * 
  22.  * Revision 3.3  85/09/16  18:32:26  sob
  23.  * This version will use the getpath subroutine used by uupath/uumail
  24.  * and is not dependant on whether it is DBM or not.
  25.  * Also uses the uuconf include file.
  26.  * 
  27.  * Revision 3.2  85/09/16  17:50:07  sob
  28.  * Added to RCS
  29.  * 
  30.  */
  31.  
  32. static char *opathsccsid = "@(#)opath.c    3.2 (peora) 15:21:51 - 85/09/12";
  33. static char opathrcsid[] = "$Header: opath.c,v 3.7 85/11/14 20:22:16 sob Exp $";
  34. #ifdef OPATH
  35. #include "uuconf.h"
  36.  
  37. /**
  38.  ** User-configurable parameters
  39.  **/
  40.  
  41. /**
  42.  ** Global Variables
  43.  **/
  44.  
  45. static char pval[150]; /* the path string is built here */
  46.  
  47. /*
  48.  * the following are used to pass results by side-effect from the domain()
  49.  * routine.
  50.  */
  51.  
  52. static char prefix[80], suffix[80], fullsite[80];
  53.  
  54. /**
  55.  ** Subroutines
  56.  **/
  57.  
  58. /*
  59.  * The Domain Table and its associated routines
  60.  */
  61.  
  62. static struct domains
  63. {
  64.     char dom[50];
  65.     char pre[50];
  66.     char suf[50];
  67.     char map[50];
  68. } domtab[100];
  69.  
  70. /* Inline routine to copy a domain into the domain table */
  71.  
  72. #define DOMCPY(fld) { int i = 0; q=dp->fld; while (*p!=','&&*p!='\n'&&*p) \
  73.             {*q++ = *p++; if (i++>=48) break;} \
  74.             *q++ = '\0'; if (!*p) { \
  75.             fprintf(stderr,"opath: fld(s) missing in %s at %s\n", \
  76.             s, buf); \
  77.             dp++; continue;} p++; }
  78.  
  79. /* Load the domain table from disk */
  80.  
  81. static int
  82. loaddomtab(s)
  83. char *s;
  84. {
  85. FILE *f;
  86. char buf[100];
  87. register char *p,*q;
  88. struct domains *dp;
  89.  
  90.     f = fopen(s,"r");
  91.     if (f==NULL)
  92.     {
  93.         fprintf(stderr,"opath: can't open domain file '%s'\n",s);
  94.         exit(1);
  95.     }
  96.  
  97.     dp = domtab;
  98.  
  99.     while (fgets(buf,100,f))
  100.     {
  101.         if (buf[0]=='#') continue; /* comments start with "#" */
  102.         p = buf;
  103.         DOMCPY(dom);
  104.         DOMCPY(pre);
  105.         DOMCPY(suf);
  106.         DOMCPY(map);
  107.         if (dp->map[0] == '\0')
  108.         {
  109.             fprintf(stderr,"opath: bad route template in %s\n",s);
  110.             strcpy(dp->map,"Invalid");
  111.         }
  112.         dp++;
  113.     }
  114.  
  115.     dp->map[0] = '\0';  /* mark end of table */
  116.     fclose(f);
  117.  
  118.     return(0);
  119. }
  120.  
  121. /* Get a UUCP path from the pathalias database 
  122.  * and compensate for the format.
  123.  * Typically the format is
  124.  * site1!site2!site3!%s to be used in *printf statements.
  125.  * We don't want the !%s, so we cut it off.
  126.  * Pretty gross, but it works.
  127.  */
  128.  
  129. static char *
  130. gpath(s)
  131. char *s;
  132. {
  133. static char path[100];
  134. int getpath(),x;
  135.     getpath(s,&path[0],paths);
  136.         x=strindex(&path[0],"!%s");
  137.     if (x >0) path[x] = '\0';
  138. #ifdef DEBUG
  139.     if (Debug>1)fprintf(stderr,"Getpath returns: %s\n",&path[0]);
  140. #endif
  141.     return(&path[0]);
  142. }
  143.  
  144. /* returns location of tx in sx */
  145. strindex(sx,tx)
  146. char * sx, *tx;
  147. {
  148.     int i,n;
  149.     n = strlen(tx);
  150.     for (i=0;sx[i] != '\0'; i++)
  151.         if (strncmp(sx+i,tx,n) ==0)
  152.             return(i);
  153.     return (-1);
  154. }
  155.  
  156. /* String compare: entire first argument must match suffix of 2nd argument */
  157.  
  158. static int
  159. domcmp(ss,tt)
  160. char *ss, *tt;
  161. {
  162. char *s, *t;
  163. int cmp;
  164.  
  165.     s = ss + strlen(ss) - 1;
  166.     t = tt + strlen(tt) - 1;
  167.  
  168.     do
  169.     {
  170.         if (*s - *t) break;
  171.         s--;
  172.         t--;
  173.     } while (s >= ss);
  174.  
  175.     if (++s == ss) return(0);
  176.     else return(1);
  177. }
  178.  
  179. /* Look up a domain, and by side effect set prefix and suffix appropriately */
  180.  
  181. char *domain(s)
  182. char *s;
  183. {
  184. struct domains *d;
  185. char *p;
  186. static int loaded = 0;
  187.  
  188.     if (!loaded++) loaddomtab(ConfFile);
  189.  
  190.     if (*s!='.') /* default to UUCP domain */
  191.     {
  192.         prefix[0]=suffix[0]='\0';
  193.         return("%R!%U");
  194.     }
  195.  
  196.     for (p=s; *p; p++) if (*p>='a' && *p<='z') *p -= 'a'-'A';
  197.  
  198.     for (d = &domtab[0]; (int)d->map[0]; d++)
  199.     {
  200.         if (domcmp(d->dom,s)==0) break;
  201.     }
  202.  
  203.     strcpy(prefix,(d->pre[0]=='>')? gpath(&d->pre[1]) : d->pre);
  204.     strcpy(suffix,d->suf);
  205.  
  206.     return(d->map);
  207. }
  208.  
  209. /* opath: generates a UUCP path from an RFC-822 address */
  210.  
  211. #define COPYON(s) {char *r; r=s; while (*r) *p++ = *r++; *p = '\0';}
  212.  
  213. char *
  214. opath(s)
  215. char *s;
  216. {
  217. char user[50],site[50];
  218. static char cm[150];
  219. FILE *f;
  220. char *p, *q, *t;
  221. char *d;
  222. int i;
  223. int found;
  224. char *suf;
  225.  
  226.     for (p=user,q=s;(*p = *q)!='@'; p++,q++)
  227.         if (*q=='\0') return(s);
  228.     *p = '\0';
  229.  
  230.     strcpy(fullsite,++q);
  231.  
  232.     for (p=site;(*p = *q)!='.'; p++,q++)
  233.         if (*q=='\0') break;
  234.     *p = '\0';
  235.  
  236.     d = domain(q);
  237.  
  238.     if (d[0]=='\0') return(s); /* unknown domain - do nothing */
  239.  
  240.     for (p=pval, q=d; *q; q++)
  241.     {
  242.         if (*q=='%')
  243.         {
  244.             switch(*++q)
  245.             {
  246.             case 'P':
  247.                 COPYON(prefix);
  248.                 break;
  249.  
  250.             case 'S':
  251.                 COPYON(suffix);
  252.                 break;
  253.  
  254.             case 'U':
  255.                 COPYON(user);
  256.                 break;
  257.  
  258.             case 'N':
  259.                 COPYON(site);
  260.                 break;
  261.  
  262.             case 'D':
  263.                 COPYON(fullsite);
  264.                 break;
  265.  
  266.             case 'R':
  267.                 COPYON(gpath(site));
  268.                 break;
  269.  
  270.             case '%':
  271.                 *p++ = '%';
  272.                 break;
  273.             }
  274.         }
  275.         else
  276.             *p++ = *q;
  277.     }
  278.  
  279.     return(pval);
  280. }
  281.  
  282. /* oupath: generates a uucp path from a (possibly disconnected) uucp path */
  283.  
  284. char *oupath(s)
  285. char *s;
  286. {
  287. char *p,*q;
  288. static char adr[100];
  289. char first[100];
  290. int found;
  291.  
  292.     for (p=s,q=first,found=0; *p!='!' && *p!='\0'; p++)
  293.     {
  294.         if (*p=='.') found++;
  295.         *q++ = *p;
  296.     }
  297.     if (*p=='\0') return (s);
  298.  
  299.     *q = '\0';
  300.  
  301.     if (found)
  302.     {
  303.         strcpy(adr,++p);
  304.         strcat(adr,"@");
  305.         strcat(adr,first);
  306.         return(opath(adr));
  307.     }
  308.     else
  309.     {
  310.     int i;
  311.         strcpy(adr,gpath(first));
  312.         strcat(adr,/* ++ */p);
  313.  
  314.         return(adr);
  315.     }
  316. }
  317.  
  318.  
  319. opathlog(fmt,a,b,c,d)
  320. char *fmt;
  321. int a,b,c,d;
  322. {
  323. FILE *f;
  324.  
  325.     f = fopen(logfile,"a");
  326.     if (f==NULL) return;
  327.  
  328.     fprintf(f,fmt,a,b,c,d);
  329.     fclose(f);
  330. }
  331. #endif
  332.