/**
 * This handler will do any mud base manipulations of the twiki
 * database.
 * @author Pinkfish
 * @started Wed May 24 17:19:06 PDT 2000
 */
#include <board.h>
void do_check_changes();
#define SAVE_FILE "/save/twiki"
#define CHECK_TIME (7 * 24 * 60 * 60)
private int _last_check;
void create() {
   seteuid(getuid());
   restore_object(SAVE_FILE);
   if (_last_check + CHECK_TIME < time()) {
      do_check_changes();
   }
   call_out("do_check_changes", _last_check + CHECK_TIME - time());
} /* setup() */
/**
 * This method prints out any changes since the specified limit.
 * @param limit the time after which to show any changes
 * @return the changes since that time
 */
string find_changes(int limit) {
   string* lines;
   int i;
   int tim;
   string page;
   string author;
   mixed* bits;
   mapping changes_pages;
   string ret;
   lines = explode(read_file("/twiki/data/Main/.changes"), "\n");
   ret = "Changes since " + ctime(limit) + " in\n"
         "http://discworld.imaginary.com/twiki/bin/view/Main/WebHome\n\n";
   changes_pages = ([ ]);
   for (i = 1; i < sizeof(lines); i++) {
      bits = reg_assoc(lines[<i], ({ "([a-zA-Z][a-zA-Z0-9]+)", "[0-9]+" }), ({ 1, 2 }) );
      if (sizeof(bits[0]) == 7) {
         page = bits[0][1];
         author = bits[0][3];
         sscanf(bits[0][5], "%d", tim);
         if (tim > limit) {
            //
            // Ignore all the user page changes...
            //
            if (page[0..4] != "TWiki" &&
                author != "PeterThoeny" &&
                page != author &&
                page != "WebPreferences") {
               //
               // Read the page and determine from that.
               //
               if (file_size("/twiki/data/Main/" + page + ".txt") > 0 &&
                   strsrch(read_file("/twiki/data/Main/" + page + ".txt"),
                           "\t* Login Name:") == -1) {
                  if (!changes_pages[page]) {
                     changes_pages[page] = ({ });
                  }
                  if (member_array(author, changes_pages[page]) == -1) {
                     changes_pages[page] += ({ author });
                  }
               }
            }
         } else {
            break;
         }
      }
   }
   foreach (page in sort_array(keys(changes_pages), 1)) {
      ret += sprintf("%-30s changed by %s\n", page,
                      query_multiple_short(changes_pages[page]));
   }
   return ret;
} /* find_changes() */
/**
 * This method finds the specified file and shows it to us.
 * @param fname the file name to find
 * @return the text of the file, 0 if the file is not found
 */
string query_file(string fname) {
   if (strsrch(fname, ".") > 0) {
      fname = "/twiki/data/" + replace_string(fname, ".", "/") + ".txt";
   } else {
      fname = "/twiki/data/Main/" + fname + ".txt";
   }
   if (sizeof(stat(fname))) {
      return read_file(fname);
   }
   return 0;
} /* query_file() */
/** @ignore yes */
void do_check_changes() {
   BOARD_HAND->add_message("creator", "Twiki Database",
                           "Changes in the Twiki database",
                           find_changes(_last_check));
   _last_check = time();
   save_object(SAVE_FILE);
   call_out("do_check_changes", _last_check + CHECK_TIME - time());
} /* do_changes() */