#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <descrip.h>
#include <processes.h>
#include "bboard.h"

static void build_note_file(char * o_fl, char * conf, FILE * in, int err_note);

static struct jnet_file_header * f_save;

/* Now that we've received the file, we're supposed to do something with it */

void use_file(struct jnet_file_header * f)
{
    FILE * note;

    f_save = f; 			/* Save this for local gloabl usage */

    note = fopen(f->local_file, "r");   /* Open it */

    scan_header(note, NULL);		/* Fill up the header IDs */
    do_rules(note);			/* And make them make sense */

    fclose(note);			/* Close the mail file */
    remove(f->local_file);		/* And toss it out the window */

    delete(f->jnet_file);		/* Kill Jnet's version of the file */
    rou_got_file();			/* And tell Jnet that we're finished */

    mail_received++;			/* And another one rides the bus */
}

/* Set up for, and then run, a .COM file to write the message to NOTES */

void put_note(FILE * note, struct rule_info * rule, struct list_info * list,
    int error_note)
{
    char * from;			/* Name of author */
    char * subj;			/* Subject from header */
    char * conf_to_use; 		/* Just figure this out once */
    char note_fn[200];			/* Where the text goes */
    FILE * com_file;			/* Ditto */

    /* Figure out which NOTES conference it gets put into */

    conf_to_use = (error_note ? error_conf.dsc$a_pointer : list->conf_name);

    /* And save the name */

    strcpy(f_save->conf_name, conf_to_use);

    /* Is this a digest to be processed? */

    if (!error_note && list->flag_digest) {
	digest_run(note, rule, list);
	return;
    }

    /* Build the text file and let us know where it is */

#ifdef DEBUG
    printf("put_note: about to build note text\n");
#endif

    build_note_file(note_fn, conf_to_use, note, error_note);

    /* Now let's get the pieces of the header that we need */
    /* This is only possible if we got here through a rule */

    if (!error_note) {
	from = find_source(rule->id_from);
	if (!from) {			/* If no one home, use a blank to */
	    from = "";                  /*  prevent ACCVIO */
	}

	from_text_fondle(from); 	/* Do all the right things with this */

	/* Now onto the Subject */

	subj = find_source(rule->id_subj);
	if (!subj) {
	    subj = "";
	}

	subj_text_fondle(subj);		/* Do all the right things with this */
    }

    /* Open up the .COM file */

    com_file = com_open();

#ifdef DEBUG
    printf("put_note: starting to construct .COM file\n");
#endif

    /* Let's see if we need a new topic written */

    if (!error_note) {
	fondle_note_log_file(com_file, list->new_topic_interval,
		list->conf_name, list->conf_hdr);
    }

    /* Are we being told to become someone else? This only works if you've */
    /*	got SETUNAME. Otherwise, don't fill in this field in the control */
    /*	file. */

    if (!error_note && list->conf_user[0]) {
	fprintf(com_file, "$ setuname %s\n", list->conf_user);
	setuname_used = TRUE;
    }

    /* Were we someone else but we're not posting as anyone else? If so, we */
    /*	need to change back to our hook name. */

    if ((error_note || !list->conf_user[0]) && setuname_used) {
	fprintf(com_file, "$ setuname %s\n", hook_name.dsc$a_pointer);
    }

    /* Enter NOTES */

    fprintf(com_file, "$ notes/nonotebook 0::%s\n", conf_to_use);

    /* Need moderator privs since the confs are /NOWRITE */

    fputs("set moderator\n", com_file);

    /* If we're not in an error situation, we want to set the personal name */
    /*	to the original From: of the message */

    if (error_note) {
	fputs("set prof/temp/pers=\"Notes Poster Daemon\"\n", com_file);
    } else {
	fprintf(com_file, "set prof/temp/pers=\"%s\"\n", from);
    }

    /* Go to the last note of the last topic */

    fputs("l.l\n", com_file);

    /* Reply with the text file */

    fprintf(com_file, "reply/noedit %s\n", note_fn);

    /* Write out the subject */

    if (!error_note) {
	fprintf(com_file, "%s\n", subj);
    } else {
	fprintf(com_file, "Unknown subject\n");
    }

    /* Yes, we want to enter the reply */

    fputs("yes\n", com_file);

    /* Do we want to add a keyword? */

    if (!error_note && list->flag_keyword && !list->conf_user[0]) {
	fprintf(com_file, "add keyword %s\n", list->scan_data);
    }

    /* And jump out of NOTES */

    fputs("exit\n", com_file);

    /* Make sure that we delete the note file */

    fprintf(com_file, "$ delete %s;*\n", note_fn);

    /* Wrap it up ready to go */

    com_close(1);
}

/* Build a text file name and copy the message text to it */

static void build_note_file(char * o_fl, char * conf, FILE * in, int err_note)
{
    SYSTIM cur_time;			/* When is now? */
    struct $NUMTIM t;			/* Need this to break it out */
    FILE * out; 			/* Where do we write it? */

    if (err_note) {			/* If error, want the header also */
	rewind(in);
    }

    /* Get time and date for the text file name */

    get_time(cur_time);
    get_numtim(&t, cur_time);

    /* Build the name into our caller's buffer so they know what it is */

    sprintf(o_fl, "%s%s.%04d-%04d%04d", scratch_dir.dsc$a_pointer, conf,
	    t.month * 100 + t.day, t.hour * 100 + t.minute,
	    t.second * 100 + t.h_second);

    /* Open the file in a way that makes sense for EDT, etc. */

    out = fopen(o_fl, "w", "ctx=rec", "rat=cr", "rfm=var");

    /* Copy it over while there's more text there */

    while (TRUE) {
	if (!fgets(temp, 255, in)) {
	    break;
	}
	fputs(temp, out);
    }

    /* Close the text file */

    fclose(out);

    /* And let's run away */

    return;
}
