#include <stdio.h>

int mailfrom(char *buf,int bsize)
{
FILE *p;
char tmpp[201],*ki;
// szukaj zdefiniowanej
sprintf(tmpp,"%s/.gg/wap-mailname",home);
if(p=fopen(tmpp,"r"))
    {
    fgets(buf,bsize-1,p);
    fclose(p);
    
    ki=(char *)index(buf,10);
    if(ki!=NULL)ki[0]=0;
    ki=(char *)index(buf,13);
    if(ki!=NULL)ki[0]=0;
    
    if(strlen(buf)>0)return 1;  
    }

// jesli jej nie ma to szukaj w /etc/mailname
if(p=fopen("/etc/mailname","r"))
    {
    fgets(tmpp,bsize-1,p);
    fclose(p);

    ki=(char *)index(tmpp,10);
    if(ki!=NULL)ki[0]=0;
    ki=(char *)index(tmpp,13);
    if(ki!=NULL)ki[0]=0;
    
    if((strlen(logname)+strlen(tmpp)+2)<bsize) {
	sprintf(buf,"%s@%s",logname,tmpp);
	if(strlen(buf)>1)return 1;
	}
    }
// no mailname no mail
sprintf(buf,"");
return 0;
}

/*
Sprawdzanie mailboxa i maildira
mailboxpath/maildirpath - sciezka do skrytki
mailboxname/maildirname - nazwa skrytki
outbuf - bufor wyjsciowy
cmd - komenda
  0 - wyswietl krotki raport skrytki
  1 - lista wszystkich maili
  2 - lista nowych maili
  3 - wyswietl dany mail (tresc)
ofs - od ktorego maila dla listy, lub ktory mail
ile - ile maili
emails - ilosc nowych emaili narastajaco
*/

int wap_ckmdir(char *maildirpath,char *maildirname,char *outbuf, int cmd, int ofs, int ile, int *emails)
{
char name[20],tmppath[300];
struct dirent **namelist;
int ileold,ilenew,n;

if(cmd==0) {
    sprintf(name,"%.19s",maildirname);
    sprintf(tmppath,"%snew",maildirpath);
//    printf("%s\n",tmppath);
    ilenew = scandir(tmppath, &namelist, 0, alphasort);
    n=ilenew;
    if (ilenew < 0) return 0;
    else while(--n) { free(namelist[n-1]); }
    free(namelist);
    ilenew-=2;
    sprintf(tmppath,"%scur",maildirpath);
//    printf("%s\n",tmppath);
    ileold = scandir(tmppath, &namelist, 0, alphasort);
    n=ileold;
    if (ilenew < 0) return 0;
    else while(--n) { free(namelist[n-1]); }
    free(namelist);
    ileold-=2;
    sprintf(outbuf,"<p>%s: <a href=\"?c=mail;n=%s;t=10\">%d</a> new <a href=\"?c=mail;n=%s;t=20\">%d</a></p>",
	name,
	name,
	ileold,
	name,
	ilenew);
    emails[0]+=ilenew;
    return 1;
    }
if(cmd==1 || cmd==2 || cmd==3 || cmd==4) {
    int k,head,maili=0,nowy,quotedp,boundaryhead,textct,read;
    FILE *p;
    char buf[501],odkogo[500],subiect[500],*ki,tmpp[500],returnpath[500],returnpath2[201];
    sprintf(name,"%.19s",maildirname);
    if(cmd==1 || cmd==4) {
	sprintf(tmppath,"%scur",maildirpath);
	nowy=0;
	}
    else {
	sprintf(tmppath,"%snew",maildirpath);
	nowy=1;
	}
    sprintf(outbuf,"");
    n = scandir(tmppath, &namelist, 0, alphasort);
    k=n;
    if (n < 0) return 0;
    else 
	while(n--) {
	if(namelist[k-n-1]->d_name[0]!='.') {
	    head=1;
	    quotedp=0;
	    boundaryhead=0;
	    textct=1;
	    read=0;
	    sprintf(tmpp,"%s/%s",tmppath,namelist[k-n-1]->d_name);
	    if(p=fopen(tmpp,"r")) {
		maili++;
		if((cmd==3 || cmd==4) && maili==ofs) {
		// odczyt tego wlasnie maila
		    sprintf(subiect,"");
		    sprintf(odkogo,"");
		    while(fgets(buf,501,p)) {
			ki=(char *)index(buf,10);
			if(ki!=NULL)ki[0]=0;
			ki=(char *)index(buf,13);
			if(ki!=NULL)ki[0]=0;

			if(strncmp(buf,"From: ",6)==0 && head==1)
			    {
			    sprintf(odkogo,"%.499s",buf+6);
			    }
			else
			if(strncmp(buf,"Return-Path: <",14)==0 && head==1)
        		    {
	        	    sprintf(returnpath,"%s",buf+14);
		            if(strlen(returnpath)>1)
			    returnpath[strlen(returnpath)-1]=0;
			    }
			else
			if(head==1 && strncmp(buf,"Subject: ",9)==0)
        		    {
	        	    if(strlen(buf+9)>0)
		    		sprintf(subiect,"%.499s",buf+9);
			    else
				sprintf(subiect,"none");
			    }				    						    
			else
			if((strncmp(buf,"Content-Transfer-Encoding: ",27)==0) && (head==1 || boundaryhead==1))
			    {
			    if(strstr(buf,"quoted-printable")!=0 || strstr(buf,"QUOTED-PRINTABLE")!=0)
				quotedp=1;
			    else
				quotedp=0;
			    }
			else
			if(head==1 && strlen(buf)==0) {
			    head=0;
			    sprintf(returnpath2,"%.200s",returnpath);
			    decodesubiect(odkogo,499);
			    dozwolonys(odkogo);
			    decodesubiect(subiect,499);
			    dozwolonys(subiect);
			    sprintf(outbuf,"%s<p>%s<b>%d %s</b></p><p><u>%s</u></p><p>",
				outbuf,
				(nowy==1)?"N ":"",
				maili,
				odkogo,
				subiect);
			    read=1;
			    }
			else
			if(head==0 && boundaryhead==0 && textct==1)
			    {
			    if(quotedp==1)
				decodeqp(buf,4999);
			    dozwolonys(buf);
			    if((strlen(outbuf)+strlen(buf))<2500)
			    sprintf(outbuf,"%s<br/>%s",outbuf,buf);
			    }
			
			}
		    if(read==1)sprintf(outbuf,"%s</p>",outbuf);
		    }
	
		if((cmd==1 || cmd==2) && maili>=ofs && maili<(ofs+ile)) {
		// odczyt glowek
		    sprintf(subiect,"");
		    sprintf(odkogo,"");
		    while(fgets(buf,501,p)) {
			ki=(char *)index(buf,10);
			if(ki!=NULL)ki[0]=0;
			ki=(char *)index(buf,13);
			if(ki!=NULL)ki[0]=0;
			
			if(strncmp(buf,"From: ",6)==0 && head==1)
			    {
			    sprintf(odkogo,"%.499s",buf+6);
			    }
			else
			if(head==1 && strncmp(buf,"Subject: ",9)==0)
        		    {
	        	    if(strlen(buf+9)>0)
		    		sprintf(subiect,"%.499s",buf+9);
			    else
				sprintf(subiect,"none");
			    }				    						    
			else
			if(head==1 && strlen(buf)==0) {
			    head=0;
			    decodesubiect(odkogo,499);
			    dozwolonys(odkogo);
			    decodesubiect(subiect,499);
			    dozwolonys(subiect);
			    sprintf(outbuf,"%s<p>%s<b><a href=\"?c=mail;n=%s;t=%d%d\">%d</a> %s</b></p><p>%s</p>",
				outbuf,
				(nowy==1)?"N ":"",
				name,
				(nowy==1)?3:4,
				maili,
				maili,
				odkogo,
				subiect);
			    }
			}
		    //show
		    }
		fclose(p);
		}
	    }
	free(namelist[k-n-1]);
	}
    free(namelist);

if(cmd==1 || cmd==2)
    sprintf(outbuf,"%s<do type=\"next\" label=\"next\"><go href=\"?c=mail;n=%s;t=%d%d\"/></do>
<do type=\"back\" label=\"back\"><go href=\"?c=mail;n=%s;t=%d%d\"/></do>
<do type=\"last\" label=\"last\"><go href=\"?c=mail;n=%s;t=%d%d\"/></do>
<do type=\"prev\" label=\"Mailbox list\"><go href=\"?c=mail;n=0;t=0\"/></do>
<do type=\"Mailbox list\" label=\"Mailbox list\"><go href=\"?c=mail;n=0;t=0\"/></do>",
    outbuf,
    name,
    cmd,
    ofs+10,
    name,
    cmd,
    ((ofs-10)>0)?ofs-10:0,
    name,
    cmd,
    ((maili-9)>0)?maili-9:0
    );
if(cmd==3 || cmd==4)
    sprintf(outbuf,"%s<do type=\"reply\" label=\"reply\"><go href=\"?c=newmail;n=%s;t=0\"/></do>
<do type=\"next\" label=\"next\"><go href=\"?c=mail;n=%s;t=%d%d\"/></do>
<do type=\"previous\" label=\"previous\"><go href=\"?c=mail;n=%s;t=%d%d\"/></do>
<do type=\"Mailbox\" label=\"Mailbox\"><go href=\"?c=mail;n=%s;t=1%d\"/></do>
<do type=\"prev\" label=\"Mailbox\"><go href=\"?c=mail;n=%s;t=1%d\"/></do>
<do type=\"Mailbox list\" label=\"Mailbox list\"><go href=\"?c=mail;n=0;t=0\"/></do>",
    outbuf,
    returnpath2,
    name,
    cmd,
    (ofs!=maili)?ofs+1:ofs,
    name,
    cmd,
    (ofs>1)?ofs-1:1,
    name,
    (ofs>4)?ofs-4:1,
    name,
    (ofs>4)?ofs-4:1
    );    
    return 1;
    }
return 0;
}

int wap_ckm(char *mailboxpath,char *mailboxname,char *outbuf, int cmd, int ofs, int ile, int *emails)
{
FILE *f;
char buf[5000],*k,odkogo[500],name[20],subiect[500],returnpath[201],boundary[500],parttype[500];
char returnpath2[201];
int head=0,read=0,quotedp=0,boundaryhead=0;
int maili=0,body=0,readed=0,unread=0,nowy,textct=0;;

sprintf(name,"%.19s",mailboxname);

if(!(f=fopen(mailboxpath,"r")))
    return 0;

outbuf[0]=0;

while(fgets(buf,501,f))
    {
    k=(char *)index(buf,10);
    if(k!=NULL)k[0]=0;
    k=(char *)index(buf,13);
    if(k!=NULL)k[0]=0;

    if(strncmp(buf,"From ",5)==0 && head==0)
	{
	head=1;
	maili++;
	nowy=1;
	read=0;
	quotedp=0;
	textct=0;
	sprintf(boundary,"serekserekserekserekserek");
	sprintf(subiect,"");
	sprintf(returnpath,"");
	}
    else
    if(strncmp(buf,"Return-Path: <",14)==0 && head==1 && cmd==3)
	{
	sprintf(returnpath,"%s",buf+14);
	if(strlen(returnpath)>1)
	    returnpath[strlen(returnpath)-1]=0;
	}
    else
    if(strncmp(buf,"From: ",6)==0 && head==1)
	{
	sprintf(odkogo,"%.499s",buf+6);
	}
    else
    if(strlen(buf)==0 && head==1)
	{
	if(nowy==1)unread++;
	// tu sie zaczyna tresc
	if(cmd==1 && ofs<=maili && (ofs+ile)>maili) {
	    decodesubiect(odkogo,499);
	    dozwolonys(odkogo);
	    decodesubiect(subiect,499);
	    dozwolonys(subiect);
	    sprintf(outbuf,"%s<p>%s<b><a href=\"?c=mail;n=%s;t=3%d\">%d</a> %s</b></p><p>%s</p>",outbuf,(nowy==1)?"N ":"",name,maili,maili,odkogo,subiect);
	    }
	else
	if(cmd==2 && nowy==1 && ofs<=unread && (ofs+ile)>unread) {
	    decodesubiect(odkogo,499);
	    dozwolonys(odkogo);
	    decodesubiect(subiect,499);
	    dozwolonys(subiect);
	    sprintf(outbuf,"%s<p>N <b><a href=\"?c=mail;n=%s;t=3%d\">%d</a> %s</b></p><p>%s</p>",outbuf,name,maili,maili,odkogo,subiect);
	    }
	if(cmd==3 && ofs==maili) {
	    decodesubiect(odkogo,499);
	    dozwolonys(odkogo);
	    decodesubiect(subiect,499);
	    dozwolonys(subiect);
	    sprintf(returnpath2,"%.200s",returnpath);
	    sprintf(outbuf,"%s<p>%s<b>%d %s</b></p><p><u>%s</u></p><p>",outbuf,(nowy==1)?"N ":"",maili,odkogo,subiect);
	    read=1;
	    }
	head=0;
	body++;
	nowy=0;
	}
    else
    if((head==1 || boundaryhead==1) && strncasecmp(buf,"Content-Type: ",14)==0)
	{
	if(strncasecmp(buf,"Content-Type: text/",19)==0)
	    textct=1;
	sprintf(parttype,"%.499s",buf+14);

//czasami boundary jest w tej samej linii co content-type
	if(cmd==3 && head==1 && (strstr(buf,"boundary=")!=0 || strstr(buf,"BOUNDARY=")!=0))
	    {
	    char *k;
	    //wiadomosc wieloczesciowa
	    if(strstr(buf,"boundary=")!=0)
		sprintf(boundary,"--%.490s",strstr(buf,"boundary=")+10);
	    else
		sprintf(boundary,"--%.490s",strstr(buf,"BOUNDARY=")+10);
	    k=(char *)index(boundary,'"');
	    if(k!=NULL)k[0]=0;
	    }
	}
    else
    if(strlen(buf)==0 && head==0 && boundaryhead==1)
	{
	if(textct!=1)
	    sprintf(outbuf,"%s<br/>Cannot display this part: %s",outbuf,parttype);
	boundaryhead=0;
	}
    else
    if((strncmp(buf,"Content-Transfer-Encoding: ",27)==0) && (head==1 || boundaryhead==1))
	{
	if(strstr(buf,"quoted-printable")!=0 || strstr(buf,"QUOTED-PRINTABLE")!=0)
	    quotedp=1;
	else
	    quotedp=0;
	}
    else
    if(cmd==3 && head==1 && (strstr(buf,"boundary=")!=0 || strstr(buf,"BOUNDARY=")!=0))
	{
	char *k;
	//wiadomosc wieloczesciowa
        if(strstr(buf,"boundary=")!=0)
            sprintf(boundary,"--%.490s",strstr(buf,"boundary=")+10);
        else
            sprintf(boundary,"--%.490s",strstr(buf,"BOUNDARY=")+10);
	k=(char *)index(boundary,'"');
	if(k!=NULL)k[0]=0;
	}
    else
    if(read==1 && head==0 && strcmp(buf,boundary)==0)
	{
	printf("\nnext part\n");
	boundaryhead=1;
	textct=0;
	}
    else
    if(head==0 && read==1 && boundaryhead==0 && textct==1)
	{
	if(quotedp==1)
	    decodeqp(buf,4999);
	dozwolonys(buf);
	if((strlen(outbuf)+strlen(buf))<2500)
	    sprintf(outbuf,"%s<br/>%s",outbuf,buf);
	}
    else
    if(head==1 && strncmp(buf,"Status:",7)==0)
	{
	if(index(buf,'R')!=0)
	    {
	    readed++;
	    nowy=0;
	    }
	else
	    nowy=1;
	}
    else
    if(head==1 && strncmp(buf,"Subject: ",9)==0)
	{
	if(strlen(buf+9)>0)
    	    sprintf(subiect,"%.499s",buf+9);
	else
	    sprintf(subiect,"none");
	}
    }
fclose(f);

if(cmd==3)sprintf(outbuf,"%s</p>
<do type=\"reply\" label=\"reply\"><go href=\"?c=newmail;n=%s;t=0\"/></do>
<do type=\"next\" label=\"next\"><go href=\"?c=mail;n=%s;t=3%d\"/></do>
<do type=\"previous\" label=\"previous\"><go href=\"?c=mail;n=%s;t=3%d\"/></do>
<do type=\"Mailbox\" label=\"Mailbox\"><go href=\"?c=mail;n=%s;t=1%d\"/></do>
<do type=\"prev\" label=\"Mailbox\"><go href=\"?c=mail;n=%s;t=1%d\"/></do>
<do type=\"Mailbox list\" label=\"Mailbox list\"><go href=\"?c=mail;n=0;t=0\"/></do>",
    outbuf,
    returnpath2,
    name,
    (ofs!=maili)?ofs+1:ofs,
    name,
    (ofs>1)?ofs-1:1,
    name,
    (ofs>4)?ofs-4:1,
    name,
    (ofs>4)?ofs-4:1
    );
if(cmd==0)
    sprintf(outbuf,"<p>%s: <a href=\"?c=mail;n=%s;t=10\">%d</a> new <a href=\"?c=mail;n=%s;t=20\">%d</a></p>",
	name,
	name,
	maili,
	name,
	unread);
if(cmd==1 || cmd==2) {
sprintf(outbuf,"%s<do type=\"next\" label=\"next\"><go href=\"?c=mail;n=%s;t=%d%d\"/></do>
<do type=\"back\" label=\"back\"><go href=\"?c=mail;n=%s;t=%d%d\"/></do>
<do type=\"last\" label=\"last\"><go href=\"?c=mail;n=%s;t=%d%d\"/></do>
<do type=\"prev\" label=\"Mailbox list\"><go href=\"?c=mail;n=0;t=0\"/></do>
<do type=\"Mailbox list\" label=\"Mailbox list\"><go href=\"?c=mail;n=0;t=0\"/></do>",
    outbuf,
    name,
    cmd,
    ofs+10,
    name,
    cmd,
    ((ofs-10)>0)?ofs-10:0,
    name,
    cmd,
    (cmd==1)?(((maili-9)>0)?maili-9:0):(((unread-9)>0)?unread-9:0)
    );
}
emails[0]+=unread;
return 1;
}
