Newer
Older
tuved / podz.c
@reddawg reddawg on 3 Oct 2007 5 KB TUveD
/*
  (c) 2007 Christopher Olsen
  $Id$
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "podzd.h"

int podzGetData(myConnections_t *userConnection) {

  writeLog("Getting Data\n");

  memset(&userConnection->data,0x0,1024);
  userConnection->recLen = sReadSocket(userConnection->fd,&userConnection->data,1024);
  if (userConnection->recLen == 0)
    return(-1);

  userConnection->userInfo.pong = time(NULL);

  #ifdef DEBUG
  writeLog("Got: [%i][%s]\n",userConnection->recLen,userConnection->data);
  #endif

  if (userConnection->recLen == 1) {
    writeLog("[%i][%c]\n",userConnection->data[0],userConnection->data[0]);
    while(1);
    }

  podzProcessData(userConnection);

  return(0x0);
  }


int podzSendAll(char *output,int fd) {
  myConnections_t *tmpConnection = 0x0;

  for (tmpConnection = connections;tmpConnection != 0x0;tmpConnection = tmpConnection->next) {
    if (tmpConnection->fd != fd)
      send(tmpConnection->fd,output,strlen(output),MSG_NOSIGNAL);
    }

  return(0x0);
  }

int podzProcessData(myConnections_t *userConnection) {
  myConnections_t *tmpConnection = 0x0;
  char *cmd  = 0x0;
  char *msg  = 0x0;
  char *data = 0x0; 
  char *tok_last = 0x0;
  char  output[1024];
  char  buffer[1024];

  cmd = userConnection->data;
  if (cmd[userConnection->recLen - 1] == '\n')
    cmd[userConnection->recLen - 1] = '\0';

  if (cmd[userConnection->recLen - 2] == 13)
    cmd[userConnection->recLen - 2] = '\0';

  if (strstr(userConnection->data," ")) {
    cmd  = strtok_r(userConnection->data," ",&tok_last);
    data = strtok_r(NULL,"\n",&tok_last); 
    writeLog("DATA[%s:%s]\n",cmd,data);
    tok_last = 0x0;
    }
  else {
    writeLog("DATA[%s]\n",cmd);
    }

  if (!strcasecmp(cmd,"ident")) {
    writeLog("(ident)\n");
    if (strstr(data,":")) {
      sprintf(userConnection->userInfo.username,strtok_r(data,":",&tok_last));
      userConnection->userInfo.uid = atoi(strtok_r(NULL,"\n",&tok_last));
      sprintf(output,"ison:%i:1",userConnection->userInfo.uid);
      podzSendAll(output,userConnection->fd);
      userConnection->userInfo.ident = 0x1;
      tok_last = 0x0;
      }
    else {
      send(userConnection->fd,"invalid ident",sizeof("invalid ident"),0);
      }
    }
  else if (strcasecmp(cmd,"<policy-file-request/>") == 0x0) {
    sprintf(output,"<?xml version=\"1.0\"?>\n<cross-domain-policy>\n<allow-access-from domain=\"*\" to-ports=\"*\" />\n</cross-domain-policy>\n");
    send(userConnection->fd,output,strlen(output) + 1,MSG_NOSIGNAL);
    writeLog("Send CROSSDOMAIN\n");
    }
  else if (userConnection->userInfo.ident != 0x1) {
    send(userConnection->fd,"Please Auth",sizeof("Please Auth"),0);
    }
  else if (strcasecmp(cmd,"whoson") == 0x0) {
    writeLog("(whoson)\n");
    for (tmpConnection = connections->next;tmpConnection != 0x0;tmpConnection = tmpConnection->next) {
      sprintf(output,"User: [%s:%i]\n",tmpConnection->userInfo.username,tmpConnection->userInfo.uid);
      send(userConnection->fd,output,strlen(output),MSG_NOSIGNAL);
      }
    }
  else if (strcasecmp(cmd,"mail") == 0x0) {
    for (tmpConnection = connections->next;tmpConnection != 0x0;tmpConnection = tmpConnection->next) {
      if (tmpConnection->userInfo.uid == atoi(data)) {
        writeLog("Found User\n");
        break;
        }
      }

    if (tmpConnection == 0x0)
      send(userConnection->fd,"User Not On",strlen("User Not On"),MSG_NOSIGNAL);
    else
      send(tmpConnection->fd,"gotmail",strlen("gotmail"),MSG_NOSIGNAL);
    }
  else if (strcasecmp(cmd,"logoff") == 0x0) {
    for (tmpConnection = connections->next;tmpConnection != 0x0;tmpConnection = tmpConnection->next) {
      if (tmpConnection->userInfo.uid == atoi(data))
        break;
      }
    if (tmpConnection == 0x0)
      send(userConnection->fd,"User Not On",strlen("User Not On"),MSG_NOSIGNAL);
    else
      send(tmpConnection->fd,"signoff",strlen("signoff"),MSG_NOSIGNAL);
    }
  else if (strcasecmp(cmd,"ison") == 0x0) {
    sprintf(buffer,data);
    sprintf(output,"ison");
    for (msg = strtok_r(buffer,",",&tok_last);msg;msg = strtok_r(NULL, ",", &tok_last)) {
      for (tmpConnection = connections->next;tmpConnection != 0x0;tmpConnection = tmpConnection->next) {
        if (tmpConnection->userInfo.uid == atoi(msg))
          break;
        }
      if (tmpConnection == 0x0)
        sprintf(output,"%s:%i:0",output,atoi(msg));
      else
        sprintf(output,"%s:%i:1",output,atoi(msg));
      }
    send(userConnection->fd,output,strlen(output),MSG_NOSIGNAL);
    }
  else if (strcasecmp(cmd,"privmsg") == 0x0) {
    for (tmpConnection = connections->next;tmpConnection != 0x0;tmpConnection = tmpConnection->next) {
      if (tmpConnection->userInfo.uid == atoi(strtok(data," ")))
        break;
      }
    if (tmpConnection == 0x0) {
      sprintf(output,"privmsg:System Notification:%i:User is Offline",atoi(data));
      send(userConnection->fd,output,strlen(output),MSG_NOSIGNAL);
      }
    else {
      msg = data + strlen(data) + 1;//strtok(NULL,"\n");
      sprintf(output,"privmsg:%s:%i:%s",userConnection->userInfo.username,userConnection->userInfo.uid,msg);
      send(tmpConnection->fd,output,strlen(output),MSG_NOSIGNAL);
      }
    }
  else if (strcasecmp(cmd,"!die") == 0x0) {
    writeLog("Podz Daemon Shutting Down\n");
    fflush(logFile);
    exit(0);
    }
  else if (strcasecmp(cmd,"PONG!") == 0x0) {
    writeLog("(PONG! :NULL)\n");
    userConnection->userInfo.pfailed = 0x0;
    }
  else if (strcasecmp(cmd,"chanmsg") == 0x0) {
    sprintf(output,"chanmsg:%s\n",data);
    podzSendAll(output,userConnection->fd);
    }
  else {
    send(userConnection->fd,"invalid command",sizeof("invalid command"),MSG_NOSIGNAL);
    writeLog("Invalid CMD: [%s]\n",cmd);
    }

  return(0x0);
  }