home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo3.zoo / demo / misc / mgrmail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-24  |  5.8 KB  |  238 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: mgrmail.c,v 4.2 88/06/22 14:37:50 bianchi Exp $
  9.     $Source: /tmp/mgrsrc/demo/misc/RCS/mgrmail.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/demo/misc/RCS/mgrmail.c,v $$Revision: 4.2 $";
  12.  
  13. /* check for new mail  (experimental version) */
  14.  
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <signal.h>
  18. #include <stdio.h>
  19. #include "term.h"
  20.  
  21. #define MSG_1    "\fLooking for new mail"
  22. #define MSG_2    "\f\007You have new mail"
  23. #define MSG_3    "\freading mail ...\r"
  24. #define MSG_4    "\rChecking for new mail..."
  25. #define MSG_5    "\fMail window is active"
  26. #define MSG_6    "\rYou don't have mail   "
  27.  
  28. #define MAILF        "/usr/spool/mail"    /* spool file */
  29. #define MAIL        "mail"            /* name of mail command */
  30. #define POLL        60            /* polling interval */
  31. #define XPOS        240            /* x start of mail window */
  32. #define YPOS        190            /* y start of mail window */
  33. #define W_WIDE        650            /* width of mail window */
  34. #define W_HIGH        394            /* height of mail window */
  35.  
  36. #define PROCESSED    2            /* new mail already processed */
  37.  
  38. #define S(x)            statb.x
  39. #define Isflag(arg,flag)    (!strncmp(arg,flag,strlen(flag)))
  40. #define Max(x,y)        ((x)>(y)?(x):(y))
  41. #define dprintf            if(debug) fprintf
  42.  
  43. #define MENU_COUNT        (sizeof(menu)/sizeof(struct menu_entry))
  44.  
  45. struct menu_entry menu[] = {
  46.     "print","t\r",
  47.     "delete","dt\r",
  48.     "next","n\r",
  49.     "quit","q\r",
  50.     "help","?\r",
  51.     "headers","h *\r",
  52.     "abort","x\r",
  53. };
  54.  
  55. struct    stat statb;            /* spool file status */
  56. char    mail[255];            /* spool file path name */
  57. long omtime=0l;                /* previous file mod. time */
  58. int state = 0;                /* mail & window state */
  59. int poll = POLL;            /* poll interval */
  60. int debug=0;                /* for mgrmail -d >& /dev/tty?? */
  61.  
  62. main(argc,argv)
  63.     char **argv;
  64. {
  65.     register int i;
  66.     int xpos = XPOS;        /* screen position of mail subwindow */
  67.     int ypos = YPOS;
  68.     int font = -1;            /* font to use for mail subwindow */
  69.     int shape = 1;            /* initially reshape window */
  70.     char *command = MAIL;        /* name of readmail command */
  71.  
  72.     char *getenv();
  73.     char *user = getenv("USER");
  74.     char line[80];            /* event input buffer */
  75.  
  76.     int clean(), update();
  77.  
  78.     ckmgrterm( *argv );
  79.  
  80.     /* make sure environment is ok */
  81.     if (user==NULL || *user=='\0') {
  82.         fprintf(stderr,"%s: Who are you?\n",argv[0]);
  83.         exit(2);
  84.     }
  85.  
  86.     /* process arguments */
  87.  
  88.     for(i=1;i<argc;i++) {
  89.         if (Isflag(argv[i],"-s"))
  90.             shape = 0;
  91.         else if (Isflag(argv[i],"-d"))
  92.             debug = 1;
  93.         else if (Isflag(argv[i],"-x"))
  94.             xpos = atoi(argv[i]+2);
  95.         else if (Isflag(argv[i],"-y"))
  96.             ypos = atoi(argv[i]+2);
  97.         else if (Isflag(argv[i],"-f"))
  98.             font = atoi(argv[i]+2);
  99.         else if (Isflag(argv[i],"-p"))
  100.             poll  = Max(atoi(argv[i]+2),10);
  101.         else if (Isflag(argv[i],"-M"))
  102.             command  = argv[i]+2;
  103.         else
  104.             usage(argv[0],argv[i]);
  105.     }
  106.     sprintf(mail,"%s/%s",MAILF,user);
  107.  
  108.     /* set up window environment */
  109.  
  110.     m_setup(M_FLUSH);
  111.     m_ttyset();
  112.     m_push(P_MENU|P_EVENT|P_FLAGS);
  113.     dprintf(stderr,"pushing environment\n"); fflush(stderr);
  114.     m_setmode(M_NOWRAP);
  115.  
  116.     signal(SIGTERM,clean);
  117.     signal(SIGINT,clean);
  118.     signal(SIGALRM,update);
  119.  
  120.     m_setmode(M_ACTIVATE);
  121.     if (shape) {
  122.         m_size(strlen(MSG_1),1);
  123.         }
  124.  
  125.     m_setevent(ACTIVATE,"A\r");
  126.     m_setevent(REDRAW,"R\r");
  127.  
  128.     m_clearmode(M_ACTIVATE);
  129.     m_clear();
  130.     m_printstr(MSG_1);
  131.  
  132.     dprintf(stderr,"Starting state 0x%x\n",state); fflush(stderr);
  133.  
  134.     update();
  135.  
  136.     /* wait for an event */
  137.  
  138.     while(1) {
  139.         m_gets(line);
  140.         dprintf(stderr,"state 0x%x line : %c\n",state,*line); fflush(stderr);
  141.         switch(*line) {
  142.             case 'A':    /* window is activated */
  143.                 if (!stat(mail,&statb) && S(st_size))
  144.                     do_mail(command,font,xpos,ypos);
  145.                                 else {
  146.                                     m_printstr(MSG_6);
  147.                                         sleep(2);
  148.                                         m_clearmode(M_ACTIVATE);
  149.                                         }
  150.                 state &= ~PROCESSED;
  151.                 update();
  152.                 break;
  153.             case 'R':    /* screen is redrawn */
  154.                 state &= ~PROCESSED;
  155.                 update();
  156.                 break;
  157.         }
  158.     }
  159. }
  160.  
  161. /* run readmail in a subwindow */
  162.  
  163. do_mail(command,font,xpos,ypos)
  164. char *command;
  165. int font,xpos,ypos;
  166.     {
  167.     int code;
  168.     int n;
  169.  
  170.     alarm(0);
  171.     dprintf(stderr,"doing mail\n"); fflush(stderr);
  172.     n=m_makewindow(xpos,ypos,W_WIDE,W_HIGH);
  173.     if (n==0) {    /* can't make window */
  174.         m_printstr("\007\fCan't open mail window, sorry");
  175.         return(0);
  176.         }
  177.     m_clearevent(ACTIVATE);
  178.     m_printstr(MSG_5);
  179.     m_selectwin(n);
  180.     menu_load(1,MENU_COUNT,menu);
  181.     m_selectmenu(1);
  182.     m_printstr(MSG_3);
  183.     m_ttyreset();
  184.     code = system(command);
  185.     m_printstr(MSG_4);
  186.     sleep(1);    /* for "New mail arrived" message */
  187.     dprintf(stderr,"Readmail completed code %d\n",code); fflush(stderr);
  188.     m_ttyset();
  189.     m_destroywin(n);
  190.     m_setevent(ACTIVATE,"A\r");
  191.     m_clearmode(M_ACTIVATE);
  192.     dprintf(stderr,"window deactivated\n"); fflush(stderr);
  193.     }
  194.  
  195. /* check the spool file for new mail and update message */
  196.  
  197. int
  198. update()
  199. {
  200.     alarm(0);
  201.     dprintf(stderr,"checking mail state 0x%x\n",state); fflush(stderr);
  202.     if (!stat(mail,&statb) && S(st_mtime)>S(st_atime) && S(st_size)) {
  203.         state &= ~PROCESSED;
  204.         if (S(st_mtime) != omtime) {
  205.         dprintf(stderr,"    First time New mail\n"); fflush(stderr);
  206.             m_printstr(MSG_2);
  207.             m_setmode(M_WOB);
  208.             omtime = S(st_mtime);
  209.         }
  210.     }
  211.     else if (!(state&PROCESSED)) {
  212.         dprintf(stderr,"    Clearing new mail\n"); fflush(stderr);
  213.         m_clearmode(M_WOB);
  214.         m_printstr(MSG_1);
  215.         state |= PROCESSED;
  216.     }
  217.     alarm(poll);
  218. }
  219.  
  220. /*    Clean up and exit */
  221.  
  222. clean()
  223. {
  224.     m_popall();
  225.     m_ttyreset();
  226.     exit(1);
  227. }
  228.  
  229. usage(name,error)
  230. char *name, *error;
  231. {
  232.     fprintf(stderr,"Invalid flag: %s\n",error);
  233.     fprintf(stderr,
  234.         "usage: %s -[s|x<pos>|y<pos>|f<font>|p<poll>|M<mail_program>]\n"
  235.         ,name);
  236.     exit(1);
  237. }
  238.