#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 "utils.h"
#include "www.h"

int
fewww (char *out, char *query_str, int max_len, int xe_count, int fblw)
{
  char buf[1001];
  char wapfile[256], wapfile2[256];
  char cmd[51];
  char tresc[1001], nick[51], *nickw, *trescw, *ztrescw;
  char openokna[1000][50];
  int oocount = 0;
  int listtimeout = 30;
  int chattimeout = 15;
  int od;
  int nopl = 0;
  int noold = 0;
  int ulmax = 500;
  int jabber = 0;
  char userlisttab[ulmax][500];
  char sortlist[ulmax][2];
  int ulcount = 0;
  FILE *wap;
  struct dirent **namelist;
  time_t czas = time (NULL);

  if (!fblw)
    jabber = 1;

  dologu (noold, "%24.24s %.950s\n", ctime (&czas), query_str);

  sprintf (out, "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/html\r\n" "\r\n");

/* pierwsze przekierowanie do wlasciwego okna */

  if (strcmp (query_str, "www") == 0)
    {
      sprintf (out,
	       "%s<html><script language=\"javascript\">window.open(\"?www=0&c=main&n=0&t=0\",\"GG\",\"menubar=no,width=200,height=400,toolbar=no,resizable=yes,scrollbars=yes,status=yes\");</script>"
	       "<head>"
	       "<META http-equiv=\"Refresh\" content=\"0;URL=http://gg.overwap.net/\""
	       "</head></html>", out);
      return 0;
    }

  if (strlen (query_str) < 7)
    {
      sprintf (out, "%s<html><body bgcolor=\"#80e080\">What?!</body></html>",
	       out);
      return 0;
    }

  sprintf (buf, "%.999s", query_str + 6);

  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, "%sBlad skladni 1", out);
	  return 0;
	}
      nickw++[0] = 0;

      trescw = (char *) index (nickw, '&');
      if (trescw == NULL)
	{
	  sprintf (out, "%sBlad skladni 2", out);
	  return 0;
	}
      trescw++[0] = 0;

      ztrescw = (char *) index (trescw, '&');
      if (ztrescw)
	ztrescw[0] = 0;

      if (strlen (nickw) < 3 || strlen (trescw) < 2)
	{
	  sprintf (out, "%sBlad skladni 3", out);
	  return 0;
	}

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

// usunac enkodowane znaczki z tresci

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

//    memset(&openokna,0,sizeof(openokna));


// wiadomosc jabbera
      if (strcmp (cmd, "c=mesgs") == 0 && jabber && ztrescw)
	{
	  char wiadbuf[2000], *end;

//    sprintf(out,"%s%s",out,query_str);
//    return 0;

	  end = (char *) index (++ztrescw, '&');
	  if (end)
	    {
	      end[0] = 0;
	      snprintf (wiadbuf, sizeof (wiadbuf), "%s", ztrescw + 3);
	      decode (wiadbuf, sizeof (wiadbuf) - 1, nopl);
	      sprintf (out, "%s %s %s %s", out, nick, tresc, wiadbuf);

	      iso2jabber (tresc, sizeof (tresc) - 1);
	      iso2jabber (wiadbuf, sizeof (wiadbuf) - 1);

	      dojbr ("msgsbj %s %s\n", nick, tresc);
	      dojbr ("msgsnd %s %s\n", nick, wiadbuf);

	      return 0;
	    }
	  else
	    {
	      sprintf (cmd, "c=mesg");
	      sprintf (tresc, "0");
	    }
	}

// usluga add

      if (strcmp (cmd, "c=adds") == 0)
	{
	  if (strstr (nick, "@") && jabber && strlen (nick) > 0)
	    {
	      dojbr ("add %s %s\n", nick, tresc);
	    }
	  else
	    {
	      long int nicki = atoi (nick);
	      if (nicki > 0)
		{
		  doekg ("/add %s %s\n", nick, tresc);
		}
	      dologu (noold, "/add %s %s\n", nick, tresc);
	    }
	  dologu (noold, "/add %s %s\n", nick, tresc);
	  sprintf (cmd, "c=lista");
	  sprintf (nick, "0");
	  sprintf (tresc, "0");
	}

      if (strcmp (cmd, "c=delete") == 0)
	{
	  if (strstr (nick, "@") && jabber && strlen (nick) > 0)
	    {
	      dojbr ("del %s\n", nick);
	    }
	  else
	    {
	      doekg ("/del %s\n", nick);
	    }
	  dologu (noold, "delete %s\n", nick);
	  sprintf (cmd, "c=output");
	}

      if (strcmp (cmd, "c=sub") == 0)
	{
// subskrypcje jabbera
	  if (jabber && strstr (nick, "@"))
	    {
	      int t = atol (tresc);

	      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);
	    }
	  sprintf (cmd, "c=output");
	}

// del usluga

      if (strcmp (cmd, "c=d") == 0)
	{
	  struct stat buf;
	  char tmpczas[50];
	  int n, k;

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

	  snprintf (wapfile, sizeof (wapfile), "%s/", home);
	  n = scandir (wapfile, &namelist, 0, alphasort);
	  if (n < 0)
	    perror ("scandir");
	  else
	    {
	      k = n;
	      while (n--)
		{
		  snprintf (wapfile, sizeof (wapfile), "%7.7s",
			    namelist[k - n - 1]->d_name + 3);
//              sprintf(out,"%s%s",out,wapfile);
		  if ((strstr (tresc, wapfile) != 0) &&
		      (strstr (namelist[k - n - 1]->d_name, nick) != 0) &&
		      (namelist[k - n - 1]->d_name[5] == ':') &&
		      (strncmp (namelist[k - n - 1]->d_name, "wap", 3) == 0))
		    {
		      snprintf (wapfile, sizeof (wapfile), "%s/%s", home,
				namelist[k - n - 1]->d_name);
		      // sprintf(out,"%sk: %s<br>",out,wapfile);
		      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
	  sprintf (cmd, "c=output");
	  sprintf (tresc, "0");
	}			//if c=d

      if (strcmp (cmd, "c=co") == 0)
	{
	  doekg ("/connect\n");
	  dologu (noold, "/connect\n");
	  sprintf (cmd, "c=lista");
	}

      if (strcmp (cmd, "c=dc") == 0)
	{
	  doekg ("/disconnect\n");
	  dologu (noold, "/disconnect\n");
	  sprintf (cmd, "c=lista");
	}

      if (strcmp (cmd, "c=listl") == 0)
	{
	  doekg ("/list -g\n");
	  dologu (noold, "list download\n");
	  sprintf (cmd, "c=lista");
	}


// okienko add

      if (strcmp (cmd, "c=add") == 0)
	{
	  sprintf (out, "%s<html><head>"
		   "<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; CHARSET=iso-8859-2\">"
		   "</head>"
		   "<body bgcolor=\"#80e080\"><p align=center><font face=Verdana size=1>"
		   "<form action=\"?\">"
		   "<input type=hidden name=www value=0>"
		   "<input type=hidden name=c value=adds>"
		   "Add contact:<br>"
		   "Gg/JID<br><input name=n><br>"
		   "Nick<br><input name=t><br>"
		   "<input type=submit name=OK value=Add><br><br>"
		   "<a href=\"?www=0&c=lista&n=0&t=0\">Back</a>"
		   "</form>" "</font></body></html>", out);
	  return 0;
	}

      if (strcmp (cmd, "c=output") == 0 || strcmp (cmd, "c=lista") == 0 ||
	  strcmp (cmd, "c=hist") == 0)
	{
	  int n = 0, ki = 0;
	  int lli = (strcmp (cmd, "c=lista") == 0) ? 1 : 0;
	  int his = (strcmp (cmd, "c=hist") == 0) ? 1 : 0;
	  char tuin[50], fuin[50], lastt[50], *fu;
	  char dellist[300];

	  memset (&dellist, sizeof (dellist), 0);
	  if (!lli)
	    {
	      sprintf (out, "%s<html><head>"
		       "<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; CHARSET=iso-8859-2\">",
		       out);
	      if (!his)
		sprintf (out,
			 "%s<META http-equiv=\"Refresh\" content=\"%d;URL=http:?www=0&c=output&n=%s&t=0\">",
			 out, chattimeout, nick);
	      sprintf (out,
		       "%s</head>"
		       "<body onLoad=\"javascript:location.replace('http:#end');\"><font face=Verdana size=1>",
		       out);
	      gettuin (nick, tuin, sizeof (tuin));
	    }
	  sprintf (wapfile, "%s/", home);
	  if (his && tresc[0] == '0')
	    memset (&tresc, sizeof (tresc), 0);
	  n = scandir (wapfile, &namelist, 0, alphasort);
	  if (n < 0)
	    perror ("scandir");
	  else
	    {
	      ki = n;
	      while (n--)
		{
		  if (((strncmp (namelist[ki - n - 1]->d_name, "wap", 3) == 0)
		       || (strncmp (namelist[ki - n - 1]->d_name, "oldwap", 6)
			   == 0 && his))
		      && index (namelist[ki - n - 1]->d_name, ':') != 0)
		    {
		      int oldp =
			(strncmp (namelist[ki - n - 1]->d_name, "oldwap", 6)
			 == 0) ? 1 : 0;

		      memset (&fuin, sizeof (fuin), 0);
		      fu = (char *) index (namelist[ki - n - 1]->d_name, '_');
		      if (fu)
			snprintf (fuin, sizeof (fuin), "%s", fu + 1);
		      sprintf (wapfile, "%s/%s", home,
			       namelist[ki - n - 1]->d_name);
		      if ((wap = fopen (wapfile, "r")))
			{
			  while (!feof (wap))
			    {
			      memset (buf, 0, sizeof (buf));
			      fgets (buf, 1000, wap);
			      if (strlen (buf) > 0)
				{
				  char *k = (char *) index (buf, 10);
				  if (k != NULL)
				    k[0] = 0;
				  trescw = (char *) index (buf, ';');
				  if (trescw != NULL)
				    {
				      trescw++[0] = 0;
//                          if(strcmp(buf,nick)==0) {
				      if (strcmp (fuin, nick) == 0)
					{
					  char tre[3001];
					  char msgt[50];

					  if (!oldp)
					    {
					      sprintf (msgt, "%5.5s:%2.2s",
						       namelist[ki - n -
								1]->d_name +
						       3,
						       namelist[ki - n -
								1]->d_name +
						       8);
					    }
					  else
					    {
					      sprintf (msgt,
						       "%8.8s-%5.5s:%2.2s",
						       namelist[ki - n -
								1]->d_name +
						       6,
						       namelist[ki - n -
								1]->d_name +
						       14,
						       namelist[ki - n -
								1]->d_name +
						       19);
					    }
					  sprintf (tre, "%.3000s", trescw);
					  dozwolonys (tre, sizeof (tre), 4, 1,
						      0, 0, xe_count);
					  if (!lli)
					    {
					      if (strlen (out) +
						  strlen (tre) + 200 <
						  max_len)
						{
						  if (his)
						    {
						      if (oldp
							  &&
							  (strcmp
							   (msgt, tresc) > 0))
							{
							  sprintf (out,
								   "%s[%s] <b>%s</b> %s<br>\n",
								   out, msgt,
								   buf, tre);
							  sprintf (lastt,
								   "%s",
								   msgt);
							}
						    }
						  else
						    {
						      sprintf (out,
							       "%s[%s] <b>%s</b> %s<a href=\"?www=0&c=d&n=%s&t=%5.5s%2.2s\"> Del</a><br>\n",
							       out, msgt, buf,
							       tre, nick,
							       msgt,
							       msgt + 6);
						      sprintf (dellist,
							       "%s%5.5s%2.2s;",
							       dellist, msgt,
							       msgt + 6);
						    }
						}
					    }
					}
				      if (lli)
					{
					  int ii;
					  int kk = 0;

					  for (ii = 0; ii < oocount; ii++)
					    if (strcmp (openokna[ii], fuin) ==
						0)
					      kk = 1;
					  if (strcmp (buf, "ja") == 0)	//todo: tu lepiej po uinie
					    kk = 1;
					  if (!kk)
					    {
					      sprintf (openokna[oocount],
						       "%s", fuin);
					      oocount++;
					    }
					}
				    }
				}
			    }
			  fclose (wap);
			}
		    }
		  free (namelist[ki - n - 1]);
		}
	      free (namelist);
	    }			//else ok open dirent
	  if (!lli)
	    {
	      if (his)
		sprintf (out, "%s"
			 "<a href=\"?www=0&c=hist&n=%s&t=%s\">next<br>"
			 "<a href=\"?www=0&c=output&n=%s&t=0\">chat</a><br>",
			 out, nick, lastt, nick);
	      if (strlen (dellist) > 0)
		sprintf (out,
			 "%s<a href=\"?www=0&c=d&n=%s&t=%s\">Del all</a>",
			 out, nick, dellist);
	      sprintf (out, "%s</font><a name=#end></a></body></html>", out);
	      return 0;
	    }
	}			//c=output

      if (strcmp (cmd, "c=lista") == 0)
	{
	  int licz = 0, iis;
	  char tabmenu[10], *descr, *e, *ek;
	  char gguserlist[301];
	  FILE *ggul;

	  memset (&tabmenu, 0, sizeof (tabmenu));
	  od = atol (nick);
	  ulcount = 0;
	  // memset(userlist,0,sizeof(userlist));
	  sprintf (wapfile, "%s/wapstatus", home);
	  sprintf (gguserlist, "%s/userlist", home);
	  ggul = fopen (gguserlist, "r");
	  if ((wap = fopen (wapfile, "r")))
	    {
	      while (fgets (wapfile, 100, wap))
		{
		  char reason[301];
		  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)
			    {
			      sprintf (reason, "%.200s", descr);
			      dozwolonys (reason, sizeof (reason), 4, 1, 0, 0,
					  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 + 300) > licz))
			    {
			      char tuin[50];
			      gettuin (wapfile, tuin, sizeof (tuin));
			      if (ulcount == ulmax)
				ulcount--;
			      sprintf (sortlist[ulcount], "%s",
				       (st == 0x01 || st == 0x15 || st == 0x14
					|| st == 0x16
					|| st == 0x06) ? "0" : "1");
			      snprintf (userlisttab[ulcount++],
					sizeof (userlisttab[0]),
					"%s %s<a href=\"#\" onclick=\"parent.owin('%s');\">%s</a>%s%s\n",
					((st == 0x01 || st == 0x15
					  || st == 0x14 || st == 0x16
					  || st ==
					  0x06) ?
					 "<img src=\"?sleep.gif\" alt=\"disc\"/>"
					 // "Disc -"
					 :
					 ((st == 0x02 || st == 0x04) ?
					  "<img src=\"?jest.gif\" alt=\"conn\"/>"
					  //      "Conn - "
					  :
					  "<img src=\"?busy.gif\" alt=\"busy\"/>"
					  //      "Busy - "
					 )),
					(e != NULL) ?
					((strlen (e) > 0
					  && strcmp (wapfile,
						     gguserlist) ==
					  0)
					 ?
					 ("<img src=\"?komorka.gif\" alt=\"tel\"/>"
					  // "tel - "
					 )
					 :
					 "")
					:
					"",
					tuin,
					wapfile,
					(descr != NULL) ? "<br>" : "",
					(descr != NULL) ? reason : "");
			    }
			  licz++;
			}
		    }
		}
	      fclose (wap);
	      if (ggul > 0)
		fclose (ggul);
	    }


	  /* roster */
	  sprintf (wapfile, "%s/wap-roster", home);
	  if ((wap = fopen (wapfile, "r")))
	    {
	      char jid[501];
	      char *enick, *estat, *egrupa, *ereason, *e;
	      char eereason[500], rstatus[100];

	      while (fgets (jid, 500, wap))
		{
		  e = (char *) index (jid, 10);
		  if (e)
		    e[0] = 0;
		  e = (char *) index (jid, 13);
		  if (e)
		    e[0] = 0;

		  enick = (char *) index (jid, ';');
		  if (enick)
		    {
		      enick++[0] = 0;
		      estat = (char *) index (enick, ';');
		      if (estat && index (jid, '@'))
			{
			  estat++[0] = 0;
			  egrupa = (char *) index (estat, ';');
			  if (egrupa)
			    {
			      egrupa++[0] = 0;
			      ereason = (char *) index (egrupa, ';');
			      if (ereason)
				{
				  ereason++[0] = 0;

				  snprintf (eereason, sizeof (eereason), "%s",
					    ereason);
				  dozwolonys (eereason, sizeof (eereason), 4,
					      1, 0, 0, xe_count);

				  snprintf (rstatus, sizeof (rstatus), "%s",
					    estat);
				  jbr_status (rstatus, sizeof (rstatus), 1, 1,
					      0);

				  if (ulcount >= ulmax)
				    ulcount--;
				  sprintf (sortlist[ulcount], "%s",
					   (strcmp (estat, "offline") ==
					    0) ? "0" : "1");
				  snprintf (userlisttab[ulcount++],
					    sizeof (userlisttab[0]),
					    "%s <a href=\"#\" onclick=\"parent.owin('%s');\">%s</a>%s%s\n",
					    rstatus, jid,
					    (strlen (enick) >
					     0) ? enick : jid,
					    (strlen (eereason) >
					     0) ? "<br>" : "",
					    (strlen (eereason) >
					     0) ? eereason : "");
				}
			    }
			}
		    }
		}
	      fclose (wap);
	    }


// window.pageYOffset

	  sprintf (out, "%s<html><head><title>RTG</title>"
		   "<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; CHARSET=iso-8859-2\">",
		   out);

	  sprintf (out, "%s\n\n<script language=\"javascript\">\n", out);

	  sprintf (out, "%s"
		   "function myreload() {\n"
		   "	co=\"http:?www=0&c=lista&n=0&t=0\";\n"
		   "//	nstr.document.URL=co;\n"
		   "//	alert(typeof nstr);\n"
		   "	document.location.reload(co);\n"
		   "	}\n"
		   "setTimeout(\"myreload()\",%d000);\n", out, listtimeout);

	  for (iis = 0; iis < oocount; iis++)
	    {
	      sprintf (out, "%sparent.owin('%s');\n", out, openokna[iis]);
	    }
	  sprintf (out, "%s</script>\n\n</head>", out);
	  sprintf (out,
		   "%s<body bgcolor=\"#80e080\"><font face=Verdana size=1>",
		   out);

	  for (iis = 0; iis < ulcount; iis++)
	    {
	      if (sortlist[iis][0] == '1')
		sprintf (out, "%s%s<br>", out, userlisttab[iis]);
	    }
	  for (iis = 0; iis < ulcount; iis++)
	    {
	      if (sortlist[iis][0] == '0')
		sprintf (out, "%s%s<br>", out, userlisttab[iis]);
	    }

	  sprintf (out, "%s</font></body></html>", out);
	  return 0;
	}			//userlista

      if (strcmp (cmd, "c=f") == 0)
	{			//frameset okienka
	  char tnick[50];

	  if (strstr (nick, "@"))
	    gettnickr (nick, tnick, sizeof (tnick));
	  else
	    gettnick (nick, tnick, sizeof (tnick));
	  sprintf (out, "%s<html>"
		   "<head>"
		   "<title>%s/%s - RTG</title>\n"
		   "<script>var winOpen=true;</script>\n"
		   "</head>\n"
		   "<frameset rows=\"24px, *, 24px\" onUnload=\"javascript:window.opener.clwin('%s');\" framespacing=0>\n"
		   "<frame src=\"http:?www=0&c=umenu&n=%s&t=0\" noresize scrolling=no name=%smenu frameborder=\"no\">\n"
		   "<frame src=\"http:?www=0&c=output&n=%s&t=0#end\" noresize scrolling=yes name=%sout frameborder=\"no\">\n"
		   "<frame src=\"http:?www=0&c=input&n=%s&t=0\" noresize scrolling=no name=%sin frameborder=\"no\">\n"
		   "</frameset>\n"
		   "</html>\n", out, tnick, nick, nick, nick, nick, nick,
		   nick, nick, nick);
	  return 0;
	}			//c=f

      if (strcmp (cmd, "c=main") == 0)
	{			//glowne okno frameset
	  sprintf (out, "%s<html>"
		   "<head>\n"
		   "<title>RTG</title>\n\n"
		   "</head>\n\n"
		   "<script language=\"javascript\">\n"
		   "var oknaa=new Object();\n"
		   "\n"
		   "function owin(a) {\n"
		   "if( typeof oknaa[a] != \"undefined\")\n"
		   "	if( oknaa[a] == \"c\" )\n"
		   "		oknaa[a]=window.open('http:?www=0&c=f&n='+a+'&t=a','','menubar=no,width=400,height=200,toolbar=no,resizable=yes,scrollbars=yes,status=yes');\n"
		   "if( typeof oknaa[a] == \"undefined\")\n"
		   "	oknaa[a]=window.open('http:?www=0&c=f&n='+a+'&t=a','','menubar=no,width=400,height=200,toolbar=no,resizable=yes,scrollbars=yes,status=yes');\n"
		   "};\n"
		   "function clwin(a) {\n"
		   "if( typeof oknaa[a] != \"undefined\")\n"
		   "	oknaa[a]=\"c\";\n"
		   "};\n"
		   "function clall() {\n"
		   "for(k in oknaa) {\n"
		   "	window.oknaa[k].close();\n"
		   "	}\n"
		   "};\n"
		   "</script>\n\n"
		   "<frameset rows=\"50px, *, 50px\" framespacing=0 onUnload=\"javascript:clall();\">"
		   "<frame src=\"http:?www=0&c=menu&n=0&t=0\" noresize scrolling=yes frameborder=\"no\">"
		   "<frame src=\"http:?www=0&c=lista&n=0&t=0\" noresize scrolling=yes name=\"lista\" frameborder=\"no\">"
		   "<frame src=\"http:?www=0&c=status&n=0&t=0\" noresize scrolling=no frameborder=\"no\">"
		   "</frameset>" "</html>", out);
	  return 0;
	}			//c=f

      if (strcmp (cmd, "c=menu") == 0)
	{
	  sprintf (out, "%s<html>"
		   "<head>"
		   "<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; CHARSET=iso-8859-2\">"
		   "</head>"
		   "<body bgcolor=\"#80e080\"><p align=center><font size=1 face=Verdana>"
		   "<a href=\"http:?www=0&c=co&n=0&t=0\" target=\"lista\">Connect</a> "
		   "<a href=\"http:?www=0&c=add&n=0&t=0\" target=\"lista\">Add</a> "
// "<a href=\"http:?www=0&c=msg&n=0&t=0\" target=\"lista\">Mgs</a> "
		   "<a href=\"http:?www=0&c=listl&n=0&t=0\" target=\"lista\">Load gg list</a> "
		   "<a href=\"http:?www=0&c=dc&n=0&t=0\" target=\"lista\">Disconnect</a> "
		   "</font></p></body>", out);
	  return 0;
	}

      if (strcmp (cmd, "c=status") == 0)
	{
	  FILE *twap;
	  char sgg[3], sjb[3];

	  sprintf (sgg, "D");
	  sprintf (sjb, "D");

	  if ((twap = vfopen ("r", "%s/wapstatus", home)))
	    {
	      fgets (sgg, 2, twap);
	      fclose (twap);
	    }
	  if (jabber)
	    {
	      if ((twap = vfopen ("r", "%s/wap-roster", home)))
		{
		  fgets (sjb, 2, twap);
		  fclose (twap);
		}
	    }

	  sprintf (out, "%s<html>"
		   "<head>"
		   "<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; CHARSET=iso-8859-2\">"
		   "<META http-equiv=\"Refresh\" content=\"15\""
		   "</head>"
		   "<body bgcolor=\"#80e080\"><p align=center><font size=1 face=Verdana>"
		   "Gg: %s%s<br>"
		   "<a href=\"#\" onClick=\"javascript:ok=window.open('?www=0&c=statusm&n=0&t=0','RTGStatus','menubar=no,width=200,height=400,toolbar=no,resizable=yes,scrollbars=yes,status=yes')\">Status</a>"
		   "</font></p></body>",
		   out,
		   (sgg[0] == 'C') ? "connected" : "disconnected",
		   (jabber) ? (sjb[0] ==
			       'C') ? " Jbr: connected" : " Jbr: disconnected"
		   : "");
	  return 0;
	}

      if (strcmp (cmd, "c=input") == 0 || strcmp (cmd, "c=send") == 0)
	{
	  if (strcmp (cmd, "c=send") == 0)
	    {
	      char waptime2[30], waptime[256];
	      char targetuin[100];
	      FILE *wap;

	      // send
	      dologu (noold, "/chat %s %s\n", nick, tresc);
	      snprintf (targetuin, sizeof (targetuin), "%s", nick);

	      strftime (waptime2, sizeof (waptime2), "%H:%M%S",
			localtime ((time_t *) & czas));
	      snprintf (waptime, sizeof (waptime), "%s/wap%7s_%s", home,
			waptime2, targetuin);
	      if ((wap = fopen (waptime, "a")))
		{
		  fprintf (wap, "ja;%s\n", tresc);
		  fclose (wap);
		}

	      if (index (nick, '@') && jabber)
		{
		  iso2jabber (tresc, sizeof (tresc));
		  dojbr ("chat %s %s\n", nick, tresc);
		}
	      else
		doekg ("/chat %s %s\n", nick, tresc);
	    }
	  sprintf (out, "%s<html>"
		   "<head>"
		   "<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; CHARSET=iso-8859-2\">"
		   "</head>"
		   "<body bgcolor=\"#80e080\" onLoad=\"javascript:document.f.t.focus();\">"
		   "<form action=\"http:?\" method=get name=f>"
		   "<input type=hidden name=www value=0>"
		   "<input type=hidden name=c value=send>"
		   "<input type=hidden name=n value=%s>"
		   "<input type=text name=t STYLE=\"position:absolute; top: 0; left: 0; width: 100%%\">"
		   "</form></body>" "</html>", out, nick);
	  return 0;
	}			//c=input

      if (strcmp (cmd, "c=umenu") == 0)
	{
	  sprintf (out, "%s<html>"
		   "<head>"
		   "<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; CHARSET=iso-8859-2\">"
		   "</head>"
		   "<body bgcolor=\"#80e080\" leftmargin=0 topmargin=0 marginwidth=0 marginheight=0>"
		   "<font face=Verdana size=1>"
		   "<a href=\"?www=0&c=uemenu&n=%s&t=0\" target=\"%sout\">menu</a> "
		   "<a href=# onclick=\"javascript:window.open('?www=0&c=mesg&n=%s&t=0','','menubar=no,width=500,height=300,toolbar=no,resizable=yes,scrollbars=yes,status=yes')\">Message</a>"
		   "</font>" "</body>" "</html>", out, nick, nick, nick);
	  return 0;
	}

      if (strcmp (cmd, "c=statusm") == 0)
	{
	  sprintf (out, "%s<html>"
		   "<head>"
		   "<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; CHARSET=iso-8859-2\">"
		   "<title>Status - RTG</title>"
		   "</head>"
		   "<body bgcolor=\"#80e080\">"
		   "<font face=Verdana size=1>"
		   "<form action=\"\">"
		   "<input type=hidden name=www value=0>"
		   "<input type=hidden name=c value=statusm>"
		   "<input type=hidden name=n value=0>"
		   "Reason: <input name=t>"
		   "</form>" "</body>" "</html>", out);
	  return 0;
	}

      if (strcmp (cmd, "c=mesg") == 0 && jabber)
	{
	  char tnick[50];

	  if (strstr (nick, "@"))
	    gettnickr (nick, tnick, sizeof (tnick));

	  sprintf (out, "%s<html>"
		   "<head>"
		   "<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; CHARSET=iso-8859-2\">"
		   "<title>%s/%s - RTG</title>"
		   "</head>"
		   "<body bgcolor=\"#80e080\">"
		   "<font face=Verdana size=1>"
		   "<form action=\"?\">"
		   "To: %s<br>"
		   "<input type=hidden name=www value=\"0\">"
		   "<input type=hidden name=c value=\"mesgs\">"
		   "<input type=hidden name=n value=\"%s\">"
		   "Subject: <input name=t size=40><br>"
		   "<textarea name=tt cols=\"50\" rows=\"10\" wrap=\"soft\"></textarea>"
		   "<input type=submit name=ok value=Send>"
		   "</form>"
		   "</font>"
		   "</body>" "</html>", out, tnick, nick, tnick, nick);
	  return 0;
	}

      if (strcmp (cmd, "c=uemenu") == 0)
	{
// historia, delete
// subscribe, unsubscribe, approve, deny
	  sprintf (out, "%s<html>"
		   "<head>"
		   "<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; CHARSET=iso-8859-2\">"
		   "</head>"
		   "<body bgcolor=\"#80e080\">"
		   "<font face=Verdana size=1>"
		   "<p align=center>"
		   "<b>Menu</b><br><br>"
		   "<a href=\"?www=0&c=hist&n=%s&t=0\">history</a><br><br>"
		   "<a href=\"?www=0&c=delete&n=%s&t=0\">delete</a><br><br>",
		   out, nick, nick);

	  if (strstr (nick, "@") && jabber)
	    {
	      sprintf (out, "%s"
		       "<a href=\"?www=0&c=sub&n=%s&t=1\">subscribe</a><br>"
		       "<a href=\"?www=0&c=sub&n=%s&t=0\">unsubscribe</a><br>"
		       "<a href=\"?www=0&c=sub&n=%s&t=3\">approve request</a><br>"
		       "<a href=\"?www=0&c=sub&n=%s&t=2\">deny request</a><br>",
		       out, nick, nick, nick, nick);
	    }

	  sprintf (out, "%s"
		   "<br><a href=\"?www=0&c=output&n=%s&t=0\">chat</a><br>"
		   "</p></font></body></html>", out, nick);
	  return 0;
	}

    }				//strlen(buf)>0

  sprintf (out,
	   "%s<html><title>RTG</title><body bgcolor=\"#80e080\">What?!</body></html>",
	   out);
  return 0;
}
