home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / nethack-3.1 / sys / vms / vmsmail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-17  |  15.8 KB  |  467 lines

  1. /*    SCCS Id: @(#)vmsmail.c    3.1    92/12/16    */
  2. /* Copyright (c) Robert Patrick Rankin, 1991.              */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. #define EXTERN_H    /* don't need all those prototypes */
  6. #include "config.h"
  7.  
  8. #ifdef MAIL
  9. #include "winprocs.h"
  10. #include "mail.h"
  11. #include <ctype.h>
  12. #include <descrip.h>
  13. #include <errno.h>
  14. # ifndef __GNUC__
  15. #include <msgdef.h>
  16. # else
  17. #  define MSG$_TRMHANGUP  6
  18. #  define MSG$_TRMBRDCST 83
  19. # endif /*__GNUC__*/
  20. #include <signal.h>
  21. /* #include <string.h> */
  22. # define vms_ok(sts) ((sts)&1)
  23.  
  24. static struct mail_info *FDECL(parse_brdcst, (char *));
  25. static void FDECL(filter_brdcst, (char *));
  26. static void NDECL(flush_broadcasts);
  27. static void FDECL(broadcast_ast, (int));
  28. extern char *strcpy(), *strcat(), *strrchr(), *strstri(), *eos();
  29. extern int strspn(), strncmpi();
  30. #ifndef __DECC
  31. extern int VDECL(sscanf, (const char *,const char *,...));
  32. #endif
  33. extern unsigned long
  34.     SMG$CREATE_PASTEBOARD(),
  35.     SMG$GET_BROADCAST_MESSAGE(),
  36.     SMG$SET_BROADCAST_TRAPPING(),
  37.     SMG$DISABLE_BROADCAST_TRAPPING();
  38.  
  39. extern volatile int broadcasts;        /* defining declaration in mail.c */
  40.  
  41. static long pasteboard_id = 0;        /* SMG's magic cookie */
  42.  
  43. /*
  44.  * Mail (et al) overview:
  45.  *
  46.  *    When a broadcast is asynchronously captured, a volatile counter
  47.  * ('broadcasts') is incremented.  Each player turn, ckmailstatus() polls
  48.  * the counter and calls parse_next_broadcast() if it's positive; this
  49.  * returns some display text, object name, and response command, which is
  50.  * passed to newmail().  Routine newmail() generates a mail-daemon monster
  51.  * who approaches the character, "speaks" the display text, and delivers
  52.  * a scroll of mail pre-named to the object name; the response command is
  53.  * initially appended to the name, so that the object is tagged with both
  54.  * of them; a NUL is inserted to terminate the ordinary name and hide the
  55.  * command.  (If the player renames such a scroll, the hidden command will
  56.  * be lost; who cares?)  Unrecognized broadcasts result in the mail-daemon
  57.  * arriving and announcing the display text, but no scroll being created.
  58.  * If SHELL is undefined, then all broadcasts are treated as 'other'; since
  59.  * no subproceses are allowed, there'd be no way to respond to the scroll.
  60.  *
  61.  *    When a scroll of mail is read by the character, readmail() extracts
  62.  * the hidden command string and uses it for the default when prompting the
  63.  * player for a system command to spawn.  The player may enter any command
  64.  * he or she chooses, or just <return> to accept the default or <escape> to
  65.  * avoid executing any command.  If the command is "SPAWN", a regular shell
  66.  * escape to DCL is performed; otherwise, the indicated single command is
  67.  * spawned.  Either way, NetHack resumes play when the subprocess terminates
  68.  * or explicitly reattaches to its parent.
  69.  *
  70.  * Broadcast parsing:
  71.  *
  72.  *    The following broadcast messages are [attempted to be] recognized:
  73.  *    text fragment          name for scroll          default command
  74.  *    New mail        VMSmail            MAIL
  75.  *    New ALL-IN-1 MAIL    A1mail            A1M
  76.  *    Software Tools mail    STmail            MSG [+folder]
  77.  *    MM mail            MMmail            MM
  78.  *    WPmail: New mail    WPmail            OFFICE/MAIL
  79.  *    **M400 mail        M400mail        M400
  80.  *    " mail", ^"mail "    unknown mail        SPAWN
  81.  *    " phoning"        Phone call        PHONE ANSWER
  82.  *    talk-daemon...by...foo    Talk request        TALK[/OLD] foo@bar
  83.  *    (node)user -        Bitnet noise        XYZZY user@node
  84.  * Anything else results in just the message text being passed along, no
  85.  * scroll of mail so consequently no command to execute when scroll read.
  86.  * The user can set up ``$ XYZZY :== SEND'' prior to invoking NetHack if
  87.  * vanilla JNET responses to Bitnet messages are prefered.
  88.  *
  89.  *    Static return buffers are used because only one broadcast gets
  90.  * processed at a time, and the essential information in each one is
  91.  * either displayed and discarded or copied into a scroll-of-mail object.
  92.  *
  93.  *    The test driver code below can be used to check out potential new
  94.  * entries without rebuilding NetHack itself.  CC/DEFINE="TEST_DRIVER"
  95.  * Link it with hacklib.obj or nethack.olb/incl=hacklib (not nethack/lib).
  96.  */
  97.  
  98. static struct mail_info msg;    /* parse_*()'s return buffer */
  99. static char nam_cmd_buf[63],    /* maximum onamelth, size of ONAME(object) */
  100.         txt_buf[255+1];    /* same size as used for message buf[] */
  101.  
  102. /* try to decipher and categorize broadcast message text
  103. */
  104. static struct mail_info *
  105. parse_brdcst(buf)        /* called by parse_next_broadcast() */
  106. char *buf;            /* input: filtered broadcast text */
  107. {
  108.     int typ;
  109.     char *txt, *nam, *cmd;
  110. # ifdef SHELL        /* only parse if spawned commands are enabled */
  111.     register char *p, *q;
  112.     boolean is_jnet_send;
  113.     char cmd_buf[127+1], user[127+1], node[127+1], sentinel;
  114.  
  115.     /* Check these first; otherwise, their arbitrary text would enable
  116.     easy spoofing of some other message patterns.  Unfortunately,
  117.     any home-grown broadcast delivery program poses a similar risk. */
  118.     if (!strncmpi(buf, "reply received", 14)) goto other;
  119.     is_jnet_send = (sscanf(buf, "(%[^)])%s -%c", node, user, &sentinel) == 3);
  120.     if (is_jnet_send) goto jnet_send;
  121.  
  122.     /* scan the text more or less by brute force */
  123.     if ((q = strstri(buf, " mail")) != 0 ||    /* all known mail broadcasts */
  124.     !strncmpi(q = buf, "mail ", 5)) {    /* unexpected alternative */
  125.     typ = MSG_MAIL;
  126.     p = strstri(q, " from");
  127.     txt = p ? strcat(strcpy(txt_buf, "Mail for you"), p) : (char *) 0;
  128.  
  129.     if (!strncmpi(buf, "new mail", 8)) {
  130. /*
  131. New mail [on node FOO] from [SPAM::]BAR [\"personal_name\"] [\(HH:MM:SS\)]
  132. */
  133.         nam = "VMSmail";        /* assume VMSmail */
  134.         cmd = "MAIL";
  135.         if (txt && (p = strrchr(txt, '(')) > txt && /* discard time */
  136.         (--p, strspn(p, "0123456789( :.)") == strlen(p))) *p = '\0';
  137.     } else if (!strncmpi(buf, "new all-in-1", 12)) {
  138.         int i;
  139. /*
  140. New ALL-IN-1 MAIL message [on node FOO] from Personal Name \(BAR@SPAM\) [\(DD-MMM-YYYY HH:MM:SS\)]
  141. */
  142.         nam = "A1mail";
  143.         cmd = "A1M";
  144.         if (txt && (p = strrchr(txt, '(')) > txt && /* discard date+time */
  145.         sscanf(p-1," (%*d-%*[^-]-%*d %*d:%*d:%d) %c",&i,&sentinel) == 1)
  146.         *--p = '\0';
  147.     } else if (!strncmpi(buf, "software tools", 14)) {
  148. /*
  149. Software Tools mail has arrived on FOO from \'BAR\' [in SPAM]
  150. */
  151.         nam = "STmail";
  152.         cmd = "MSG";
  153.         if (txt && (p = strstri(p, " in ")) != 0)    /* specific folder */
  154.         cmd = strcat(strcpy(cmd_buf, "MSG +"), p + 4);
  155.     } else if (q - 2 >= buf && !strncmpi(q - 2, "mm", 2)) {
  156. /*
  157. {MultiNet\ |PMDF\/}MM mail has arrived on FOO from BAR\n
  158. [Subject: subject_text] (PMDF only)
  159. */
  160.         nam = "MMmail";        /* MultiNet's version of MM */
  161.         cmd = "MM";            /*{ perhaps "MM READ"? }*/
  162.     } else if (!strncmpi(buf, "wpmail:", 7)) {
  163. /*
  164. WPmail: New mail from BAR.  subject_text
  165. */
  166.         nam = "WPmail";        /* WordPerfect [sic] Office */
  167.         cmd = "OFFICE/MAIL";
  168.     } else if (!strncmpi(buf, "**m400 mail", 7)) {
  169. /*
  170. **M400 mail waiting**
  171. */
  172.         nam = "M400mail";        /* Messenger 400 [not seen] */
  173.         cmd = "M400";
  174.     } else {
  175.         /* not recognized, but presumed to be mail */
  176.         nam = "unknown mail";
  177.         cmd = "SPAWN";        /* generic escape back to DCL */
  178.         txt = (char *) 0;        /* don't rely on "from" info here */
  179.     }
  180.  
  181.     if (!txt) txt = strcat(strcpy(txt_buf, "Mail for you: "), buf);
  182.     /*
  183.      :    end of mail recognition; now check for call-type interruptions...
  184.      */
  185.     } else if ((q = strstri(buf, " phoning")) != 0) {
  186. /*
  187. BAR is phoning you [on FOO] \(HH:MM:SS\)
  188. */
  189.     typ = MSG_CALL;
  190.     nam = "Phone call";
  191.     cmd = "PHONE ANSWER";
  192.     if (!strncmpi(q + 8, " you", 4)) q += (8 + 4), *q = '\0';
  193.     txt = strcat(strcpy(txt_buf, "Do you hear ringing?  "), buf);
  194.     } else if ((q = strstri(buf, " talk-daemon")) != 0 ||
  195.            (q = strstri(buf, " talk_daemon")) != 0) {
  196. /*
  197. Message from TALK-DAEMON@FOO at HH:MM:SS\n
  198. Connection request by BAR@SPAM\n
  199. \[Respond with: TALK[/OLD] BAR@SPAM\]
  200. */
  201.     typ = MSG_CALL;
  202.     nam = "Talk request";        /* MultiNet's TALK and/or TALK/OLD */
  203.     cmd = "TALK";
  204.     if ((p = strstri(q, " by ")) != 0) {
  205.         txt = strcat(strcpy(txt_buf, "Talk request from"), p + 3);
  206.         if ((p = strstri(p, "respond with")) != 0) {
  207.         if (*(p-1) == '[') *(p-1) = '\0'; else *p = '\0'; /* terminate */
  208.         p += (sizeof "respond with" - sizeof "");
  209.         if (*p == ':') p++;
  210.         if (*p == ' ') p++;
  211.         cmd = strcpy(cmd_buf, p);    /* "TALK[/OLD] bar@spam" */
  212.         p = eos(cmd);
  213.         if (*--p == ']') *p = '\0';
  214.         }
  215.     } else
  216.         txt = strcat(strcpy(txt_buf, "Pardon the interruption: "), buf);
  217.     } else if (is_jnet_send) {    /* sscanf(,"(%[^)])%s -%c",,,)==3 */
  218. jnet_send:
  219. /*
  220. \(SPAM\)BAR - arbitrary_message_text (from BAR@SPAM)
  221. */
  222.     typ = MSG_CALL;
  223.     nam = "Bitnet noise";        /* RSCS/NJE message received via JNET */
  224.     Sprintf(cmd = cmd_buf, "XYZZY %s@%s", user, node);
  225.     /*{ perhaps just vanilla SEND instead of XYZZY? }*/
  226.     Sprintf(txt = txt_buf, "Message from %s@%s:%s", user, node,
  227.         &buf[1+strlen(node)+1+strlen(user)+2-1]);  /* "(node)user -" */
  228.     /*
  229.      :    end of call recognition; anything else is none-of-the-above...
  230.      */
  231.     } else {
  232. other:
  233. # endif /* SHELL */
  234. /* arbitrary broadcast: batch job completed, system shutdown imminent, &c */
  235.     typ = MSG_OTHER;
  236.     nam = (char *) 0; /*"captured broadcast message"*/
  237.     cmd = (char *) 0;
  238.     txt = strcat(strcpy(txt_buf, "Message for you: "), buf);
  239. # ifdef SHELL
  240.     }
  241.     /* Daemon in newmail() will append period when the text is displayed */
  242.     if ((p = eos(txt)) > txt && *--p == '.') *p = '\0';
  243.  
  244.     /* newmail() and readmail() assume that nam and cmd are concatenated */
  245.     if (nam) {        /* object name to attach to scroll of mail */
  246.     nam = strcpy(nam_cmd_buf, nam);
  247.     if (cmd) {    /* append command to name; readmail() requires it */
  248.         int len = sizeof nam_cmd_buf - sizeof "" - (strlen(nam) + 1);
  249.         cmd_buf[len] = '\0';    /* possibly truncate */
  250.         (void) strcat(nam, " ");
  251.         cmd = strcpy(eos(nam), cmd);
  252.     }
  253.     }
  254. # endif /* SHELL */
  255.     msg.message_typ  = typ;    /* simple index */
  256.     msg.display_txt  = txt;    /* text for daemon to pline() */
  257.     msg.object_nam   = nam;    /* 'name' for mail scroll */
  258.     msg.response_cmd = cmd;    /* command to spawn when scroll read */
  259.     return &msg;
  260. }
  261.  
  262. /* filter out non-printable characters and redundant noise
  263. */
  264. static void
  265. filter_brdcst(buf)        /* called by parse_next_broadcast() */
  266. register char *buf;        /* in: original text; out: filtered text */
  267. {
  268.     register char c, *p, *buf_p;
  269.  
  270.     /* filter the text; restrict consecutive spaces or dots to just two */
  271.     for (p = buf_p = buf; *buf_p; buf_p++) {
  272.     c = *buf_p & '\177';
  273.     if (c == ' ' || c == '\t' || c == '\n')
  274.         if (p == buf ||        /* ignore leading whitespace */
  275.         (p >= buf+2 && *(p-1) == ' ' && *(p-2) == ' ')) continue;
  276.         else c = ' ';
  277.     else if (c == '.' || c < ' ' || c == '\177')
  278.         if (p == buf ||        /* skip leading beeps & such */
  279.         (p >= buf+2 && *(p-1) == '.' && *(p-2) == '.')) continue;
  280.         else c = '.';
  281.     else if (c == '%' &&        /* trim %%% OPCOM verbosity %%% */
  282.         p >= buf+2 && *(p-1) == '%' && *(p-2) == '%') continue;
  283.     *p++ = c;
  284.     }
  285.     *p = '\0';            /* terminate, then strip trailing junk */
  286.     while (p > buf && (*--p == ' ' || *p == '.')) *p = '\0';
  287.     return;
  288. }
  289.  
  290. /* fetch the text of a captured broadcast, then mangle and decipher it
  291. */
  292. struct mail_info *
  293. parse_next_broadcast()        /* called by ckmailstatus(mail.c) */
  294. {
  295.     short length, msg_type;
  296.     $DESCRIPTOR(message, "");    /* string descriptor for buf[] */
  297.     struct mail_info *result = 0;
  298.     /* messages could actually be longer; let long ones be truncated */
  299.     char buf[255+1];
  300.  
  301.     message.dsc$a_pointer = buf,  message.dsc$w_length = sizeof buf - 1;
  302.     msg_type = length = 0;
  303.     SMG$GET_BROADCAST_MESSAGE(&pasteboard_id, &message, &length, &msg_type);
  304.     if (msg_type == MSG$_TRMBRDCST) {
  305.     buf[length] = '\0';
  306.     filter_brdcst(buf);        /* mask non-printable characters */
  307.     result = parse_brdcst(buf);    /* do the real work */
  308.     } else if (msg_type == MSG$_TRMHANGUP) {
  309.     (void) gsignal(SIGHUP);
  310.     }
  311.     return result;
  312. }
  313.  
  314. /* spit out any pending broadcast messages whenever we leave
  315. */
  316. static void
  317. flush_broadcasts()    /* called from disable_broadcast_trapping() */
  318. {
  319.     if (broadcasts > 0) {
  320.     short len, typ;
  321.     $DESCRIPTOR(msg, "");
  322.     char buf[512+1];
  323.  
  324.     msg.dsc$a_pointer = buf,  msg.dsc$w_length = sizeof buf - 1;
  325.     raw_print("");        /* print at least one line for wait_synch() */
  326.     do {
  327.         typ = len = 0;
  328.         SMG$GET_BROADCAST_MESSAGE(&pasteboard_id, &msg, &len, &typ);
  329.         if (typ == MSG$_TRMBRDCST) buf[len] = '\0',  raw_print(buf);
  330.     } while (--broadcasts);
  331.     wait_synch();        /* prompt with "Hit return to continue: " */
  332.     }
  333. }
  334.  
  335. /* AST routine called when the terminal's associated mailbox receives a message
  336. */
  337. static void
  338. broadcast_ast(dummy)        /* called asynchronously by terminal driver */
  339. int dummy;    /* not used */
  340. {
  341.     broadcasts++;
  342. }
  343.  
  344. /* initialize the broadcast manipulation code; SMG makes this easy
  345. */
  346. unsigned long init_broadcast_trapping()   /* called by setftty() [once only] */
  347. {
  348.     unsigned long sts, preserve_screen_flag = 1;
  349.  
  350.     /* we need a pasteboard to pass to the broadcast setup/teardown routines */
  351.     sts = SMG$CREATE_PASTEBOARD(&pasteboard_id, 0, 0, 0, &preserve_screen_flag);
  352.     if (!vms_ok(sts)) {
  353.     errno = EVMSERR,  vaxc$errno = sts;
  354.     raw_print("");
  355.     perror("?can't create SMG pasteboard for broadcast trapping");
  356.     wait_synch();
  357.     broadcasts = -1;    /* flag that trapping is currently broken */
  358.     }
  359.     return sts;
  360. }
  361.  
  362. /* set up the terminal driver to deliver $brkthru data to a mailbox device
  363. */
  364. unsigned long enable_broadcast_trapping()    /* called by setftty() */
  365. {
  366.     unsigned long sts = 1;
  367.  
  368.     if (broadcasts >= 0) {    /* (-1 => no pasteboard, so don't even try) */
  369.     /* register callback routine to be triggered when broadcasts arrive */
  370.     /* Note side effect:  also intercepts hangup notification. */
  371.     /* Another note:  TMPMBX privilege is required. */
  372.     sts = SMG$SET_BROADCAST_TRAPPING(&pasteboard_id, broadcast_ast, 0);
  373.     if (!vms_ok(sts)) {
  374.         errno = EVMSERR,  vaxc$errno = sts;
  375.         raw_print("");
  376.         perror("?can't enable broadcast trapping");
  377.         wait_synch();
  378.     }
  379.     }
  380.     return sts;
  381. }
  382.  
  383. /* return to 'normal'; $brkthru data goes straight to the terminal
  384. */
  385. unsigned long disable_broadcast_trapping()    /* called by settty() */
  386. {
  387.     unsigned long sts = 1;
  388.  
  389.     if (broadcasts >= 0) {
  390.     /* disable trapping; releases associated MBX so that SPAWN can work */
  391.     sts = SMG$DISABLE_BROADCAST_TRAPPING(&pasteboard_id);
  392.     if (!vms_ok(sts)) errno = EVMSERR,  vaxc$errno = sts;
  393.     flush_broadcasts();    /* don't hold on to any buffered ones */
  394.     }
  395.     return sts;
  396. }
  397. #else    /* MAIL */
  398.     /* simple stubs for non-mail configuration */
  399. unsigned long init_broadcast_trapping() { return 1; }
  400. unsigned long enable_broadcast_trapping() { return 1; }
  401. unsigned long disable_broadcast_trapping() { return 1; }
  402. #endif    /* MAIL */
  403.  
  404. /*----------------------------------------------------------------------*/
  405.  
  406. #ifdef TEST_DRIVER
  407.     /* (Take parse_next_broadcast for a spin. :-) */
  408.  
  409. volatile int broadcasts = 0;
  410.  
  411. void newmail(foo)
  412. struct mail_info *foo;
  413. {
  414. # define STRING(s) ((s) ? (s) : "<null>")
  415.     printf("\n\
  416.   message type = %d\n\
  417.   display text = \"%s\"\n\
  418.   object name  = \"%.*s\"\n\
  419.   response cmd = \"%s\"\n\
  420. ",      foo->message_typ, STRING(foo->display_txt),
  421.     (foo->object_nam && foo->response_cmd) ?
  422.         (foo->response_cmd - foo->object_nam - 1) :
  423.         strlen(STRING(foo->object_nam)),
  424.     STRING(foo->object_nam), STRING(foo->response_cmd));
  425. # undef STRING
  426. }
  427.  
  428. void ckmailstatus()
  429. {
  430.     struct mail_info *brdcst, *parse_next_broadcast();
  431.  
  432.     while (broadcasts > 0) {    /* process all trapped broadcasts [until] */
  433.     broadcasts--;
  434.     if ((brdcst = parse_next_broadcast()) != 0) {
  435.         newmail(brdcst);
  436.         break;        /* only handle one real message at a time */
  437.     } else
  438.         printf("\n--< non-broadcast encountered >--\n");
  439.     }
  440. }
  441.  
  442. int main()
  443. {
  444.     char dummy[BUFSIZ];
  445.  
  446.     init_broadcast_trapping();
  447.     enable_broadcast_trapping();
  448.     for (;;) {
  449.     ckmailstatus();
  450.     printf("> "), fflush(stdout);    /* issue a prompt */
  451.     if (!gets(dummy)) break;    /* wait for a response */
  452.     }
  453.     disable_broadcast_trapping();
  454.     return 1;
  455. }
  456.  
  457. void panic(s) char *s; { raw_print(s); exit(1); }
  458.  
  459. void raw_print(s) char *s; { puts(s); fflush(stdout); }
  460.  
  461. void wait_synch() { char dummy[BUFSIZ];
  462.   printf("\nPress <return> to continue: "); fflush(stdout); (void) gets(dummy);
  463. }
  464. #endif    /* TEST_DRIVER */
  465.  
  466. /*vmsmail.c*/
  467.