musicmud-2.1.6/data/
musicmud-2.1.6/data/help/
musicmud-2.1.6/data/policy/
musicmud-2.1.6/data/wild/
musicmud-2.1.6/data/world/
musicmud-2.1.6/doc/
musicmud-2.1.6/src/ident/
musicmud-2.1.6/src/lua/
musicmud-2.1.6/src/lua/include/
musicmud-2.1.6/src/lua/src/lib/
musicmud-2.1.6/src/lua/src/lua/
musicmud-2.1.6/src/lua/src/luac/
/* 
 * MusicMUD - Boards module
 * Copyright (C) 1998-2003 Abigail Brady
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * 
 */
#include <dirent.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <ctype.h>

#include "musicmud.h"
#include "zoneload.h"
#include "util.h"
#include "verbs.h"
#include "State.h"
#include "Player.h"
#include "misc.h"
#include "util.h"
#include "pflags.h"
#include "paths.h"
#include "mailboard.h"
#include "href.h"

#define MODULE "news"

static const char *find_board(MudObject *where) {
  MudObject *o;
  int i;
  foreach(where->children, o, i) {
    if (o->get("newsgroup")) return o->get("newsgroup");
  }
  return 0;
}

static bool verb_scan(MudObject *who, int, const char **) {

  const char *board = find_board(who->owner);
  if (!board) { who->printf("There is no board here.\n"); return true; }
  
  MessageFile test(DATA_NEWS, board);
  
  if (test.count_messages()) {
    
    who->printf("%s\n", title_for(board, who).c_str());
    who->printf("^BNew No From           Subject\n");
    string mid = "^B";
    int width = columns(who)-1;
    int i =0;
    while (width>0) {
      if (i==3 || i==6 || i==21)
	mid += "^+";
      else
	mid  += "^-";
      width--;
      i++;
    }
    mid += "^n";
    who->printf("%s\n", mid.c_str());
    test.list_contents(who, "message ");
    who->printf("%s\n", footer_for(who).c_str());
  } else {
      who->printf("No postings have been made to the board.\n");
  }
  who->printf("%s", dollar_parse("See $(info board$) for how to work the board.\n").c_str());
  return true;
}

static bool verb_message(MudObject *who, int argc, const char **argv) {
  const char *board = find_board(who->owner);
  if (!board) { who->printf("There is no board here.\n"); return true; }
  int which = -1;
  if (argc >= 2) {
    char *e;
    which = strtol(argv[1], &e, 10);
    if (*e) {
      who->printf("Enter 'message' or 'message <id>'.\n");
      return true;
    }
  }
  Divert d(who, "message");
  MessageFile test(DATA_NEWS, board);
  if (!test.show_message(who, which)) {
    if (which==-1) {
      who->printf("No new messages.\n");
    } else {
      who->printf("No such message.\n");
    }
  }
  return true;
}

static void state_board(Player *who, const char *what) {
    PersonState *p = who->get_person_state();
    if (streq(what, ".") || streq(what, "**")) {
	
	MessageFile testing(DATA_NEWS, p->get("to"));
	testing.send_message(
			     who->get("name"),
			     who->id, 
			     p->get("subject"),
			     ctime(&now),
			     p->get("buffer"));

	
	if (streq(p->get("to"), "cs.misc")) {
	  MudObject *smith = MUD_ANNOUNCER;
	  if (smith) {
	    who->set_bflag(FL_LOGGEDIN, 0);
	    smith->interpretf("fakeint %#M^Y has placed a message on the board.", who);
	    who->set_bflag(FL_LOGGEDIN, 1);
	  }
	}
	
	who->pop_state();
	return;
    }
    if (streq(what, "!") || streq(what, "~")) {
	who->printf("OK. abandoning board posting.\n");
	who->pop_state();
	return;
    }
    if (*what == '*') {
	who->interpret(what+1);
	return;
    }
    string mailbuffer = p->get("buffer");
    mailbuffer += what;
    mailbuffer += "\n";
    p->set("buffer", mailbuffer.c_str());
}

static bool verb_write(MudObject *who, int argc, const char **argv) {
    const char *board = find_board(who->owner);
    if (!board) { who->printf("There is no board here.\n"); return true; }
    if (argc < 2) {
	who->printf("syntax : %s <subject>\n", argv[0]);
	return true;
    }
    if (!is_player(who)) {
	who->printf("Only players can do that.\n");
	return true;
    }

    string what = the_rest(argc, argv, 1);
    char *e = 0;
    strtol(what.c_str(), &e, 10);
    if (!*e) {
      who->printf("Specify a subject.\n");
      return true;
    }

    Player *p = (Player *)who;
    
    p->push_state("board");
    p->get_person_state()->set("buffer", "");
    p->get_person_state()->set("to", board);
    p->get_person_state()->set("subject", what.c_str());
    
    return true;
}

static bool verb_erase(MudObject *who, int argc, const char **argv) {
	const char *board = find_board(who->owner);
	if (!board) { who->printf("There is no board here.\n"); return true; }
	if (argc < 2) {
		who->printf("syntax : erase <number>\n");
		return true;
	}
	int which = atoi(argv[1]);
	MessageFile test(DATA_NEWS, board);
	if (!test.get_header(which, "owner")) {
	        who->printf("Cannot find message : %s\n", argv[1]);
                return true;
        }
	MessageFile testing(DATA_NEWS, board);
	string owner = testing.get_header(which, "owner");
	if (owner[0]) {
		bool candelete = false;
		if (streq(owner.c_str(), who->id)) candelete=true;
		if (who->get_priv(PFL_ERASEOTHERS)) candelete = true;
		if (!candelete) {
			who->printf("You can't delete messages belonging to others..\n");
		} else {
			
			testing.delete_message(which);
			who->printf("Deleted.\n");
			testing.list_contents(who, "message ");
		}
	} else {
		who->printf("Cannot find message : %s\n", argv[1]);
	}
	return true;
}

static bool verb_hstable(MudObject *who, int, const char **)
{
  MudObject *where = who->owner;
  if (!where)
    return true;
  MudObject *table = where->get_object("scoring");
  if (!table)
    return true;

  who->printf("%s\n\n", title_for("Scores", who).c_str());
  Object::intit i = table->ints.begin();
  while(i != table->ints.end()) {
    const char *key = i->first.c_str();
    if (strncmp(key,"$wins.",6)==0) {
      MudObject *qui = planet->get(key+6);
      string str = ssprintf("^p%#s^n", key+6);
      if (qui) 
	str = qui->get("name");
      int plays = table->get_int(ssprintf("$plays.%s", key+6).c_str());
      if (plays == -1) {
	who->printf("%20s %i\n", str.c_str(), i->second);
      } else {
	who->printf("%20s %i/%i\n", str.c_str(), i->second, plays);
      }
    }
    i++;
  }
  who->printf("\n%s\n", footer_for(who).c_str());
  
  return true;
}

#include "verbmodule.h"
void startup() {

AUTO_VERB(hstable, 6, 0, PFL_NONE);

AUTO_VERB(scan, 3, 0, PFL_NONE);
AUTO_VERB(erase, 2, 0, PFL_NONE);
AUTO_VERB(message, 2, 0, PFL_NONE);

ADD_ALIAS(peruse, 2, message);

AUTO_VERB(write, 2, 0, PFL_BOARD);

ADD_STATE("board", "Board> ^n^e", state_board, false);
}