#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "bbscdef.h"


char            save_old_m[99];
extern int      user_priv, last_msg_read, read_flag, read_number, reply_sw;
extern int      first_msg_in, blocked_m;
int             cont_read;
int             contin;
int             zip;


parse_zip(string)
	char           *string;
{

	register char  *file_ptr, *xptr;
	register int    i;

	strcpy(m_pathname, ORGPATH);
	file_ptr = (m_pathname + strlen(m_pathname));
	xptr = who_am_I;

	i = 0;
	while (string[i] != ' ') {
		*file_ptr = string[i];
		*xptr = string[i];
		++xptr;
		++file_ptr;
		++i;
	}
	*file_ptr = '/';
	++file_ptr;
	*file_ptr = '\0';
	*xptr = '\0';

}
cmd_z()
{
	FILE           *fpt, *fopen();
	char           *fgets(), *getenv();
	char            choice[4];
#ifndef SYSV
	char            dir_priv_ascii[7];
#endif
#ifdef SYSV
	char            dir_priv_ascii[20];
#endif
	char           *buf_ptr, *file_ptr, *char_ptr;
	int             line_cnt, ret, i;
	int             index_value, ptr;
	int             length, strl, ii;
	int             read_type;
	int             ret_type;

	strcpy(buf128, MSGS);
	strcpy(save_old_m, m_pathname);
	first_msg_in = TRUE;
	read_type = 0;
	stop_that = FALSE;
	zip = TRUE;
	portsout("\n\r\n\rY(our messages) or N(ew messages) Y/n? ");
	portsin(choice, 1);
	portsout(CRLF);
	if (choice[0] == 'n' || choice[0] == 'N') {
		read_type = 1;
		cont_read = 0;
		portsout("C(ontinous read) or S(ingle at a time) c/S? ");
		portsin(choice, 1);
		portsout(CRLF);
		if (choice[0] == 'c' || choice[0] == 'C') {
			cont_read = 1;
			contin = TRUE;
		}
	}
	if ((fpt = fopen(buf128, "r")) == NULL) {
		portsout("\n\rError Opening File Area List: Notify Sysop!\n\r");
		zip = FALSE;
		return (-1);
	}
	line_cnt = 0;
	while (fpt) {
		zfl(f_lines[line_cnt], 81);
		if ((fgets(f_lines[line_cnt], 80, fpt)) == NULL) {
			if (line_cnt == 0) {
				portsout("\n\rEOF Unexpected in Message Area List: Notify Sysop!\n\r");
				zip = FALSE;
				return (-1);
			}
			break;	/* if not 1st line */
		}		/* end of if ((fgets)) */
		if (line_cnt > 0) {
			length = strlen(f_lines[line_cnt]);
			length -= 57;
			if (length > 6)
				length = 6;
			substr(f_lines[line_cnt], dir_priv_ascii, 57, length);
			strl = strlen(dir_priv_ascii);
			if (strl == 0) {
				portsout("\n\rError reading privilege level\n\r");
				exit(1);
			}
			dir_priv[line_cnt] = atoi(dir_priv_ascii);
			if (dir_priv[line_cnt] > user_priv)
				goto next_read;
			strcpy(who_am_i, f_lines[line_cnt]);
			buf_ptr = who_am_i;
			buf_ptr += 56;
			for (ptr = 0; ptr < 5; ptr++)
				*buf_ptr++ = ' ';
			*buf_ptr = '\0';
			sprintf(buf128, "%2d) %s", line_cnt, who_am_i);
		}
next_read:
		++line_cnt;
	}			/* end of while (fpt) */
	fclose(fpt);

	if (line_cnt <= 1) {
		zip = FALSE;
		return;
	}
	for (index_value = 1; index_value < line_cnt; index_value++) {
		if (dir_priv[index_value] <= user_priv) {
			if (stop_that)
				goto dats_all;
			parse_zip(f_lines[index_value]);
			hdrread();
			portsout("\n\rSwitching to area '");
			portsout(who_am_I);
			portsout("'\n\r");
			ret_type = 1;
			if (!read_type)
				cmd_y();
			else
				ret_type = new_msgs();
			if (ret_type == 0 || ret_type == ERROR)
				goto dats_all;
			portsout("\n\r*************************************************\n\r");
		}
	}
dats_all:
	strcpy(m_pathname, save_old_m);
	i = strlen(m_pathname);
	if (i > 0) {
		hdrread();
		set_first_read();
	}
	if (contin == TRUE && cont_read == 1)
		contin = FALSE;
	stop_that = FALSE;
	zip = FALSE;
}
int
new_msgs()
{
	int             rc, fd, q_msg;
	int             t1;
	char            chr[2];
	int             new_msg;
	int             how;
	set_first_read();
	new_msg = last_msg_read + 1;
	q_msg = new_msg;
	read_flag = TRUE;
	if ((fd = msgopen(0)) == ERROR) {
		zip = FALSE;
		return (ERROR);
	}
	how = 1;
	while ((rc = msgreadx(fd, q_msg++)) != ERROR) {
		if (rc == 0 || rc == -10) {
			continue;
		}
		read_number = atoi(msg_no);
		cmd_r();
		last_msg_read = read_number;
		set_write();
		if (cont_read == 1)
			goto cont_rd;
		if (user_priv >= mpE) {
			portsout("\n\rDo you wish to reply to this message? (y/N): ");
			portsin(chr, 1);
			portsout(CRLF);
			if (chr[0] == 'Y' || chr[0] == 'y') {
				portsout("\n\rDo you wish to use blocked (right justified) format?  (y/N: ");
				portsin(chr, 1);
				portsout(CRLF);
				if (chr[0] == 'y' || chr[0] == 'Y') 
					blocked_m = 1;
				else
					blocked_m = 0;
				reply_sw = TRUE;
				strcpy(msg_to, msg_from);
				strcpy(x_pathandfile, "(R) ");
				strcat(x_pathandfile, msg_subject);
				substr(x_pathandfile, msg_subject, 1, 20);
				cmd_e();
				reply_sw = FALSE;
			}
		}
		portsout("\n\r\n\rContinue reading messages within this message base? (Y/n): ");
		portsin(chr, 1);
		portsout(CRLF);
		if (chr[0] == 'n' || chr[0] == 'N')
			break;
cont_rd:	if (stop_that) {
			stop_that = FALSE;
			read_flag = FALSE;
			how = 0;
			break;
		}
		if (cont_read == 1) {
			portsout(CRLF);
			portsout("----------------------------------------");
		}
		portsout(CRLF);
	}
	read_flag = FALSE;
	msgclose(fd);
	return (how);
}
