#include <stdio.h>
#include <dirent.h>
#include "decode.c"
#include "ckm.c"

int addtoquick(char *n) {
char wapfile[160];
char nick1[51],nick2[51],nick3[51],*k;
FILE *wap;
memset(&nick1,0,sizeof(nick1));
memset(&nick2,0,sizeof(nick2));
memset(&nick3,0,sizeof(nick3));
sprintf(wapfile,"%.100s/.gg/wap-quick.log",home);
if(wap=fopen(wapfile,"r")) {
    if(!feof(wap))fgets(nick1,50,wap);
    k=(char *)index(nick1,10);if(k!=NULL)k[0]=0;
    if(!feof(wap))fgets(nick2,50,wap);
    k=(char *)index(nick2,10);if(k!=NULL)k[0]=0;
    if(!feof(wap))fgets(nick3,50,wap);
    k=(char *)index(nick3,10);if(k!=NULL)k[0]=0;
    fclose(wap);
    }
if(wap=fopen(wapfile,"w")) {
    int ok=0;
    fprintf(wap,"%s\n",n);
    if(strcmp(n,nick1)!=0)fprintf(wap,"%s\n",nick1);
    else ok=1;
    if(strcmp(n,nick2)!=0)fprintf(wap,"%s\n",nick2);
    else ok=1;
    if(ok==1 && strcmp(n,nick3)!=0)fprintf(wap,"%s\n",nick3);
    fclose(wap);
    }
return 0;
}

int dologu(char *a)
{
char wapfile[160];
FILE *wap;
sprintf(wapfile,"%.100s/.gg/wap-query.log",home);
if(wap=fopen(wapfile,"a")) {
    fprintf(wap,"%s\n",a);
    fclose(wap);
    }
return 0;
}

//TODO: kontola max_len to dlugosc out-a

int wap(char *out, char *query_str, int max_len)
{
struct dirent **namelist;
int n,k,i;
FILE *wap;
char wapfile[160],wapfile2[160],delp[200];
char buf[1001];
char *trescw,cmd[51],*nickw;
char tresc[1001],nick[51];
char userlist[3700];
int od;
int emails=0,msgs=0;
time_t czas=time(NULL);

sprintf(delp,"");
sprintf(buf,"%24.24s %.950s",ctime(&czas),query_str);
dologu(buf);
sprintf(buf,"%.1000s",query_str);

if(strncmp(buf,"check",5)!=0) {
sprintf(out,"Expires: Mon, 26 Jul 1997 05:00:00 GMT\r
Cache-Control: no-cache, must-revalidate\r
Pragma: no-cache\r
Content-type: text/vnd.wap.wml\r
\r
<?xml version=\"1.0\"?>
<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum.org/DTD/wml_1.1.xml\">

<wml>
<head>
<meta forua=\"true\" http-equiv=\"Cache-Control\" content=\"max-age=0\"/>
</head>
      
<card id=\"gg\" title=\"RTG\" newcontext=\"true\">");
if(strlen(buf)==0) {
    sprintf(out,"%s<p align=\"center\"><img src=\"%sekg.wbmp\" alt=\"ekg-wap\"/></p>
<p align=\"center\"><a href=\"?check0\">Check</a></p>",out,(icgi==0)?"?":"/rtg/");
    }
}
else {
sprintf(out,"Expires: Mon, 26 Jul 1997 05:00:00 GMT\r
Cache-Control: no-cache, must-revalidate\r
Pragma: no-cache\r
Content-type: text/vnd.wap.wml\r
\r
<?xml version=\"1.0\"?>
<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum.org/DTD/wml_1.1.xml\">

<wml>
<head>
<meta forua=\"true\" http-equiv=\"Cache-Control\" content=\"max-age=0\"/>
</head>

<card id=\"gg\" title=\"RTG Check\" ontimer=\"?check%d\">
<timer name=\"time%d\" value=\"3000\"/>
<p align=\"center\"><b>%5.5s</b></p>",time(NULL),time(NULL),ctime(&czas)+11);
sprintf(buf,"c=check;n=0;t=0");
}

//sprintf(out,"%s<p>%s</p>",out,home);

sprintf(wapfile,"%s/.gg/wapstatus",home);
if(wap=fopen(wapfile,"r")) {
    fgets(wapfile,100,wap);
    fclose(wap);
    if(wapfile[0]=='D')
	sprintf(out,"%s<p align=\"center\"><small>Ekg not connected, try <a href=\"?c=co;n=0;t=0\">connect</a> first</small></p>",out);
}

//dlugosc out=318

sprintf(cmd,"");
if(strlen(buf)>0) {
// jesli jest jakiskolwiek query to musi byc prawidlowy
// proste parsowanie jednoznakowych name-ow
    nickw=(char *)index(buf,';');
    if(nickw==NULL) {
	sprintf(out,"%sBlad skladni 1",out);
	return 0;
	}
    nickw++[0]=0;
    trescw=(char *)index(nickw,';');
    if(trescw==NULL) {
	sprintf(out,"%sBlad skladni 2",out);
	return 0;
	}
    trescw++[0]=0;
    
    if(strlen(nickw)<3 || strlen(trescw)<2) {
	sprintf(out, "%sBlad skladni 3",out);
	return 0;
	}
    
    sprintf(cmd,"%.50s",buf);
    sprintf(nick,"%.50s",nickw+2);
    sprintf(tresc,"%.1000s",trescw+2);

// usunac enkodowane znaczki z tresci

//printf("przed: %s %s\n",nick,tresc);
    decode(nick,50);
    decode(tresc,1000);
//printf("po: %s %s\n",nick,tresc);

// send mail service
    if(strcmp(cmd,"c=smail")==0) {
	sprintf(out,"%s<p align=\"center\">MAIL</p>",out);
	sprintf(out,"%s<p>mail to %s ",out,nick);
//log
	sprintf(wapfile,"%.100s/.gg/wap-query.log",home);
	if(wap=fopen(wapfile,"a")) {
	    char mojadres[101];
	    fprintf(wap,"mail %s %s\n",nick,tresc);
	    fclose(wap);
	    
	    if(mailfrom(mojadres,100)==1) {
		char *ki;
		ki=(char *)index(tresc,'|');
		if(ki!=NULL) {
		    ki++[0]=0;
		    if(sm_sendmail("127.0.0.1", mojadres, nick, tresc, ki)==1)
			sprintf(out,"%ssend</p>",out);
		    else
			sprintf(out,"%serror</p>",out);
		    }
		else
		    sprintf(out,"%serror - no body</p>",out);
		}
	    else
		sprintf(out,"%serror - no mailname</p>",out);
	    }
//endlog
    sprintf(cmd,"c=mail");
    sprintf(nick,"0");
    sprintf(tresc,"0");
    }

    //mail check
    if(strcmp(cmd,"c=mail")==0 || strcmp(cmd,"c=check")==0) {
	char buf[3000],*k;
	FILE *f;
	int cmdi,ofs;
	
	// check in iNBOX
	sprintf(buf,"/var/spool/mail/%s",logname);
	if(strcmp(nick,"0")==0)cmdi=0;
	else {
	    cmdi=(int)tresc[0]-48;
	    ofs=atoi(tresc+1);
	}

printf("Mail: cmd: %d, ofs: %d\n",cmdi,ofs);
	
	if(cmdi==0 || strcmp("INBOX",nick)==0)
	    if(wap_ckm(buf,"INBOX",buf,cmdi,ofs,10,&emails)==1) {
		if(strcmp(cmd,"c=mail")==0)
		    sprintf(out,"%s%s",out,buf);
	    }

	//check other defined mailbox'es
	sprintf(buf,"%s/.gg/wap-mailbox",home);
	if(f=fopen(buf,"r")) {
	    while(fgets(buf,199,f)) {
		k=(char *)index(buf,10);if(k!=NULL)k[0]=0;
		k=(char *)index(buf,13);if(k!=NULL)k[0]=0;
		k=(char *)index(buf,':');
		if(k!=NULL) {
		    if(strlen(buf)>0) {
			k++[0]=0;
			if(cmdi==0 || strcmp(k,nick)==0) {
			    if(buf[strlen(buf)-1]=='/') {
			    // maildir
				if(wap_ckmdir(buf,k,buf,cmdi,ofs,10,&emails)==1) {
				    if(strcmp(cmd,"c=mail")==0)
					sprintf(out,"%s%s",out,buf);
				}
			    } else {
			    // mailbox
				if(wap_ckm(buf,k,buf,cmdi,ofs,10,&emails)==1) {
				    if(strcmp(cmd,"c=mail")==0)
					sprintf(out,"%s%s",out,buf);
				}
			    }
			}
		    }
		}
	    }
	fclose(f);
	}
	if(strcmp(cmd,"c=mail")==0) {
	    if(cmdi==0)
		sprintf(out,"%s<do type=\"prev\" label=\"RTG\"><go href=\"?\"/></do>",out);
	    sprintf(out,"%s<do type=\"accept\" label=\"New mail\"><go href=\"?c=newmail;n=0;t=0\"/></do>
<do type=\"New mail\" label=\"New mail\"><go href=\"?c=newmail;n=0;t=0\"/></do>
<do type=\"RTG\" label=\"RTG\"><go href=\"?\"/></do>
<do type=\"Check\" label=\"Check\"><go href=\"?check0\"/></do></card></wml>\n",out);
	    return 0;
	}
    }

    //add nick formularz
    // formularz nigdy nie przekroczy 1500 znakow, jest statyczny
    if(strcmp(cmd,"c=fa")==0) {
	if(strcmp(nick,"0")==0) {
	    sprintf(out,"%s<p>Enter gg:</p><p><input format=\"*N\" name=\"n\" type=\"text\"/></p>
<p>Nick:<input format=\"*M\" name=\"t\" type=\"text\"/></p>
<p><anchor>Send<go href=\"?c=add;n=$(n:encode);t=$(t:encode)\" method=\"get\"/></anchor></p>",out);
	}
	// out 543
	sprintf(out,"%s<do type=\"accept\" label=\"RTG\"><go href=\"?\"/></do>
<do type=\"prev\" label=\"RTG\"><go href=\"?\"/></do>
</card></wml>\n",out);
	return 0;
    }

    //new mail formularz
    // formularz nigdy nie przekroczy 1500 znakow, jest statyczny
    if(strcmp(cmd,"c=newmail")==0) {
        if(strcmp(nick,"0")==0) {
	sprintf(out,"%s<p>Enter email:<input format=\"*M\" name=\"n\" type=\"text\"/></p>
<p>Subiect:<input format=\"*M\" name=\"su\" type=\"text\"/></p>
<p><input format=\"*M\" name=\"t\" type=\"text\"/></p>
<p><anchor>Send<go href=\"?c=smail;n=$(n:encode);t=$(su:encode)|$(t:encode)\" method=\"get\"/></anchor></p>",out);
	} else {
	sprintf(out,"%s<p>Email: %.50s</p>
<p>Subiect:<input format=\"*M\" name=\"su\" type=\"text\"/></p>
<p><input format=\"*M\" name=\"t\" type=\"text\"/></p>
<p><anchor>Send<go href=\"?c=smail;n=%.50s;$t=$(su:encode)|$(t:encode)\" method=\"get\"/></anchor></p>",out,nick,nick);
	}
	// out 543
	sprintf(out,"%s<do type=\"accept\" label=\"Mail\"><go href=\"?c=mail;n=0;t=0\"/></do>
<do type=\"prev\" label=\"Mail\"><go href=\"?c=mail;n=0;t=0\"/></do>
</card></wml>\n",out);
	return 0;
    }

    //add phone formularz
    // formularz nigdy nie przekroczy 1500 znakow, jest statyczny
    if(strcmp(cmd,"c=fp")==0) {
	if(strcmp(nick,"0")==0) {
	    sprintf(out,"%s<p>Enter nick:</p><p><input format=\"*M\" name=\"n\" type=\"text\"/></p>
<p>Phone:<input format=\"*N\" name=\"t\" type=\"text\"/></p>
<p><anchor>Send<go href=\"?c=addp;n=$(n:encode);t=$(t:encode)\" method=\"get\"/></anchor></p>",out);
	}
	// out 543
	sprintf(out,"%s<do type=\"accept\" label=\"RTG\"><go href=\"?\"/></do>
<do type=\"prev\" label=\"RTG\"><go href=\"?\"/></do>
</card></wml>\n",out);
	return 0;
    }

    
    //chce forme dla tego nicke aby przygotowac msg
    // ten tez nie przepelni out, bo nick ma ograniczona dlugosc
    if(strcmp(cmd,"c=f")==0) {
	if(strcmp(nick,"0")==0) {
	    sprintf(out,"%s<p>Enter gg/tel:</p><p><input format=\"*N\" name=\"n\" type=\"text\"/></p>",out);
	    sprintf(out,"%s<p>Message:<input format=\"*M\" name=\"t\" type=\"text\"/></p>",out);
    	    sprintf(out,"%s<p><anchor>Send<go href=\"?c=m;n=$(n:encode);t=$(t:encode)\" method=\"get\"/></anchor></p>
<p><anchor>Send SMS<go href=\"?c=sms;n=$(n:encode);t=$(t:encode)\" method=\"get\"/></anchor></p>",out);
	}
	else {
	    sprintf(out,"%s<p>Msg to %s</p>",out,nick);
	    sprintf(out,"%s<p><input format=\"*M\" name=\"t\" type=\"text\"/></p>",out);
    	    sprintf(out,"%s<p><anchor>Send<go href=\"?c=m;n=%s;t=$(t:encode)\" method=\"get\"/></anchor></p>
<p><anchor>Send SMS<go href=\"?c=sms;n=%s;t=$(t:encode)\" method=\"get\"/></anchor></p>",out,nick,nick);
	}
	sprintf(out,"%s<do type=\"accept\" label=\"RTG\"><go href=\"?\"/></do>
<do type=\"prev\" label=\"RTG\"><go href=\"?\"/></do>
</card></wml>\n",out);
	return 0;
    }

// msg service
    if(strcmp(cmd,"c=m")==0) {
	sprintf(out,"%s<p align=\"center\">MSG</p>",out);
	sprintf(out,"%s<p>msg to %s send</p>\n",out,nick);
	addtoquick(nick);
	sprintf(wapfile,"%s/.gg/pipewap",home);
	if(wap=fopen(wapfile,"r+")) {
	    fprintf(wap,"chat %s %s\n",nick,tresc);
	    fclose(wap);
	    }
	else
	    sprintf(out,"%s<p>ekg not running</p>",out);
//log 
	sprintf(wapfile,"%.100s/.gg/wap-query.log",home);
	if(wap=fopen(wapfile,"a")) {
	    fprintf(wap,"chat %s %s\n",nick,tresc);
	    fclose(wap);
	    }
//endlog
    // out 887
    }

// sms service
    if(strcmp(cmd,"c=sms")==0) {
	sprintf(out,"%s<p align=\"center\">SMS</p>",out);
	sprintf(out,"%s<p>sms to %s send</p>\n",out,nick);
	addtoquick(nick);
	sprintf(wapfile,"%s/.gg/pipewap",home);
	if(wap=fopen(wapfile,"r+")) {
	    fprintf(wap,"sms %s %s\n",nick,tresc);
	    fclose(wap);
	    }
	else
	    sprintf(out,"%s<p>ekg not running</p>",out);
//log 
	sprintf(wapfile,"%.100s/.gg/wap-query.log",home);
	if(wap=fopen(wapfile,"a")) {
	    fprintf(wap,"sms %s %s\n",nick,tresc);
	    fclose(wap);
	    }
//endlog
    // out 887
    }



// back service
    if(strcmp(cmd,"c=back")==0) {
	sprintf(out,"%s<p align=\"center\">Status: back</p>",out);
	sprintf(wapfile,"%s/.gg/pipewap",home);
	if(wap=fopen(wapfile,"r+")) {
	    fprintf(wap,"back %s\n",(tresc[0]!='0')?tresc:"");
	    fclose(wap);
	    }
	else
	    sprintf(out,"%s<p>ekg not running</p>",out);
//log 
	sprintf(wapfile,"%.100s/.gg/wap-query.log",home);
	if(wap=fopen(wapfile,"a")) {
	    fprintf(wap,"back");
	    fclose(wap);
	    }
//endlog
    // out 887
    }

// away service
    if(strcmp(cmd,"c=away")==0) {
	sprintf(out,"%s<p align=\"center\">Status: away</p>",out);
	sprintf(wapfile,"%s/.gg/pipewap",home);
	if(wap=fopen(wapfile,"r+")) {
	    fprintf(wap,"away %s\n",(tresc[0]!='0')?tresc:"");
	    fclose(wap);
	    }
	else
	    sprintf(out,"%s<p>ekg not running</p>",out);
//log 
	sprintf(wapfile,"%.100s/.gg/wap-query.log",home);
	if(wap=fopen(wapfile,"a")) {
	    fprintf(wap,"away");
	    fclose(wap);
	    }
//endlog
    // out 887
    }

// invisible service
    if(strcmp(cmd,"c=invisible")==0) {
	sprintf(out,"%s<p align=\"center\">Status: invisible</p>",out);
	sprintf(wapfile,"%s/.gg/pipewap",home);
	if(wap=fopen(wapfile,"r+")) {
	    fprintf(wap,"invisible %s\n",(tresc[0]!='0')?tresc:"");
	    fclose(wap);
	    }
	else
	    sprintf(out,"%s<p>ekg not running</p>",out);
//log 
	sprintf(wapfile,"%.100s/.gg/wap-query.log",home);
	if(wap=fopen(wapfile,"a")) {
	    fprintf(wap,"invisible");
	    fclose(wap);
	    }
//endlog
    // out 887
    }

// connect service
    if(strcmp(cmd,"c=co")==0) {
	sprintf(out,"%s<p align=\"center\">CONNECT</p>",out);
	sprintf(wapfile,"%s/.gg/pipewap",home);
	if(wap=fopen(wapfile,"r+")) {
	    fprintf(wap,"connect\n");
	    fclose(wap);
	    }
	else
	    sprintf(out,"%s<p>ekg not running</p>",out);
//log 
	sprintf(wapfile,"%.100s/.gg/wap-query.log",home);
	if(wap=fopen(wapfile,"a")) {
	    fprintf(wap,"connect\n");
	    fclose(wap);
	    }
//endlog
    // out 887
    }

// disconnect service
    if(strcmp(cmd,"c=dc")==0) {
	sprintf(out,"%s<p align=\"center\">DISCONNECT</p>",out);
	sprintf(wapfile,"%s/.gg/pipewap",home);
	if(wap=fopen(wapfile,"r+")) {
	    fprintf(wap,"disconnect %s\n",(tresc[0]!='0')?tresc:"");
	    fclose(wap);
	    }
	else
	    sprintf(out,"%s<p>ekg not running</p>",out);
//log 
	sprintf(wapfile,"%.100s/.gg/wap-query.log",home);
	if(wap=fopen(wapfile,"a")) {
	    fprintf(wap,"disconnect\n");
	    fclose(wap);
	    }
//endlog
    // out 887
    }

// add nick service
    if(strcmp(cmd,"c=add")==0) {
	sprintf(out,"%s<p align=\"center\">SEND</p>",out);
	sprintf(out,"%s<p>add %s %s</p>\n",out,nick,tresc);
	sprintf(wapfile,"%s/.gg/pipewap",home);
	if(wap=fopen(wapfile,"r+")) { //r+ ?
	    fprintf(wap,"add %s %s\n",nick,tresc);
	    fclose(wap);
	    }
	else
	    sprintf(out,"%s<p>ekg not running</p>",out);
//log
	sprintf(wapfile,"%.100s/.gg/wap-query.log",home);
	if(wap=fopen(wapfile,"a")) {
	    fprintf(wap,"add %s %s\n",nick,tresc);
	    fclose(wap);
	    }
//endlog
    //out 887
    }

// add phone service
    if(strcmp(cmd,"c=addp")==0) {
	sprintf(out,"%s<p align=\"center\">SEND</p>",out);
	sprintf(out,"%s<p>list %s -p %s</p>\n",out,nick,tresc);
	sprintf(wapfile,"%s/.gg/pipewap",home);
	if(wap=fopen(wapfile,"r+")) { //r+ ?
	    fprintf(wap,"list %s -p %s\n",nick,tresc);
	    fclose(wap);
	    }
	else
	    sprintf(out,"%s<p>ekg not running</p>",out);
//log
	sprintf(wapfile,"%.100s/.gg/wap-query.log",home);
	if(wap=fopen(wapfile,"a")) {
	    fprintf(wap,"list %s -p %s\n",nick,tresc);
	    fclose(wap);
	    }
//endlog
    //out 887
    }

// delete msg service (don't show me again)
    if(strcmp(cmd,"c=d")==0) {
	sprintf(out,"%s<p align=\"center\">DELETE</p>",out);
	sprintf(out,"%s<p>%s</p>",out,nick);

    sprintf(wapfile,"%s/.gg/",home);
    n = scandir(wapfile, &namelist, 0, alphasort);
    if (n < 0)
	perror("scandir");
    else {
	k=n;
	while(n--) {
	    sprintf(wapfile,"wap%s",nick);
	    if(strncmp(namelist[k-n-1]->d_name,wapfile,8)==0) {
		sprintf(wapfile,"%s/.gg/%s",home,namelist[k-n-1]->d_name);
		sprintf(wapfile2,"%s/.gg/old%s",home,namelist[k-n-1]->d_name);
		rename(wapfile,wapfile2);
	    }
	free(namelist[k-n-1]);
	}
    }
    free(namelist);	
    // out 388
    } //if c=d

// delete page service (don't show me page again)
    if(strcmp(cmd,"c=delp")==0) {
	sprintf(out,"%s<p align=\"center\">DELETE</p>",out);
	sprintf(out,"%s<p>%s</p>",out,tresc);

    sprintf(wapfile,"%s/.gg/",home);
    n = scandir(wapfile, &namelist, 0, alphasort);
    if (n < 0)
	perror("scandir");
    else {
	k=n;
	while(n--) {
	    if(strncmp(namelist[k-n-1]->d_name,"wap",3)==0 && namelist[k-n-1]->d_name[5]==':') {
		sprintf(wapfile,"%5.5s",(namelist[k-n-1]->d_name)+3);
		if(strstr(tresc,wapfile)!=0) {
		    sprintf(wapfile,"%s/.gg/%s",home,namelist[k-n-1]->d_name);
		    sprintf(wapfile2,"%s/.gg/old%s",home,namelist[k-n-1]->d_name);
		    rename(wapfile,wapfile2);
		}
	    }
	free(namelist[k-n-1]);
	}
    }
    free(namelist);	
    // out 388
    } //if c=d


/*

Etykieta Wartosc Znaczenie
GG_STATUS_NOT_AVAIL 0x0001 Niedostepny
GG_STATUS_NOT_AVAIL_DESCR 0x0015 Niedostepny (z opisem)
GG_STATUS_AVAIL 0x0002 Dostepny
GG_STATUS_AVAIL_DESCR 0x0004 Dostepny (z opisem)
GG_STATUS_BUSY 0x0003 Zajety
GG_STATUS_BUSY_DESCR 0x0005 Zajety (z opisem)
GG_STATUS_INVISIBLE 0x0014 Niewidoczny
GG_STATUS_INVISIBLE_DESCR 0x0016 Niewidoczny z opisem
GG_STATUS_BLOCKED 0x0006 Zablokowany
GG_STATUS_FRIENDS_MASK 0x8000 Maska bitowa oznaczajaca tryb tylko dla
przyjaciól/

*/


    if(strcmp(cmd,"c=lista")==0) {
// pobieranie userlisty
	int licz=0,la=0,kk;
	char tabmenu[10],*descr,*e,*ek;
	char gguserlist[301];
	FILE *ggul;
	
	memset(&tabmenu,0,sizeof(tabmenu));
	od=atol(nick);
	sprintf(userlist,"");
	sprintf(wapfile,"%s/.gg/wapstatus",home);
	sprintf(gguserlist,"%s/.gg/userlist",home);
	ggul=fopen(gguserlist,"r");
	if(wap=fopen(wapfile,"r")) {
	    while(fgets(wapfile,100,wap)) {
		char reason[201];
		char *k=(char *)index(wapfile,10);
		if(k!=NULL)k[0]=0;
		k=(char *)index(wapfile,':');
		if(k!=NULL) {
		//jesli linia z ekg jest prawidlowa
		    k++[0]=0;
		    descr=(char*)index(k,':');
		    if(descr!=NULL) {
			int il;
			descr++[0]=0;
			if(strlen(descr)>0) {
			    sprintf(reason,"%.200s",descr);
			    dozwolonys(reason);
			}
		    }
		    e=NULL;
		    sprintf(gguserlist,"");
		    if(ggul>0)
			if(!feof(ggul)) {
			    fgets(gguserlist,200,ggul);
    			    e=(char *)index(gguserlist,';');
			    if(e!=NULL) {
				e++[0]=0;
				e=(char *)index(e,';');
				if(e!=NULL) {
				    e++[0]=0;
				    e=(char *)index(e,';');
				    if(e!=NULL) {
					e++[0]=0;
					e=(char *)index(e,';');
					if(e!=NULL) {
					    e++[0]=0;
					    ek=(char *)index(e,';');
					    if(ek!=NULL) {
						ek[0]=0;
					    }
					}
				    }
				}
			    }
			}
		    if(strlen(wapfile)>0 && strlen(wapfile)<50 && atol(k)!=0) {
		    //umiescic czy nie na aktualnie wyswietlanej liscie 	
			int st=atol(k);
			if((od<=licz) && ((od+10)>licz) && (strlen(userlist)<3500)) {
			    sprintf(userlist,"%s<p mode=\"nowrap\">%s %s<a href=\"?c=f;n=%s;t=a\">%s</a> %s</p>\n",
				userlist,
				(icgi==0)?
				    ((st==0x01 || st==0x15 || st==0x14 || st==0x16 || st==0x06)?
				    "<img src=\"?sleep.wbmp\" alt=\"disc\"/>":
				    ((st==0x02 || st==0x04)?"<img src=\"?jest.wbmp\" alt=\"conn\"/>":
				    "<img src=\"?busy.wbmp\" alt=\"busy\"/>"))
				    :
				    ((st==0x01 || st==0x15 || st==0x14 || st==0x16 || st==0x06)?
				    "<img src=\"/rtg/sleep.wbmp\" alt=\"disc\"/>":
				    ((st==0x02 || st==0x04)?"<img src=\"/rtg/jest.wbmp\" alt=\"conn\"/>":
				    "<img src=\"/rtg/busy.wbmp\" alt=\"busy\"/>")),
				(e!=NULL)?((strlen(e)>0 && strcmp(wapfile,gguserlist)==0)?((icgi==0)?"<img src=\"?komorka.wbmp\" alt=\"tel\"/>":"<img src=\"/rtg/komorka.wbmp\" alt=\"tel\"/>"):""):"",
				wapfile,
				wapfile,
				(descr!=NULL)?reason:"");
			}
		    licz++;
		    }
		if((la/10.0)==(0.0+(la/10)) && la<100 && la>=0) {
		//jesli la sie ladnie dzieli przez 10 tzn ze to nowa strona
		    tabmenu[(int)la/10]=wapfile[0];
		    }
		la++;
		}
	    }
//	printf("strony: %s\n",tabmenu);
	for(kk=0;kk<10;kk++) {
	    if(tabmenu[kk]!=0)
		sprintf(userlist,"%s<do type=\"%c\" label=\"od %c\"><go href=\"?c=lista;n=%d;t=a\"/></do>",
		    userlist,
		    tabmenu[kk],
		    tabmenu[kk],
		    kk*10);
	    }
	fclose(wap);
	if(ggul>0)fclose(ggul);
	}
    sprintf(out,"%s<p align=\"center\">USERLIST</p>
%s
<do type=\"accept\" label=\"reload\"><go href=\"?\"/></do>
<do type=\"prev\" label=\"reload\"><go href=\"?\"/></do>
<do type=\"next\" label=\"next\"><go href=\"?c=lista;n=%d;t=a\"/></do>
<do type=\"back\" label=\"back\"><go href=\"?c=lista;n=%d;t=a\"/></do>
<do type=\"RTG\" label=\"RTG\"><go href=\"?\"/></do>
<do type=\"Check\" label=\"Check\"><go href=\"?check0\"/></do>
</card></wml>",out,userlist,od+10,((od-10)>0)?od-10:0);
    return 0;
    } //userlista

} // qstr!=0

// max out 887

// bez parametrow lub na koniec niektorych leca aktywne wiadomosci
// sprintf(out,"%s<p align=\"center\">MSG</p>\n",out); // out ~900
//printf("wap\n");
sprintf(wapfile,"%s/.gg/",home);
n = scandir(wapfile, &namelist, 0, 0 /*alphasort*/);
if (n < 0)
   perror("scandir");
else {
    int raz=0;
    k=n;
    while(n--) {
    if(strncmp(namelist[k-n-1]->d_name,"wap",3)==0 && index(namelist[k-n-1]->d_name,':')!=0) {
//	printf("%s<br>\n", namelist[k-n-1]->d_name);
	sprintf(wapfile,"%s/.gg/%s",home,namelist[k-n-1]->d_name);
	raz=0;
	if(wap=fopen(wapfile,"r")) {
	    while(!feof(wap)) {
		memset(buf,0,sizeof(buf));
		fgets(buf,1000,wap);
		if(strlen(buf)>0) {
		    char *k=(char *)index(buf,10);
		    if(k!=NULL)k[0]=0;
		    trescw=(char *)index(buf,'(');
		    if(trescw!=NULL) {
			trescw++[0]=0;
			msgs++;
			if(strcmp(cmd,"c=check")!=0) {
			//usunac niewapowe znaczki (moze jeszcze jakies ida ?
			//moze konwersje niektorych niekoniecznie na _
			    char tre[3001];
			    sprintf(tre,"%.3000s",trescw);
			    dozwolonys(tre);
			    if((strlen(out)+2*strlen(buf)+strlen(tre)+1500)<max_len) { //jesli sie jeszcze zmiesi
				sprintf(out,"%s<p><a href=\"?c=f;n=%s;t=a\">%s</a>(%s<a href=\"?c=d;n=%5.5s;t=a\"> Del</a></p>\n",out,buf,buf,tre,tre);
				if(raz++==0)
				    sprintf(delp,"%s%5.5s;",delp,tre);
			    }
			}
		    }
		}
	    }
	    fclose(wap);
	}
    }
    free(namelist[k-n-1]);
    }
free(namelist);
}

if(strcmp(cmd,"c=check")!=0) {
    sprintf(wapfile,"%.100s/.gg/wap-quick.log",home);
    if(wap=fopen(wapfile,"r")) {
	char nicktmp[51],*k;
	int ile=0;
	while(!feof(wap) && ile<3) {
	    fgets(nicktmp,50,wap);
	    k=(char *)index(nicktmp,10);
	    if(k!=NULL)k[0]=0;
	    if(strlen(nicktmp)>0) {
		sprintf(out,"%s<do type=\"lista\" label=\"%s\"><go href=\"?c=f;n=%s;t=a\"/></do>\n",out,nicktmp,nicktmp);
		ile++;
		}
	    }
	fclose(wap);
	}
    }
    else {
	sprintf(out,"%s<p align=\"center\">mail: <b>%d</b> gg: <b>%d</b></p>
<do type=\"Mailinfo\" label=\"Mail info\"><go href=\"?c=mail;n=0;t=0\"/></do>
<do type=\"accept\" label=\"gg\"><go href=\"?\"/></do>
<do type=\"prev\" label=\"gg\"><go href=\"?\"/></do>
</card></wml>",out,emails,msgs);
    return 0;
    } //if c=check

if(strlen(delp)>1) {
    sprintf(out,"%s<p align=\"center\"><a href=\"?c=delp;n=0;t=%s\">Del page</a></p>",out,delp);
}

sprintf(out,"%s<p align=\"center\"><a href=\"?\">Reload</a></p>
<do type=\"accept\" label=\"reload\"><go href=\"?\"/></do>
<do type=\"prev\" label=\"reload\"><go href=\"?\"/></do>
<do type=\"lista\" label=\"list\"><go href=\"?c=lista;n=0;t=a\"/></do>
<do type=\"msg\" label=\"msg\"><go href=\"?c=f;n=0;t=a\"/></do>
<do type=\"add\" label=\"add\"><go href=\"?c=fa;n=0;t=a\"/></do>
<do type=\"add phone\" label=\"add phone\"><go href=\"?c=fp;n=0;t=a\"/></do>
<do type=\"more\" label=\"more..\"><go href=\"#Status\"/></do>
<do type=\"Mailinfo\" label=\"Mail info\"><go href=\"?c=mail;n=0;t=0\"/></do>
</card>
<card id=\"Status\" title=\"Status\">
<p>More options in menu</p>
<p>Status with reason<input format=\"*M\" name=\"t\" type=\"text\" emptyok=\"true\"/></p>
<p><anchor>Back<go href=\"?c=back;n=0;t=$(t:encode)\" method=\"get\"/></anchor>
<anchor>Away<go href=\"?c=away;n=0;t=$(t:encode)\" method=\"get\"/></anchor>
<anchor>Invisible<go href=\"?c=invisible;n=0;t=$(t:encode)\" method=\"get\"/></anchor>
<anchor>Quit<go href=\"?c=dc;n=0;t=$(t:encode)\" method=\"get\"/></anchor></p>
<do type=\"back\" label=\"back\"><go href=\"?c=back;n=0;t=0\"/></do>
<do type=\"away\" label=\"away\"><go href=\"?c=away;n=0;t=0\"/></do>
<do type=\"invisible\" label=\"invisible\"><go href=\"?c=invisible;n=0;t=a\"/></do>
<do type=\"connect\" label=\"connect\"><go href=\"?c=co;n=0;t=0\"/></do>
<do type=\"disconnect\" label=\"disconnect\"><go href=\"?c=dc;n=0;t=0\"/></do>
<do type=\"prev\" label=\"reload\"><go href=\"?\"/></do>
<do type=\"accept\" label=\"reload\"><go href=\"?\"/></do>
</card>
</wml>\n",out); //~228
return 0;
}
