/*
 * adduser.c
 * (C) Christian Magnusson 2000, Email: d1mag@dtek.chalmers.se
 * This program is copyright, and covered by the Gnu Public License.
 * The Q bot.
 */

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "settings.h"
#include "bottypes.h"
#include "globals.h"
#include "dl.h"

void m_adduser(char *a);

int _init_adduser(void *a, void *b)
{
  dl_command_entry_t *dl = a;
  if(!a) return 0;

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s ADDUSER <#channel> <nickname>\n"\
	   "Add a user to a channel.\nYou should use CHANLEV to view/set the flags, butthis command adds a user with default flags +aot\n"\
	   "/msg %s ADDUSER #channel nick    Add AUTHED user.\n"\
	   "/msg %s ADDUSER #channel #nick   Add user (if not authed)\n%s",
	   BOT_NAME, BOT_NAME, BOT_NAME, NICK_MESSAGE);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN, "Add a user to a channel.");
  dl->level = 1;
#if 0
  dl->func = m_adduser;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_adduser");
#endif
  return 1;
}

int _remove_adduser(char *a, char *b)
{
  remove_command("adduser");
  return 1;
}

void m_adduser(char *a)
{
  int i;
  char channel[CHANNELLEN+1],victim[NICKLEN+2];
  unsigned int newlevel,slevel,vlevel;
  struct dbaseuser *d;
  struct chanpriv *p,*q;
  struct chanopts *c;
  int oper;

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s adduser [%s]\n", db->nick, a);
  } else
    return;

  i = 0;
  strncpy(channel,getword(a,&i),CHANNELLEN);
  channel[CHANNELLEN] = '\0';
  if(!strlen(channel)) {
    say_sender("You forgot the channel parameter");
    say_sender_( "/msg %s ADDUSER #channel nick", BOT_NAME);
    say_sender_( "/msg %s ADDUSER #channel #nick", BOT_NAME);
    return;
  }

  if(channel[0]!='#') {
    say_sender( "Couldn't find that channel (must begin with '#').");
    return;
  }
  c = findchannel(channel);
  if(!c) {
    say_sender("Couldn't find that channel");
    return;
  }

  oper = is_oper();

  slevel = userlevelonchan_d(sender_au->dbuser, channel);
  strncpy(victim, getword(a,&i), NICKLEN+1);
  victim[NICKLEN+1] = '\0';

  if(!strlen(victim)) {
    say_sender("You forgot the nickname parameter");
    return;
  }
  /*
    '#' will look in the global database
    ' ' will look in active userlist.
  */
  if(victim[0]!='#') {
    d = finduserfromnick(victim);
  } else {
    d = finduserfromname(victim+1);
  }

  if(!d) {
    say_sender_("Can't find that nickname. If user is known by %s, but isn't logged on, then", BOT_NAME);
    say_sender_("Try: /msg %s ADDUSER #chan #nickname", BOT_NAME);
    return;
  }
  vlevel = userlevelonchan_d(d, channel);
  newlevel = vlevel;
  updatemodes(&newlevel, "+aot");
  newlevel = (newlevel & CHAN_ALL);

  if(slevel==0) {
    if(!oper || (sender_au->auth<950)) {
      /*
	Check to see if they have a high enough level to dump userlist
      */
      say_sender( "You are not known on that channel and can't change the channelflags!");
      return;
    }
  }

  if(!oper && (sender_au->auth<950)) {
    if((!(slevel & CHAN_MASTER)) && (!(slevel & CHAN_OWNER))) {
      say_sender("You have to be master to change someone's flags!");
      return;
    }
    if((!(vlevel & CHAN_MASTER)) && (newlevel & CHAN_MASTER)) {
      /* Someone's being made a master. */
      if (!(slevel & CHAN_OWNER)) {
	say_sender("You have to be owner to change a master's flags!");
	return;
      }
    }
    if((vlevel & CHAN_OWNER) || (newlevel & CHAN_OWNER)) {
      if(!(slevel & CHAN_OWNER)) {
	say_sender("You have to be owner to change an owners flags!");
	return;
      }
    }
  }

  c = findchannel(channel);
  if(c && c->suspended && !is_oper()) { say_sender("Channel is suspended"); return; }

  /*
    OK, the security stuff is out of the road, time to just make the
    damn change.
    Look at a users channel list and try to find victim on #channel
  */
  if(!vlevel) {
    /*
      couldn't find the channel for that user.
      Add it first in the linked list.
    */
    p = (struct chanpriv *)Malloc(sizeof(struct chanpriv));
    strncpy(p->channel,channel,CHANNELLEN);
    p->channel[CHANNELLEN] = '\0';
    p->chanlevel = newlevel;
    p->next = d->privs;
    d->privs = p;
  } else {
    /*
      User is known in that channel
    */
    q = NULL;
    p = d->privs;
    while(p) {
      if(!strcasecmp(channel,p->channel)) break;
      q = p;
      p = p->next;
    }
    if (newlevel==0) {
      /*
	all modes on that channel are removed for that user now.
      */
      if(q==NULL) {
	d->privs = p->next;
      } else {
	q->next = p->next;
      }
      Free(p);
      if(is_empty_channel(channel)) {
	delchannel(channel);
      }
    } else {
      p->chanlevel = newlevel;
    }
  }
  say_sender("Done.");
  return;
}
