home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / PGP / pgpv26ui.lha / PGPAmiga / contrib / PGPSendMail / source / rfcstuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-13  |  8.4 KB  |  260 lines

  1. /*
  2.  *      $Filename: rfcstuff.c $
  3.  *      $Revision: 1.3 $
  4.  *      $Date: 1994/03/13 18:24:34 $
  5.  *
  6.  *      Copyright (C) 1993 by Peter Simons <simons@peti.GUN.de>
  7.  *
  8.  *      This program is free software; you can redistribute it and/or
  9.  *      modify it under the terms of the GNU General Public License as
  10.  *      published by the Free Software Foundation; either version 2 of
  11.  *      the License, or (at your option) any later version.
  12.  *
  13.  *      This program is distributed in the hope that it will be useful,
  14.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  *      General Public License for more details.
  17.  *
  18.  *      You should have received a copy of the GNU General Public License
  19.  *      along with this program; if not, write to the Free Software
  20.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  *      $Id: rfcstuff.c 1.3 1994/03/13 18:24:34 simons Exp simons $
  23.  *
  24.  */
  25.  
  26.  
  27. /**************************************************************************
  28.  *                                                                        *
  29.  * Section: Macros, Definitions, Includes, Structures                     *
  30.  *                                                                        *
  31.  **************************************************************************/
  32.  
  33. /************************************* Includes ***********/
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <ctype.h>
  38.  
  39. #include "protos.h"
  40. #include "pgpsendmail.h"
  41.  
  42. /************************************* Prototypes *********/
  43.  
  44. /************************************* Defines ************/
  45.  
  46. /************************************* global Variables ***/
  47. static const char __RCSId[] = "$Id: rfcstuff.c 1.3 1994/03/13 18:24:34 simons Exp simons $";
  48.  
  49.  
  50. /**************************************************************************
  51.  *                                                                        *
  52.  * SECTION: Subroutines                                                   *
  53.  *                                                                        *
  54.  **************************************************************************/
  55.  
  56.  /*
  57.   * This routine expects a buffer holding a RFC mail and does all the parsing
  58.   * for the receipients, namely the "To:" and the "Cc:" line. It will
  59.   * return an array of pointers, each entry containing one address without
  60.   * comments and stuff like that.
  61.   */
  62.  
  63. char **FindReceipients(char *header)
  64. {
  65.         char **receipients, *line;
  66.  
  67.         if (!(receipients = malloc(sizeof(char *[MAX_RECEIPIENTS])))) {
  68.                 MakeLogEntry(PRGNAME, MLE_ERROR, "Failed allocating my buffers!");
  69.                 return NULL;
  70.         }
  71.         *receipients = NULL;
  72.  
  73.         if (line = FindHeaderEntry(header, "To: ")) {
  74.                 SplitAddressLine(line, receipients);
  75.                 free(line);
  76.         }
  77.         if (line = FindHeaderEntry(header, "Cc: ")) {
  78.                 SplitAddressLine(line, receipients);
  79.                 free(line);
  80.         }
  81.  
  82.         ParseAddressLines(receipients);
  83.  
  84.         return receipients;
  85. }
  86.  
  87.  
  88.  
  89.  /*
  90.   * This routine finds a certain header entry in the provided textbuffer,
  91.   * even if the entry is split over several lines.
  92.   */
  93.  
  94. char *FindHeaderEntry(char *header, char *entry)
  95. {
  96.         char *linebuf, *tmp;
  97.         int i = 0;
  98.  
  99.         if (!(linebuf = malloc(MY_BUFLEN*5)))
  100.                 return NULL;
  101.         tmp = linebuf;
  102.  
  103.         while (*header) {
  104.                 if (!strncmp(header, entry, strlen(entry))) {
  105.                         header += strlen(entry);
  106.                         while (isspace(*header))       /* skip the entry-introducer */
  107.                                 header++;
  108.                         do {
  109.                                 while (i < MY_BUFLEN && *header != '\n')
  110.                                         tmp[i++] = *(header++);
  111.                                 if (i < MY_BUFLEN && isspace(header[1])) {
  112.                                         header++;
  113.                                         tmp[i++] = ' ';
  114.                                 }
  115.                                 else
  116.                                         break;
  117.                         } while (1);
  118.                         break;
  119.                 }
  120.                 else
  121.                 while (*header != '\n' && *header)      /* goto next line */
  122.                         header++;
  123.                 if (*header == '\n')
  124.                         header++;
  125.         }
  126.         if (i)
  127.                 tmp[i] = '\0';
  128.         else {
  129.                 free(linebuf);          /* The entry is not available. */
  130.                 return NULL;
  131.         }
  132.  
  133.         return linebuf;
  134. }
  135.  
  136.  
  137.  
  138.  /*
  139.   * This routine splits a line of addresses up and inserts each address
  140.   * in an array of pointers.
  141.   */
  142.  
  143. void SplitAddressLine(char *line, char *receipients[])
  144. {
  145.         int brackets, i;
  146.  
  147.  
  148.         if (!line || !receipients)      /* security check */
  149.                 return;
  150.  
  151.  
  152.         /* Find end of array and append new entries there. */
  153.  
  154.         while (*receipients)
  155.                 receipients++;
  156.  
  157.  
  158.         /* Split the provided line. */
  159.  
  160.         *(receipients++) = line;
  161.         for (i = 0, brackets = 0; line[i]; i++) {
  162.                 if (line[i] == '(')
  163.                         brackets++;
  164.                 if (line[i] == ')')
  165.                         brackets--;
  166.                 if (line[i] == ',' && !brackets) {
  167.  
  168.                         /*
  169.                          * Found end of address. Insert \0 mark and copy the
  170.                          * buffer for our array.
  171.                          */
  172.  
  173.                         line[i++] = '\0';
  174.                         receipients[-1] = strdup(receipients[-1]);
  175.  
  176.  
  177.                         /* Skip spaces until the next address. */
  178.  
  179.                         while (isspace(line[i]))
  180.                                 i++;
  181.                         *receipients++ = &line[i];
  182.                 }
  183.         }
  184.         if (*(receipients[-1])) {
  185.                 receipients[-1] = strdup(receipients[-1]);
  186.                 *receipients = NULL;
  187.         }
  188.         else
  189.                 receipients[-1] = NULL;
  190. }
  191.  
  192.  
  193.  
  194.  /*
  195.   * This routines browses through an array of addresses and removes
  196.   * all this RFC comment stuff, just leaving the plain address.
  197.   */
  198.  
  199. void ParseAddressLines(char **receipients)
  200. {
  201.         char line[MY_BUFLEN];
  202.         int brackets, i, z;
  203.  
  204.         while (*receipients) {
  205.                 strcpy(line, *receipients);
  206.                 for (i = 0, z = 0, brackets = 0; line[i] != '\0'; i++) {
  207.                         if (line[i] == '(' && (i == 0 || isspace(line[i-1])))
  208.                                 brackets++;
  209.                         if (line[i] == ')' && (line[i+1] == '\0' || isspace(line[i+1])))
  210.                                 brackets--;
  211.                         if (line[i] == '<' && brackets == 0) {
  212.                                 i++;
  213.                                 while (1) {             /* Forever! */
  214.                                         if (line[i] == '>' && (line[i+1] == '\0' || isspace(line[i+1])))
  215.                                                 break;
  216.                                         (*receipients)[z++] = line[i++];
  217.                                 }
  218.                                 (*receipients)[z] = '\0';
  219.                                 break;
  220.                         }
  221.                 }
  222.                 if (z == 0) {   /* No <address> occured. */
  223.                         i = 0;
  224.                         while (!isspace((*receipients)[i]))
  225.                                 i++;
  226.                         (*receipients)[i] = '\0';
  227.                 }
  228.                 receipients++;
  229.         }
  230. }
  231.  
  232.  
  233.  /*
  234.   * MakeDomainAddress browses through an array of addresses and
  235.   * looks for addresses which do not contain either a "!" or a
  236.   * "@". These addresses will be converted in full domain addresses
  237.   * of the local site.
  238.   *
  239.   * PORT NOTE: This routine has to determine the name and domain
  240.   * of the local site. This is done using the netsupport.library.
  241.   */
  242.  
  243. void MakeDomainAddress(char **receipients)
  244. {
  245.         char line[128], *p;
  246.  
  247.         while (*receipients) {
  248.                 if (!stristr(*receipients, "@") && !stristr(*receipients, "!")) {
  249.                         sprintf(line, "%s@%s", *receipients, FindConfig(NODENAME));
  250.                         strcat(line, FindConfig(DOMAINNAME));
  251.                         if (p = strdup(line)) {
  252.                                 free(*receipients);
  253.                                 *receipients = p;
  254.                         }
  255.                 }
  256.                 receipients++;
  257.         }
  258. }
  259.  
  260.