#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "b64.h"
#include "globals.h"
#include "decode.h"
#include "ckm.h"
#include "sendmail.h"
#include "utils.h"
#include "wap.h"

#include "version.c"

char vcsbeep[] = "BEGIN:VCALENDAR\n" "VERSION:1.0\n" "BEGIN:VEVENT\n" "SUMMARY:new messages\n" "CATEGORIES:RTG\n" "END:VEVENT\n" "END:VCALENDAR\n";

int is_irssi(void)
{
    DIR *d;
    char home[150];

    snprintf(home, sizeof(home), "%.100s/.irssi/scripts/rtg", (char *) getenv("HOME"));
    if ((d = opendir(home))) {
	closedir(d);
	return 1;
    }
    return 0;
}

/*
Zwraca n-te wystąpienie c w s
*/

char *nindex(const char *s, int c, int n)
{
    char *r;
    int i;

    r = (char *) s;
    for (i = 0; i < n; i++) {
	if (r)
	    r = (char *) index(r, c);
	if (r)
	    r++;
    }
    return r - 1;
}

int addtoquick(char *n)
{
    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));
    if ((wap = vfopen("r", "%.100s/wap-quick.log", home))) {
	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 = vfopen("w", "%.100s/wap-quick.log", home))) {
	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 addtoquickmsg(char *n)
{
    char msg1[2001], msg2[2001], msg3[2001], *k;
    FILE *wap;

    memset(&msg1, 0, sizeof(msg1));
    memset(&msg2, 0, sizeof(msg2));
    memset(&msg3, 0, sizeof(msg3));
    if ((wap = vfopen("r", "%.100s/wap-quickmsg.log", home))) {
	if (!feof(wap))
	    fgets(msg1, 2000, wap);
	k = (char *) index(msg1, 10);
	if (k != NULL)
	    k[0] = 0;
	if (!feof(wap))
	    fgets(msg2, 2000, wap);
	k = (char *) index(msg2, 10);
	if (k != NULL)
	    k[0] = 0;
	if (!feof(wap))
	    fgets(msg3, 2000, wap);
	k = (char *) index(msg3, 10);
	if (k != NULL)
	    k[0] = 0;
	fclose(wap);
    }
    if ((wap = vfopen("w", "%.100s/wap-quickmsg.log", home))) {
	int ok = 0;
	fprintf(wap, "%s\n", n);
	if (strcmp(n, msg1) != 0)
	    fprintf(wap, "%s\n", msg1);
	else
	    ok = 1;
	if (strcmp(n, msg2) != 0)
	    fprintf(wap, "%s\n", msg2);
	else
	    ok = 1;
	if (ok == 1 && strcmp(n, msg3) != 0)
	    fprintf(wap, "%s\n", msg3);
	fclose(wap);
    }
    return 0;
}

/* zapis konfiguracji RTG do userlisty */

int saveconfig(int graph, int rotmsg, int nopl, int emoticon, int checkreason, int ckto, int cknicks)
{
    doekg("/del 950\n");
    doekg("/add 950 rtg -n %d-%d-%d-%d-%d-%d-%d\n", graph, rotmsg, nopl, emoticon, checkreason, ckto, cknicks);
    sleep(1);
    return 0;
}

/* odczyt konfiguracji RTG z userlisty */

int loadconfig(void)
{
    int ii;
    char linia[201], wapfile[256], *n;
    FILE *wap;

    if ((wap = vfopen("r", "%.100s/userlist", home))) {
	while (fgets(linia, 200, wap)) {
	    n = (char *) nindex(linia, ';', 6);
	    if (n != NULL) {
		if (strlen(n) > 1) {
		    if (atoi(n + 1) == 950) {
			// tu i teraz w linia powinien być config
			if (debug > 0)
			    printf("[rtg] config on userlist: found\n");
			fclose(wap);
			n = (char *) index(linia, ';');
			if (!n)
			    return 0;
			n = (char *) index(n + 1, ';');
			if (!n)
			    return 0;

			ii = atoi(++n);

			// ii = graph
			snprintf(wapfile, sizeof(wapfile), "%s/wap-nograph", home);
			if (ii == 1)
			    unlink(wapfile);
			else {
			    if ((wap = fopen(wapfile, "w")))
				fclose(wap);
			}

			n = (char *) index(n + 1, '-');
			if (!n)
			    return 0;
			ii = atoi(++n);

			// ii = rotmsg
			snprintf(wapfile, sizeof(wapfile), "%s/wap-rotmsg", home);
			if (ii == 0)
			    unlink(wapfile);
			else {
			    if ((wap = fopen(wapfile, "w")))
				fclose(wap);
			}

			n = (char *) index(n + 1, '-');
			if (!n)
			    return 0;
			ii = atoi(++n);

			// ii = nopl
			snprintf(wapfile, sizeof(wapfile), "%s/wap-nopl", home);
			if (ii == 0)
			    unlink(wapfile);
			else {
			    if ((wap = fopen(wapfile, "w")))
				fclose(wap);
			}

			n = (char *) index(n + 1, '-');
			if (!n)
			    return 0;
			ii = atoi(++n);

			// ii = emoticon
			snprintf(wapfile, sizeof(wapfile), "%s/wap-emoticon", home);
			if (ii == 0)
			    unlink(wapfile);
			else {
			    if ((wap = fopen(wapfile, "w"))) {
				if (ii != 1)
				    fprintf(wap, "2\n");
				fclose(wap);
			    }
			}

			n = (char *) index(n + 1, '-');
			if (!n)
			    return 0;
			ii = atoi(++n);

			// ii = checkreason
			snprintf(wapfile, sizeof(wapfile), "%s/wap-ckreason", home);
			if (ii == 0)
			    unlink(wapfile);
			else {
			    if ((wap = fopen(wapfile, "w")))
				fclose(wap);
			}

			n = (char *) index(n + 1, '-');
			if (!n)
			    return 0;
			ii = atoi(++n);

			// ii = ckto
			snprintf(wapfile, sizeof(wapfile), "%s/wap-timeout", home);
			if (ii == 0)
			    unlink(wapfile);
			else {
			    if ((wap = fopen(wapfile, "w"))) {
				fprintf(wap, "%d", ii);
				fclose(wap);
			    }
			}

			n = (char *) index(n + 1, '-');
			if (!n)
			    return 0;
			ii = atoi(++n);

			// ii = cknicks
			snprintf(wapfile, sizeof(wapfile), "%s/wap-cknicks", home);
			if (ii == 0)
			    unlink(wapfile);
			else {
			    if ((wap = fopen(wapfile, "w"))) {
				fprintf(wap, "%d\n", ii);
				fclose(wap);
			    }
			}

			if (debug > 0)
			    printf("[rtg] load config from userlist: success\n");
			return 0;
		    }
		}
	    }
	}
	fclose(wap);
    }
    return 0;
}


//TODO: kontola max_len to dlugosc out-a

/* Glowna funkcja/cgi wap, cały engine rtg
Zwraca stronę w zmiennej out
*/

int wap(char *out, char *query_str, int max_len, int icgi, int fblw, int xe_count, int noold)
{
    struct dirent **namelist, **namelist2;
    int n, k, graph = 1, transfer = 0, connected;
    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, ckto, cknicks = 0, beep = 0;
    int emails = 0, msgs = 0;
    time_t czas = time(NULL);
    char uin[21];
    char stat2[4][201];
    char statx[300];
    int rotmsg = 0;		//kolejnosc wiadomosci 0 od najstrszej 1 od najmlodszej
    int emoticon = 0;		//zamiana emoticon na wbmp 0 nie zamieniaj 1 zamieniaj 2 zamieniaj
    //ale opis pozostaw
    int xemot = 0;		//zewnetrzne emoticony
    int jabber = 0;		//obsluga jabbera
    int nopl = 0;		//co z polskimi
    int mkbd = 1;		//wiecej klawiatury (obsluga prev i accept)
    int checkreason = 0;
    int irc = 0;		//wspolpraca z irssi

    memset(&delp, 0, sizeof(delp));
    dologu(noold, "%24.24s %.950s\n", ctime(&czas), query_str);
    snprintf(buf, sizeof(buf), "%.1000s", query_str);

    irc = is_irssi();

    if ((wap = vfopen("r", "%s/wap-nograph", home))) {
	fclose(wap);
	graph = 0;
    }
    else {
	graph = 1;
    }

    if ((wap = vfopen("r", "%s/wap-transfer", home))) {
	fclose(wap);
	transfer = 1;
    }
    else {
	transfer = 0;
    }

    if ((wap = vfopen("r", "%s/wap-rotmsg", home))) {
	fclose(wap);
	rotmsg = 1;
    }
    else {
	rotmsg = 0;
    }

    if ((wap = vfopen("r", "%s/wap-mkbd", home))) {
	fclose(wap);
	mkbd = 0;
    }

    if ((wap = vfopen("r", "%s/wap-nopl", home))) {
	char ss[5];
	ss[0] = 0;
	if (!feof(wap))
	    fgets(ss, 4, wap);
	fclose(wap);
	if (strlen(ss) > 0)
	    nopl = 2;
	else
	    nopl = 1;
    }

    if ((wap = vfopen("r", "%s/wap-xemot", home))) {
	fclose(wap);
	xemot = 1;
    }

    if ((wap = vfopen("r", "%s/wap-jabber", home))) {
	fclose(wap);
	if (!fblw)
	    jabber = 1;
    }

    if ((wap = vfopen("r", "%s/wap-emoticon", home))) {
	char ss[5];

	ss[0] = 0;
	if (!feof(wap))
	    fgets(ss, 4, wap);
	fclose(wap);
	if (strlen(ss) > 0)
	    emoticon = 2;
	else
	    emoticon = 1;
    }

    if ((wap = vfopen("r", "%s/wap-beep", home))) {
	char ss[5];

	snprintf(ss, 5, "0");
	if (!feof(wap))
	    fgets(ss, 4, wap);
	fclose(wap);
	beep = atoi(ss);
    }

    if ((wap = vfopen("r", "%s/wap-ckreason", home))) {
	fclose(wap);
	checkreason = 1;
    }

    memset(&uin, 0, sizeof(uin));
    if ((wap = vfopen("r", "%s/config", home))) {
	char tmpuin[51];
	char *k;

	if (!feof(wap)) {
	    fgets(tmpuin, 50, wap);
	    k = (char *) index(tmpuin, 10);
	    if (k != NULL)
		k[0] = 0;
	    k = (char *) index(tmpuin, 13);
	    if (k != NULL)
		k[0] = 0;
	    if (strncmp(tmpuin, "uin ", 4) == 0)
		snprintf(uin, sizeof(uin), "%.20s", tmpuin + 4);
	}
	fclose(wap);
    }

    ckto = 0;
    if ((wap = vfopen("r", "%s/wap-timeout", home))) {
	char cktos[49];
	if (!feof(wap)) {
	    fgets(cktos, 50, wap);
	    ckto = atol(cktos);
	}
	fclose(wap);
    }
    if (ckto == 0)
	ckto = 3000;

    if ((wap = vfopen("r", "%s/wap-cknicks", home))) {
	char cktos[49];
	if (!feof(wap)) {
	    fgets(cktos, 50, wap);
	    cknicks = atol(cktos);
	}
	fclose(wap);
    }
    if (cknicks > 4)
	cknicks = 4;
    if (cknicks < 0)
	cknicks = 0;

    snprintf(out, max_len, "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n"
	     "Cache-Control: no-cache, must-revalidate\r\n"
	     "Pragma: no-cache\r\n"
	     "Content-type: text/vnd.wap.wml\r\n"
	     "\r\n"
	     "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
	     "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum.org/DTD/wml_1.1.xml\">\n"
	     "\n"
	     "<wml>\n"
	     "<head>"
	     "<meta forua=\"true\" http-equiv=\"Cache-Control\" content=\"max-age=0\"/>"
	     "%s" "</head>" "\n", (nopl == 2) ? "<meta http-equiv=\"Content-type\" content=\"text/vnd.wap.wml;charset=iso-8859-1\"/>" : "");

    if (strncmp(buf, "check", 5) != 0) {
	snprintf(out + strlen(out), max_len - strlen(out), "<card id=\"gg\" title=\"RTG %s\" newcontext=\"true\">", uin);
	if (strlen(buf) == 0 && graph == 1) {
	    snprintf(out + strlen(out), max_len - strlen(out),
		     "<p align=\"center\"><img src=\"%sekg.%s\" alt=\"ekg-wap\"/></p>"
		     "<p align=\"center\"></p>", (icgi == 0) ? "?" : "/rtg/", (xemot) ? "gif" : "wbmp");
	}
    }
    else {
	char *cp;
	int cpi;

	cp = (char *) index(buf, ';');
	if (cp) {
	    cp++;
	    cpi = atoi(cp);
	    snprintf(buf, sizeof(buf), "c=check;n=%d;t=0", cpi);
	}
	else
	    snprintf(buf, sizeof(buf), "c=check;n=a;t=0");	//żeby się później dobrze wykonało
    }

// Zobacz czy ekg jest polaczony
// weź status do 4 osób dla strony check

    memset(&stat2[0], 0, sizeof(stat2[0]));
    memset(&stat2[1], 0, sizeof(stat2[1]));
    memset(&stat2[2], 0, sizeof(stat2[2]));
    memset(&stat2[3], 0, sizeof(stat2[3]));

    connected = 1;
    if ((wap = vfopen("r", "%s/wapstatus", home))) {
	int w;
	fgets(wapfile, sizeof(wapfile) - 1, wap);

	for (w = 0; w < cknicks; w++) {
	    if (!feof(wap)) {
		char *k;

		fgets(stat2[w], sizeof(stat2[w]) - 1, wap);
		k = (char *) index(stat2[w], 10);
		if (k != NULL)
		    k[0] = 0;
		k = (char *) index(stat2[w], 13);
		if (k != NULL)
		    k[0] = 0;
	    }
	}

	fclose(wap);
	if (wapfile[0] == 'D') {
	    if (strncmp(buf, "c=check", 7) != 0) {
		snprintf(out + strlen(out), max_len - strlen(out),
			 "<p align=\"center\"><small>Ekg not connected, try <a href=\"?c=co;n=0;t=0\">connect</a> first</small></p>");
	    }
	    connected = 0;
	}
    }

// status 2 osób na check z grafiką
    memset(statx, 0, sizeof(statx));
    if (strncmp(buf, "c=check", 7) == 0 && cknicks) {
	int z;
	snprintf(statx + strlen(statx), sizeof(statx) - strlen(statx), "<p>");
	for (z = 0; z < cknicks; z++) {
	    if (strlen(stat2[z]) > 0) {
		int st = 0;
		char *k, *reason, reasontxt[200];

		k = (char *) index(stat2[z], ':');
		if (k != NULL) {

		    k++[0] = 0;

		    reason = (char *) index(k, ':');
		    if (reason != NULL) {
			reason++[0] = 0;
			snprintf(reasontxt, sizeof(reasontxt), "%s", reason);
			dozwolonys(reasontxt, sizeof(reasontxt), 1, 0, 0, nopl, xe_count);
		    }
		    st = atol(k);

		    if (graph) {
			if (xemot) {
			    snprintf(statx + strlen(statx),
				     sizeof(statx) - strlen(statx),
				     " %s%s%s%s\n",
				     ((icgi == 0) ?
				      ((st == 0x01 || st == 0x15 || st == 0x14 || st == 0x16 || st == 0x06) ?
				       "<img src=\"?sleep.gif\" alt=\"disc\"/>"
				       : ((st == 0x02 || st == 0x04) ? "<img src=\"?jest.gif\" alt=\"conn\"/>" : "<img src=\"?busy.gif\" alt=\"busy\"/>")
				      )
				      : ((st == 0x01 || st == 0x15 || st == 0x14 || st == 0x16 || st == 0x06) ?
					 "<img src=\"/rtg/sleep.gif\" alt=\"disc\"/>"
					 :
					 ((st == 0x02 || st == 0x04) ?
					  "<img src=\"/rtg/jest.gif\" alt=\"conn\"/>"
					  :
					  "<img src=\"/rtg/busy.gif\" alt=\"busy\"/>"))),
				     stat2[z], (reason && checkreason) ? " " : "", (reason && checkreason) ? reasontxt : "");
			}
			else {
			    snprintf(statx + strlen(statx),
				     sizeof(statx) - strlen(statx),
				     " %s%s%s%s\n",
				     ((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\"/>"))),
				     stat2[z], (reason && checkreason) ? " " : "", (reason && checkreason) ? reasontxt : "");
			}
		    }
		    else {
			snprintf(statx + strlen(statx),
				 sizeof(statx) - strlen(statx),
				 " %s%s%s%s\n",
				 ((st == 0x01 || st == 0x15 || st == 0x14
				   || st == 0x16
				   || st == 0x06) ? "- " : ((st == 0x02
							     || st ==
							     0x04) ? "+ " :
							    "/ ")), stat2[z], (reason && checkreason) ? " " : "", (reason && checkreason) ? reasontxt : "");
		    }
		}
	    }
	}
	snprintf(statx + strlen(statx), sizeof(statx) - strlen(statx), "</p>");
    }

//dlugosc out=318

    memset(&cmd, 0, sizeof(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) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p>Blad skladni 1</p></card></wml>");
	    return 0;
	}
	nickw++[0] = 0;
	trescw = (char *) index(nickw, ';');
	if (trescw == NULL) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p>Blad skladni 2</p></card></wml>");
	    return 0;
	}
	trescw++[0] = 0;

	if (strlen(nickw) < 3 || strlen(trescw) < 2) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p>Blad skladni 3</p></card></wml>");
	    return 0;
	}

	snprintf(cmd, sizeof(cmd), "%.50s", buf);
	snprintf(nick, sizeof(nick), "%.50s", nickw + 2);
	snprintf(tresc, sizeof(tresc), "%.1000s", trescw + 2);

// usunac enkodowane znaczki z tresci

	decode(nick, 50, nopl);
	decode(tresc, 1000, nopl);

// wspolpraca z irssi

	if (irc && (strcmp(cmd, "c=ircsend") == 0) && (fblw == 0)) {
	    if (strcmp(nick, "0") == 0 || tresc[0] == '/') {
		doirssi("snd 0 %s\n", tresc);
	    }
	    else {
		char tmpnick[100], *ircnet;
		int pos;

		pos = b64_decode(nick, tmpnick, sizeof(tmpnick));
		tmpnick[pos] = 0;
		ircnet = rindex(tmpnick, '@');
		if (ircnet) {
		    ircnet++[0] = 0;
		    doirssi("snd %s /msg %s %s\n", ircnet, tmpnick, tresc);
		}
	    }
	    snprintf(cmd, sizeof(cmd), "c=irc");
	}

	if (irc && (strcmp(cmd, "c=irc") == 0 || strcmp(cmd, "c=ircdel") == 0)
	    && (fblw == 0)) {
	    char ircp[150];
	    char todel[2000] = "";
	    char tmpnick[100];
	    int pos;

	    if (strcmp(nick, "0") != 0) {
		pos = b64_decode(nick, tmpnick, sizeof(tmpnick));
		tmpnick[pos] = 0;
		snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">IRC %s</p>", tmpnick);
	    }
	    else {
		snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">IRSSI</p>");
	    }

	    snprintf(out + strlen(out), max_len - strlen(out), "<p>");

	    snprintf(ircp, sizeof(ircp), "%.100s/.irssi/scripts/rtg", (char *) getenv("HOME"));

// glowne okno
	    if (strcmp(nick, "0") == 0) {
		n = scandir(ircp, &namelist, 0, alphasort);
		if (n < 0)
		    perror("scandir");
		else {
		    k = n;
		    while (n--) {
			if (namelist[k - n - 1]->d_name[0] != '.') {
			    char tmpkan[200];
			    char tmppath[150];
			    int tn, pos;

			    snprintf(tmppath, sizeof(tmppath), "%s/%s", ircp, namelist[k - n - 1]->d_name);
			    tn = scandir(tmppath, &namelist2, 0, alphasort);
			    if (tn >= 0)
				free(namelist2);

			    pos = b64_decode(namelist[k - n - 1]->d_name, tmpkan, sizeof(tmpkan));
			    tmpkan[pos] = 0;
			    dozwolonys(tmpkan, sizeof(tmpkan), (emoticon)
				       ? ((emoticon == 1) ? 4 : 5) : 1, xemot, icgi, nopl, xe_count);

			    snprintf(out + strlen(out), max_len - strlen(out),
				     "<a href=\"?c=irc;n=%s;t=0\">%s (%d)</a><br/>", namelist[k - n - 1]->d_name, tmpkan, ((tn - 2) >= 0) ? tn - 2 : 0);
			}
			free(namelist[k - n - 1]);
		    }
		    free(namelist);
		}
	    }
	    else {
		char *ri;

// a nawet tylko base64
		ri = (char *) index(nick, '.');
		if (ri)
		    ri[0] = '_';
		ri = (char *) index(nick, '/');
		if (ri)
		    ri[0] = '_';
//

		snprintf(ircp, sizeof(ircp), "%.100s/.irssi/scripts/rtg/%s", (char *) getenv("HOME"), nick);

		n = scandir(ircp, &namelist, 0, alphasort);
		if (n < 0)
		    perror("scandir");
		else {
		    k = n;
		    while (n--) {
			char bufx[1024];
			FILE *f;
			if (namelist[k - n - 1]->d_name[0] != '.') {
			    if (strcmp(cmd, "c=ircdel") == 0) {
				if (strstr(tresc, namelist[k - n - 1]->d_name) != 0) {
				    snprintf(bufx, sizeof(bufx), "%s/%s", ircp, namelist[k - n - 1]->d_name);
				    unlink(bufx);
				}
			    }

			    if ((f = vfopen("r", "%s/%s", ircp, namelist[k - n - 1]->d_name))) {
				while (fgets(bufx, 1023, f)) {
				    char *ks;
				    char tmpczas[8];
				    long int tt;

				    ks = (char *) index(bufx, 13);
				    if (ks)
					ks[0] = 0;
				    ks = (char *) index(bufx, 10);
				    if (ks)
					ks[0] = 0;

				    tt = atol(namelist[k - n - 1]->d_name);
				    strftime(tmpczas, sizeof(tmpczas) - 1, "%H:%M", localtime((time_t *) & tt));

				    dozwolonys(bufx, sizeof(bufx), (emoticon)
					       ? ((emoticon == 1) ? 4 : 5) : 1, xemot, icgi, nopl, xe_count);
				    if ((strlen(out) + strlen(bufx) + strlen(todel) + 522) < max_len) {	//jesli sie jeszcze zmiesi
					snprintf(out + strlen(out), max_len - strlen(out), "<b>%s</b> %s<br/>", tmpczas, bufx);
					snprintf(todel + strlen(todel), sizeof(todel) - strlen(todel), "%s;", namelist[k - n - 1]->d_name);
				    }
				}
				fclose(f);
			    }
			}
			free(namelist[k - n - 1]);
		    }
		    free(namelist);

		    if (strlen(todel) > 0) {
			snprintf(out + strlen(out), max_len - strlen(out), "<a href=\"?c=ircdel;n=%s;t=%s\">del</a>", nick, todel);
		    }
		}
	    }

	    snprintf(out + strlen(out), max_len - strlen(out),
		     "</p><p><input format=\"*M\" name=\"t\" type=\"text\" emptyok=\"true\"/></p>"
		     "<p><anchor>Send<go href=\"?c=ircsend;n=%s;t=$(t:escape)\" method=\"get\"/></anchor></p>", nick);
	    snprintf(out + strlen(out), max_len - strlen(out), "<p><a href=\"?c=irc;n=%s;t=0\">Reload</a></p>", nick);

	    snprintf(out + strlen(out), max_len - strlen(out),
		     "<do type=\"irc\" label=\"Irssi\"><go href=\"?c=irc;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");
	    return 0;
	}

// send mail service
	if ((strcmp(cmd, "c=smail") == 0) && (fblw == 0)) {
	    char mojadres[101];

	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">MAIL</p>" "<p>mail to %s ", nick);
	    dologu(noold, "mail %s %s\n", nick, tresc);

	    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)
			snprintf(out + strlen(out), max_len - strlen(out), "send</p>");
		    else
			snprintf(out + strlen(out), max_len - strlen(out), "error</p>");
		}
		else
		    snprintf(out + strlen(out), max_len - strlen(out), "error - no body</p>");
	    }
	    else
		snprintf(out + strlen(out), max_len - strlen(out), "error - no mailname</p>");

	    snprintf(cmd, sizeof(cmd), "c=mail");
	    snprintf(nick, sizeof(nick), "0");
	    snprintf(tresc, sizeof(tresc), "0");
	}

	//mail check
	if ((strcmp(cmd, "c=mail") == 0 && fblw == 0)
	    || strcmp(cmd, "c=check") == 0) {
	    char buf[5000], *k;
	    FILE *f;
	    int cmdi, ofs = 0;

	    // check in iNBOX
	    snprintf(buf, sizeof(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, ((max_len - 800) < sizeof(buf)) ? max_len - 800 : sizeof(buf), cmdi, ofs, 10, &emails, nopl, xe_count) == 1) {
		    if (strcmp(cmd, "c=mail") == 0)
			snprintf(out + strlen(out), max_len - strlen(out), "%s", buf);
		}

	    //check other defined mailbox'es
	    if ((f = vfopen("r", "%s/wap-mailbox", home))) {
		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,
					 ((max_len - 800) < sizeof(buf)) ? max_len - 800 : sizeof(buf), cmdi, ofs, 10, &emails, nopl, xe_count) == 1) {
					if (strcmp(cmd, "c=mail") == 0)
					    snprintf(out + strlen(out), max_len - strlen(out), "%s", buf);
				    }
				}
				else {
				    // mailbox
				    if (wap_ckm
					(buf, k, buf,
					 ((max_len - 600) < sizeof(buf)) ? max_len - 600 : sizeof(buf), cmdi, ofs, 10, &emails, nopl, xe_count) == 1) {
					if (strcmp(cmd, "c=mail") == 0)
					    snprintf(out + strlen(out), max_len - strlen(out), "%s", buf);
				    }
				}
			    }
			}
		    }
		}
		fclose(f);
	    }

	    if (strcmp(cmd, "c=mail") == 0) {
		if (cmdi == 0 && mkbd) {
		    snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"prev\" label=\"RTG\"><go href=\"?\"/></do>");
		}
		if (mkbd)
		    snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"accept\" label=\"New mail\"><go href=\"?c=newmail;n=0;t=0\"/></do>");
		snprintf(out + strlen(out), max_len - strlen(out),
			 "<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");
		return 0;
	    }
	}

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

	//new mail formularz
	// formularz nigdy nie przekroczy 1500 znakow, jest statyczny
	if (strcmp(cmd, "c=newmail") == 0) {
	    if (strcmp(nick, "0") == 0) {
		snprintf(out + strlen(out), max_len - strlen(out),
			 "<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:escape);t=$(su:escape);$(t:escape)\" method=\"get\"/></anchor></p>");
	    }
	    else {
		snprintf(out + strlen(out), max_len - strlen(out),
			 "<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:escape);$(t:escape)\" method=\"get\"/></anchor></p>", nick, nick);
	    }
	    // out 543
	    if (mkbd)
		snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"accept\" label=\"Mail\"><go href=\"?c=mail;n=0;t=0\"/></do>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"prev\" label=\"Mail\"><go href=\"?c=mail;n=0;t=0\"/></do></card></wml>\n");
	    return 0;
	}

	//add phone formularz
	// formularz nigdy nie przekroczy 1500 znakow, jest statyczny
	if (strcmp(cmd, "c=fp") == 0) {
	    if (strcmp(nick, "0") == 0) {
		snprintf(out + strlen(out), max_len - strlen(out),
			 "<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:escape);t=$(t:escape)\" method=\"get\"/></anchor></p>");
	    }
	    // out 543
	    if (mkbd)
		snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"accept\" label=\"RTG\"><go href=\"?\"/></do>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"prev\" label=\"RTG\"><go href=\"?\"/></do></card></wml>\n");
	    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) {
		snprintf(out + strlen(out), max_len - strlen(out),
			 "<p>Enter gg/jid/tel:</p><p><input format=\"*M\" name=\"n\" type=\"text\"/></p>"
			 "<p>Message:<input format=\"*M\" name=\"t\" type=\"text\"/></p>"
			 "<p><anchor>Send<go href=\"?c=m;n=$(n:escape);t=$(t:escape)\" method=\"get\"/></anchor></p>"
			 "<p><anchor>Send SMS<go href=\"?c=sms;n=$(n:escape);t=$(t:escape)\" method=\"get\"/></anchor></p>");
	    }
	    else {
		char targetuin[21], subscribe[1000];
		FILE *f;
		char qmsg[3][2001], *rr;

		memset(&qmsg[0], 0, sizeof(qmsg[0]));
		memset(&qmsg[1], 0, sizeof(qmsg[1]));
		memset(&qmsg[2], 0, sizeof(qmsg[2]));

		if ((f = vfopen("r", "%s/wap-quickmsg.log", home))) {
		    if (!feof(f))
			fgets(qmsg[0], 2000, f);
		    rr = (char *) index(qmsg[0], 10);
		    if (rr)
			rr[0] = 0;
		    if (!feof(f))
			fgets(qmsg[1], 2000, f);
		    rr = (char *) index(qmsg[1], 10);
		    if (rr)
			rr[0] = 0;
		    if (!feof(f))
			fgets(qmsg[2], 2000, f);
		    rr = (char *) index(qmsg[2], 10);
		    if (rr)
			rr[0] = 0;
		    fclose(f);
		}

		qmsg[0][50] = 0;
		qmsg[1][50] = 0;
		qmsg[2][50] = 0;

		dozwolonys(qmsg[0], 2000, (emoticon) ? ((emoticon == 1) ? 4 : 5) : 1, xemot, icgi, nopl, xe_count);
		dozwolonys(qmsg[1], 2000, (emoticon) ? ((emoticon == 1) ? 4 : 5) : 1, xemot, icgi, nopl, xe_count);
		dozwolonys(qmsg[2], 2000, (emoticon) ? ((emoticon == 1) ? 4 : 5) : 1, xemot, icgi, nopl, xe_count);

		rr = (char *) index(nick, '@');
		if (rr && jabber) {
		    snprintf(out + strlen(out), max_len - strlen(out), "<p>Msg to %s</p>", nick);
		    snprintf(subscribe, sizeof(subscribe),
			     "<p align=\"center\">Presence</p>"
			     "<p><anchor>Subscribe<go href=\"?c=sub;n=%s;t=1\" method=\"get\"/></anchor></p>"
			     "<p><anchor>Unsubscribe<go href=\"?c=sub;n=%s;t=0\" method=\"get\"/></anchor></p>"
			     "<p><anchor>Approve Request<go href=\"?c=sub;n=%s;t=3\" method=\"get\"/></anchor></p>"
			     "<p><anchor>Deny Request<go href=\"?c=sub;n=%s;t=2\" method=\"get\"/></anchor></p>", nick, nick, nick, nick);
		}
		else {
		    memset(&targetuin, 0, sizeof(targetuin));
		    gettuin(nick, targetuin, sizeof(targetuin));
		    snprintf(out + strlen(out), max_len - strlen(out), "<p>Msg to %s/%s</p>", nick, targetuin);
		}
		snprintf(out + strlen(out), max_len - strlen(out),
			 "<p><input format=\"*M\" name=\"t\" type=\"text\" emptyok=\"true\"/></p>"
			 "<p><anchor>Send<go href=\"?c=m;n=%s;t=$(t:escape)\" method=\"get\"/></anchor></p>"
			 "<p><anchor>Send SMS<go href=\"?c=sms;n=%s;t=$(t:escape)\" method=\"get\"/></anchor></p>"
			 "<p><anchor>Send: tak<go href=\"?c=m;n=%s;t=tak\" method=\"get\"/></anchor></p>"
			 "<p><anchor>Send: nie<go href=\"?c=m;n=%s;t=nie\" method=\"get\"/></anchor></p>"
			 "<p><anchor>Send: :)<go href=\"?c=m;n=%s;t=%%3a%%29\" method=\"get\"/></anchor></p>"
			 "%s%s%s%s%s"
			 "%s%s%s%s%s"
			 "%s%s%s%s%s"
			 "<p><anchor>History<go href=\"?c=his;n=%s;t=0\" method=\"get\"/></anchor></p>"
			 "%s"
			 "<p><anchor>Delete %s<go href=\"?c=del;n=%s;t=0\" method=\"get\"/></anchor></p>",
			 nick,
			 nick,
			 nick,
			 nick,
			 nick,
			 (strlen(qmsg[0]) == 0) ? "" : "<p><anchor>Send: ",
			 (strlen(qmsg[0]) == 0) ? "" : qmsg[0],
			 (strlen(qmsg[0]) ==
			  0) ? "" : "...<go href=\"?c=mq;n=",
			 (strlen(qmsg[0]) == 0) ? "" : nick,
			 (strlen(qmsg[0]) ==
			  0) ? "" : ";t=0\" method=\"get\"/></anchor></p>",
			 (strlen(qmsg[1]) == 0) ? "" : "<p><anchor>Send: ",
			 (strlen(qmsg[1]) == 0) ? "" : qmsg[1],
			 (strlen(qmsg[1]) ==
			  0) ? "" : "...<go href=\"?c=mq;n=",
			 (strlen(qmsg[1]) == 0) ? "" : nick,
			 (strlen(qmsg[1]) ==
			  0) ? "" : ";t=1\" method=\"get\"/></anchor></p>",
			 (strlen(qmsg[2]) == 0) ? "" : "<p><anchor>Send: ",
			 (strlen(qmsg[2]) == 0) ? "" : qmsg[2],
			 (strlen(qmsg[2]) ==
			  0) ? "" : "...<go href=\"?c=mq;n=",
			 (strlen(qmsg[2]) == 0) ? "" : nick,
			 (strlen(qmsg[2]) ==
			  0) ? "" : ";t=2\" method=\"get\"/></anchor></p>", (rr && jabber) ? nick : targetuin, (rr && jabber) ? subscribe : "", nick, nick);
	    }
	    if (mkbd)
		snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"accept\" label=\"RTG\"><go href=\"?\"/></do>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"prev\" label=\"RTG\"><go href=\"?\"/></do></card></wml>\n");
	    return 0;
	}

	if (strcmp(cmd, "c=rt") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out),
		     "<p>Check timeout (s):</p><p><input format=\"*N\" name=\"n\" type=\"text\"/></p>"
		     "<p><anchor>Set<go href=\"?c=rts;n=$(n:escape);t=0\" method=\"get\"/></anchor></p>"
		     "<do type=\"accept\" label=\"RTG\"><go href=\"?\"/></do>" "<do type=\"prev\" label=\"RTG\"><go href=\"?\"/></do>" "</card></wml>\n");
	    return 0;
	}

	if (strcmp(cmd, "c=cknicks") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out),
		     "<p>Check nicks:</p><p><input format=\"*N\" name=\"n\" type=\"text\"/></p>"
		     "<p><anchor>Set<go href=\"?c=cknickss;n=$(n:escape);t=0\" method=\"get\"/></anchor></p>"
		     "<do type=\"accept\" label=\"RTG\"><go href=\"?\"/></do>" "<do type=\"prev\" label=\"RTG\"><go href=\"?\"/></do>" "</card></wml>\n");
	    return 0;
	}

// rts - ustaw timeout dla check
	if (strcmp(cmd, "c=rts") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">Check :%s</p>", nick);
	    if ((wap = vfopen("w", "%s/wap-timeout", home))) {
		fprintf(wap, "%d\n", (int) atol(nick) * 10);
		fclose(wap);
	    }
	    else
		snprintf(out + strlen(out), max_len - strlen(out), "<p>error open file</p>");
	    dologu(noold, "rtg %s\n", nick);
	}

// cknicks - ustaw ilosc nickow dla check 0 - 4
	if (strcmp(cmd, "c=cknickss") == 0) {
	    int ii = 0;
	    if ((wap = vfopen("w", "%s/wap-cknicks", home))) {
		ii = (int) atol(nick);
		if (ii < 0)
		    ii = 0;
		if (ii > 4)
		    ii = 4;
		fprintf(wap, "%d\n", ii);
		fclose(wap);
		snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">Check nicks:%d</p>", ii);
	    }
	    else
		snprintf(out + strlen(out), max_len - strlen(out), "<p>error open file</p>");
	    dologu(noold, "ch nicks %d\n", ii);
	}

// beep
	if (strcmp(cmd, "c=sound") == 0) {
	    int ii = 0;
	    if ((wap = vfopen("w", "%s/wap-beep", home))) {
		ii = (int) atol(nick);
		if (ii < 0)
		    ii = 0;
		if (ii > 2)
		    ii = 2;
		fprintf(wap, "%d\n", ii);
		fclose(wap);
		snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">Sound: %s</p>", (ii == 0) ? "off" : (ii == 1) ? "s45" : "vcs");
	    }
	    else
		snprintf(out + strlen(out), max_len - strlen(out), "<p>error open file</p>");
	    dologu(noold, "sound %d\n", ii);
	}

// msg service
	if (strcmp(cmd, "c=m") == 0) {
	    char *kc;
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">MSG</p>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<p>msg to %s send</p>\n", nick);
	    addtoquick(nick);
	    addtoquickmsg(tresc);
	    kc = (char *) index(nick, '@');
	    if (kc && jabber) {
		iso2jabber(tresc, sizeof(tresc));
		if (!dojbr("chat %s %s\n", nick, tresc))
		    snprintf(out + strlen(out), max_len - strlen(out), "<p>uje not running</p>");
	    }
	    else {
		if (!doekg("/chat %s %s\n", nick, tresc))
		    snprintf(out + strlen(out), max_len - strlen(out), "<p>ekg not running</p>");
	    }
	    dologu(noold, "chat %s %s\n", nick, tresc);
	    // out 887
	}

// subskrypcje jabbera
	if ((strcmp(cmd, "c=sub") == 0) && jabber) {
	    int t = atol(tresc);
	    snprintf(out + strlen(out), max_len - strlen(out),
		     "<p align=\"center\">SUBSCRIBE<br/>%s %s</p>", nick, (t == 0) ? "off" : (t == 1) ? "on" : (t == 2) ? "acc" : "deny");
	    if (t == 1) {
		if (!dojbr("sub %s\n", nick))
		    snprintf(out + strlen(out), max_len - strlen(out), "<p>uje not running</p>");
	    }
	    else if (t == 0) {
		if (!dojbr("uns %s\n", nick))
		    snprintf(out + strlen(out), max_len - strlen(out), "<p>uje not running</p>");
	    }
	    else if (t == 2) {
		if (!dojbr("sdr %s\n", nick))	//deny request
		    snprintf(out + strlen(out), max_len - strlen(out), "<p>uje not running</p>");
	    }
	    else {
		if (!dojbr("sar %s\n", nick))	// accept request
		    snprintf(out + strlen(out), max_len - strlen(out), "<p>uje not running</p>");
	    }

	    dologu(noold, "subscribe %s %s\n", nick, tresc);
	}

// msg quick service
	if (strcmp(cmd, "c=mq") == 0) {
	    FILE *f;
	    char qmsg[3][2001], *rr, *kc;
	    int s;

	    if ((f = vfopen("r", "%s/wap-quickmsg.log", home))) {
		if (!feof(f))
		    fgets(qmsg[0], 2000, f);
		rr = (char *) index(qmsg[0], 10);
		if (rr)
		    rr[0] = 0;
		if (!feof(f))
		    fgets(qmsg[1], 2000, f);
		rr = (char *) index(qmsg[1], 10);
		if (rr)
		    rr[0] = 0;
		if (!feof(f))
		    fgets(qmsg[2], 2000, f);
		rr = (char *) index(qmsg[2], 10);
		if (rr)
		    rr[0] = 0;
		fclose(f);
	    }

	    s = atoi(tresc);
	    if (s < 0)
		s = 0;
	    if (s > 2)
		s = 2;

	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">MSG</p>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<p>msg to %s send</p>\n", nick);
	    addtoquick(nick);
	    dologu(noold, "chat %s %s\n", nick, qmsg[s]);

	    kc = (char *) index(nick, '@');
	    if (kc && jabber) {
		iso2jabber(qmsg[s], sizeof(qmsg[s]));
		if (!dojbr("chat %s %s\n", nick, qmsg[s]))
		    snprintf(out + strlen(out), max_len - strlen(out), "<p>uje not running</p>");
	    }
	    else {
		if (!doekg("/chat %s %s\n", nick, qmsg[s]))
		    snprintf(out + strlen(out), max_len - strlen(out), "<p>ekg not running</p>");
	    }

	    // out 887
	}

// sms service
	if (strcmp(cmd, "c=sms") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">SMS</p>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<p>sms to %s send</p>\n", nick);
	    addtoquick(nick);
	    if (!doekg("/sms %s %s\n", nick, tresc))
		snprintf(out + strlen(out), max_len - strlen(out), "<p>ekg not running</p>");
	    dologu(noold, "sms %s %s\n", nick, tresc);
	    // out 887
	}

// back service
	if (strcmp(cmd, "c=back") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">Status: back</p>");
	    if (!doekg("/back %s\n", (tresc[0] != '0') ? tresc : ""))
		snprintf(out + strlen(out), max_len - strlen(out), "<p>ekg not running</p>");
	    dologu(noold, "back\n");
	    // out 887
	}

// away service
	if (strcmp(cmd, "c=away") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">Status: away</p>");
	    if (!doekg("/away %s\n", (tresc[0] != '0') ? tresc : ""))
		snprintf(out + strlen(out), max_len - strlen(out), "<p>ekg not running</p>");
	    dologu(noold, "away\n");
	    // out 887
	}

// invisible service
	if (strcmp(cmd, "c=invisible") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">Status: invisible</p>");
	    if (!doekg("/invisible %s\n", (tresc[0] != '0') ? tresc : ""))
		snprintf(out + strlen(out), max_len - strlen(out), "<p>ekg not running</p>");
	    dologu(noold, "invisible\n");
	}

// connect service
	if (strcmp(cmd, "c=co") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">CONNECT</p>");
	    if (!doekg("/connect\n"))
		snprintf(out + strlen(out), max_len - strlen(out), "<p>ekg not running</p>");
	    dologu(noold, "connect\n");
	}

// download list from server gg service
	if (strcmp(cmd, "c=listl") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">DOWNLOAD LIST</p>");
	    if (!doekg("/list -g\n"))
		snprintf(out + strlen(out), max_len - strlen(out), "<p>ekg not running</p>");
	    dologu(noold, "download list\n");
	    sleep(2);
	    loadconfig();
	}

// save list to server gg service
	if (strcmp(cmd, "c=lists") == 0) {
	    saveconfig(graph, rotmsg, nopl, emoticon, checkreason, ckto, cknicks);
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">SAVE LIST</p>");
	    if (!doekg("/list -p\n"))
		snprintf(out + strlen(out), max_len - strlen(out), "<p>ekg not running</p>");
	    dologu(noold, "save list\n");
	}

// disconnect gg service
	if (strcmp(cmd, "c=dc") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">DISCONNECT</p>");
	    if (!doekg("/disconnect %s\n", (tresc[0] != '0') ? tresc : ""))
		snprintf(out + strlen(out), max_len - strlen(out), "<p>ekg not running</p>");
	    dologu(noold, "disconnect\n");
	}

// priv on gg
	if (strcmp(cmd, "c=privon") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">PRIVATE ON</p>");
	    if (!doekg("/private 1\n"))
		snprintf(out + strlen(out), max_len - strlen(out), "<p>ekg not running</p>");
	    dologu(noold, "private on\n");
	}
// priv off gg
	if (strcmp(cmd, "c=privoff") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">PRIVATE OFF</p>");
	    if (!doekg("/private 0\n"))
		snprintf(out + strlen(out), max_len - strlen(out), "<p>ekg not running</p>");
	    dologu(noold, "private on\n");
	}

// add nick service gg
	if (strcmp(cmd, "c=add") == 0) {
	    char *rr;
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">ADD</p>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<p>%s %s</p>\n", nick, tresc);
	    rr = (char *) index(nick, '@');
	    if (rr && jabber) {
		if (!dojbr("add %s %s\n", nick, tresc))
		    snprintf(out + strlen(out), max_len - strlen(out), "<p>jabber not running</p>");
	    }
	    else {
		if (!doekg("/add %s %s\n", nick, tresc))
		    snprintf(out + strlen(out), max_len - strlen(out), "<p>ekg not running</p>");
	    }
	    dologu(noold, "add %s %s\n", nick, tresc);
	}

// add phone service
	if (strcmp(cmd, "c=addp") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">SEND</p>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<p>list %s -p %s</p>\n", nick, tresc);
	    if (!doekg("/list %s -p %s\n", nick, tresc))
		snprintf(out + strlen(out), max_len - strlen(out), "<p>ekg not running</p>");
	    dologu(noold, "list %s -p %s\n", nick, tresc);
	}

// delete contact service
	if (strcmp(cmd, "c=del") == 0) {
	    char *rr;
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">DELETE</p>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<p>del %s</p>\n", nick);
	    rr = (char *) index(nick, '@');
	    if (rr && jabber) {
		if (!dojbr("del %s\n", nick))
		    snprintf(out + strlen(out), max_len - strlen(out), "<p>jabber not running</p>");
	    }
	    else {
		if (!doekg("/del %s\n", nick))
		    snprintf(out + strlen(out), max_len - strlen(out), "<p>ekg not running</p>");
	    }
	    dologu(noold, "del %s\n", nick);
	}

// reset counter service
	if (strcmp(cmd, "c=cntrest") == 0) {
	    char poms[512];
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">COUNTER RESET</p>");
	    snprintf(poms, sizeof(poms), "%s/wap-counters", home);
	    unlink(poms);
	    dologu(noold, "reset counter\n");
	}

//more service
	if (strcmp(cmd, "c=more") == 0) {
	    FILE *plik;

	    if (transfer) {
		if ((plik = vfopen("r", "%s/wap-counters", home))) {
		    char ins[42], outs[42];

		    if (!feof(plik))
			fgets(ins, 40, plik);
		    if (!feof(plik))
			fgets(outs, 40, plik);
		    snprintf(out + strlen(out), max_len - strlen(out),
			     "<p align=\"center\"><u>Transfer</u><br/>in: %ldkiB<br/>out: %ldkiB<br/>"
			     "<anchor>Reset<go href=\"?c=cntrest;n=0;t=0\" method=\"get\"/></anchor></p>",
			     (long int) (1 + (atol(ins) / 1024)), (long int) (1 + (atol(outs) / 1024)));
		    fclose(plik);
		}
	    }

	    snprintf(out + strlen(out), max_len - strlen(out), "<p>Reason:<input format=\"*M\" name=\"t\" type=\"text\" emptyok=\"true\"/></p>");

/* gg *//* if(ggadu) ;P */
	    snprintf(out + strlen(out), max_len - strlen(out),
		     "<p>Gg:<br/>"
		     "<anchor>Back<go href=\"?c=back;n=0;t=$(t:escape)\" method=\"get\"/></anchor><br/>"
		     "<anchor>Away<go href=\"?c=away;n=0;t=$(t:escape)\" method=\"get\"/></anchor><br/>"
		     "<anchor>Invisible<go href=\"?c=invisible;n=0;t=$(t:escape)\" method=\"get\"/></anchor><br/>"
		     "<anchor>Private on<go href=\"?c=privon;n=0;t=0\" method=\"get\"/></anchor><br/>"
		     "<anchor>Private off<go href=\"?c=privoff;n=0;t=0\" method=\"get\"/></anchor><br/>"
		     "<anchor>Connect<go href=\"?c=co;n=0;t=0\" method=\"get\"/></anchor><br/>"
		     "<anchor>Disconnect<go href=\"?c=dc;n=0;t=$(t:escape)\" method=\"get\"/></anchor><br/>" "</p>");

/* jabber */
	    if (jabber)
		snprintf(out + strlen(out), max_len - strlen(out),
			 "<p>Jabber:<br/>"
			 "<anchor>Online<go href=\"?c=jstt;n=0;t=$(t:escape)\" method=\"get\"/></anchor><br/>"
			 "<anchor>Chat<go href=\"?c=jstt;n=1;t=$(t:escape)\" method=\"get\"/></anchor><br/>"
			 "<anchor>Away<go href=\"?c=jstt;n=2;t=$(t:escape)\" method=\"get\"/></anchor><br/>"
			 "<anchor>Ext. away<go href=\"?c=jstt;n=3;t=$(t:escape)\" method=\"get\"/></anchor><br/>"
			 "<anchor>Dnd<go href=\"?c=jstt;n=4;t=$(t:escape)\" method=\"get\"/></anchor><br/>"
			 "<anchor>Offline<go href=\"?c=jstt;n=5;t=$(t:escape)\" method=\"get\"/></anchor><br/>" "</p>");

/* setup */
	    snprintf(out + strlen(out), max_len - strlen(out),
		     "<p align=\"center\">Setup:</p><p>"
		     "Graph: %s <anchor>%s<go href=\"?c=gr;n=0;t=0\" method=\"get\"/></anchor><br/>"
		     "Check reasons: %s <anchor>%s<go href=\"?c=ckreason;n=0;t=0\" method=\"get\"/></anchor><br/>"
		     "Check nicks: %d <anchor>set<go href=\"?c=cknicks;n=0;t=0\"/></anchor><br/>"
		     "Check timeout: %ds <anchor>set<go href=\"?c=rt;n=0;t=0\"/></anchor><br/>"
		     "PL fonts: %s <anchor>%s<go href=\"?c=nopl;n=%s;t=0\" method=\"get\"/></anchor><br/>"
		     "Msg order: older %s <anchor>%s<go href=\"?c=rotmsg;n=0;t=0\" method=\"get\"/></anchor><br/>"
		     "Emoticons: %s <anchor>%s<go href=\"?c=emot;n=0;t=0\" method=\"get\"/></anchor><br/>"
		     "Color: %s <anchor>%s<go href=\"?c=xemot;n=0;t=0\" method=\"get\"/></anchor><br/>"
		     "Transfer: %s <anchor>%s<go href=\"?c=trans;n=0;t=0\" method=\"get\"/></anchor><br/>"
		     "Jabber: %s <anchor>%s<go href=\"?c=jabber;n=0;t=0\" method=\"get\"/></anchor><br/>"
		     "Sound: %s "
		     "<anchor>off<go href=\"?c=sound;n=0;t=0\" method=\"get\"/></anchor>"
		     "<anchor>s45<go href=\"?c=sound;n=1;t=0\" method=\"get\"/></anchor>"
		     "<anchor>vcs<go href=\"?c=sound;n=2;t=0\" method=\"get\"/></anchor>"
		     "<br/>"
		     "Menu: %s <anchor>%s<go href=\"?c=mkbd;n=0;t=0\" method=\"get\"/></anchor><br/>"
		     "RTG ver: %s</p>"
		     "<do type=\"prev\" label=\"reload\"><go href=\"?\"/></do>"
		     "<do type=\"accept\" label=\"reload\"><go href=\"?\"/></do>"
		     "</card>"
		     "</wml>\n",
		     (graph) ? "on" : "off", (graph) ? "off" : "on",
		     (checkreason) ? "on" : "off",
		     (checkreason) ? "off" : "on", cknicks, ckto / 10,
		     (nopl == 1) ? "off" : (nopl == 0) ? "utf" : "iso",
		     (nopl == 1) ? "utf" : (nopl == 0) ? "iso" : "off",
		     (nopl == 1) ? "0" : (nopl == 0) ? "2" : "1",
		     (rotmsg) ? "last" : "first",
		     (rotmsg) ? "first" : "last",
		     (emoticon) ? ((emoticon == 1) ? "on" : "on w/desc") :
		     "off",
		     (emoticon) ? ((emoticon == 1) ? "on w/desc" : "off") :
		     "on", (xemot) ? "on" : "off", (xemot) ? "off" : "on",
		     (transfer) ? "on" : "off", (transfer) ? "off" : "on",
		     (jabber) ? "on" : "off", (jabber) ? "off" : "on",
		     (beep == 0) ? "off" : (beep == 1) ? "s45" : "vcs", (mkbd) ? "extra keys" : "simple", (mkbd) ? "simple" : "extra keys", rtgversion);
	    return 0;
	}

// graph on off service
	if (strcmp(cmd, "c=gr") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">GRAPH</p>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">%s</p>\n", (graph == 1) ? "off" : "on");
	    snprintf(wapfile, sizeof(wapfile), "%s/wap-nograph", home);
	    if (graph == 0) {
		unlink(wapfile);
	    }
	    else {
		if ((wap = fopen(wapfile, "w")))
		    fclose(wap);
	    }
	    dologu(noold, "graph\n");
	}

// jabber on off service
	if (strcmp(cmd, "c=jabber") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">JABBER</p>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">%s</p>\n", (jabber) ? "off" : "on");
	    snprintf(wapfile, sizeof(wapfile), "%s/wap-jabber", home);
	    if (jabber) {
		unlink(wapfile);
	    }
	    else {
		if ((wap = fopen(wapfile, "w")))
		    fclose(wap);
	    }
	    dologu(noold, "jabber\n");
	}

// mkbd on off service
	if (strcmp(cmd, "c=mkbd") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">Menu</p>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">%s</p>\n", (mkbd) ? "simple" : "extra keys");
	    snprintf(wapfile, sizeof(wapfile), "%s/wap-mkbd", home);
	    if (mkbd == 0) {
		unlink(wapfile);
	    }
	    else {
		if ((wap = fopen(wapfile, "w")))
		    fclose(wap);
	    }
	    dologu(noold, "mkbd\n");
	}

// jabber status service
	if (strcmp(cmd, "c=jstt") == 0 && jabber) {
	    char poms[10];
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">JABBER STATUS</p>");
	    if (strcmp(nick, "0") == 0)
		snprintf(poms, sizeof(poms), "online");
	    else if (strcmp(nick, "1") == 0)
		snprintf(poms, sizeof(poms), "chat");
	    else if (strcmp(nick, "2") == 0)
		snprintf(poms, sizeof(poms), "away");
	    else if (strcmp(nick, "3") == 0)
		snprintf(poms, sizeof(poms), "xa");
	    else if (strcmp(nick, "4") == 0)
		snprintf(poms, sizeof(poms), "dnd");
	    else if (strcmp(nick, "5") == 0)
		snprintf(poms, sizeof(poms), "offline");
	    else
		snprintf(poms, sizeof(poms), "online");
	    iso2jabber(tresc, sizeof(tresc));
	    if (!dojbr("stt %s %s\n", poms, tresc))
		snprintf(out + strlen(out), max_len - strlen(out), "<p>uje not running</p>");
	    dologu(noold, "jabber status %s %s\n", poms, tresc);
	}

// trans on off service
	if (strcmp(cmd, "c=trans") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">COUNTER</p>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">%s</p>\n", (transfer == 1) ? "off" : "on");
	    snprintf(wapfile, sizeof(wapfile), "%s/wap-transfer", home);
	    if (transfer == 1) {
		unlink(wapfile);
	    }
	    else {
		if ((wap = fopen(wapfile, "w")))
		    fclose(wap);
	    }
	    dologu(noold, "trans\n");
	}

// rotmsg on off service
	if (strcmp(cmd, "c=rotmsg") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">ROTATE MSG</p>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">%s</p>\n", (rotmsg) ? "off" : "on");
	    snprintf(wapfile, sizeof(wapfile), "%s/wap-rotmsg", home);
	    if (rotmsg) {
		unlink(wapfile);
	    }
	    else {
		if ((wap = fopen(wapfile, "w")))
		    fclose(wap);
	    }
	    dologu(noold, "rotmsg\n");
	    rotmsg = (rotmsg) ? 0 : 1;
	}

// Emoticon on off service
	if (strcmp(cmd, "c=emot") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out),
		     "<p align=\"center\">EMOTICONS %s</p>\n", (emoticon) ? ((emoticon == 1) ? "on w/desc" : "off") : "on");
	    snprintf(wapfile, sizeof(wapfile), "%s/wap-emoticon", home);
	    if (emoticon == 2) {
		unlink(wapfile);
	    }
	    else if (emoticon == 1) {
		if ((wap = fopen(wapfile, "w"))) {
		    fprintf(wap, "2");
		    fclose(wap);
		}
	    }
	    else {
		if ((wap = fopen(wapfile, "w")))
		    fclose(wap);
	    }
	    dologu(noold, "emoticon\n");
	    emoticon = (emoticon) ? ((emoticon == 1) ? 2 : 0) : 1;
	}

// pl fonts on off service
	if (strcmp(cmd, "c=nopl") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">PL fonts</p>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">%s</p>\n", (nick[0] == '0') ? "utf" : (nick[0] == '1') ? "off" : "iso");
	    snprintf(wapfile, sizeof(wapfile), "%s/wap-nopl", home);
	    if (nick[0] == '0') {
		unlink(wapfile);
	    }
	    else {
		if ((wap = fopen(wapfile, "w"))) {
		    if (nick[0] == '2')
			fprintf(wap, "2\n");
		    fclose(wap);
		}
	    }
	    dologu(noold, "nopl %d\n", nopl);
	}

// xternal emoticons on off service
	if (strcmp(cmd, "c=xemot") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">COLOR");
	    snprintf(out + strlen(out), max_len - strlen(out), " %s</p>\n", (xemot) ? "off" : "on");
	    snprintf(wapfile, sizeof(wapfile), "%s/wap-xemot", home);
	    if (xemot) {
		unlink(wapfile);
	    }
	    else {
		if ((wap = fopen(wapfile, "w")))
		    fclose(wap);
	    }
	    dologu(noold, "xemot %d\n", xemot);
	}

// check reason on off service
	if (strcmp(cmd, "c=ckreason") == 0) {
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">Check reason</p>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">%s</p>\n", (checkreason) ? "off" : "on");
	    snprintf(wapfile, sizeof(wapfile), "%s/wap-ckreason", home);
	    if (checkreason) {
		unlink(wapfile);
	    }
	    else {
		if ((wap = fopen(wapfile, "w")))
		    fclose(wap);
	    }
	    dologu(noold, "ckreason %d\n", checkreason);
	}

// delete msg service (don't show me again)
	if (strcmp(cmd, "c=d") == 0) {
	    struct stat buf;
	    char tmpczas[50];

	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">DELETE</p>");
	    snprintf(out + strlen(out), max_len - strlen(out), "<p>%s</p>", nick);

	    snprintf(wapfile, sizeof(wapfile), "%s/", home);
	    n = scandir(wapfile, &namelist, 0, alphasort);
	    if (n < 0)
		perror("scandir");
	    else {
		k = n;
		while (n--) {
		    int nmsg = (strlen(nick) == 5) ? 0 : 1;	//kasuj nowy format pliku msg
		    snprintf(wapfile, sizeof(wapfile), "wap%s", nick);
		    if (((strncmp(namelist[k - n - 1]->d_name, wapfile, 8)
			  == 0 && !nmsg)
			 || (strncmp(namelist[k - n - 1]->d_name, wapfile, 10)
			     == 0 && nmsg))
			&& (namelist[k - n - 1]->d_name[5] == ':')) {
			snprintf(wapfile, sizeof(wapfile), "%s/%s", home, namelist[k - n - 1]->d_name);
			if (noold == 0) {
			    if (lstat(wapfile, &buf) == 0)
				strftime(tmpczas, sizeof(tmpczas) - 1, "%Y%m%d", localtime((time_t *) & buf.st_mtime));
			    else
				snprintf(tmpczas, sizeof(tmpczas), "20000101");
			    snprintf(wapfile2, sizeof(wapfile2), "%s/oldwap%s%s", home, tmpczas, (namelist[k - n - 1]->d_name) + 3);
			    rename(wapfile, wapfile2);
			}
			else
			    unlink(wapfile);
		    }
		    free(namelist[k - n - 1]);
		}
	    }
	    free(namelist);
	    // out 388
	}			//if c=d

//kill chat windows

	if (strcmp(cmd, "c=altk") == 0) {
	    int loop;
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">ALL CHAT WINDOWS CLOSED</p>");
	    for (loop = 2; loop <= 10; loop++)
		doekg("/window kill %d\n", loop);
	    dologu(noold, "windows kill\n");
	}

// delete page service (don't show me page again)
	if (strcmp(cmd, "c=delp") == 0) {
	    struct stat buf;
	    char tmpczas[50];

	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">DELETE PAGE</p>");

	    snprintf(wapfile, sizeof(wapfile), "%s/", 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] == ':') {
			int nmsg = (strlen(namelist[k - n - 1]->d_name) == 8) ? 0 : 1;
			if (!nmsg)
			    snprintf(wapfile, sizeof(wapfile), "%5.5s", (namelist[k - n - 1]->d_name) + 3);
			else
			    snprintf(wapfile, sizeof(wapfile), "%7.7s", (namelist[k - n - 1]->d_name) + 3);
			if (strstr(tresc, wapfile) != 0) {
			    snprintf(wapfile, sizeof(wapfile), "%s/%s", home, namelist[k - n - 1]->d_name);
			    if (noold == 0) {
				if (lstat(wapfile, &buf) == 0)
				    strftime(tmpczas, sizeof(tmpczas) - 1, "%Y%m%d", localtime((time_t *) & buf.st_mtime));
				else
				    snprintf(tmpczas, sizeof(tmpczas), "20000101");
				snprintf(wapfile2, sizeof(wapfile2), "%s/oldwap%s%s", home, tmpczas, (namelist[k - n - 1]->d_name) + 3);
				rename(wapfile, wapfile2);
			    }
			    else
				unlink(wapfile);
			}
		    }
		    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);
	    memset(&userlist, 0, sizeof(userlist));
	    ggul = vfopen("r", "%s/userlist", home);
	    if ((wap = vfopen("r", "%s/wapstatus", home))) {
		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) {
			    descr++[0] = 0;
			    if (strlen(descr) > 0) {
				snprintf(reason, sizeof(reason), "%.200s", descr);
				dozwolonys(reason, sizeof(reason), 1, 0, 0, nopl, xe_count);
			    }
			}
			e = NULL;
			memset(gguserlist, 0, sizeof(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)) {
				snprintf(userlist + strlen(userlist),
					 sizeof(userlist) -
					 strlen(userlist),
					 "<p> %s %s <a href=\"?c=f;n=%s;t=a\"> %s </a> %s </p> \n",
					 (graph ==
					  1) ? ((xemot ==
						 1) ? ((icgi ==
							0) ? ((st == 0x01
							       || st ==
							       0x15
							       || st ==
							       0x14
							       || st ==
							       0x16
							       || st ==
							       0x06) ?
							      "<img src=\"?sleep.gif\" alt=\"disc\"/>"
							      : ((st ==
								  0x02
								  || st ==
								  0x04) ? "<img src=\"?jest.gif\" alt=\"conn\"/>" : "<img src=\"?busy.gif\" alt=\"busy\"/>"))
						       : ((st == 0x01
							   || st == 0x15
							   || st == 0x14
							   || st == 0x16
							   || st ==
							   0x06) ?
							  "<img src=\"/rtg/sleep.gif\" alt=\"disc\"/>"
							  : ((st == 0x02
							      || st ==
							      0x04) ?
							     "<img src=\"/rtg/jest.gif\" alt=\"conn\"/>" : "<img src=\"/rtg/busy.gif\" alt=\"busy\"/>"))
						)
						:
						((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\"/>"))

						)
					 )
					 : (((st == 0x01 || st == 0x15 || st == 0x14 || st == 0x16 || st == 0x06) ? "-" : ((st == 0x02 || st == 0x04)
															   ? "+" : "/"))
					 ), (e != NULL) ? ((strlen(e) > 0 && strcmp(wapfile, gguserlist)
							    == 0) ? ((graph == 1) ? ((icgi == 0)
										     ? ((xemot) ? ("<img src=\"?komorka.gif\" alt=\"tel\"/>")
											: ("<img src=\"?komorka.wbmp\" alt=\"tel\"/>")
										     )
										     : ((xemot) ? ("<img src=\"/rtg/komorka.gif\" alt=\"tel\"/>")
											: ("<img src=\"/rtg/komorka.wbmp\" alt=\"tel\"/>")
										     )
								     )
								     : ("p ")
							   ) : "") : (""), 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)
			snprintf(userlist + strlen(userlist),
				 sizeof(userlist) - strlen(userlist),
				 "<do type=\"%c%d\" label=\"od %c\"><go href=\"?c=lista;n=%d;t=a\"/></do>", tabmenu[kk], kk, tabmenu[kk], kk * 10);
		}
		fclose(wap);
		if (ggul > 0)
		    fclose(ggul);
	    }
	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">GG LIST</p>%s", userlist);
	    if (mkbd)
		snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"accept\" label=\"reload\"><go href=\"?\"/></do>");
	    snprintf(out + strlen(out), max_len - strlen(out),
		     "<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=\"lolist\" label=\"load\"><go href=\"?c=listl;n=0;t=a\"/></do>"
		     "<do type=\"salist\" label=\"save\"><go href=\"?c=lists;n=0;t=a\"/></do>"
		     "<do type=\"RTG\" label=\"RTG\"><go href=\"?\"/></do>"
		     "<do type=\"Check\" label=\"Check\"><go href=\"?check0\"/></do>" "</card></wml>", od + 10, ((od - 10) > 0) ? od - 10 : 0);
	    return 0;
	}			//userlista

	if (strcmp(cmd, "c=roster") == 0) {
	    int licz = 0;
	    char *e, *rnick, *rstat, *rreas, rstatus[300], rreason[100], rnicks[100];

	    memset(&userlist, 0, sizeof(userlist));
	    memset(&rstatus, 0, sizeof(rstatus));
	    memset(&rreason, 0, sizeof(rreason));

	    od = atoi(nick);

	    if ((wap = vfopen("r", "%s/wap-roster", home))) {
		while (fgets(wapfile, 100, wap)) {
		    e = (char *) index(wapfile, 10);
		    if (e)
			e[0] = 0;
		    e = (char *) index(wapfile, 13);
		    if (e)
			e[0] = 0;

		    rreas = (char *) nindex(wapfile, ';', 4);
		    if (rreas) {
			snprintf(rreason, sizeof(rreason), "%s", rreas + 1);
		    }

		    rnick = (char *) index(wapfile, ';');
		    if (rnick) {
			rnick++[0] = 0;
			rstat = (char *) index(rnick, ';');
			if (rstat) {
			    rstat++[0] = 0;
			    e = (char *) index(rstat, ';');
			    if (e) {
				e++[0] = 0;
				snprintf(rstatus, sizeof(rstatus), "%s", rstat);
				snprintf(rnicks, sizeof(rnicks), "%s", rnick);

				jbr_status(rstatus, sizeof(rstatus), graph, xemot, icgi);
				dozwolonys(rnicks, sizeof(rnicks), (emoticon) ? ((emoticon == 1) ? 4 : 5) : 1, xemot, icgi, nopl, xe_count);
				dozwolonys(rreason, sizeof(rreason), (emoticon) ? ((emoticon == 1) ? 4 : 5) : 1, xemot, icgi, nopl, xe_count);

				e = (char *) index(wapfile, '@');
				if (e) {
				    if ((od <= licz) && ((od + 10) > licz)) {
					snprintf(userlist +
						 strlen(userlist),
						 sizeof(userlist) -
						 strlen(userlist),
						 "<p>%s <a href=\"?c=f;n=%s;t=0\">%s</a> %s</p>",
						 rstatus, wapfile, (strlen(rnicks) > 0) ? rnicks : wapfile, rreason);
				    }
				    licz++;
				}
			    }
			}
		    }
		}
		fclose(wap);
	    }

	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">JABBER ROSTER</p>%s", userlist);
	    if (mkbd)
		snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"accept\" label=\"reload\"><go href=\"?\"/></do>");
	    snprintf(out + strlen(out), max_len - strlen(out),
		     "<do type=\"prev\" label=\"reload\"><go href=\"?\"/></do>"
		     "<do type=\"next\" label=\"next\"><go href=\"?c=roster;n=%d;t=a\"/></do>"
		     "<do type=\"back\" label=\"back\"><go href=\"?c=roster;n=%d;t=a\"/></do>"
		     "<do type=\"RTG\" label=\"RTG\"><go href=\"?\"/></do>"
		     "<do type=\"Check\" label=\"Check\"><go href=\"?check0\"/></do>" "</card></wml>", od + 10, ((od - 10) > 0) ? od - 10 : 0);
	    return 0;
	}			//roster

// przegladanie historii wiadomosci z danym nickiem (oldwapy)
	if (strcmp(cmd, "c=his") == 0) {
	    char nexttime[18];
	    int dispmsg = 0, allmsg = 0, firstmsg = 0;

	    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">MSG HISTORY<br/>%s</p>", nick);

	    snprintf(wapfile, sizeof(wapfile), "%s/", home);
//      if(rotmsg)
//          n = scandir(wapfile, &namelist, 0, notalphasort);
//      else
	    n = scandir(wapfile, &namelist, 0, alphasort);
	    if (n < 0)
		perror("scandir");
	    else {
		char tmpuin[40];

		snprintf(tmpuin, sizeof(tmpuin), "_%s", nick);
		k = n;
		while (n--) {
		    if (strncmp(namelist[k - n - 1]->d_name, "oldwap", 6)
			== 0 && (namelist[k - n - 1]->d_name[16] == ':')
			&& strstr(namelist[k - n - 1]->d_name, tmpuin) != 0) {
			// tu sa oldwapy od danego uin-u

			if ((wap = vfopen("r", "%s/%s", home, namelist[k - n - 1]->d_name))) {
			    while (!feof(wap)) {
				memset(buf, 0, sizeof(buf));
				fgets(buf, 1000, wap);
				if (strlen(buf) > 0) {
				    char *kk = (char *) index(buf, 10);
				    if (kk != NULL)
					kk[0] = 0;
				    trescw = (char *) index(buf, ';');
				    if (trescw != NULL) {
					char tre[3001];
					char tmpczas[18];

					trescw++[0] = 0;
					snprintf(tmpczas, sizeof(tmpczas),
						 "%2.2s-%2.2s-%2.2s_%2.2s-%2.2s-%2.2s",
						 (namelist[k - n - 1]->
						  d_name) + 8,
						 (namelist[k - n - 1]->
						  d_name) + 10,
						 (namelist[k - n - 1]->
						  d_name) + 12,
						 (namelist[k - n - 1]->d_name) + 14, (namelist[k - n - 1]->d_name) + 17, (namelist[k - n - 1]->d_name) + 19);
					snprintf(tre, sizeof(tre), "%.3000s", trescw);
					dozwolonys(tre, sizeof(tre), (emoticon)
						   ? ((emoticon == 1) ? 4 : 5) : 1, xemot, icgi, nopl, xe_count);
					if (strcmp(tresc, tmpczas) < 0) {
					    if ((strlen(out) + 14 + strlen(buf) + strlen(tre) + 1500) < max_len) {	//jesli sie jeszcze zmiesci
						snprintf(out + strlen(out), max_len - strlen(out), "<p>%s(%14.14s) %s</p>", buf, tmpczas, tre);
						snprintf(nexttime, sizeof(nexttime), "%s", tmpczas);
						dispmsg++;
					    }
					}
					else {
					    firstmsg++;
					}
					allmsg++;
				    }
				}
			    }
			    fclose(wap);
			}
		    }
		    free(namelist[k - n - 1]);
		}
		free(namelist);
	    }

	    if (dispmsg > 0 && (allmsg > (dispmsg + firstmsg))) {
		snprintf(out + strlen(out), max_len - strlen(out),
			 "<p><a href=\"?c=his;n=%s;t=%s\">Next %d/%d</a></p>", nick, nexttime, dispmsg + firstmsg, allmsg);
	    }

	    if (mkbd) {
		snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"accept\" label=\"reload\"><go href=\"?\"/></do>");
		snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"prev\" label=\"reload\"><go href=\"?\"/></do>");
	    }
	    snprintf(out + strlen(out), max_len - strlen(out),
		     "<do type=\"RTG\" label=\"RTG\"><go href=\"?\"/></do>" "<do type=\"Check\" label=\"Check\"><go href=\"?check0\"/></do>" "</card></wml>");
	    return 0;
	}

    }				// qstr!=0

// max out 887

// bez parametrow lub na koniec niektorych leca aktywne wiadomosci

    snprintf(wapfile, sizeof(wapfile), "%s/", home);
    if (rotmsg)
	n = scandir(wapfile, &namelist, 0, notalphasort);
    else
	n = scandir(wapfile, &namelist, 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) {
		int nmsg = (strlen(namelist[k - n - 1]->d_name) == 8) ? 0 : 1;
//      printf("%s<br>\n", namelist[k-n-1]->d_name);
		raz = 0;
		if ((wap = vfopen("r", "%s/%s", home, namelist[k - n - 1]->d_name))) {
		    while (!feof(wap)) {
			memset(buf, 0, sizeof(buf));
			fgets(buf, 1000, wap);
			if (strlen(buf) > 0 && !nmsg) {
			    char *kk = (char *) index(buf, 10);
			    if (kk != NULL)
				kk[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];
				    snprintf(tre, sizeof(tre), "%.3000s", trescw);
				    dozwolonys(tre, sizeof(tre), (emoticon)
					       ? ((emoticon == 1) ? 4 : 5) : 1, xemot, icgi, nopl, xe_count);
				    if ((strlen(out) + 2 * strlen(buf) + strlen(tre) + 1500) < max_len) {	//jesli sie jeszcze zmiesi
					snprintf(out + strlen(out),
						 max_len - strlen(out),
						 "<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", buf, buf, tre, tre);
					if (raz++ == 0)
					    snprintf(delp + strlen(delp), sizeof(delp) - strlen(delp), "%5.5s;", tre);
				    }
				}
			    }
			}
			else if (strlen(buf) > 0 && nmsg) {	//nowy typ wiadomosci
			    char timetodel[8];
			    char *kk = (char *) index(buf, 10);
			    if (kk != NULL)
				kk[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];
				    snprintf(timetodel, sizeof(timetodel), "%7.7s", namelist[k - n - 1]->d_name + 3);
				    snprintf(tre, sizeof(tre), "%.3000s", trescw);
				    dozwolonys(tre, sizeof(tre), (emoticon)
					       ? ((emoticon == 1) ? 4 : 5) : 1, xemot, icgi, nopl, xe_count);
				    if ((strlen(out) + 2 * strlen(buf) + strlen(tre) + 1500) < max_len) {	//jesli sie jeszcze zmiesci
					snprintf(out + strlen(out),
						 max_len - strlen(out),
						 "<p><a href=\"?c=f;n=%s;t=a\">%s</a>(%5.5s:%2.2s) %s<a href=\"?c=d;n=%7.7s;t=a\"> Del</a></p>\n",
						 buf, buf, timetodel, timetodel + 5, tre, timetodel);
					if (raz++ == 0)
					    snprintf(delp + strlen(delp), sizeof(delp) - strlen(delp), "%7.7s;", timetodel);
				    }
				}
			    }
			}
		    }
		    fclose(wap);
		}
	    }
	    free(namelist[k - n - 1]);
	}
	free(namelist);
    }

    if (strcmp(cmd, "c=check") != 0) {
	if ((wap = vfopen("r", "%s/wap-quick.log", home))) {
	    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) {
		    snprintf(out + strlen(out), max_len - strlen(out),
			     "<do type=\"lista%d\" label=\"%s\"><go href=\"?c=f;n=%s;t=a\"/></do>\n", ile, nicktmp, nicktmp);
		    ile++;
		}
	    }
	    fclose(wap);
	}
    }
    else {
	snprintf(out + strlen(out), max_len - strlen(out),
		 "<card id=\"gg\" title=\"RTG %s\" ontimer=\"?check;%d;%d\">\n"
		 "<timer name=\"time%d\" value=\"%d\"/>\n"
		 "<p align=\"center\"><b>%5.5s</b></p>", uin, emails + msgs, (int) time(NULL), (int) time(NULL), ckto, ctime(&czas) + 11);
	if (!connected)
	    snprintf(out + strlen(out), max_len - strlen(out),
		     "<p align=\"center\"><small>Ekg not connected, try <a href=\"?c=co;n=0;t=0\">connect</a> first</small></p>");
	if (beep && nick[0] != 'a') {
	    int oldmsg = atoi(nick);
	    if (oldmsg < (emails + msgs)) {
		if (beep == 1)
		    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\"><img src=\"?beep.midi\" alt=\"New messages\"/></p>");
		else {
		    snprintf(out, max_len,
			     "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n"
			     "Cache-Control: no-cache, must-revalidate\r\n" "Pragma: no-cache\r\n" "Content-type: text/x-vcalendar\r\n" "\r\n%s", vcsbeep);
		    return 0;
		}
	    }
	}

	snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\">mail: %d im: %d</p>%s", emails, msgs, statx);
	if (irc)
	    snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"irc\" label=\"Irssi\"><go href=\"?c=irc;n=0;t=0\"/></do>");
	snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"mail\" label=\"mail\"><go href=\"?c=mail;n=0;t=0\"/></do>");
	if (mkbd)
	    snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"accept\" label=\"im\"><go href=\"?\"/></do>");
	snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"prev\" label=\"im\"><go href=\"?\"/></do></card></wml>");
	return 0;
    }				//if c=check



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

    snprintf(out + strlen(out), max_len - strlen(out), "<p align=\"center\"><a href=\"?\">Reload</a> <a href=\"?check0\">Check</a></p>");

    if (mkbd)
	snprintf(out + strlen(out), max_len - strlen(out), "<do type=\"accept\" label=\"reload\"><go href=\"?\"/></do>");
    snprintf(out + strlen(out), max_len - strlen(out),
	     "<do type=\"prev\" label=\"reload\"><go href=\"?\"/></do>"
	     "<do type=\"lista\" label=\"gg list\"><go href=\"?c=lista;n=0;t=a\"/></do>"
	     "%s"
	     "<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>"
	     "%s"
	     "<do type=\"more\" label=\"more..\"><go href=\"?c=more;n=0;t=a\"/></do>"
	     "%s" "%s"
	     "<do type=\"About\" label=\"About\"><go href=\"http://gg.overwap.net/copyright.wml\"/></do>"
	     "</card>" "</wml>\n",
	     (jabber) ?
	     "<do type=\"roster\" label=\"roster\"><go href=\"?c=roster;n=0;t=a\"/></do>"
	     : "",
	     (!jabber) ?
	     "<do type=\"add phone\" label=\"add phone\"><go href=\"?c=fp;n=0;t=a\"/></do>"
	     : "",
	     (fblw) ?
	     "<do type=\"Lolist\" label=\"Load list\"><go href=\"?c=listl;n=0;t=a\"/></do>"
	     :
	     "<do type=\"Mailinfo\" label=\"Mail info\"><go href=\"?c=mail;n=0;t=0\"/></do>",
	     (fblw) ?
	     "<do type=\"disconnect\" label=\"Disconnect\"><go href=\"?c=dc;n=0;t=0\"/></do>"
	     : ((!irc) ?
		"<do type=\"winclose\" label=\"Win Close\"><go href=\"?c=altk;n=0;t=a\"/></do>"
		: "<do type=\"irc\" label=\"Irssi\"><go href=\"?c=irc;n=0;t=0\"/></do>")
	);			//~228
    return 0;
}
