Newer
Older
tuved / tuve.c
@reddawg reddawg on 19 Oct 2007 6 KB Sync
/*
  (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 "tuved.h"

int podzGetData(myConnections_t *userConnection) {

  writeLog(2,"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);

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

  if (userConnection->recLen == 1) {
    writeLog(2,"[%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 *chan = 0x0;
  char *nick = 0x0;
  char *cmd  = 0x0;
  char *msg  = 0x0;
  char *data = 0x0; 
  char *tok_last = 0x0;
  char  output[1024];
  char  buffer[1024];

  memset(&output,0x0,1024);
  memset(&buffer,0x0,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(2,"DATA[%s:%s]\n",cmd,data);
    tok_last = 0x0;
    }
  else {
    writeLog(2,"DATA[%s]\n",cmd);
    }

  if (!strcasecmp(cmd,"ident")) {
    writeLog(2,"(ident)\n");
    sprintf(userConnection->userInfo.username,data); //strtok_r(data,":",&tok_last));
    userConnection->userInfo.uid = time(NULL); //atoi(strtok_r(NULL,"\n",&tok_last));
    userConnection->userInfo.ident = 0x1;
    }
  else if (!strcasecmp(cmd,"join")) {
    tuveChanJoin(data,userConnection);
    }
  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(2,"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(2,"(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(2,"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(1,"Podz Daemon Shutting Down\n");
    fflush(logFile);
    exit(0);
    }
  else if (strcasecmp(cmd,"PONG!") == 0x0) {
    writeLog(2,"(PONG! :NULL)\n");
    userConnection->userInfo.pfailed = 0x0;
    }
  else if (strcasecmp(cmd,"chanmsg") == 0x0) {
    msg = strtok_r(data,":",&tok_last);
    sprintf(output,"CHANMSG:%s:%s:%s\n",userConnection->userInfo.username,msg,strtok_r(NULL,"\n",&tok_last));
    tuveSendAllInChan(msg,userConnection,output);
    }
  else if (strcasecmp(cmd,"topic") == 0x0) {
    msg = strtok_r(data,":",&tok_last);
    tuveSetTopic(userConnection,msg,strtok_r(NULL,"\n",&tok_last));
    }
  else if (strcasecmp(cmd,"NICK") == 0x0) {
    sprintf(output,"NICK:%s:%s\n",userConnection->userInfo.username,data);
    sprintf(userConnection->userInfo.username,data);
    tuveSendAllInUsersChans(userConnection,output);
    }
  else if (strcasecmp(cmd,"KICK") == 0x0) {
    tok_last = 0x0;
    chan = strtok_r(data,":",&tok_last);
    nick = strtok_r(NULL,":",&tok_last);
    msg  = strtok_r(NULL,"\n",&tok_last);
    sprintf(output,"CHANMSG:TUveD:%s:%s Has Been Kick By %s, Reason: %s\n",chan,nick,userConnection->userInfo.username,msg);
    tuveSendAllInChan(chan,0x0,output);
    }
  else if (strcasecmp(cmd,"GETPOS") == 0x0) {
    sprintf(output,"GETPOS\n");
    if (data != 0x0)
      tuveSendAllInChan(data,userConnection,output);
    }
  else if (strcasecmp(cmd,"CURPOS") == 0x0) {
    tok_last = 0x0;
    chan = strtok_r(data,":",&tok_last);
    msg = strtok_r(NULL,":",&tok_last);
    nick = strtok_r(NULL,"\n",&tok_last);
    if ((nick != 0x0) && (msg != 0x0)) {
      sprintf(output,"CURPOS:%s:%s:%s\n",chan,msg,nick);
      tuveSendAllInChan(chan,userConnection,output);
      }
    }
  else {
    send(userConnection->fd,"invalid command\n",sizeof("invalid command\n"),MSG_NOSIGNAL);
    writeLog(1,"Invalid CMD: [%s]\n",cmd);
    }

  return(0x0);
  }