home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Uip / rcvalert / flagmail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  4.7 KB  |  256 lines

  1. /* client.c: client for alert program */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Uip/rcvalert/RCS/flagmail.c,v 6.0 1991/12/18 20:39:41 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Uip/rcvalert/RCS/flagmail.c,v 6.0 1991/12/18 20:39:41 jpo Rel $
  9.  *
  10.  * $Log: flagmail.c,v $
  11.  * Revision 6.0  1991/12/18  20:39:41  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include <stdio.h>
  19. #include <isode/general.h>
  20. #include <isode/manifest.h>
  21. #include <isode/internet.h>
  22. #include <sys/time.h>
  23. #include <pwd.h>
  24. #include <ctype.h>
  25.  
  26. #include "data.h"
  27.  
  28. char    displayline[BUFSIZ];
  29. char    from[BUFSIZ];
  30. char    username[40];
  31. char    hostname[BUFSIZ];
  32. char    homedir[512];
  33. char    *filename = ".alert";
  34. char    *extra = "NEW: ";
  35. char    *format = "%extra(%size) %20from %subject << %80body";
  36. int    width = 80;
  37. int    debug = 0;
  38. int    theport = 0;
  39.  
  40. char    *myname;
  41.  
  42. extern char    *index ();
  43.  
  44. main (argc, argv)
  45. int    argc;
  46. char    *argv[];
  47. {
  48.     extern char    *optarg;
  49.     extern int    optind;
  50.     int    opt;
  51.     int    size = 0;
  52.     int    use_tty = 0;
  53.     int    use_x    = 0;
  54.  
  55.     myname = argv[0];
  56.     pp_initialise (myname, 0);
  57.     while((opt = getopt(argc, argv, "de:f:h:p:s:txw:")) != EOF)
  58.         switch (opt) {
  59.             case 'e':
  60.             extra = optarg;
  61.             break;
  62.  
  63.             case 'f':
  64.             format = optarg;
  65.             break;
  66.  
  67.             case 'd':
  68.             debug = 1;
  69.             break;
  70.  
  71.             case 'h':
  72.             filename = optarg;
  73.             break;
  74.  
  75.             case 's':
  76.             size = atoi (optarg);
  77.             break;
  78.  
  79.             case 'p':
  80.             theport = atoi (optarg);
  81.             break;
  82.  
  83.             case 't':
  84.             use_tty = 1;
  85.             break;
  86.  
  87.             case 'w':
  88.             width = atoi (optarg);
  89.             break;
  90.  
  91.             case 'x':
  92.             use_x = 1;
  93.             break;
  94.  
  95.             default:
  96.             fprintf (stderr, "Usage: %s [options]\n",
  97.                  myname);
  98.             break;
  99.         }
  100.     argc -= optind;
  101.     argv += optind;
  102.  
  103.     init_stuff ();
  104.     
  105.     parse_msg (displayline, from, format, BUFSIZ-1, size, extra);
  106.     chop(displayline);
  107.  
  108.     if (debug == 0 && fork () > 0)
  109.         exit (1);
  110.     if (use_tty || (notify_x () == 0 && ! use_x))
  111.     {    if (width > 0 && width < sizeof displayline)
  112.             displayline[width] = 0;
  113.         chop(displayline);
  114.         (void) notify_normal (displayline, username);
  115.     }
  116.     exit (1);
  117. }
  118.  
  119. init_stuff ()
  120. {
  121.     struct passwd *pwd;
  122.     char    *cp, *getenv ();
  123.  
  124.     if ((pwd = getpwuid (getuid())) == NULL) {
  125.         fprintf (stderr, "No user name\n");
  126.         exit(-1);
  127.     }
  128.  
  129.     (void) strcpy (username, pwd -> pw_name);
  130.     if ((cp = getenv ("HOME")) != NULL && *cp)
  131.         (void) strcpy (homedir, cp);
  132.     else
  133.         (void) strcpy (homedir, pwd -> pw_dir);
  134. }
  135.  
  136.  
  137. notify_x ()
  138. {
  139.     struct data data;
  140.     struct sockaddr_in sin;
  141.     struct hostent *hp;
  142.     int    i;
  143.     int    sd;
  144.     
  145.     if (!findhost(hostname))
  146.         return 0;
  147.  
  148.     if ((sd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {
  149.         perror ("socket");
  150.         exit (1);
  151.     }
  152.  
  153.     bzero ((char *)&sin, sizeof sin);
  154.  
  155.     sin.sin_family = AF_INET;
  156.     sin.sin_port = htons (theport);
  157.  
  158.     bzero ((char *)&data, sizeof data);
  159.     data.refid = htonl(getpid ());
  160.     (void) strcpy (data.user, username);
  161.     (void) strncpy (data.data, displayline, sizeof (data.data) - 1);
  162.     data.data[sizeof data.data - 1] = 0;
  163.     (void) strncpy (data.from, from, sizeof(data.from) - 1);
  164.     data.from[sizeof data.from - 1] = 0;
  165.  
  166.     if ((hp = gethostbyname (hostname)) == NULL) {
  167.         fprintf (stderr, "No such host %s\n", hostname);
  168.         return 0;
  169.     }
  170.     bcopy (hp -> h_addr, (char *)&sin.sin_addr, hp -> h_length);
  171.     for (i = 0; i < N_RETRYS; i++) {
  172.         if (debug) printf("Send to %s.%d\n", hostname, theport);
  173.         if (sendto (sd, (char *)&data, sizeof data, 0,
  174.                 (struct sockaddr *)&sin, sizeof sin) < 0)
  175.             perror ("sendto");
  176.  
  177.         if (replys (sd))
  178.             return 1;
  179.     }
  180.     if (debug) printf("No reply from %s.%d\n", hostname, theport);
  181.     return 0;
  182. }
  183.  
  184. replys (sd)
  185. int    sd;
  186. {
  187.     fd_set rfds;
  188.     char    buffer[20];
  189.     struct timeval timer;
  190.     struct sockaddr_in nsin;
  191.     int    len;
  192.  
  193.     timer.tv_usec = 0;
  194.     timer.tv_sec = 10;
  195.     FD_ZERO (&rfds);
  196.     FD_SET (sd, &rfds);
  197.  
  198.     if (select (sd + 1, &rfds, NULLFD, NULLFD, &timer) <= 0)
  199.         return 0;
  200.  
  201.     if (FD_ISSET (sd, &rfds)) {
  202.         len = sizeof nsin;
  203.  
  204.         if (recvfrom (sd, buffer, 3, 0,
  205.                   (struct sockaddr *)&nsin, &len) < 0)
  206.             perror ("recvfrom");
  207.         return 1;
  208.     }
  209.     if (debug) printf("No reply from %s.%d\n", hostname, theport);
  210.     return 0;
  211. }
  212.  
  213. findhost (host)
  214. char    *host;
  215. {
  216.     FILE    *fp;
  217.     char    *cp;
  218.     char    buf[BUFSIZ];
  219.  
  220.     if (*filename == '/' || strncmp (filename, "./", 2) == 0 ||
  221.         strncmp (filename, "../", 3) == 0)
  222.         (void) strcpy (buf, filename);
  223.     else
  224.         (void) sprintf (buf, "%s/%s", homedir, filename);
  225.  
  226.     if ((fp = fopen (buf, "r")) == NULL &&
  227.         (fp = fopen (filename, "r")) == NULL)
  228.         return 0;
  229.  
  230.     if (fgets (host, BUFSIZ, fp) == NULL) {
  231.         (void) fclose (fp);
  232.         return 0;
  233.     }
  234.     (void) fclose (fp);
  235.     if (cp = index (hostname, '\n'))
  236.         *cp = '\0';
  237.     if (cp = index (hostname, ' ')) {
  238.         *cp++ = '\0';
  239.         if (theport == 0)
  240.             theport = atoi (cp);
  241.     }
  242.  
  243.     if (theport == 0) {
  244.         fprintf (stderr, "No port!\n");
  245.         return 0;
  246.     }
  247.         
  248.     return 1;
  249. }
  250.  
  251. chop(string)
  252. char *string;
  253. {    char *end = string + strlen(string) -1;
  254.     while (end != string && (*end == ' ' || *end == '\t')) *end-- = 0;
  255. }
  256.