/*
 * chanflags.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_chanflags(char *a);

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,

	   "/msg %s CHANFLAGS <channel> [flags]\nChange/view the flags on a channel. They are specified in the normal way - e.g. +f-b and so 
on.\nThe flags are as follows:\n"\
	   "/msg %s CHANFLAGS #chan         View the flags on the channel.\n"\
	   "/msg %s CHANFLAGS #chan +-flag  Change the flags on the channel.\n"\
	   "+b - Bitch\n"\
	   "     Prevents anyone who hasn't got operator privs on the channel from getting OP.\n"\
	   "+c - Channel-limit\n"\
	   "     Activate channel-limit protection. - Set parameters with AUTOLIMIT command.\n"\
	   "+f - Force topic\n"\
	   "     Prevent people change the topic with /topic command - SETTOPIC is needed\n"\
	   "+k - Force key\n"\
	   "     Set a key permanent on the channel. - Set the key with the KEY command.\n"\
	   "+l - Force limit\n"\
	   "     Set a limit permanent on the channel. - Set the limit with the LIMIT command.\n"\
	   "+p - Protect opers\n"\
	   "     Prevent people who have with OP privs on the channel from being deopped.\n"\
	   "+t - Topic save\n"\
	   "     Allow Q to set the topic if Q is restarted.\n"\
	   "+w - Welcome message\n"\
	   "     Send a welcome message (set by WELCOME) to all joining users.\n"
	   , BOT_NAME, BOT_NAME, BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Change/view the flags on a channel");
  dl->level = 1;
#if 0
  dl->func = m_chanflags;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_chanflags");
#endif
  return 1;
}

MODULE_PUBLIC int _remove_chanflags(char *a, char *b)
{
  remove_command("chanflags");
  return 1;
}

MODULE_PUBLIC void m_chanflags(char *a)
{
  int i;
  unsigned int flags;
  char channel[CHANNELLEN+1];
  char modestr[64+1];
  char buffer[512];
  struct chanopts *c;
  int oper;

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s chanflags [%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!");
    return;
  }
  c=findchannel(channel);
  if(!c) {
    say_sender("Can't find that channel!");
    return;
  }
  if(c && c->suspended && !is_oper()) { say_sender("Channel is suspended"); return; }

  strncpy(modestr,getword(a,&i),64);
  modestr[64] = '\0';
  if(!strlen(modestr)) {
    sprintf(buffer,"Current channel flags are: ");
    createmodes(c->flags,buffer+strlen(buffer));
    say_sender(buffer);
    return;
  }

  oper = is_oper();
  
  if((oper && (sender_au->auth>=950)) ||
     (userlevelonchan_d(sender_au->dbuser, channel) & (CHAN_MASTER | CHAN_OWNER))) {
    flags = 0;
    updatemodes(&flags, modestr);
    if(flags & CO_MUST_BE_WRONG) {
      say_sender("Invalid flag specified!");
      return;
    }
    if(flags & CO_FORCE_TOPIC) {
      settopic(c, c->topic);
    }
    if(flags & CO_KEY) {
      c->flags |= CO_KEY;
      addkey_ch(c, c->key, 1);
    }
    if(flags & CO_LIMIT) {
      char tmp[LIMITLEN+1];
      int nr;
      c->flags |= CO_LIMIT;
      if(!c->limit[0])
	strcpy(c->limit, c->cur_limit);
      if(sscanf(c->limit, "%d", &nr) != 1)
	nr = 50;  // just a dummy default value
      sprintf(tmp, "%d", nr);
      addlimit_ch(c, tmp, 1);
      //addlimit_ch(c, c->limit, 1);
    }
    if(flags & CO_CHANLIMIT) {
      c->flags |= CO_CHANLIMIT;
      addchanlimit_ch(c, c->limit_change, 60);
    }

    flags = CO_ALL;
    updatemodes(&flags, modestr);
    if(!(flags & CO_KEY)) {
      c->flags &= (~0-CO_KEY);
      delkey_ch(c, 0);
    }
    if(!(flags & CO_LIMIT)) {
      c->flags &= (~0-CO_LIMIT);
      dellimit_ch(c, 0);
    }
    if(!(flags & CO_CHANLIMIT)) {
      c->flags &= (~0-CO_CHANLIMIT);
      delchanlimit_ch(c);
    }

    flags = c->flags;
#ifdef FORCE_AUTOJOIN
    flags |= CO_AUTO_JOIN;
#endif
    updatemodes(&flags,modestr);

    c->flags = flags;
    say_sender("Done.");
  } else {
    say_sender("You have to be owner or master to change the CHANFLAGS!");
  }
  return;
}
