/***********************************************
 * released under (E) licensing ...            *
 *        (E) RULES AND REGULATIONS            *
 * permission to use/rewrite/add     : granted *
 * permission to trojan/steal        : denied  *
 * permission to use illegally       : denied  *
 * permission to use on /dev/urandom : denied  *
 ***********************************************/
/* contact el8@press.co.jp for full license    */
/* code copyrighted by ~el8 -- don't infringe! */

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int DPORT = 31336;
#define ACCEPT_NO_CONNEX 0
#define NOCONNEX4U "n0 c0nn3cti0nz f0r y0u m0th3rfuqr, baqup\r\n"
#define NIQS1ZE 15
#define MAXINPUT 512
#define CMDCHAR /

typedef struct cHaTbuFF
{
  int fd;
  char niq[NIQS1ZE];
  struct sockaddr_in sin;
}
chat_t;

#define MAXCH4TZ 20

chat_t gchat[MAXCH4TZ];

void
come_g1t_y0rz (void)
{
  int x;

  for (x = 0; x <= MAXCH4TZ; x++)
    {
      gchat[x].fd = -1;
      bzero (&gchat[x].niq, NIQS1ZE);
    }

  return;
}

void
gr33t (int key, struct sockaddr_in *sin)
{
  int x;
  char buff[520];

  memset (buff, 0, sizeof (buff));

  snprintf (buff, (sizeof (buff) - 1),
            "*** Everyone welcome [%s] to ~el8chat ***\r\n", gchat[key].niq);

  for (x = 0; x < MAXCH4TZ; x++)
    {
      if (x == key)
        continue;               /* d0nt greet 0urs3lvez? */
      if (gchat[x].fd != -1)
        send (gchat[x].fd, buff, strlen (buff), 0x00);
    }

  return;
}

int
g3tn1q (int key)
{
  char c;
  int len = -1;
  char hello[512];

  char n1qy[] = "Enter your ~el8 nickname> ";
  send (gchat[key].fd, n1qy, strlen (n1qy), 0x00);
  while (1)
    {
      len += 1;
      recv (gchat[key].fd, &c, 1, 0x00);
      if (c == '\r' || c == '\n')
        break;
      if (len == NIQS1ZE)
        break;                  /* haqrz no */
      gchat[key].niq[len] = c;
    }

  if (len == 0 || len < 0)
    {
      send (gchat[key].fd, "Illegal nick\r\n", 15, 0x00);
      return -1;
    }

  for (len = 0; len < MAXCH4TZ; len++)
    {
      if (len == key)
        continue;
      if (!strncasecmp
          (gchat[len].niq, gchat[key].niq, strlen (gchat[key].niq)))
        {
          send (gchat[key].fd, "Illegal nick, already in use\r\n", 30, 0x00);
          return -1;
        }
    }

  for (len = 0; len < NIQS1ZE; len++)
    if (gchat[key].niq[len] == '=')
      {
        send (gchat[key].fd, "Illegal char in nick, gbye\r\n", 28, 0x00);
        return -1;
      }

  memset (hello, 0, sizeof (hello));
  snprintf (hello, (sizeof (hello) - 1), "*** HEY %s!!!!!!!!!! ***\r\n",
            gchat[key].niq);
  send (gchat[key].fd, hello, strlen (hello), 0x00);

  return 1;
}

int
g00dbye (int key, struct sockaddr_in *sin)
{
  int x;
  char gbYEmsg[512];

  memset (gbYEmsg, 0, sizeof (gbYEmsg));
  snprintf (gbYEmsg, sizeof (gbYEmsg) - 1,
            "*** [%s] has left the building ***\r\n", gchat[key].niq);

  for (x = 0; x < MAXCH4TZ; x++)
    {
      if (x == key)
        {
          write (gchat[key].fd, "*** CYA !!!!!!!!!!!!!!! ***\r\n", 29);
          continue;
        }
      else if (gchat[x].fd != -1)
        {
          send (gchat[x].fd, gbYEmsg, strlen (gbYEmsg), 0x00);
        }
    }
  shutdown (gchat[key].fd, 2);
  close (gchat[key].fd);
  gchat[key].fd = -1;
  bzero (&gchat[key].niq, sizeof (gchat[key].niq));
  return 1;
}

int
w4ll (int key, char *buff)
{
  char urniq[] = "=", format[MAXINPUT + NIQS1ZE + 1];
  int x;

  for (x = 0; x < MAXCH4TZ; x++)
    {
      if (x == key)             /* ur s3lf */
        {
          bzero (&format, sizeof (format));
          snprintf (format, (sizeof (format) - 1), "<%s> %s\r\n", urniq,
                    buff);
          send (gchat[key].fd, format, strlen (format), 0x00);
          continue;
        }
      if (gchat[x].fd != -1)
        {
          bzero (&format, sizeof (format));
          snprintf (format, (sizeof (format) - 1), "<%s> %s\r\n",
                    gchat[key].niq, buff);
          send (gchat[x].fd, format, strlen (format), 0x00);
        }
    }
  return 1;
}

/* c0mm4nd */

int
do_cmd_msg (int key, struct sockaddr *sin, char *msg)
{
  return 1;
}

int
do_cmd_who (int key, char *buff)
{
  int x;
  char hold[255];

  if (buff == '\0')
    {
      send (gchat[key].fd, "BCMD:2\r\n", 8, 0x00);
      for (x = 0; x < MAXCH4TZ; x++)
        {
          if (gchat[x].fd == -1)
            continue;
          bzero (&hold, sizeof (hold));
          snprintf (hold, sizeof (hold) - 1, "%s@%s\r\n", gchat[x].niq,
                    inet_ntoa (gchat[x].sin.sin_addr));
          send (gchat[key].fd, hold, strlen (hold), 0x00);
        }
      send (gchat[key].fd, "ECMD:2\r\n", 8, 0x00);
    }

  return 1;
}

int
s3rv_U (int key, int fd, struct sockaddr_in *sin)
{
  char buff[MAXINPUT];
  int n;
  if (g3tn1q (key) < 0)
    {
      g00dbye (key, sin);
    }
  gr33t (key, sin);
  while (31337)
    {
      bzero (&buff, sizeof (buff));
      n = recv (gchat[key].fd, buff, (sizeof (buff) - 1), 0x00);
      if (n > MAXINPUT)
        {
          printf
            ("WTF , N IZ %i and MAXINPUT IZ %i, REINSTALL Y0R OS ITS FUKT!!!!!!
\n",
             n, MAXINPUT);
        }
      if (buff[0] == '/')
        {                       /* command shit */
          if (!strncasecmp (&buff[1], "quit", 4))
            {
              g00dbye (key, sin);
              return 1;
            }
          if (!strncasecmp (&buff[1], "who", 3))
            {
              do_cmd_who (key, '\0');
              continue;
            }

          if (!strncasecmp (&buff[1], "msg", 3))
            {
//do_cmd_msg(key, sin, buff);
              continue;
            }
          continue;
        }
      else
        {
          w4ll (key, buff);
        }
    }
  g00dbye (key, sin);
  return 1;
}

int
main (int argc, char *argv[])
{
  int sfd, nfd, sfromlen, key;
  struct sockaddr_in sin, sfrom;

  bzero (&sin, sizeof (sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = INADDR_ANY;

  come_g1t_y0rz ();
  sfd = socket (AF_INET, SOCK_STREAM, 0);
  if (sfd < 0)
    {
      perror ("socket");
      exit (-1);
    }

  nfd = -1;
  while (nfd == -1)
    {
      DPORT += 1;
      sin.sin_port = htons (DPORT);
      nfd = bind (sfd, (struct sockaddr *) &sin, sizeof (sin));
    }
  nfd = 0;
  printf ("p0rt t0 c0nn3ct t0 = %i\n", DPORT);

  if (listen (sfd, MAXCH4TZ) < 0)
    {
      perror ("listen");
      exit (-1);
    }

  sfromlen = sizeof (struct sockaddr_in);
  while (31337)
    {
      if ((nfd = accept (sfd, &sfrom, &sfromlen)) < 0)
        {
          perror ("acc3pt");
          continue;
        }
      printf ("c0nn3cti0n fr0m %s\n", inet_ntoa (sfrom.sin_addr));
      if (ACCEPT_NO_CONNEX)
        {
          write (nfd, NOCONNEX4U, strlen (NOCONNEX4U));
          close (nfd);
          continue;
        }
      for (key = 0; key < MAXCH4TZ; key++)
        if (gchat[key].fd == -1)
          break;
      if (key == MAXCH4TZ)
        {
          printf ("p0ssible pr0blem\n");
          continue;
        }
      else
        {
          gchat[key].fd = nfd;
          gchat[key].sin = sfrom;
        }

      switch (fork ())
        {
        case 0:
          s3rv_U (key, nfd, &sfrom);
          //exit (31337);
          break;
        case -1:
/* p0ssible f0rkb0mb att4x, we sh0uld ex1t */
          puts ("W3'R3 B3ING ATT4CKT!!!!!!!!!!!!!!!!!!!!!!!!!");
          exit (-1);
          break;
        default:
/* i think i hafto close nfd!?!? */
//close(nfd);
          break;
        }
    }

  return 31337;
}


syntax highlighted by Code2HTML, v. 0.9.1