1 /* NetHack 3.6 mail.c $NHDT-Date: 1436754892 2015/07/13 02:34:52 $ $NHDT-Branch: master $:$NHDT-Revision: 1.20 $ */
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
25 * - Do something to the text when the scroll is enchanted or cancelled.
26 * - Make the daemon always appear at a stairwell, and have it find a
29 * Note by Olaf Seibert: On the Amiga, we usually don't get mail. So we go
30 * through most of the effects at 'random' moments.
31 * Note by Paul Winner: The MSDOS port also 'fakes' the mail daemon at
35 STATIC_DCL boolean FDECL(md_start, (coord *));
36 STATIC_DCL boolean FDECL(md_stop, (coord *, coord *));
37 STATIC_DCL boolean FDECL(md_rush, (struct monst *, int, int));
38 STATIC_DCL void FDECL(newmail, (struct mail_info *));
40 extern char *viz_rmin, *viz_rmax; /* line-of-sight limits (vision.c) */
42 #if !defined(UNIX) && !defined(VMS)
49 /* DON'T trust all Unices to declare getpwuid() in <pwd.h> */
50 #if !defined(_BULL_SOURCE) && !defined(__sgi) && !defined(_M_UNIX)
51 #if !defined(SUNOS4) && !(defined(ULTRIX) && defined(__GNUC__))
52 /* DO trust all SVR4 to typedef uid_t in <sys/types.h> (probably to a long) */
53 #if defined(POSIX_TYPES) || defined(SVR4) || defined(HPUX)
54 extern struct passwd *FDECL(getpwuid, (uid_t));
56 extern struct passwd *FDECL(getpwuid, (int));
60 static struct stat omstat, nmstat;
61 static char *mailbox = (char *) 0;
62 static long laststattime;
64 #if !defined(MAILPATH) && defined(AMS) /* Just a placeholder for AMS */
65 #define MAILPATH "/dev/null"
67 #if !defined(MAILPATH) && (defined(LINUX) || defined(__osf__))
68 #define MAILPATH "/var/spool/mail/"
70 #if !defined(MAILPATH) && defined(__FreeBSD__)
71 #define MAILPATH "/var/mail/"
73 #if !defined(MAILPATH) && (defined(BSD) || defined(ULTRIX))
74 #define MAILPATH "/usr/spool/mail/"
76 #if !defined(MAILPATH) && (defined(SYSV) || defined(HPUX))
77 #define MAILPATH "/usr/mail/"
83 if (!mailbox && !(mailbox = nh_getenv("MAIL"))) {
86 struct passwd ppasswd;
88 (void) memcpy(&ppasswd, getpwuid(getuid()), sizeof(struct passwd));
90 mailbox = (char *) alloc((unsigned) strlen(ppasswd.pw_dir)
91 + sizeof(AMS_MAILBOX));
92 Strcpy(mailbox, ppasswd.pw_dir);
93 Strcat(mailbox, AMS_MAILBOX);
97 const char *pw_name = getpwuid(getuid())->pw_name;
98 mailbox = (char *) alloc(sizeof(MAILPATH) + strlen(pw_name));
99 Strcpy(mailbox, MAILPATH);
100 Strcat(mailbox, pw_name);
106 if (stat(mailbox, &omstat)) {
107 #ifdef PERMANENT_MAILBOX
108 pline("Cannot get status of MAIL=\"%s\".", mailbox);
118 * Pick coordinates for a starting position for the mail daemon. Called
119 * from newmail() and newphone().
125 coord testcc; /* scratch coordinates */
126 int row; /* current row we are checking */
127 int lax; /* if TRUE, pick a position in sight. */
128 int dd; /* distance to current point */
129 int max_distance; /* max distance found so far */
132 * If blind and not telepathic, then it doesn't matter what we pick ---
133 * the hero is not going to see it anyway. So pick a nearby position.
135 if (Blind && !Blind_telepat) {
136 if (!enexto(startp, u.ux, u.uy, (struct permonst *) 0))
137 return FALSE; /* no good positions */
142 * Arrive at an up or down stairwell if it is in line of sight from the
145 if (couldsee(upstair.sx, upstair.sy)) {
146 startp->x = upstair.sx;
147 startp->y = upstair.sy;
150 if (couldsee(dnstair.sx, dnstair.sy)) {
151 startp->x = dnstair.sx;
152 startp->y = dnstair.sy;
157 * Try to pick a location out of sight next to the farthest position away
158 * from the hero. If this fails, try again, just picking the farthest
159 * position that could be seen. What we really ought to be doing is
160 * finding a path from a stairwell...
162 * The arrays viz_rmin[] and viz_rmax[] are set even when blind. These
163 * are the LOS limits for each row.
165 lax = 0; /* be picky */
168 for (row = 0; row < ROWNO; row++) {
169 if (viz_rmin[row] < viz_rmax[row]) {
170 /* There are valid positions on this row. */
171 dd = distu(viz_rmin[row], row);
172 if (dd > max_distance) {
176 startp->x = viz_rmin[row];
178 } else if (enexto(&testcc, (xchar) viz_rmin[row], row,
179 (struct permonst *) 0)
180 && !cansee(testcc.x, testcc.y)
181 && couldsee(testcc.x, testcc.y)) {
186 dd = distu(viz_rmax[row], row);
187 if (dd > max_distance) {
191 startp->x = viz_rmax[row];
193 } else if (enexto(&testcc, (xchar) viz_rmax[row], row,
194 (struct permonst *) 0)
195 && !cansee(testcc.x, testcc.y)
196 && couldsee(testcc.x, testcc.y)) {
204 if (max_distance < 0) {
206 lax = 1; /* just find a position */
216 * Try to choose a stopping point as near as possible to the starting
217 * position while still adjacent to the hero. If all else fails, try
218 * enexto(). Use enexto() as a last resort because enexto() chooses
219 * its point randomly, which is not what we want.
222 md_stop(stopp, startp)
223 coord *stopp; /* stopping position (we fill it in) */
224 coord *startp; /* starting position (read only) */
226 int x, y, distance, min_distance = -1;
228 for (x = u.ux - 1; x <= u.ux + 1; x++)
229 for (y = u.uy - 1; y <= u.uy + 1; y++) {
230 if (!isok(x, y) || (x == u.ux && y == u.uy))
233 if (accessible(x, y) && !MON_AT(x, y)) {
234 distance = dist2(x, y, startp->x, startp->y);
235 if (min_distance < 0 || distance < min_distance
236 || (distance == min_distance && rn2(2))) {
239 min_distance = distance;
244 /* If we didn't find a good spot, try enexto(). */
245 if (min_distance < 0 && !enexto(stopp, u.ux, u.uy, &mons[PM_MAIL_DAEMON]))
251 /* Let the mail daemon have a larger vocabulary. */
252 static NEARDATA const char *mail_text[] = { "Gangway!", "Look out!",
254 #define md_exclamations() (mail_text[rn2(3)])
257 * Make the mail daemon run through the dungeon. The daemon will run over
258 * any monsters that are in its path, but will replace them later. Return
259 * FALSE if the md gets stuck in a position where there is a monster. Return
265 register int tx, ty; /* destination of mail daemon */
267 struct monst *mon; /* displaced monster */
268 register int dx, dy; /* direction counters */
269 int fx = md->mx, fy = md->my; /* current location */
270 int nfx = fx, nfy = fy, /* new location */
271 d1, d2; /* shortest distances */
274 * It is possible that the monster at (fx,fy) is not the md when:
275 * the md rushed the hero and failed, and is now starting back.
277 if (m_at(fx, fy) == md) {
278 remove_monster(fx, fy); /* pick up from orig position */
283 * At the beginning and exit of this loop, md is not placed in the
287 /* Find a good location next to (fx,fy) closest to (tx,ty). */
288 d1 = dist2(fx, fy, tx, ty);
289 for (dx = -1; dx <= 1; dx++)
290 for (dy = -1; dy <= 1; dy++)
291 if ((dx || dy) && isok(fx + dx, fy + dy)
292 && !IS_STWALL(levl[fx + dx][fy + dy].typ)) {
293 d2 = dist2(fx + dx, fy + dy, tx, ty);
301 /* Break if the md couldn't find a new position. */
302 if (nfx == fx && nfy == fy)
305 fx = nfx; /* this is our new position */
308 /* Break if the md reaches its destination. */
309 if (fx == tx && fy == ty)
312 if ((mon = m_at(fx, fy)) != 0) /* save monster at this position */
313 verbalize1(md_exclamations());
314 else if (fx == u.ux && fy == u.uy)
315 verbalize("Excuse me.");
317 place_monster(md, fx, fy); /* put md down */
318 newsym(fx, fy); /* see it */
319 flush_screen(0); /* make sure md shows up */
320 delay_output(); /* wait a little bit */
322 /* Remove md from the dungeon. Restore original mon, if necessary. */
324 if ((mon->mx != fx) || (mon->my != fy))
325 place_worm_seg(mon, fx, fy);
327 place_monster(mon, fx, fy);
329 remove_monster(fx, fy);
334 * Check for a monster at our stopping position (this is possible, but
335 * very unlikely). If one exists, then have the md leave in disgust.
337 if ((mon = m_at(fx, fy)) != 0) {
338 place_monster(md, fx, fy); /* display md with text below */
340 verbalize("This place's too crowded. I'm outta here.");
342 if ((mon->mx != fx) || (mon->my != fy)) /* put mon back */
343 place_worm_seg(mon, fx, fy);
345 place_monster(mon, fx, fy);
351 place_monster(md, fx, fy); /* place at final spot */
354 delay_output(); /* wait a little bit */
359 /* Deliver a scroll of mail. */
363 struct mail_info *info;
367 boolean message_seen = FALSE;
369 /* Try to find good starting and stopping places. */
370 if (!md_start(&start) || !md_stop(&stop, &start))
373 /* Make the daemon. Have it rush towards the hero. */
374 if (!(md = makemon(&mons[PM_MAIL_DAEMON], start.x, start.y, NO_MM_FLAGS)))
376 if (!md_rush(md, stop.x, stop.y))
380 verbalize("%s, %s! %s.", Hello(md), plname, info->display_txt);
382 if (info->message_typ) {
383 struct obj *obj = mksobj(SCR_MAIL, FALSE, FALSE);
385 if (info->object_nam)
386 obj = oname(obj, info->object_nam);
387 if (info->response_cmd)
388 new_omailcmd(obj, info->response_cmd);
390 if (distu(md->mx, md->my) > 2)
392 display_nhwindow(WIN_MESSAGE, FALSE);
393 obj = hold_another_object(obj, "Oops!", (const char *) 0,
397 /* zip back to starting location */
399 (void) md_rush(md, start.x, start.y);
401 /* deliver some classes of messages even if no daemon ever shows up */
403 if (!message_seen && info->message_typ == MSG_OTHER)
404 pline("Hark! \"%s.\"", info->display_txt);
407 #if !defined(UNIX) && !defined(VMS)
412 if (u.uswallow || !flags.biff)
414 if (mustgetmail < 0) {
415 #if defined(AMIGA) || defined(MSDOS) || defined(TOS)
416 mustgetmail = (moves < 2000) ? (100 + rn2(2000)) : (2000 + rn2(3000));
420 if (--mustgetmail <= 0) {
421 static struct mail_info deliver = { MSG_MAIL,
422 "I have some mail for you", 0,
434 static char *junk[] = {
435 NULL, /* placeholder for "Report bugs to <devteam@nethack.org>.", */
436 "Please disregard previous letter.", "Welcome to NetHack.",
438 "Only Amiga makes it possible.", "CATS have all the answers.",
440 "This mail complies with the Yendorian Anti-Spam Act (YASA)",
441 "Please find enclosed a small token to represent your Owlbear",
442 "**FR33 P0T10N 0F FULL H34L1NG**",
443 "Please return to sender (Asmodeus)",
444 "Buy a potion of gain level for only $19.99! Guaranteed to be blessed!",
445 "Invitation: Visit the NetHack web site at http://www.nethack.org!"
448 /* XXX replace with more general substitution code and add local
449 * contact message. Also use DEVTEAM_URL */
450 if (junk[0] == NULL) {
451 #define BUGS_FORMAT "Report bugs to <%s>."
452 /* +2 from '%s' suffices as substitute for usual +1 for terminator */
453 junk[0] = (char *) alloc(strlen(BUGS_FORMAT) + strlen(DEVTEAM_EMAIL));
454 Sprintf(junk[0], BUGS_FORMAT, DEVTEAM_EMAIL);
458 pline("Unfortunately you cannot see what it says.");
460 pline("It reads: \"%s\"", junk[rn2(SIZE(junk))]);
463 #endif /* !UNIX && !VMS */
470 if (!mailbox || u.uswallow || !flags.biff
472 || moves < laststattime + MAILCKFREQ
477 laststattime = moves;
478 if (stat(mailbox, &nmstat)) {
479 #ifdef PERMANENT_MAILBOX
480 pline("Cannot get status of MAIL=\"%s\" anymore.", mailbox);
485 } else if (nmstat.st_mtime > omstat.st_mtime) {
486 if (nmstat.st_size) {
487 static struct mail_info deliver = {
488 #ifndef NO_MAILREADER
489 MSG_MAIL, "I have some mail for you",
491 /* suppress creation and delivery of scroll of mail */
492 MSG_OTHER, "You have some mail in the outside world",
498 getmailstatus(); /* might be too late ... */
507 #ifdef DEF_MAILREADER /* This implies that UNIX is defined */
508 register const char *mr = 0;
510 display_nhwindow(WIN_MESSAGE, FALSE);
511 if (!(mr = nh_getenv("MAILREADER")))
515 (void) execl(mr, mr, (char *) 0);
516 terminate(EXIT_FAILURE);
519 #ifndef AMS /* AMS mailboxes are directories */
520 display_file(mailbox, TRUE);
522 #endif /* DEF_MAILREADER */
524 /* get new stat; not entirely correct: there is a small time
525 window where we do not see new mail */
533 extern NDECL(struct mail_info *parse_next_broadcast);
535 volatile int broadcasts = 0;
540 struct mail_info *brdcst;
542 if (u.uswallow || !flags.biff)
545 while (broadcasts > 0) { /* process all trapped broadcasts [until] */
547 if ((brdcst = parse_next_broadcast()) != 0) {
549 break; /* only handle one real message at a time */
558 #ifdef SHELL /* can't access mail reader without spawning subprocess */
559 const char *txt, *cmd;
560 char *p, buf[BUFSZ], qbuf[BUFSZ];
563 /* there should be a command in OMAILCMD */
569 if (has_omailcmd(otmp))
570 cmd = OMAILCMD(otmp);
574 Sprintf(qbuf, "System command (%s)", cmd);
576 if (*buf != '\033') {
577 for (p = eos(buf); p > buf; *p = '\0')
579 break; /* strip trailing spaces */
581 cmd = buf; /* use user entered command */
582 if (!strcmpi(cmd, "SPAWN") || !strcmp(cmd, "!"))
583 cmd = (char *) 0; /* interactive escape */
585 vms_doshell(cmd, TRUE);