untermud/DOC/
untermud/DOC/U/
untermud/DOC/U/U-examples/
untermud/DOC/internals/
untermud/DOC/wizard/
untermud/MISC/
untermud/MISC/dbchk/
untermud/RWHO/
untermud/RWHO/rwhod/
/*
    Copyright (C) 1991, Marcus J. Ranum. All rights reserved.
*/

/* configure all options BEFORE including system stuff. */
#include    "config.h"
#include    "mud.h"
#include    "vars.h"

static int deletepost(char *who, char *aswho, int num);
static int post(char *who, char *message);

static int deletepost (who, aswho, num)
char *who;
char *aswho;
int num;
{
  char nfbuf[640];

  if (!ut_flagged (aswho, var_wiz)) {
    say (who, "Only a wizard can delete a news item.\n", (char *) 0);
    return (UERR_PERM);
  }
  (void) sprintf (nfbuf, "NEWS/%d", num);
  if (unlink (nfbuf)) {
    say (who, "Could not delete news item!\n", (char *) 0);
    return (UERR_FATAL);
  }
  say (who, "Deleted.\n", (char *) 0);
  return (UERR_NONE);
}

static int post (who, message)
char *who;
char *message;
{
  FILE *fp;
  int newart;
  char nfbuf[640];
  time_t now;
  extern char *ctime ();

  /* access and increment the system top article ID */
  if (ut_getnum (system_object, var_newsart, &newart))
    newart = 0;

  newart = newart + 1;

  /* try to open the file for output */
  (void) sprintf (nfbuf, "NEWS/%d", newart);
  if ((fp = fopen (nfbuf, "wb")) == (FILE *) 0) {
    say (who, "Cannot create new article\n", (char *) 0);
    return (UERR_FATAL);
  }

  /* if the file open worked, increment the system article count. */
  if (ut_setnum (who, system_object, var_newsart, newart)) {
    (void) fclose (fp);
    (void) unlink (nfbuf);
    say (who, "System error. Could not post.\n", (char *) 0);
    return (UERR_FATAL);
  }

  /* we now have the article # and the file open. go for it. */
  (void) time (&now);

  /* Grod, die. */
  if (message[0] == '#')
    message[0] = ' ';

  /* naughty, naughty */
  (void) fprintf (fp, " -- Article #%d, posted by %s(%s), %s%s\n",
    newart, ut_name (who), who, ctime (&now), message);

  (void) fclose (fp);
  say (who, "Posted.\n", (char *) 0);
  return (UERR_NONE);
}



/* ARGSUSED */
int cmd_NEWS (int argc, char *argv[], char *who, char *aswho)
{
  int anum;
  int nnum;
  int topart;
  char *ap;
  char nfbuf[640];
  char nxbuf[64];
  int setflg = 0;

  if ((ap =
      ut_getatt (who, 0, typ_int, var_newsart, (char *) 0)) != (char *) 0)
    anum = atoi (ap);
  else
    anum = 0;


  if (argc == 1)
    nnum = anum + 1, setflg++;
  else if (argc == 2 && !strncmp (argv[1], "next", strlen (argv[1])))
    nnum = anum + 1, setflg++;
  else if (argc == 2 && !strncmp (argv[1], "previous", strlen (argv[1])))
    nnum = anum - 1;
  else if (argc >= 2 && !strncmp (argv[1], "post", strlen (argv[1]))) {
    if (strcmp (who, aswho)) {
      say (who, "sorry. can't post NEWS for someone\n", (char *) 0);
      return (UERR_PERM);
    }
    if (argc != 3) {
      say (who, "Usage: ", argv[0], " post message-text\n", (char *) 0);
      return (UERR_BADPARM);
    }
    return (post (who, argv[2]));
  } else if (argc >= 2 && !strncmp (argv[1], "goto", strlen (argv[1]))) {
    if (argc != 3) {
      say (who, "Usage: ", argv[0], " goto message-number\n", (char *) 0);
      return (UERR_BADPARM);
    }
    nnum = atoi (argv[2]);
    setflg++;
  } else if (argc >= 2 && !strncmp (argv[1], "delete", strlen (argv[1]))) {
    if (argc != 3) {
      say (who, "Usage: ", argv[0], " delete message-number\n", (char *) 0);
      return (UERR_BADPARM);
    }
    nnum = atoi (argv[2]);
    return (deletepost (who, aswho, nnum));
  } else if (argc == 2 && isdigit (argv[1][0]))
    nnum = atoi (argv[1]);
  else {
    say (who, argv[0], " [next]\n", (char *) 0);
    say (who, argv[0], " previous\n", (char *) 0);
    say (who, argv[0], " post message-text\n", (char *) 0);
    say (who, argv[0], " delete message-number\n", (char *) 0);
    say (who, argv[0], " [goto] message-number\n", (char *) 0);
    return (UERR_NONE);
  }

  if (nnum <= 0) {
    say (who, itoa (nnum, nxbuf, 10), " isn't a valid article.\n",
      (char *) 0);
    return (UERR_BADOID);
  }

  if (ut_getnum (system_object, var_newsart, &topart)) {
    say (who, "There's no news.\n", (char *) 0);
    return (UERR_NONE);
  }
  do {
    (void) sprintf (nfbuf, "NEWS/%d", nnum);
    if (say_file (who, nfbuf)) {
      say (who, "no article ", itoa (nnum, nxbuf, 10), "\n", (char *) 0);
      nnum++;
    } else {
      say (who, " -- End Article ", itoa (nnum, nxbuf, 10), "\n", (char *) 0);
      break;
    }
  } while (nnum <= topart && setflg);

  nnum = (nnum > topart ? topart : nnum);

  /* update ".newsrc" */
  if (setflg) {
    if (ut_set (who, who, typ_int, var_newsart, itoa (nnum, nxbuf, 10)))
      return (UERR_FATAL);
  }
  return (UERR_NONE);
}