/*
 
Title:	MsgEd
  
File:   Msglist.c
  
Author:	Jim Nutt
	
Copr:   1988 by Jim Nutt
  
Description:

        message listing code
	
Revision History:
  
	0.00	12 December 1987
	
Support Files:
  
	msged.h
  
*/

#include "msged.h"

static void update(int n,int y);
static char    tmp[16];
static char    line[256];
static int f = 1;
static struct mh {
                MSGHEADER h;
                int n;
        } *headers;

void list()

{
	int	i, y, ch, a;
	char   *s;

        cls();
        sprintf(tmp,"%%-%d.%ds",maxx,maxx);
        i = a = arealist[area].current;
        headers = (struct mh *) calloc(maxy+1,sizeof(struct mh));

        y = 1;

        update(i,y);

        ch = 0;
        y = 1;

	while ((ch != ENTER) && (ch != ABORT)) {
                gotoxy(1, y);
                clreol();
                background(normfore);
                foreground(normback);
                if (f && arealist[area].netmail)
                        sprintf(line,"%4d: %-15.15s@%5d/%-5d to %-15.15s@%5d/%-5d RE: %-70s",
                                headers[y - 1].n,headers[y - 1].h.from,headers[y - 1].h.orig_net,headers[y - 1].h.orig,
                                headers[y - 1].h.to,headers[y - 1].h.dest_net,headers[y - 1].h.dest,
                                headers[y - 1].h.subj);
                else
                        sprintf(line,"%4d: %-15.15s to %-15.15s RE: %-70s",
                                headers[y - 1].n,headers[y - 1].h.from,headers[y - 1].h.to,headers[y - 1].h.subj);
                bprintf(tmp,line);
                background(normback);
                foreground(normfore);

                switch (ch = getkey()) {

                case AREA :
                        f = !f;
                        y = 1;
                        update(a,y);
                        break;

                case PGDN:
                        i = maxy;
                        while ((i > 1) && (a < arealist[area].last)) {
                                if (messages[a])
                                        i--;
                                a++;
                        }
                        y = 1;
                        update(a,y);
                        break;

                case PGUP:
                        i = maxy;
                        while ((i > 1) && (a > arealist[area].first)) {
                                if (messages[a])
                                        i--;
                                a--;
                        }
                        y = 1;
                        update(a,y);
                        break;

		case UP:
                        if (a > 1) {
                                int b = a;
                                gotoxy(1, y);
				clreol();
                                if (f && arealist[area].netmail)
                                        sprintf(line,"%4d: %-15.15s@%5d/%-5d to %-15.15s@%5d/%-5d RE: %-70s",
                                                headers[y - 1].n,headers[y - 1].h.from,headers[y - 1].h.orig_net,headers[y - 1].h.orig,
                                                headers[y - 1].h.to,headers[y - 1].h.dest_net,headers[y - 1].h.dest,
                                                headers[y - 1].h.subj);
                                else
                                        sprintf(line,"%4d: %-15.15s to %-15.15s RE: %-70s",
                                                headers[y - 1].n,headers[y - 1].h.from,headers[y - 1].h.to,headers[y - 1].h.subj);
                                                bprintf(tmp,line);

                                a--;
                                y--;

                                while (!messages[a] && (a > 1))
                                        a--;

                                if (!messages[a]) {
                                        a = b;
                                        y++;
                                }

                                if (y < 1) {
                                        y = 1;
                                        scrolldown(1, 1, maxx, maxy, 1);
                                        memmove((headers + 1),headers,(sizeof(struct mh) * maxy));
					headers[0].h = readheader(a);
					s = strchr(headers[0].h.from,'\n');
					if (s != NULL)
						*s = '\0';
					s = strchr(headers[0].h.to,'\n');
					if (s != NULL)
						*s = '\0';
                                        headers[0].n = a;
				}
                        }
			break;

                case DOWN:
                        if (a < arealist[area].last) {
                                int b = a;
                                gotoxy(1, y);
				clreol();
                                if (f && arealist[area].netmail)
                                        sprintf(line,"%4d: %-15.15s@%5d/%-5d to %-15.15s@%5d/%-5d RE: %-70s",
                                                headers[y - 1].n,headers[y - 1].h.from,headers[y - 1].h.orig_net,headers[y - 1].h.orig,
                                                headers[y - 1].h.to,headers[y - 1].h.dest_net,headers[y - 1].h.dest,
                                                headers[y - 1].h.subj);
                                else
                                        sprintf(line,"%4d: %-15.15s to %-15.15s RE: %-70s",
                                                headers[y - 1].n,headers[y - 1].h.from,headers[y - 1].h.to,headers[y - 1].h.subj);
                                                bprintf(tmp,line);
                                bprintf(tmp,line);

                                a++;
                                y++;

                                while (!messages[a] && (a < arealist[area].last))
                                        a++;

                                if (!messages[a]) {
                                        a = b;
                                        y--;
                                }

                                if (y > maxy) {
                                        y = maxy;
                                        scrollup(1, 1, maxx, maxy, 1);
                                        memmove(headers,(headers + 1),sizeof(struct mh) * (maxy));
                                        headers[maxy - 1].h = readheader(a);
					s = strchr(headers[maxy - 1].h.from,'\n');
					if (s != NULL)
						*s = '\0';
					s = strchr(headers[maxy - 1].h.to,'\n');
					if (s != NULL)
						*s = '\0';
                                        headers[maxy - 1].n = a;
				}
			}
                        break;

                case ENTER:
                        arealist[area].current = a;
                        readmsg(&message,a);
                        free(headers);
                        return;

                case ABORT:
                        free(headers);
                        return;
		}
        }
}

static void update(int i,int y)

{
	char *s;

        clrwnd(1,y,maxx,maxy);
        while ((i <= arealist[area].last) && (y <= maxy)) {
                gotoxy(1,y);
                while ((!messages[i]) && (i < arealist[area].last)) i++;
		headers[y - 1].h = readheader(i);
		s = strchr(headers[y - 1].h.from,'\n');
		if (s != NULL)
			*s = '\0';
		s = strchr(headers[y - 1].h.to,'\n');
		if (s != NULL)
			*s = '\0';
                headers[y - 1].n = i;
                if (f && arealist[area].netmail)
                        sprintf(line,"%4d: %-15.15s@%5d/%-5d to %-15.15s@%5d/%-5d RE: %-70s",
                                headers[y - 1].n,headers[y - 1].h.from,headers[y - 1].h.orig_net,headers[y - 1].h.orig,
                                headers[y - 1].h.to,headers[y - 1].h.dest_net,headers[y - 1].h.dest,
                                headers[y - 1].h.subj);
                else
                        sprintf(line,"%4d: %-15.15s to %-15.15s RE: %-70s",
                                headers[y - 1].n,headers[y - 1].h.from,headers[y - 1].h.to,headers[y - 1].h.subj);
                bprintf(tmp,line);
                i++;
                y++;
	}

}
