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