1 /* SCCS Id: @(#)mail.c 3.4 2002/01/13 */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /* NetHack may be freely redistributed. See license for details. */
11 * Notify user when new mail has arrived. Idea by Merlyn Leroy.
13 * The mail daemon can move with less than usual restraint. It can:
14 * - move diagonally from a door
15 * - use secret and closed doors
16 * - run through a monster ("Gangway!", etc.)
17 * - run over pools & traps
19 * Possible extensions:
20 * - Open the file MAIL and do fstat instead of stat for efficiency.
21 * (But sh uses stat, so this cannot be too bad.)
22 * - Examine the mail and produce a scroll of mail named "From somebody".
23 * - Invoke MAILREADER in such a way that only this single letter is read.
24 * - Do something to the text when the scroll is enchanted or cancelled.
25 * - Make the daemon always appear at a stairwell, and have it find a
28 * Note by Olaf Seibert: On the Amiga, we usually don't get mail. So we go
29 * through most of the effects at 'random' moments.
30 * Note by Paul Winner: The MSDOS port also 'fakes' the mail daemon at
34 STATIC_DCL boolean FDECL(md_start,(coord *));
35 STATIC_DCL boolean FDECL(md_stop,(coord *, coord *));
36 STATIC_DCL boolean FDECL(md_rush,(struct monst *,int,int));
37 STATIC_DCL void FDECL(newmail, (struct mail_info *));
39 extern char *viz_rmin, *viz_rmax; /* line-of-sight limits (vision.c) */
43 # if !defined(UNIX) && !defined(VMS) && !defined(LAN_MAIL)
53 /* DON'T trust all Unices to declare getpwuid() in <pwd.h> */
54 # if !defined(_BULL_SOURCE) && !defined(__sgi) && !defined(_M_UNIX)
55 # if !defined(SUNOS4) && !(defined(ULTRIX) && defined(__GNUC__))
56 /* DO trust all SVR4 to typedef uid_t in <sys/types.h> (probably to a long) */
57 # if defined(POSIX_TYPES) || defined(SVR4) || defined(HPUX)
58 extern struct passwd *FDECL(getpwuid,(uid_t));
60 extern struct passwd *FDECL(getpwuid,(int));
64 static struct stat omstat,nmstat;
65 static char *mailbox = (char *)0;
66 static long laststattime;
68 # if !defined(MAILPATH) && defined(AMS) /* Just a placeholder for AMS */
69 # define MAILPATH "/dev/null"
71 # if !defined(MAILPATH) && (defined(LINUX) || defined(__osf__))
72 # define MAILPATH "/var/spool/mail/"
74 # if !defined(MAILPATH) && defined(__FreeBSD__)
75 # define MAILPATH "/var/mail/"
77 # if !defined(MAILPATH) && (defined(BSD) || defined(ULTRIX))
78 # define MAILPATH "/usr/spool/mail/"
80 # if !defined(MAILPATH) && (defined(SYSV) || defined(HPUX))
81 # define MAILPATH "/usr/mail/"
87 if(!mailbox && !(mailbox = nh_getenv("MAIL"))) {
90 struct passwd ppasswd;
92 (void) memcpy(&ppasswd, getpwuid(getuid()), sizeof(struct passwd));
94 mailbox = (char *) alloc((unsigned) strlen(ppasswd.pw_dir)+sizeof(AMS_MAILBOX));
95 Strcpy(mailbox, ppasswd.pw_dir);
96 Strcat(mailbox, AMS_MAILBOX);
100 const char *pw_name = getpwuid(getuid())->pw_name;
101 mailbox = (char *) alloc(sizeof(MAILPATH)+strlen(pw_name));
102 Strcpy(mailbox, MAILPATH);
103 Strcat(mailbox, pw_name);
109 if(stat(mailbox, &omstat)){
110 # ifdef PERMANENT_MAILBOX
111 pline("Cannot get status of MAIL=\"%s\".", mailbox);
124 * Pick coordinates for a starting position for the mail daemon. Called
125 * from newmail() and newphone().
131 coord testcc; /* scratch coordinates */
132 int row; /* current row we are checking */
133 int lax; /* if TRUE, pick a position in sight. */
134 int dd; /* distance to current point */
135 int max_distance; /* max distance found so far */
138 * If blind and not telepathic, then it doesn't matter what we pick ---
139 * the hero is not going to see it anyway. So pick a nearby position.
141 if (Blind && !Blind_telepat) {
142 if (!enexto(startp, u.ux, u.uy, (struct permonst *) 0))
143 return FALSE; /* no good posiitons */
148 * Arrive at an up or down stairwell if it is in line of sight from the
151 if (couldsee(upstair.sx, upstair.sy)) {
152 startp->x = upstair.sx;
153 startp->y = upstair.sy;
156 if (couldsee(dnstair.sx, dnstair.sy)) {
157 startp->x = dnstair.sx;
158 startp->y = dnstair.sy;
163 * Try to pick a location out of sight next to the farthest position away
164 * from the hero. If this fails, try again, just picking the farthest
165 * position that could be seen. What we really ought to be doing is
166 * finding a path from a stairwell...
168 * The arrays viz_rmin[] and viz_rmax[] are set even when blind. These
169 * are the LOS limits for each row.
171 lax = 0; /* be picky */
174 for (row = 0; row < ROWNO; row++) {
175 if (viz_rmin[row] < viz_rmax[row]) {
176 /* There are valid positions on this row. */
177 dd = distu(viz_rmin[row],row);
178 if (dd > max_distance) {
182 startp->x = viz_rmin[row];
184 } else if (enexto(&testcc, (xchar)viz_rmin[row], row,
185 (struct permonst *) 0) &&
186 !cansee(testcc.x, testcc.y) &&
187 couldsee(testcc.x, testcc.y)) {
192 dd = distu(viz_rmax[row],row);
193 if (dd > max_distance) {
197 startp->x = viz_rmax[row];
199 } else if (enexto(&testcc, (xchar)viz_rmax[row], row,
200 (struct permonst *) 0) &&
201 !cansee(testcc.x,testcc.y) &&
202 couldsee(testcc.x, testcc.y)) {
211 if (max_distance < 0) {
213 lax = 1; /* just find a position */
223 * Try to choose a stopping point as near as possible to the starting
224 * position while still adjacent to the hero. If all else fails, try
225 * enexto(). Use enexto() as a last resort because enexto() chooses
226 * its point randomly, which is not what we want.
229 md_stop(stopp, startp)
230 coord *stopp; /* stopping position (we fill it in) */
231 coord *startp; /* starting positon (read only) */
233 int x, y, distance, min_distance = -1;
235 for (x = u.ux-1; x <= u.ux+1; x++)
236 for (y = u.uy-1; y <= u.uy+1; y++) {
237 if (!isok(x, y) || (x == u.ux && y == u.uy)) continue;
239 if (ACCESSIBLE(levl[x][y].typ) && !MON_AT(x,y)) {
240 distance = dist2(x,y,startp->x,startp->y);
241 if (min_distance < 0 || distance < min_distance ||
242 (distance == min_distance && rn2(2))) {
245 min_distance = distance;
250 /* If we didn't find a good spot, try enexto(). */
251 if (min_distance < 0 &&
252 !enexto(stopp, u.ux, u.uy, &mons[PM_MAIL_DAEMON]))
258 /* Let the mail daemon have a larger vocabulary. */
259 static NEARDATA const char *mail_text[] = {
264 #define md_exclamations() (mail_text[rn2(3)])
267 * Make the mail daemon run through the dungeon. The daemon will run over
268 * any monsters that are in its path, but will replace them later. Return
269 * FALSE if the md gets stuck in a position where there is a monster. Return
275 register int tx, ty; /* destination of mail daemon */
277 struct monst *mon; /* displaced monster */
278 register int dx, dy; /* direction counters */
279 int fx = md->mx, fy = md->my; /* current location */
280 int nfx = fx, nfy = fy, /* new location */
281 d1, d2; /* shortest distances */
284 * It is possible that the monster at (fx,fy) is not the md when:
285 * the md rushed the hero and failed, and is now starting back.
287 if (m_at(fx, fy) == md) {
288 remove_monster(fx, fy); /* pick up from orig position */
293 * At the beginning and exit of this loop, md is not placed in the
297 /* Find a good location next to (fx,fy) closest to (tx,ty). */
298 d1 = dist2(fx,fy,tx,ty);
299 for (dx = -1; dx <= 1; dx++) for(dy = -1; dy <= 1; dy++)
300 if ((dx || dy) && isok(fx+dx,fy+dy) &&
301 !IS_STWALL(levl[fx+dx][fy+dy].typ)) {
302 d2 = dist2(fx+dx,fy+dy,tx,ty);
310 /* Break if the md couldn't find a new position. */
311 if (nfx == fx && nfy == fy) break;
313 fx = nfx; /* this is our new position */
316 /* Break if the md reaches its destination. */
317 if (fx == tx && fy == ty) break;
319 if ((mon = m_at(fx,fy)) != 0) /* save monster at this position */
320 verbalize(md_exclamations());
321 else if (fx == u.ux && fy == u.uy)
322 verbalize("Excuse me.");
324 place_monster(md,fx,fy); /* put md down */
325 newsym(fx,fy); /* see it */
326 flush_screen(0); /* make sure md shows up */
327 delay_output(); /* wait a little bit */
329 /* Remove md from the dungeon. Restore original mon, if necessary. */
331 if ((mon->mx != fx) || (mon->my != fy))
332 place_worm_seg(mon, fx, fy);
334 place_monster(mon, fx, fy);
336 remove_monster(fx, fy);
341 * Check for a monster at our stopping position (this is possible, but
342 * very unlikely). If one exists, then have the md leave in disgust.
344 if ((mon = m_at(fx, fy)) != 0) {
345 place_monster(md, fx, fy); /* display md with text below */
347 verbalize("This place's too crowded. I'm outta here.");
349 if ((mon->mx != fx) || (mon->my != fy)) /* put mon back */
350 place_worm_seg(mon, fx, fy);
352 place_monster(mon, fx, fy);
358 place_monster(md, fx, fy); /* place at final spot */
361 delay_output(); /* wait a little bit */
366 /* Deliver a scroll of mail. */
370 struct mail_info *info;
374 boolean message_seen = FALSE;
376 /* Try to find good starting and stopping places. */
377 if (!md_start(&start) || !md_stop(&stop,&start)) goto give_up;
379 /* Make the daemon. Have it rush towards the hero. */
380 if (!(md = makemon(&mons[PM_MAIL_DAEMON], start.x, start.y, NO_MM_FLAGS)))
382 if (!md_rush(md, stop.x, stop.y)) goto go_back;
385 verbalize("%s, %s! %s.", Hello(md), plname, info->display_txt);
387 if (info->message_typ) {
388 struct obj *obj = mksobj(SCR_MAIL, FALSE, FALSE);
389 if (distu(md->mx,md->my) > 2)
391 display_nhwindow(WIN_MESSAGE, FALSE);
392 if (info->object_nam) {
393 obj = oname(obj, info->object_nam);
394 if (info->response_cmd) { /*(hide extension of the obj name)*/
395 int namelth = info->response_cmd - info->object_nam - 1;
396 if ( namelth <= 0 || namelth >= (int) obj->onamelth )
397 impossible("mail delivery screwed up");
399 *(ONAME(obj) + namelth) = '\0';
400 /* Note: renaming object will discard the hidden command. */
403 obj = hold_another_object(obj, "Oops!",
404 (const char *)0, (const char *)0);
407 /* zip back to starting location */
409 (void) md_rush(md, start.x, start.y);
411 /* deliver some classes of messages even if no daemon ever shows up */
413 if (!message_seen && info->message_typ == MSG_OTHER)
414 pline("Hark! \"%s.\"", info->display_txt);
417 # if !defined(UNIX) && !defined(VMS) && !defined(LAN_MAIL)
422 if (u.uswallow || !flags.biff) return;
423 if (mustgetmail < 0) {
424 #if defined(AMIGA) || defined(MSDOS) || defined(TOS)
425 mustgetmail=(moves<2000)?(100+rn2(2000)):(2000+rn2(3000));
429 if (--mustgetmail <= 0) {
430 static struct mail_info
431 deliver = {MSG_MAIL,"I have some mail for you",0,0};
442 static char *junk[] = {
443 "Please disregard previous letter.",
444 "Welcome to NetHack.",
446 "Only Amiga makes it possible.",
447 "CATS have all the answers.",
449 "Report bugs to <devteam@nethack.org>.",
450 "Invitation: Visit the NetHack web site at http://www.nethack.org!"
454 pline("Unfortunately you cannot see what it says.");
456 pline("It reads: \"%s\"", junk[rn2(SIZE(junk))]);
460 # endif /* !UNIX && !VMS && !LAN_MAIL */
467 if(!mailbox || u.uswallow || !flags.biff
469 || moves < laststattime + MAILCKFREQ
474 laststattime = moves;
475 if(stat(mailbox, &nmstat)){
476 # ifdef PERMANENT_MAILBOX
477 pline("Cannot get status of MAIL=\"%s\" anymore.", mailbox);
482 } else if(nmstat.st_mtime > omstat.st_mtime) {
483 if (nmstat.st_size) {
484 static struct mail_info deliver = {
485 # ifndef NO_MAILREADER
486 MSG_MAIL, "I have some mail for you",
488 /* suppress creation and delivery of scroll of mail */
489 MSG_OTHER, "You have some mail in the outside world",
495 getmailstatus(); /* might be too late ... */
504 # ifdef DEF_MAILREADER /* This implies that UNIX is defined */
505 register const char *mr = 0;
507 display_nhwindow(WIN_MESSAGE, FALSE);
508 if(!(mr = nh_getenv("MAILREADER")))
512 (void) execl(mr, mr, (char *)0);
513 terminate(EXIT_FAILURE);
516 # ifndef AMS /* AMS mailboxes are directories */
517 display_file(mailbox, TRUE);
519 # endif /* DEF_MAILREADER */
521 /* get new stat; not entirely correct: there is a small time
522 window where we do not see new mail */
530 extern NDECL(struct mail_info *parse_next_broadcast);
532 volatile int broadcasts = 0;
537 struct mail_info *brdcst;
539 if (u.uswallow || !flags.biff) return;
541 while (broadcasts > 0) { /* process all trapped broadcasts [until] */
543 if ((brdcst = parse_next_broadcast()) != 0) {
545 break; /* only handle one real message at a time */
554 # ifdef SHELL /* can't access mail reader without spawning subprocess */
555 const char *txt, *cmd;
556 char *p, buf[BUFSZ], qbuf[BUFSZ];
559 /* there should be a command hidden beyond the object name */
560 txt = otmp->onamelth ? ONAME(otmp) : "";
562 cmd = (len + 1 < otmp->onamelth) ? txt + len + 1 : (char *) 0;
563 if (!cmd || !*cmd) cmd = "SPAWN";
565 Sprintf(qbuf, "System command (%s)", cmd);
567 if (*buf != '\033') {
568 for (p = eos(buf); p > buf; *p = '\0')
569 if (*--p != ' ') break; /* strip trailing spaces */
570 if (*buf) cmd = buf; /* use user entered command */
571 if (!strcmpi(cmd, "SPAWN") || !strcmp(cmd, "!"))
572 cmd = (char *) 0; /* interactive escape */
574 vms_doshell(cmd, TRUE);
587 static int laststattime = 0;
589 if(u.uswallow || !flags.biff
591 || moves < laststattime + MAILCKFREQ
596 laststattime = moves;
597 if (lan_mail_check()) {
598 static struct mail_info deliver = {
599 # ifndef NO_MAILREADER
600 MSG_MAIL, "I have some mail for you",
602 /* suppress creation and delivery of scroll of mail */
603 MSG_OTHER, "You have some mail in the outside world",
619 # endif /* LAN_MAIL */