home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume28 / ldb / part01 / t_email.c < prev   
Encoding:
C/C++ Source or Header  |  1992-03-15  |  4.6 KB  |  150 lines

  1. /*    t_email.c        9/5/91
  2.  *
  3.  * Copyright 1991  Perry R. Ross
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation without fee is hereby granted, subject to the restrictions
  7.  * detailed in the README file, which is included here by reference.
  8.  * Any other use requires written permission from the author.  This software
  9.  * is distributed "as is" without any warranty, including any implied
  10.  * warranties of merchantability or fitness for a particular purpose.
  11.  * The author shall not be liable for any damages resulting from the
  12.  * use of this software.  By using this software, the user agrees
  13.  * to these terms.
  14.  */
  15.  
  16. #include "ldb.h"
  17.  
  18. /*======================================================================
  19.  * This file is the e-mail transport for the ldb program.  It is
  20.  * modularized to allow other transports to be substituted, most
  21.  * notably a socket transport to allow interactive network backgammon.
  22.  * For now, though, this is the only transport available.
  23.  *
  24.  * Packets are sent via TSendPacket, which takes an instance of
  25.  * struct packet and an address, and sends the packet to that address.
  26.  * TInitialize must be called before any calls to TSendPacket, and
  27.  * TFinishSession must be called before exiting.
  28.  *
  29.  * Individual files may be sent by calling TSendFile.  These files
  30.  * need not be related to a game.
  31.  *======================================================================
  32.  */
  33.  
  34. PRIVATE PStringSub();
  35.  
  36.  
  37. /*----------------------------------------------------------------------
  38.  *    TInitialize -- initialize the transport
  39.  *
  40.  * This function is called before the transport is used.  As you can
  41.  * see, email doesn't require a great deal of initialization.
  42.  *----------------------------------------------------------------------
  43.  */
  44.  
  45. TInitialize()
  46. {
  47. }
  48.  
  49.  
  50. /*----------------------------------------------------------------------
  51.  *    FeDrawMenu -- draw menu choices in menu box
  52.  *
  53.  * This function closes down the transport.
  54.  *----------------------------------------------------------------------
  55.  */
  56.  
  57. TFinishSession()
  58. {
  59. }
  60.  
  61.  
  62.  
  63. /*----------------------------------------------------------------------
  64.  *    TSendPacket -- send a packet to an email address
  65.  *
  66.  * This function sends a packet to an email address.
  67.  *----------------------------------------------------------------------
  68.  */
  69.  
  70. TSendPacket(p,addr)
  71. struct packet *p;
  72. char *addr;
  73. {
  74. char subject[80];
  75. FILE *fp;
  76.  
  77.  
  78. if ((fp = fopen(rc.tempfile,"w")) == NULL) {    /* open temp file */
  79.     fprintf(stderr,"ERROR: can't create %s!\n",rc.tempfile);
  80.     return;
  81.     }
  82. fprintf(fp,"<<<===LDB===>>>\n");    /* send magic header text */
  83. nvwrite(fp,nv_packet,p,opcodes);    /* write the contents of the packet */
  84. fclose(fp);                /* close temp file */
  85. sprintf(subject,"<=LDB=> from %s (Long Distance Backgammon)", rc.myname);
  86. TSendFile(addr,rc.tempfile,subject);
  87. unlink(rc.tempfile);        /* delete the temp file */
  88. }
  89.  
  90.  
  91. /*----------------------------------------------------------------------
  92.  *    TSendFile -- send a file to an address
  93.  *
  94.  * This function takes an e-mail address, a file, and a subject line,
  95.  * and sends the contents of the file to the specified address.  The
  96.  * subject is set to the contents of the subject argument, which should
  97.  * be one line.
  98.  *----------------------------------------------------------------------
  99.  */
  100.  
  101. TSendFile(addr,file,subj)
  102. char *addr, *file, *subj;
  103. {
  104. char cmd[256];
  105. char *subs[3];
  106.  
  107. subs[0] = addr;            /* substitute $a with opponent address */
  108. subs[1] = file;            /* substitue $f with file */
  109. subs[2] = subj;            /* substitute $s with subject */
  110. PStringSub(rc.sendcmd,"afs",subs,cmd);
  111. system(cmd);            /* execute email command */
  112. }
  113.  
  114.  
  115.  
  116. /*----------------------------------------------------------------------
  117.  *    PStringSub -- substitute variables in a string
  118.  *
  119.  * This function copies str to obuf, replacing occurrences of $x to
  120.  * a corresponding string.  The characters which can appear after
  121.  * the $ are passed in a single string in the "chars" argument, and
  122.  * the strings to replace them with are passed, in the same order as
  123.  * the characters appear in "chars", in the "strings" argument.
  124.  *----------------------------------------------------------------------
  125.  */
  126.  
  127. PRIVATE PStringSub(str,chars,strings,obuf)
  128. char *str, *chars, *strings[], obuf[];
  129. {
  130. char *s, *o, *t;
  131.  
  132. for (s = str, o = obuf; *s; s++) {
  133.     if (*s != '$') {    /* as long as we don't see a $ */
  134.         *o++ = *s;    /* just keep copying */
  135.         continue;
  136.         }
  137.     s++;
  138.     if (*s == '$') {    /* $$ is a single $ */
  139.         *o++ = '$';
  140.         continue;
  141.         }
  142.     if ( (t = strchr(chars,*s)) == NULL)    /* skip invalid $ subs */
  143.         continue;
  144.     t = strings[t - chars];        /* get ptr to replacement string */
  145.     while (*t)            /* copy it to obuf */
  146.         *o++ = *t++;
  147.     }
  148. *o = '\0';                /* null terminate obuf */
  149. }
  150.