home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / com / utils / elm / sources / calendar.c < prev    next >
C/C++ Source or Header  |  1992-03-28  |  4KB  |  164 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: calendar.c,v 4.1.1.1 90/06/21 22:16:50 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1.1.1 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log:    calendar.c,v $
  17.  * Revision 4.1.1.1  90/06/21  22:16:50  syd
  18.  * Add skip leading whitespace
  19.  * From Jerry Pendergrafyt
  20.  *
  21.  * Revision 4.1  90/04/28  22:42:36  syd
  22.  * checkin of Elm 2.3 as of Release PL0
  23.  *
  24.  *
  25.  ******************************************************************************/
  26.  
  27. /** This routine implements a rather snazzy idea suggested by Warren
  28.     Carithers of the Rochester Institute of Technology that allows
  29.     mail to contain entries formatted in a manner that will allow direct
  30.     copying into a users calendar program.
  31.  
  32.     All lines in the current message beginning with "->", e.g.
  33.  
  34.     -> Mon 04/21 1:00p meet with chairman candidate
  35.  
  36.     get copied into the user's calendar file.
  37.  
  38. **/
  39.  
  40. #include "headers.h"
  41.  
  42. #ifdef ENABLE_CALENDAR        /* if not defined, this will be an empty file */
  43.  
  44. #include <errno.h>
  45.  
  46. #ifndef OS2
  47. extern int errno;
  48. #endif
  49.  
  50. char *error_name(), *error_description(), *strcpy();
  51.  
  52. scan_calendar()
  53. {
  54.     FILE *calendar;
  55.     int  count;
  56.  
  57.     /* First step is to open the calendar file for appending... **/
  58.  
  59.     if (can_open(calendar_file, "a") != 0) {
  60.       dprint(2, (debugfile,
  61.           "Error: wrong permissions to append to calendar %s\n",
  62.           calendar_file));
  63.       dprint(2, (debugfile, "** %s - %s **\n",
  64.           error_name(errno), error_description(errno)));
  65.       error1("Not able to append to file %s!", calendar_file);
  66.       return;
  67.     }
  68.  
  69.     save_file_stats(calendar_file);
  70.  
  71.     if ((calendar = fopen(calendar_file,"a")) == NULL) {
  72.       dprint(2, (debugfile,
  73.         "Error: couldn't append to calendar file %s (scan)\n",
  74.         calendar_file));
  75.       dprint(2, (debugfile, "** %s - %s **\n",
  76.           error_name(errno), error_description(errno)));
  77.       error1("Couldn't append to file %s!", calendar_file);
  78.       return;
  79.     }
  80.  
  81.     count = extract_info(calendar);
  82.  
  83.     fclose(calendar);
  84.  
  85.     restore_file_stats(calendar_file);
  86.  
  87.     if (count > 0)
  88.       error2("%d entr%s saved in calendar file.",
  89.          count, count > 1 ? "ies" : "y");
  90.     else
  91.       error("No calendar entries found in that message.");
  92.  
  93.     return;
  94. }
  95.  
  96. int
  97. extract_info(save_to_fd)
  98. FILE *save_to_fd;
  99. {
  100.     /** Save the relevant parts of the current message to the given
  101.         calendar file.  The only parameter is an opened file
  102.         descriptor, positioned at the end of the existing file **/
  103.  
  104.     register int entries = 0, lines;
  105.     char buffer[SLEN], *cp, *is_cal_entry();
  106.  
  107.         /** get to the first line of the message desired **/
  108.  
  109.         if (fseek(mailfile, headers[current-1]->offset, 0) == -1) {
  110.              dprint(1,(debugfile,
  111.         "ERROR: Attempt to seek %d bytes into file failed (%s)",
  112.         headers[current-1]->offset, "extract_info"));
  113.              error1("ELM [seek] failed trying to read %d bytes into file.",
  114.              headers[current-1]->offset);
  115.              return(0);
  116.         }
  117.  
  118.         /* how many lines in message? */
  119.  
  120.         lines = headers[current-1]->lines;
  121.  
  122.         /* now while not EOF & still in message... scan it! */
  123.  
  124.     while (lines) {
  125.  
  126.           if(fgets(buffer, SLEN, mailfile) == NULL)
  127.         break;
  128.  
  129.       if(buffer[strlen(buffer)-1] == '\n')
  130.         lines--;                    /* got a full line */
  131.  
  132.       if((cp = is_cal_entry(buffer)) != NULL) {
  133.         entries++;
  134.         fprintf(save_to_fd,"%s", cp);
  135.       }
  136.  
  137.     }
  138.     dprint(4,(debugfile,
  139.         "Got %d calender entr%s.\n", entries, entries > 1? "ies":"y"));
  140.  
  141.     return(entries);
  142. }
  143.  
  144. char *
  145. is_cal_entry(string)
  146. register char *string;
  147. {
  148.     /* If string is of the form
  149.          * {optional white space} ->{optional white space} {stuff}
  150.      * return a pointer to stuff, otherwise return NULL.
  151.      */
  152.     while( whitespace(*string) )
  153.       string++;      /* strip leading W/S */
  154.  
  155.     if(strncmp(string, "->", 2) == 0) {
  156.       for(string +=2 ; whitespace(*string); string++)
  157.           ;
  158.       return(string);
  159.     }
  160.     return(NULL);
  161. }
  162.  
  163. #endif
  164.