#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 "globals.h"
#include "decode.h"
#include "ckm.h"
#include "sendmail.h"
#include "utils.h"
#include "wap.h"

#include "version.c"

/*
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;
  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 = 1;
  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 checkreason = 0;

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

  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-nopl", home)))
    {
      fclose (wap);
      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-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\"?>\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\"/>"
	    "</head>" "\n");


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

/* przeniesione na koniec
	sprintf(out,"%s<card id=\"gg\" title=\"RTG %s\" ontimer=\"?check%d\">\n"
"<timer name=\"time%d\" value=\"%d\"/>\n"
"<p align=\"center\"><b>%5.5s</b></p>",
	    out,
	    uin,
	    (int)time(NULL),
	    (int)time(NULL),
	    ckto,
	    ctime(&czas)+11
	    );
*/
      cp = (char *) index (buf, ';');
      if (cp)
	{
	  cp++;
	  cpi = atoi (cp);
	  sprintf (buf, "c=check;n=%d;t=0", cpi);
	}
      else
	sprintf (buf, "c=check;n=a;t=0");	//żeby się później dobrze wykonało
    }

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

// 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)
	    {
	      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);
	    }
	  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;
      sprintf (statx, "%s<p align=\"center\">", statx);
      for (z = 0; z < cknicks; z++)
	{
	  if (strlen (stat2[z]) > 0)
	    {
	      int st = 0;
	      char *k, *reason;

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

		  k++[0] = 0;

		  reason = (char *) index (k, ':');
		  if (reason != NULL)
		    reason++[0] = 0;
		  st = atol (k);

		  if (graph)
		    {
		      if (xemot)
			{
			  sprintf (statx, "%s %s%s%s%s\n",
				   statx,
				   ((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) ? reason : "");
			}
		      else
			{
			  sprintf (statx, "%s %s%s%s%s\n",
				   statx,
				   ((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) ? reason : "");
			}
		    }
		  else
		    {
		      sprintf (statx, "%s %s%s%s%s\n",
			       statx,
			       ((st == 0x01 || st == 0x15 || st == 0x14
				 || st == 0x16
				 || st == 0x06) ? "- " : ((st == 0x02
							   || st ==
							   0x04) ? "+ " :
							  "/ ")), stat2[z],
			       (reason && checkreason) ? " " : "", (reason
								    &&
								    checkreason)
			       ? reason : "");
		    }
		}
	    }
	}
      sprintf (statx, "%s</p>", statx);
    }

//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)
	{
	  sprintf (out, "%s<p>Blad skladni 1</p></card></wml>", out);
	  return 0;
	}
      nickw++[0] = 0;
      trescw = (char *) index (nickw, ';');
      if (trescw == NULL)
	{
	  sprintf (out, "%s<p>Blad skladni 2</p></card></wml>", out);
	  return 0;
	}
      trescw++[0] = 0;

      if (strlen (nickw) < 3 || strlen (trescw) < 2)
	{
	  sprintf (out, "%s<p>Blad skladni 3</p></card></wml>", out);
	  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

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

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

	  sprintf (out, "%s<p align=\"center\">MAIL</p>", out);
	  sprintf (out, "%s<p>mail to %s ", out, 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)
		    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);

	  sprintf (cmd, "c=mail");
	  sprintf (nick, "0");
	  sprintf (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 - 600) <
		  sizeof (buf)) ? max_len - 600 : sizeof (buf), cmdi, ofs, 10,
		 &emails, nopl, xe_count) == 1)
	      {
		if (strcmp (cmd, "c=mail") == 0)
		  sprintf (out, "%s%s", out, 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 - 600) <
					sizeof (buf)) ? max_len -
				       600 : sizeof (buf), cmdi, ofs, 10,
				       &emails, nopl, xe_count) == 1)
				    {
				      if (strcmp (cmd, "c=mail") == 0)
					sprintf (out, "%s%s", out, 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)
					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/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);
	    }
	  // 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:escape);t=$(su:escape);$(t:escape)\" 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:escape);$(t:escape)\" 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:escape);t=$(t:escape)\" 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/jid/tel:</p><p><input format=\"*M\" 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: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>",
		       out);
	    }
	  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)
		{
		  sprintf (out, "%s<p>Msg to %s</p>", out, 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));
		  sprintf (out, "%s<p>Msg to %s/%s</p>", out, nick,
			   targetuin);
		}

	      sprintf (out,
		       "%s<p><input format=\"*M\" name=\"t\" type=\"text\" emptyok=\"true\"/></p>",
		       out);
	      sprintf (out,
		       "%s<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>",
		       out, 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);
	    }
	  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;
	}

      if (strcmp (cmd, "c=rt") == 0)
	{
	  sprintf (out,
		   "%s<p>Check timeout (s):</p><p><input format=\"*N\" name=\"n\" type=\"text\"/></p>",
		   out);
	  sprintf (out,
		   "%s<p><anchor>Set<go href=\"?c=rts;n=$(n:escape);t=0\" method=\"get\"/></anchor></p>",
		   out);
	  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;
	}

      if (strcmp (cmd, "c=cknicks") == 0)
	{
	  sprintf (out,
		   "%s<p>Check nicks:</p><p><input format=\"*N\" name=\"n\" type=\"text\"/></p>",
		   out);
	  sprintf (out,
		   "%s<p><anchor>Set<go href=\"?c=cknickss;n=$(n:escape);t=0\" method=\"get\"/></anchor></p>",
		   out);
	  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;
	}

// rts - ustaw timeout dla check
      if (strcmp (cmd, "c=rts") == 0)
	{
	  sprintf (out, "%s<p align=\"center\">Check :%s</p>", out, nick);
	  if ((wap = vfopen ("w", "%s/wap-timeout", home)))
	    {
	      fprintf (wap, "%d\n", (int) atol (nick) * 10);
	      fclose (wap);
	    }
	  else
	    sprintf (out, "%s<p>error open file</p>", out);
	  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);
	      sprintf (out, "%s<p align=\"center\">Check nicks:%d</p>", out,
		       ii);
	    }
	  else
	    sprintf (out, "%s<p>error open file</p>", out);
	  dologu (noold, "ch nicks %d\n", ii);
	}

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

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

	  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;

	  sprintf (out, "%s<p align=\"center\">MSG</p>", out);
	  sprintf (out, "%s<p>msg to %s send</p>\n", out, 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]))
		sprintf (out, "%s<p>uje not running</p>", out);
	    }
	  else
	    {
	      if (!doekg ("/chat %s %s\n", nick, qmsg[s]))
		sprintf (out, "%s<p>ekg not running</p>", out);
	    }

	  // 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);
	  if (!doekg ("/sms %s %s\n", nick, tresc))
	    sprintf (out, "%s<p>ekg not running</p>", out);
	  dologu (noold, "sms %s %s\n", nick, tresc);
	  // out 887
	}

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

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

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

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

// download list from server gg service
      if (strcmp (cmd, "c=listl") == 0)
	{
	  sprintf (out, "%s<p align=\"center\">DOWNLOAD LIST</p>", out);
	  if (!doekg ("/list -g\n"))
	    sprintf (out, "%s<p>ekg not running</p>", out);
	  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);
	  sprintf (out, "%s<p align=\"center\">SAVE LIST</p>", out);
	  if (!doekg ("/list -p\n"))
	    sprintf (out, "%s<p>ekg not running</p>", out);
	  dologu (noold, "save list\n");
	}

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

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

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

// 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);
	  if (!doekg ("/list %s -p %s\n", nick, tresc))
	    sprintf (out, "%s<p>ekg not running</p>", out);
	  dologu (noold, "list %s -p %s\n", nick, tresc);
	}

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

// reset counter service
      if (strcmp (cmd, "c=cntrest") == 0)
	{
	  char poms[256];
	  sprintf (out, "%s<p align=\"center\">COUNTER RESET</p>", out);
	  sprintf (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);
		  sprintf (out,
			   "%s<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>",
			   out, (long int) (1 + (atol (ins) / 1024)),
			   (long int) (1 + (atol (outs) / 1024)));
		  fclose (plik);
		}
	    }

	  sprintf (out,
		   "%s<p>Reason:<input format=\"*M\" name=\"t\" type=\"text\" emptyok=\"true\"/></p>",
		   out);

/* gg *//* if(ggadu) ;P */
	  sprintf (out, "%s<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>", out);

/* jabber */
	  if (jabber)
	    sprintf (out, "%s<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>", out);

/* setup */
	  sprintf (out, "%s<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=0;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/>"
		   "RTG ver: %s</p>"
		   "<do type=\"prev\" label=\"reload\"><go href=\"?\"/></do>"
		   "<do type=\"accept\" label=\"reload\"><go href=\"?\"/></do>"
		   "</card>"
		   "</wml>\n",
		   out,
		   (graph) ? "on" : "off", (graph) ? "off" : "on",
		   (checkreason) ? "on" : "off", (checkreason) ? "off" : "on",
		   cknicks,
		   ckto / 10,
		   (nopl) ? "off" : "on", (nopl) ? "on" : "off",
		   (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",
		   rtgversion);
	  return 0;
	}

// graph on off service
      if (strcmp (cmd, "c=gr") == 0)
	{
	  sprintf (out, "%s<p align=\"center\">GRAPH</p>", out);
	  sprintf (out, "%s<p align=\"center\">%s</p>\n", out,
		   (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)
	{
	  sprintf (out, "%s<p align=\"center\">JABBER</p>", out);
	  sprintf (out, "%s<p align=\"center\">%s</p>\n", out,
		   (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");
	}

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

// trans on off service
      if (strcmp (cmd, "c=trans") == 0)
	{
	  sprintf (out, "%s<p align=\"center\">COUNTER</p>", out);
	  sprintf (out, "%s<p align=\"center\">%s</p>\n", out,
		   (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)
	{
	  sprintf (out, "%s<p align=\"center\">ROTATE MSG</p>", out);
	  sprintf (out, "%s<p align=\"center\">%s</p>\n", out,
		   (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)
	{
	  sprintf (out, "%s<p align=\"center\">EMOTICONS %s</p>\n",
		   out,
		   (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)
	{
	  sprintf (out, "%s<p align=\"center\">PL fonts</p>", out);
	  sprintf (out, "%s<p align=\"center\">%s</p>\n", out,
		   (nopl) ? "on" : "off");
	  snprintf (wapfile, sizeof (wapfile), "%s/wap-nopl", home);
	  if (nopl)
	    {
	      unlink (wapfile);
	    }
	  else
	    {
	      if ((wap = fopen (wapfile, "w")))
		fclose (wap);
	    }
	  dologu (noold, "nopl %d\n", nopl);
	}

// xternal emoticons on off service
      if (strcmp (cmd, "c=xemot") == 0)
	{
	  sprintf (out, "%s<p align=\"center\">COLOR", out);
	  sprintf (out, "%s %s</p>\n", out, (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)
	{
	  sprintf (out, "%s<p align=\"center\">Check reason</p>", out);
	  sprintf (out, "%s<p align=\"center\">%s</p>\n", out,
		   (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];

	  sprintf (out, "%s<p align=\"center\">DELETE</p>", out);
	  sprintf (out, "%s<p>%s</p>", out, 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
			    sprintf (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;
	  sprintf (out, "%s<p align=\"center\">ALL CHAT WINDOWS CLOSED</p>",
		   out);
	  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];

	  sprintf (out, "%s<p align=\"center\">DELETE PAGE</p>", out);
	  // nie drukujemy litanii
	  // sprintf(out,"%s<p>%s</p>",out,tresc);

	  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
				sprintf (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))
			    {
			      sprintf (userlist,
				       "%s<p mode=\"nowrap\">%s %s<a href=\"?c=f;n=%s;t=a\">%s</a> %s</p>\n",
				       userlist,
				       (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)
		    sprintf (userlist,
			     "%s<do type=\"%c%d\" label=\"od %c\"><go href=\"?c=lista;n=%d;t=a\"/></do>",
			     userlist, tabmenu[kk], kk, tabmenu[kk], kk * 10);
		}
	      fclose (wap);
	      if (ggul > 0)
		fclose (ggul);
	    }
	  sprintf (out, "%s<p align=\"center\">GG LIST</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=\"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>", out, userlist, 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))
				    {
				      sprintf (userlist,
					       "%s<p>%s <a href=\"?c=f;n=%s;t=0\">%s</a> %s</p>",
					       userlist, rstatus, wapfile,
					       (strlen (rnicks) >
						0) ? rnicks : wapfile,
					       rreason);
				    }
				  licz++;
				}
			    }
			}
		    }
		}
	      fclose (wap);
	    }

	  sprintf (out, "%s<p align=\"center\">JABBER ROSTER</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=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>", out, userlist, 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;

	  sprintf (out, "%s<p align=\"center\">MSG HISTORY<br/>%s</p>", out,
		   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
					      sprintf (out,
						       "%s<p>%s(%14.14s) %s</p>",
						       out, buf, tmpczas,
						       tre);
					      sprintf (nexttime, "%s",
						       tmpczas);
					      dispmsg++;
					    }
					}
				      else
					{
					  firstmsg++;
					}
				      allmsg++;
				    }
				}
			    }
			  fclose (wap);
			}
		    }
		  free (namelist[k - n - 1]);
		}
	      free (namelist);
	    }

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


	  sprintf (out,
		   "%s<do type=\"accept\" label=\"reload\"><go href=\"?\"/></do>"
		   "<do type=\"prev\" label=\"reload\"><go href=\"?\"/></do>"
		   "<do type=\"RTG\" label=\"RTG\"><go href=\"?\"/></do>"
		   "<do type=\"Check\" label=\"Check\"><go href=\"?check0\"/></do>"
		   "</card></wml>", out);
	  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
				      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);
				    }
				}
			    }
			}
		      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
				      sprintf (out,
					       "%s<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",
					       out, buf, buf, timetodel,
					       timetodel + 5, tre, timetodel);
				      if (raz++ == 0)
					sprintf (delp, "%s%7.7s;", delp,
						 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)
		{
		  sprintf (out,
			   "%s<do type=\"lista%d\" label=\"%s\"><go href=\"?c=f;n=%s;t=a\"/></do>\n",
			   out, ile, nicktmp, nicktmp);
		  ile++;
		}
	    }
	  fclose (wap);
	}
    }
  else
    {
      sprintf (out,
	       "%s<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>", out, uin,
	       emails + msgs, (int) time (NULL), (int) time (NULL), ckto,
	       ctime (&czas) + 11);
      if (!connected)
	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);
      if (beep && nick[0] != 'a')
	{
	  int oldmsg = atoi (nick);
	  if (oldmsg < (emails + msgs))
	    {
	      sprintf (out,
		       "%s<p align=\"center\"><img src=\"?beep.midi\" alt=\"New messages\"/></p>",
		       out);
	    }
	}
      sprintf (out, "%s<p align=\"center\">mail: %d im: %d</p>%s"
	       "<do type=\"mail\" label=\"mail\"><go href=\"?c=mail;n=0;t=0\"/></do>"
	       "<do type=\"accept\" label=\"im\"><go href=\"?\"/></do>"
	       "<do type=\"prev\" label=\"im\"><go href=\"?\"/></do>"
	       "</card></wml>", out, emails, msgs, statx);
      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> <a href=\"?check0\">Check</a></p>",
	   out);

  sprintf (out, "%s<do type=\"accept\" label=\"reload\"><go href=\"?\"/></do>" "<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", out, (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>" : "<do type=\"winclose\" label=\"Win Close\"><go href=\"?c=altk;n=0;t=a\"/></do>");	//~228
  return 0;
}
