/* errors command, trial out by Turrican for a commands daemon. */
#include <log.h>
#include <board.h>
#include <mail.h>
class bugs {
int bug_num;
int bugs;
string error;
string file;
string temp_body;
string ed_file;
int changed;
}
#define BUG_NUM ((class bugs)globvars[this_player()])->bug_num
#define BUGS ((class bugs)globvars[this_player()])->bugs
#define ERROR ((class bugs)globvars[this_player()])->error
#define FILE ((class bugs)globvars[this_player()])->file
#define TEMP_BODY ((class bugs)globvars[this_player()])->temp_body
#define ED_FILE ((class bugs)globvars[this_player()])->ed_file
#define CHANGED ((class bugs)globvars[this_player()])->changed
private nosave mapping globvars;
protected string get_bug(int i);
protected void request();
protected void create() {
globvars = ([]);
}
mixed cmd(string str) {
string temp, *temps;
globvars[this_player()] = new(class bugs);
if(!str)
str = this_player()->query_current_path();
if(str[<1] != '/')
str += "/";
str += ERROR_LOG;
ERROR = read_file(str);
FILE = str;
if (!ERROR) {
printf("No error found in this dir.\n");
map_delete(globvars, this_player());
} else {
rm(TEMP_MAIL_LIST);
rm(TEMP_FORWARD_LIST);
temps = explode( ERROR, "\n" );
temp = temps[ sizeof( temps ) - 1 ];
sscanf(temp, ".END %d", BUGS);
BUG_NUM = 1;
printf( "%s", get_bug( BUG_NUM ) );
request();
}
return 1;
}
protected string get_bug(int i) {
int rtime;
string fmt, bug, ret;
string title, filename;
string ftime, fname;
string time, name;
if(BUGS == 0 || i > BUGS)
return 0;
fmt = "%*s.START " + i + "\n%s.END " + i;
sscanf(ERROR, fmt, bug);
sscanf(bug, "%s\n%s\n%s\n%s\n%s\n%s\n%s",title,filename,ftime,fname,
time,name,bug);
ret = sprintf( "%s\n", title );
ret += sprintf( "%s\n", filename );
if(ftime != "") {
ret += sprintf( "Forwarded from : %s\n", capitalize( fname ) );
sscanf(ftime, "%d", rtime);
ret += sprintf( "at : %s\n", ctime( rtime ) );
}
ret += sprintf( "Made by : %s\n", capitalize( name ) );
sscanf(time, "%d", rtime);
ret += sprintf( "at : %s\n", ctime( rtime ) );
ret += bug;
return ret;
}
protected void request() {
if (BUGS > 0)
printf("[1-%d] (%d) ", BUGS, BUG_NUM);
else
printf("[No bugs] ");
printf("Choose from EBUFDNPGQX, H for help. ");
input_to("commands");
}
void delete_bug(int n) {
int i;
string fmt, bug, trash;
fmt = "%s.START " + n + "\n%s.END " + n + "\n%s";
sscanf(ERROR, fmt, ERROR, bug, trash);
ERROR += trash;
for(i=n+1;i<BUGS+1;i++) {
reset_eval_cost();
fmt = "%s.START " + i + "\n%s.END " + i + "\n%s";
sscanf(ERROR, fmt, ERROR, bug, trash);
ERROR += ".START "+(i-1)+"\n"+bug+".END "+(i-1)+"\n"+trash;
}
BUGS--;
if (BUG_NUM > BUGS)
BUG_NUM--;
CHANGED = 1;
}
void end_of_mail(string body) {
string name, bug, rtime;
if(body && body != "") {
sscanf(TEMP_BODY, "%s %s %s", name, rtime, bug);
if(sscanf(body, "%s$report$%s", body, TEMP_BODY) == 2)
body += bug + TEMP_BODY;
write_file(TEMP_MAIL_LIST, name + " " + rtime + " " + body + "END");
}
if (!BUGS)
printf("No more errors.\n");
else
printf( "%s", get_bug( BUG_NUM ) );
request();
}
void forward_bug(string NewDir) {
string fmt, trash, bug;
string title, filename;
string ftime, fname;
string rtime, name;
fmt = "%s.START " + BUG_NUM + "\n%s.END " + BUG_NUM;
sscanf(ERROR, fmt, trash, bug);
sscanf(bug, "%s\n%s\n%s\n%s\n%s\n%s\n%s",title,filename,ftime,
fname,rtime,name,bug);
if(ftime == "")
write_file(TEMP_FORWARD_LIST, name + " " + rtime + " " + filename + "\n");
else
write_file(TEMP_FORWARD_LIST, name + " " + ftime + " " + filename + "\n");
write_file(TEMP_FORWARD_LIST, ".START " + BUG_NUM + "\n" + NewDir + "\n" +
title + "\n" + filename + "\n" + ftime + "\n" + fname + "\n" +
rtime + "\n" + name + "\n" + bug + ".END " + BUG_NUM + "\n");
}
varargs void write_bug_report(string log, string title, string File,
string ftime, string fname, string rtime,
string name, string bug, int flag) {
int i;
string temp;
if (!flag)
log_file("TEMPORARY_ERRORS", name + " " + rtime + " " + File + "\n");
i = file_length( log );
if (i > 0) {
temp = read_file(log, i, 1);
if (sscanf(temp, ".END %d", i) == 1)
i += 1;
else
i = 1;
}
else
i = 1;
temp = ".START " + i + "\n" + title + File + ftime + fname +
rtime + name + bug + ".END " + i + "\n";
master()->forward_error(log, temp);
}
int commands(string str) {
int i;
string bug, ed_file, trash, fmt;
string title, filename;
string ftime, fname;
string rtime, name;
if (!str)
str = "";
if (sizeof(str) > 1)
str = lower_case(str[0..0])+str[1..];
else
str = lower_case(str);
if(str[0] == 'g')
sscanf(str, "%s %d", str, i);
if(str[0] == 'e' || str[0] == 'f' || str[0] == 'd' || str[0] == 'b')
sscanf(str, "%s %s", str, ed_file);
str = lower_case(str);
switch(str) {
case "e" :
if (!BUGS) {
printf("No more errors.\n");
request();
return 1;
}
if (!ed_file) {
fmt = "%*s.START " + BUG_NUM + "\n%s.END " + BUG_NUM;
sscanf(ERROR, fmt, bug);
sscanf(bug, "%*s\n%s\n", trash, ed_file);
ed_file += ".c";
}
printf("Editing %s\n", ed_file);
ed(ed_file, "request");
return 1;
case "b" :
if (!BUGS) {
printf("No more errors.\n");
request();
return 1;
}
if (!ed_file)
printf("You must give the name of a board to post on.\n");
else {
if (member_array(ed_file, (string *)BOARD_HAND->query_boards()) == -1)
printf("The board %s does not exist.\n", ed_file);
else {
ED_FILE = ed_file;
printf("Do you wish to edit the post ? [y/N] ");
get_char("continue_post");
return 1;
}
}
request();
return 1;
case "u" :
str = "f";
ed_file = "unknown";
case "f" :
if (!BUGS) {
printf("No more errors.\n");
request();
return 1;
}
if(!ed_file)
printf("You must either give the name of a creator, or a directory.\n");
else {
string cre;
cre = this_player()->expand_nickname(ed_file);
if( creatorp(cre) || cre == "unknown") {
if (cre == "unknown")
forward_bug("/log/" + ERROR_LOG);
else
forward_bug("/w/"+cre+"/" + ERROR_LOG);
delete_bug(BUG_NUM);
printf("Forwarded (hopefully :).\n");
} else {
if ( unguarded( (: file_size, ed_file :) ) == -2 ) {
forward_bug(ed_file + "/" + ERROR_LOG);
delete_bug(BUG_NUM);
printf("Forwarded (hopefully :).\n");
} else
printf("Dir does not exist, or you haven't got read access.\n");
}
}
if (!BUGS)
printf("No more errors.\n");
else
printf( "%s", get_bug( BUG_NUM ) );
request();
return 1;
case "d" :
if (!BUGS) {
printf("No more errors.\n");
request();
return 1;
}
if(ed_file == "f" || ed_file == "" || !ed_file) {
fmt = "%*s.START " + BUG_NUM + "\n%s.END " + BUG_NUM + "\n%*s";
sscanf(ERROR, fmt, bug);
sscanf(bug, "%s\n%s\n%s\n%s\n%s\n%s\n%s", title, filename, ftime,
fname, rtime, name, bug);
bug = "The following bug has now been fixed.\n\n"+ bug +
"\nThank you for reporting it.\n\n"+
"This was an automatic mail.\n";
write_file(TEMP_MAIL_LIST, name + " " + rtime + " " + bug + "END");
}
if(ed_file == "n") {
fmt = "%*s.START " + BUG_NUM + "\n%s.END " + BUG_NUM + "\n%*s";
sscanf(ERROR, fmt, bug);
sscanf(bug, "%s\n%s\n%s\n%s\n%s\n%s\n%s", title, filename, ftime,
fname, rtime, name, bug);
bug = "The following report by you is not a bug.\n" + bug +
"\n" + "Thank you for reporting it anyway though.\n"+
"This was an automatic mail.\n";
write_file(TEMP_MAIL_LIST, name + " " + rtime + " " + bug + "END");
}
if(ed_file == "r") {
fmt = "%*s.START " + BUG_NUM + "\n%s.END " + BUG_NUM + "\n%*s";
sscanf(ERROR, fmt, bug);
sscanf(bug, "%s\n%s\n%s\n%s\n%s\n%s\n%s", title, filename, ftime,
fname, rtime, name, bug);
TEMP_BODY = name + " " + rtime + " " + bug;
this_player()->do_edit(0,"end_of_mail");
}
delete_bug(BUG_NUM);
if (ed_file != "r") {
if (!BUGS)
printf("No more errors.\n");
else
printf( "%s", get_bug( BUG_NUM ) );
request();
}
return 1;
case "" :
case "+" :
case "n" :
if(BUG_NUM < BUGS) {
BUG_NUM += 1;
printf( "%s", get_bug( BUG_NUM ) );
}
else
printf("No more errors.\n");
request();
return 1;
case "-" :
case "p" :
if(BUG_NUM > 1) {
BUG_NUM -= 1;
printf( "%s", get_bug( BUG_NUM ) );
}
else
printf("This is the first error.\n");
request();
return 1;
case "g" :
if(i > 0 && i < BUGS+1) {
BUG_NUM = i;
printf( "%s", get_bug( BUG_NUM ) );
}
else
printf("Error number doesn't exists.\n");
request();
return 1;
case "q" :
rm(TEMP_MAIL_LIST);
rm(TEMP_FORWARD_LIST);
map_delete(globvars, this_player());
return 1;
case "x" :
if (CHANGED && rm(FILE)) {
if (BUGS)
write_file(FILE, ERROR);
ERROR = read_file(TEMP_MAIL_LIST);
if (ERROR) {
while( sscanf( ERROR, "%s %s %sEND%s", name, rtime, bug, ERROR ) ==
4 ) {
AUTO_MAILER->auto_mail( name,
this_player()->query_name(), "Bug report", "", bug, 0, 0);
}
rm(TEMP_MAIL_LIST);
}
i = 1;
while ( i < file_length( TEMP_FORWARD_LIST ) ) {
trash = read_file(TEMP_FORWARD_LIST, i++, 1);
sscanf(trash, "%s %s %s\n", fname, ftime, filename);
trash = read_file(TEMP_FORWARD_LIST, i++, 1);
sscanf(trash, ".START %s", ERROR);
ERROR = ".END " + ERROR;
TEMP_BODY = read_file(TEMP_FORWARD_LIST, i++, 1);
sscanf(TEMP_BODY, "%s\n", TEMP_BODY);
title = read_file(TEMP_FORWARD_LIST, i++, 1);
filename = read_file(TEMP_FORWARD_LIST, i++, 1);
ftime = read_file(TEMP_FORWARD_LIST, i++, 1);
fname = read_file(TEMP_FORWARD_LIST, i++, 1);
rtime = read_file(TEMP_FORWARD_LIST, i++, 1);
name = read_file(TEMP_FORWARD_LIST, i++, 1);
bug = "";
while((trash = read_file(TEMP_FORWARD_LIST, i++, 1)) != ERROR) {
bug += trash;
}
write_bug_report(TEMP_BODY, title, filename, time()+"\n",
this_player()->query_name()+"\n", rtime, name, bug, 1);
}
rm(TEMP_FORWARD_LIST);
printf("Done.\n");
} else if (CHANGED)
printf( "You don't have write permission to this directory.\n" );
else
printf("No changes need saving.\n");
map_delete(globvars, this_player());
return 1;
case "h" :
case "?" :
printf("B b - Post the error to board b.\n");
printf("E - Edit the file to correct the error.\n");
printf("E f - Edit file with name f.\n");
printf("U - Forward and delete error to unknow list.\n");
printf("F n - Forward and delete error to creator or directory n.\n");
printf("D - Delete bug and mark as fixed.\n");
printf("D F - same as above.\n");
printf("D N - Delete bug and mark as not an error.\n");
printf("D R - Delete bug and reply to sender yourself.\n");
printf( " The word $report$ will be replaced by the original\n" );
printf(" bug report.\n");
printf("N/+ - Next error.\n");
printf("P/- - Previous error.\n");
printf("G - Goto to error.\n");
printf("Q - Quit.\n");
printf("X - Save and quit.\n");
printf("H/? - This help file.\n");
request();
return 1;
default :
request();
return 1;
}
}
void continue_post(string input) {
if (!input || input == "" || input[0] == 13 || input[0] == 10 ||
lower_case(input[0..0]) == "n") {
if (lower_case(input) == "n")
printf("o.\n");
else
printf("No.\n");
BOARD_HAND->add_message(ED_FILE,
(string)this_player()->query_short(),
"Bug report", get_bug(BUG_NUM));
printf("Bug posted.\n");
request();
return;
}
if (lower_case(input[0..0]) != "y") {
printf("Huh? Please type y or n: ");
get_char("continue_post");
return;
}
if (lower_case(input) == "y")
printf("es.\n");
else
printf("Yes.\n");
printf("Entering editor...\n");
this_player()->do_edit(get_bug(BUG_NUM), "finish_edit_post");
return;
}
void finish_edit_post(string post) {
if (!post || post == "") {
request();
return;
}
BOARD_HAND->add_message(ED_FILE,
(string)this_player()->query_short(),
"Bug report", post);
printf("Bug posted.\n");
request();
return;
}
void dest_me() {
destruct(this_object());
}