int
fewww (char *out, char *query_str, int max_len)
{
  char buf[1001];
  char wapfile[160];
  char cmd[51];
  char tresc[1001], nick[51], *nickw, *trescw;
  int od;
  char userlist[10000];
  FILE *wap;
  struct dirent **namelist;

  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");

  if (strcmp (query_str, "www") == 0)
    {
      sprintf (out,
	       "%s<script language=\"javascript\">window.open(\"?www=0&c=lista&n=0&t=0\",\"GG\",\"menubar=no,width=200,height=400,toolbar=no,resizable=yes,scrollbars=yes\");</script>",
	       out);
      return 0;
    }

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

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

  memset (&cmd, 0, sizeof (cmd));	//sprintf(cmd,"");
  if (strlen (buf) > 0)
    {
// jesli jest jakiskolwiek query to musi byc prawidlowy
// proste parsowanie jednoznakowych name-ow
      nickw = (char *) index (buf, '&');
      if (nickw == NULL)
	{
	  sprintf (out, "%sBlad skladni 1", out);
	  return 0;
	}
      nickw++[0] = 0;
      trescw = (char *) index (nickw, '&');
      if (trescw == NULL)
	{
	  sprintf (out, "%sBlad skladni 2", out);
	  return 0;
	}
      trescw++[0] = 0;

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

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

// usunac enkodowane znaczki z tresci

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

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

	  memset (&tabmenu, 0, sizeof (tabmenu));
	  od = atol (nick);
	  memset (userlist, 0, sizeof (userlist));	//sprintf(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[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)
			    {
			      sprintf (reason, "%.200s", descr);
			      dozwolonys (reason, 1);
			    }
			}
		      e = NULL;
		      memset (gguserlist, 0, sizeof (gguserlist));	//sprintf(gguserlist,"");
		      if (ggul > 0)
			if (!feof (ggul))
			  {
			    fgets (gguserlist, 200, ggul);
			    e = (char *) index (gguserlist, ';');
			    if (e != NULL)
			      {
				e++[0] = 0;
				e = (char *) index (e, ';');
				if (e != NULL)
				  {
				    e++[0] = 0;
				    e = (char *) index (e, ';');
				    if (e != NULL)
				      {
					e++[0] = 0;
					e = (char *) index (e, ';');
					if (e != NULL)
					  {
					    e++[0] = 0;
					    ek = (char *) index (e, ';');
					    if (ek != NULL)
					      {
						ek[0] = 0;
					      }
					  }
				      }
				  }
			      }
			  }
		      if (strlen (wapfile) > 0 && strlen (wapfile) < 50
			  && atol (k) != 0)
			{
			  //umiescic czy nie na aktualnie wyswietlanej liscie         
			  int st = atol (k);
			  if ((od <= licz) && ((od + 300) > licz)
			      && (strlen (userlist) < 10000))
			    {
			      sprintf (userlist,
				       "%s %s %s<a href=\"#\" onclick=\"window.open('http:?www=0&c=f&n=%s&t=a','%s','menubar=no,width=400,height=200,toolbar=no,resizable=yes,scrollbars=yes');\">%s</a> %s<br>\n",
				       userlist,
				       ((st == 0x01 || st == 0x15
					 || st == 0x14 || st == 0x16
					 || st == 0x06) ?
					// "<img src=\"/rtg/sleep.wbmp\" alt=\"disc\"/>"
					"Disc -"
					: ((st == 0x02 || st == 0x04) ?
					   //      "<img src=\"/rtg/jest.wbmp\" alt=\"conn\"/>"
					   "Conn - " :
					   //      "<img src=\"/rtg/busy.wbmp\" alt=\"busy\"/>"
					   "Busy - ")),
				       (e != NULL) ?
				       ((strlen (e) > 0
					 && strcmp (wapfile,
						    gguserlist) == 0) ? (
									      // "<img src=\"/rtg/komorka.wbmp\" alt=\"tel\"/>"
									      "tel - ")
					:
					"")
				       :
				       "",
				       wapfile,
				       wapfile,
				       wapfile,
				       (descr != NULL) ? reason : "");
			    }
			  licz++;
			}
		    }
		}
	      fclose (wap);
	      if (ggul > 0)
		fclose (ggul);
	    }
	  sprintf (out,
		   "%s<html><title>RTG</title><body><font face=Verdana size=1>"
		   "%s" "</font></body></html>", out, userlist);
	  return 0;
	}			//userlista
      if (strcmp (cmd, "c=f") == 0)
	{
	  sprintf (out, "%s<html>"
		   "<title>%s - RTG</title>"
		   "<frameset rows=\"*, 60\">"
		   "<frame src=\"http:?www=0&c=output&n=%s&t=0\" noresize scrolling=yes name=%sout>"
		   "<frame src=\"http:?www=0&c=input&n=%s&t=0\" noresize scrolling=no name=%sin>"
		   "</frameset>"
		   "</html>", out, nick, nick, nick, nick, nick);
	  return 0;
	}			//c=f
      if (strcmp (cmd, "c=input") == 0)
	{
	  sprintf (out, "%s<html>"
		   "<body><form action=\"http:?\" method=get>"
		   "<input type=hidden name=www value=0>"
		   "<input type=hidden name=c value=send>"
		   "<input type=hidden bane=n value=%s>"
		   "<input type=text name=t>"
		   "</form></body>" "</html>", out, nick);
	}			//c=input

      if (strcmp (cmd, "c=output") == 0)
	{
	  int n = 0, ki = 0;
	  sprintf (out, "%s<html><body>", out);
	  sprintf (wapfile, "%s/", home);
	  n = scandir (wapfile, &namelist, 0, 0 /*alphasort */ );
	  if (n < 0)
	    perror ("scandir");
	  else
	    {
	      ki = n;
	      while (n--)
		{
		  if (strncmp (namelist[ki - n - 1]->d_name, "wap", 3) == 0
		      && index (namelist[ki - n - 1]->d_name, ':') != 0)
		    {
//      printf("%s<br>\n", namelist[ki-n-1]->d_name);
		      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)
					{
					  char tre[3001];
					  sprintf (tre, "%.3000s", trescw);
					  dozwolonys (tre, 1);
					  sprintf (out,
						   "%s(%s<a href=\"?c=d;n=%5.5s;t=a\"> Del</a><br>\n",
						   out, tre, tre);
					}
				    }
				}
			    }
			  fclose (wap);
			}
		    }
		  free (namelist[ki - n - 1]);
		}
	      free (namelist);
	    }			//else ok open dirent
	  sprintf (out, "%s</body></html>", out);
	  return 0;
	}			//c=output

    }				//strlen(buf)>0

  sprintf (out, "%s<html><title>RTG</title><body>What?!</body></html>", out);
  return 0;
}
