src/lib/commands/rea.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  rea.c: Read telegrams
00029  * 
00030  *  Known contributors to this file:
00031  *     Dave Pare
00032  *     Doug Hay, 1998
00033  *     Steve McClure, 1998-2000
00034  */
00035 
00036 #include <config.h>
00037 
00038 #include <ctype.h>
00039 #include <stdio.h>
00040 #include "commands.h"
00041 #include "match.h"
00042 #include "misc.h"
00043 #include "optlist.h"
00044 #include "tel.h"
00045 
00046 int
00047 rea(void)
00048 {
00049     static char *telnames[] = {
00050         /* must follow TEL_ defines in tel.h */
00051         "Telegram", "Announcement", "BULLETIN", "Production Report"
00052     };
00053     char *p;
00054     char *mbox;
00055     char mbox_buf[256];         /* Maximum path length */
00056     struct telstr tgm;
00057     FILE *telfp;
00058     int teles;
00059     int size;
00060     unsigned nbytes;
00061     char buf[1024];
00062     char msgbuf[4096];          /* UTF-8 */
00063     int lasttype;
00064     int lastcnum;
00065     time_t lastdate;
00066     int header;
00067     int filelen;
00068     char *kind;
00069     int n;
00070     int num = player->cnum;
00071     struct natstr *np = getnatp(player->cnum);
00072     time_t now;
00073     time_t then;
00074     time_t delta;
00075     int first = 1;
00076     int readit;
00077     int may_delete = 1; /* may messages be deleted? */
00078 
00079     now = time(NULL);
00080 
00081     if (*player->argp[0] == 'w') {
00082         kind = "announcement";
00083         if (player->argp[1] && isdigit(*player->argp[1])) {
00084             delta = days(atoi(player->argp[1]));
00085             then = now - delta;
00086             may_delete = 0;
00087         } else
00088             then = np->nat_annotim;
00089         mbox = annfil;
00090     } else {
00091         kind = "telegram";
00092         if (player->god && player->argp[1] &&
00093             (mineq(player->argp[1], "yes") == ME_MISMATCH) &&
00094             (mineq(player->argp[1], "no") == ME_MISMATCH)) {
00095             if ((n = natarg(player->argp[1], NULL)) < 0)
00096                 return RET_SYN;
00097             num = n;
00098             may_delete = 0;
00099         }
00100         mbox = mailbox(mbox_buf, num);
00101         clear_telegram_is_new(player->cnum);
00102     }
00103 
00104     if ((telfp = fopen(mbox, "rb+")) == 0) {
00105         logerror("telegram file %s", mbox);
00106         return RET_FAIL;
00107     }
00108     teles = 0;
00109     fseek(telfp, 0L, SEEK_SET);
00110     size = fsize(fileno(telfp));
00111   more:
00112     lastdate = 0;
00113     lastcnum = -1;
00114     lasttype = -1;
00115     while (fread(&tgm, sizeof(tgm), 1, telfp) == 1) {
00116         readit = 1;
00117         if (tgm.tel_length < 0) {
00118             logerror("bad telegram file header in %s", mbox);
00119             break;
00120         }
00121         if (tgm.tel_type < 0 || tgm.tel_type > TEL_LAST) {
00122             pr("Bad telegram header.  Skipping telegram...\n");
00123             readit = 0;
00124             goto skip;
00125         }
00126         if (*kind == 'a') {
00127             if (!player->god && (getrejects(tgm.tel_from, np) & REJ_ANNO)) {
00128                 readit = 0;
00129                 goto skip;
00130             }
00131             if (tgm.tel_date < then) {
00132                 readit = 0;
00133                 goto skip;
00134             }
00135         }
00136         if (first && *kind == 'a') {
00137             pr("\nAnnouncements since %s", ctime(&then));
00138             first = 0;
00139         }
00140         header = 0;
00141         if (tgm.tel_type != lasttype || tgm.tel_from != lastcnum)
00142             header++;
00143         if (abs((int)(tgm.tel_date - (long)lastdate)) > TEL_SECONDS)
00144             header++;
00145         if (header) {
00146             pr("\n> ");
00147             lastcnum = tgm.tel_from;
00148             lasttype = tgm.tel_type;
00149             pr("%s ", telnames[(int)tgm.tel_type]);
00150             if ((tgm.tel_type == TEL_NORM) ||
00151                 (tgm.tel_type == TEL_ANNOUNCE) ||
00152                 (tgm.tel_type == TEL_BULLETIN))
00153                 pr("from %s, (#%d)", cname(tgm.tel_from), tgm.tel_from);
00154             pr("  dated %s", ctime(&tgm.tel_date));
00155             lastdate = tgm.tel_date;
00156         }
00157         teles++;
00158       skip:
00159         while (tgm.tel_length > 0) {
00160             nbytes = tgm.tel_length;
00161             if (nbytes > sizeof(msgbuf) - 1)
00162                 nbytes = sizeof(msgbuf) - 1;
00163             fread(msgbuf, 1, nbytes, telfp);
00164             msgbuf[nbytes] = 0;
00165             if (readit)
00166                 uprnf(msgbuf);
00167             tgm.tel_length -= nbytes;
00168         }
00169     }
00170     if (teles > 0 && player->cnum == num && may_delete) {
00171         pr("\n");
00172         if (teles == 1) {
00173             if (chance(0.25))
00174                 p = "Forget this one? ";
00175             else
00176                 p = "Shall I burn it? ";
00177         } else {
00178             if (chance(0.25))
00179                 p = "Into the shredder, boss? ";
00180             else
00181                 p = "Can I throw away these old love letters? ";
00182         }
00183         p = getstarg(player->argp[1], p, buf);
00184         if (p && *p == 'y') {
00185             if ((filelen = fsize(fileno(telfp))) > size) {
00186                 pr("Wait a sec!  A new %s has arrived...\n", kind);
00187                 /* force stdio to re-read tel file */
00188                 (void)fflush(telfp);
00189                 (void)fseek(telfp, (long)size, SEEK_SET);
00190                 size = filelen;
00191                 now = time(NULL);
00192                 goto more;
00193             }
00194             if (*kind == 'a') {
00195                 np->nat_annotim = now;
00196                 putnat(np);
00197             } else {
00198                 /* Here, we just re-open the file for "w" only,
00199                    and that will wipe the file clean automatically */
00200                 (void)fclose(telfp);
00201                 telfp = fopen(mbox, "wb");
00202             }
00203         }
00204     }
00205     if (teles <= 0) {
00206         if (player->cnum == num)
00207             pr("No %ss for you at the moment...\n", kind);
00208         else
00209             pr("No %ss for %s at the moment...\n", kind, cname(num));
00210     }
00211     (void)fclose(telfp);
00212     if (np->nat_flags & NF_INFORM) {
00213         pr_inform(player, "\n");
00214         np->nat_tgms = 0;
00215         putnat(np);
00216     }
00217     return RET_OK;
00218 }

Generated on Fri Mar 28 11:01:12 2008 for empserver by  doxygen 1.5.2