Newer
Older
tuved / channel.c
@reddawg reddawg on 9 Oct 2007 7 KB Tweaked out a few bugs
/*
  (c) 2007 Christopher Olsen
  $Id$
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "tuved.h"

tuveChanList_t *channels = 0x0;

tuveChanList_t *findChan(char *channel);
tuveChanList_t *addChan(char *channel);

int tuveChanJoin(char *channel,myConnections_t *userConnection) {
  char output[256];

  if (tuveAddChan(channel,userConnection) != 0x0)
    return(0x1);

    /* Send JOIN to all users in the channel */
    sprintf(output,"JOIN:%s:%s\n",channel,userConnection->userInfo.username);
    tuveSendAllInChan(channel,userConnection,output);
    
    /* Notify people in the channel that someone has joined */
    sprintf(output,"CHANMSG:TUveD:%s:%s has joined the channel.\n",channel,userConnection->userInfo.username);
    tuveSendAllInChan(channel,userConnection,output);

  return(0x0);
  }

int tuveDelUserChans(myConnections_t *userConnection,char *msg) {
  char output[256];
  tuveUserChans_t *tmpChan  = 0x0;
  tuveUserChans_t *tmpChan2 = 0x0;

  tmpChan = userConnection->userInfo.chans;
  tmpChan2 = tmpChan;

  for (;tmpChan != 0x0;tmpChan = tmpChan->next) {
    sprintf(output,"CHANMSG:TUveD:%s:%s Quit (%s)\n",tmpChan->channel,userConnection->userInfo.username,msg);
    tuveSendAllInChan(tmpChan->channel,userConnection,output);
    sprintf(output,"PART:%s:%s\n",tmpChan->channel,userConnection->userInfo.username);
    tuveSendAllInChan(tmpChan->channel,userConnection,output);
    tuveRemoveFromChanList(tmpChan->channel,userConnection);
    free(tmpChan2);
    tmpChan2 = tmpChan->next;
    }

  if (tmpChan2 != 0x0) {
    free(tmpChan2);
    }

  return(0x0);
  }

int tuveAddChan(char *channel,myConnections_t *userConnection) {
  tuveUserChans_t *tmpChan = 0x0;

  if (userConnection->userInfo.chans == 0x0) {
    userConnection->userInfo.chans = (tuveUserChans_t *)malloc(sizeof(tuveUserChans_t));
    userConnection->userInfo.chans->prev = 0x0;
    userConnection->userInfo.chans->next = 0x0;
    sprintf(userConnection->userInfo.chans->channel,channel);
    }
  else {
    tmpChan = (tuveUserChans_t *)malloc(sizeof(tuveUserChans_t));   
    tmpChan->prev = 0x0;
    sprintf(tmpChan->channel,channel);
    tmpChan->next = userConnection->userInfo.chans;
    userConnection->userInfo.chans->prev = tmpChan;
    userConnection->userInfo.chans = tmpChan;
    }

  tuveAddToChanList(channel,userConnection);

  return(0x0);
  }

int tuveRemoveFromChanList(char *channel,myConnections_t *userConnection) {
  tuveChanList_t *tmpChannel = 0x0;
  tuveUserList_t *tmpUsers   = 0x0;

  tmpChannel = findChan(channel);
  if (tmpChannel == 0x0) {
    writeLog("findChan failed!\n");
    return(0x1);
    }
   
  tmpUsers = tmpChannel->users;

  writeLog("Freeing User From Chan: %s\n",channel);

  for (;tmpUsers != 0x0;tmpUsers = tmpUsers->next) {
    if (!strcasecmp(tmpUsers->user->userInfo.username,userConnection->userInfo.username)) {
      printf("Found and removed user: %s\n",tmpUsers->user->userInfo.username);

      if (tmpUsers->prev != 0x0)
        tmpUsers->prev->next = tmpUsers->next;
      else 
         tmpChannel->users = tmpUsers->next;

      if (tmpUsers->next != 0x0)
        tmpUsers->next->prev = tmpUsers->prev;

      free(tmpUsers);
      printf("User Freed\n");
      return(0x0);
      }
    }
  return(0x1);
  }

int tuveAddToChanList(char *channel,myConnections_t *userConnection) {
  tuveChanList_t *tmpChannel = 0x0;
  tuveUserList_t *tmpUsers   = 0x0;

  char output[256];
  
  tmpChannel = findChan(channel);
  if (tmpChannel == 0x0) {
    writeLog("Channel %s Does Not Exist Adding To List\n",channel);
    tmpChannel = addChan(channel);
    } 

  for (tmpUsers = tmpChannel->users;tmpUsers != 0x0;tmpUsers = tmpUsers->next) {
    if (!strcasecmp(tmpUsers->user->userInfo.username,userConnection->userInfo.username)) {
      writeLog("User: %s already in channel %s\n",userConnection->userInfo.username,channel);
      return(0x0);
      }
    }

  tmpUsers = (tuveUserList_t *)malloc(sizeof(tuveUserList_t));

  tmpUsers->user = userConnection;
  tmpUsers->prev = 0x0;

  if (tmpChannel->users == 0x0) {
    tmpUsers->next = 0x0;
    tmpChannel->users = tmpUsers;
    }
  else {
    tmpUsers->next = tmpChannel->users;
    tmpChannel->users->prev = tmpUsers;
    tmpChannel->users = tmpUsers;
    }

  memset(output,0x0,256);

  /* Send the new user the channel list */
  for (tmpUsers = tmpChannel->users->next;tmpUsers != 0x0;tmpUsers = tmpUsers->next) {
    if (strlen(output) > 0)
      sprintf(output,"%s:%s",output,tmpUsers->user->userInfo.username);
    else
      sprintf(output,"JOIN:%s:%s",tmpChannel->channel,tmpUsers->user->userInfo.username);
    }

  if (strlen(output) > 0) {
    sprintf(output,"%s\n",output);
    send(userConnection->fd,output,strlen(output),MSG_NOSIGNAL);
    }

  sprintf(output,"TOPIC:%s:%s\n",tmpChannel->channel,tmpChannel->topic);
  send(userConnection->fd,output,strlen(output),MSG_NOSIGNAL);
  
  writeLog("Added user: %s to Channel: %s\n",userConnection->userInfo.username,channel);

  return(0x0);
  }

tuveChanList_t *findChan(char *channel) {
  tuveChanList_t *tmpChannel = 0x0;

  for (tmpChannel = channels;tmpChannel != 0x0;tmpChannel = tmpChannel->next) {

    if (!strcasecmp(tmpChannel->channel,channel))
      return(tmpChannel);
      
    }

  return(0x0);
  }

tuveChanList_t *addChan(char *channel) {
  tuveChanList_t *tmpChannel = 0x0;

  if (channels == 0x0) {
    channels = (tuveChanList_t *)malloc(sizeof(tuveChanList_t));
    channels->next = 0x0;
    channels->prev = 0x0;
    channels->users = 0x0;
    sprintf(channels->channel,channel);
    sprintf(channels->topic,"No Topic Set");
    writeLog("ADDED CHAN1\n");
    tmpChannel = channels;
    }
  else {
    tmpChannel = (tuveChanList_t *)malloc(sizeof(tuveChanList_t));
    sprintf(tmpChannel->channel,channel);
    sprintf(tmpChannel->topic,"General Information About The TUve Network\n");
    tmpChannel->next = channels;
    tmpChannel->prev = 0x0;
    tmpChannel->users = 0x0;
    channels->prev = tmpChannel;
    channels = tmpChannel;  
    writeLog("ADDED CHAN2\n");
    }
  return(tmpChannel);
  }

int tuveSendAllInChan(char *channel,myConnections_t *userConnection,char *output) {
  tuveChanList_t *tmpChannel = 0x0;
  tuveUserList_t *tmpUsers   = 0x0;

  tmpChannel = findChan(channel);

  if (tmpChannel == 0x0) {
    writeLog("findChan Failed!\n");
    return(0x1);
    }

  tmpUsers = tmpChannel->users;

  for (;tmpUsers != 0x0;tmpUsers = tmpUsers->next) {
    if (userConnection == 0x0)
      send(tmpUsers->user->fd,output,strlen(output),MSG_NOSIGNAL);
    else if (strcasecmp(tmpUsers->user->userInfo.username,userConnection->userInfo.username)) {
      send(tmpUsers->user->fd,output,strlen(output),MSG_NOSIGNAL);
      }
    }
  
  return(0x0); 
  }

int tuveSetTopic(myConnections_t *userConnection,char *chan,char *topic) {
  char output[1024];
  tuveChanList_t *tmpChannel = 0x0;

  tmpChannel = findChan(chan);

  if (tmpChannel == 0x0) {
    writeLog("findChan Failed: SetTopic\n");
    return(0x1);
    }

  sprintf(tmpChannel->topic,topic);
  sprintf(output,"TOPIC:%s:%s\n",chan,tmpChannel->topic);
  tuveSendAllInChan(chan,0x0,output);
  sprintf(output,"CHANMSG:TUveD:%s:%s Has Set The Topic\n",chan,userConnection->userInfo.username);
  tuveSendAllInChan(chan,userConnection,output);

  return(0x0);
  }