home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / ELM23-2 / ELM23-2.ZIP / utils / autoreply.c < prev    next >
C/C++ Source or Header  |  1992-03-29  |  6KB  |  246 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: autoreply.c,v 4.1 90/04/28 22:44:35 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.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:    autoreply.c,v $
  17.  * Revision 4.1  90/04/28  22:44:35  syd
  18.  * checkin of Elm 2.3 as of Release PL0
  19.  *
  20.  *
  21.  ******************************************************************************/
  22.  
  23. /** This is the front-end for the autoreply system, and performs two
  24.     functions: it either adds the user to the list of people using the
  25.     autoreply function (starting the daemon if no-one else) or removes
  26.     a user from the list of people.
  27.  
  28.     Usage:  autoreply filename
  29.         autoreply "off"
  30.     or  autoreply        [to find current status]
  31.  
  32. **/
  33.  
  34. #include <stdio.h>
  35. #include <errno.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38.  
  39. #ifdef PWDINSYS
  40. #  include <sys/pwd.h>
  41. #else
  42. #  include <pwd.h>
  43. #endif
  44.  
  45. #include "defs.h"
  46.  
  47. static char ident[] = { WHAT_STRING };
  48.  
  49. char     autoreply_file[SLEN];    /* autoreply data file  */
  50.  
  51. char     username[NLEN];    /* login name of user   */
  52.  
  53. main(argc, argv)
  54. int    argc;
  55. char *argv[];
  56. {
  57.     char filename[SLEN];
  58.     int userid;
  59.     struct passwd *pass;
  60. #ifndef    _POSIX_SOURCE
  61.     struct passwd *getpwuid();
  62. #endif
  63.  
  64.         initpaths();
  65.         sprintf(autoreply_file, "%s/%s", elmhome, AUTOREP_FILE);
  66.  
  67.     if (argc > 2) {
  68.       printf("\nUsage: %s <filename>\tto start autoreply,\n", argv[0]);
  69.       printf("       %s off\t\tto turn off autoreply\n", argv[0]);
  70.       printf("   or  %s    \t\tto check current status\n", argv[0]);
  71.       exit(1);
  72.     }
  73.  
  74.     userid  = getuid();
  75.  
  76.     /*
  77.      * Get username (logname) field from the password entry for this user id.
  78.      */
  79.  
  80.     if((pass = getpwuid(userid)) == NULL) {
  81.       printf("You have no password entry!");
  82.       exit(1);
  83.     }
  84.     strcpy(username, pass->pw_name);
  85.  
  86.     if (argc == 1 || strcmp(argv[1], "off") == 0)
  87.       remove_user((argc == 1));
  88.     else {
  89.       strcpy(filename, argv[1]);
  90.  
  91.       if (filename[0] != '/') /* prefix home directory */
  92.         sprintf(filename,"%s/%s", pass->pw_dir, argv[1]);
  93.  
  94.       if (access(filename,READ_ACCESS) != 0) {
  95.         printf("Error: Can't read file '%s'\n", filename);
  96.         exit(1);
  97.       }
  98.  
  99.       add_user(filename);
  100.     }
  101.  
  102.     exit(0);
  103. }
  104.  
  105. remove_user(stat_only)
  106. int stat_only;
  107. {
  108.     /** Remove the user from the list of currently active autoreply
  109.         people.  If 'stat_only' is set, then just list the name of
  110.         the file being used to autoreply with, if any. **/
  111.  
  112.     FILE *temp, *repfile;
  113.     char  tempfile[SLEN], user[SLEN], filename[SLEN];
  114.     int   c, copied = 0, found = 0;
  115.     long  filesize, bytes();
  116.  
  117.     if (! stat_only) {
  118.       sprintf(tempfile, "%s%d.ar", tempdir, getpid());
  119.  
  120.       if ((temp = fopen(tempfile, "w")) == NULL) {
  121.         printf("Error: couldn't open tempfile '%s'.  Not removed\n",
  122.             tempfile);
  123.         exit(1);
  124.       }
  125.     }
  126.  
  127.     if ((repfile = fopen(autoreply_file, "r")) == NULL) {
  128.       if (stat_only) {
  129.         printf("\nYou're not currently autoreplying to mail.\n");
  130.         exit(0);
  131.       }
  132.       printf("\nNo-one is autoreplying to their mail!\n");
  133.       exit(0);
  134.     }
  135.  
  136.     /** copy out of real replyfile... **/
  137.  
  138.     while (fscanf(repfile, "%s %s %ld", user, filename, &filesize) != EOF)
  139.  
  140.       if (strcmp(user, username) != 0) {
  141.         if (! stat_only) {
  142.           copied++;
  143.           fprintf(temp, "%s %s %ld\n", user, filename, filesize);
  144.         }
  145.       }
  146.       else {
  147.         if (stat_only) {
  148.           printf("\nYou're currently autoreplying to mail with the file %s\n",              filename);
  149.           exit(0);
  150.         }
  151.         found++;
  152.       }
  153.  
  154.     if (! stat_only)
  155.       fclose(temp);
  156.  
  157.     fclose(repfile);
  158.  
  159.     if (! found) {
  160.       printf("\nYou're not currently autoreplying to mail%s\n",
  161.           stat_only? "." : "!");
  162.       if (! stat_only)
  163.         unlink(tempfile);
  164.       exit(! stat_only);
  165.     }
  166.  
  167.     /** now copy tempfile back into replyfile **/
  168.  
  169.     if (copied == 0) {    /* removed the only person! */
  170.       unlink(autoreply_file);
  171.     }
  172.     else {            /* save everyone else   */
  173.  
  174.       if ((temp = fopen(tempfile,"r")) == NULL) {
  175.         printf("Error: couldn't reopen tempfile '%s'.  Not removed.\n",
  176.             tempfile);
  177.         unlink(tempfile);
  178.         exit(1);
  179.       }
  180.  
  181.       if ((repfile = fopen(autoreply_file, "w")) == NULL) {
  182.         printf(
  183.           "Error: couldn't reopen autoreply file for writing!  Not removed.\n");
  184.         unlink(tempfile);
  185.         exit(1);
  186.       }
  187.  
  188.       while ((c = getc(temp)) != EOF)
  189.         putc(c, repfile);
  190.  
  191.       fclose(temp);
  192.       fclose(repfile);
  193.  
  194.     }
  195.     unlink(tempfile);
  196.  
  197.     if (found > 1)
  198.       printf("\nWarning: your username appeared %d times!!   Removed all\n",
  199.           found);
  200.     else
  201.       printf("\nYou've been removed from the autoreply table.\n");
  202. }
  203.  
  204. add_user(filename)
  205. char *filename;
  206. {
  207.     /** add the user to the autoreply file... **/
  208.  
  209.     FILE *repfile;
  210.     char  mailfile[SLEN];
  211.     long  bytes();
  212.  
  213.     if ((repfile = fopen(autoreply_file, "a")) == NULL) {
  214.       printf("Error: couldn't open the autoreply file!  Not added\n");
  215.       exit(1);
  216.     }
  217.  
  218.     sprintf(mailfile,"%s/%s", mailhome, username);
  219.  
  220.     fprintf(repfile,"%s %s %ld\n", username, filename, bytes(mailfile));
  221.  
  222.     fclose(repfile);
  223.  
  224.     printf("\nYou've been added to the autoreply system.\n");
  225. }
  226.  
  227.  
  228. long
  229. bytes(name)
  230. char *name;
  231. {
  232.     /** return the number of bytes in the specified file.  This
  233.         is to check to see if new mail has arrived....  **/
  234.  
  235.     int ok = 1;
  236.     struct stat buffer;
  237.  
  238.     if (stat(name, &buffer) != 0)
  239.       if (errno != 2)
  240.        exit(fprintf(stderr,"Error %d attempting fstat on %s", errno, name));
  241.       else
  242.         ok = 0;
  243.  
  244.     return(ok ? buffer.st_size : 0L);
  245. }
  246.