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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s AUTOLIMIT #channel #10\nSet the auto-limit on a channel. Limit will be adjusted automatically to avoid join floods. The limit must be combined with \"/msg %s CHANFLAGS #channel +c\"\n", BOT_NAME, BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Set the auto-limit on a channel.");
  dl->level = 1;
#if 0
  dl->func = m_autolimit;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_autolimit");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_autolimit(char *a)
{
  int i, l;
  char limit[LIMITLEN+1];
  char channel[CHANNELLEN+1];
  unsigned int slevel;
  struct chanopts *c;
  int oper;

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s autolimit [%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 AUTOLIMIT #channel        - View current auto-limit", BOT_NAME);
    say_sender_( "/msg %s AUTOLIMIT #channel #inc   - Set a new auto-limit", BOT_NAME);
    return;
  }

  oper = is_oper();
  slevel = userlevelonchan_d(sender_au->dbuser, channel);

  if(slevel==0) {
    if(!oper || (sender_au->auth<950)) {
      say_sender( "You are not known on this channel and can't use LIMIT!");
      return;
    }
  }

  if(!oper && (sender_au->auth<950)) {
    if ((!(slevel & CHAN_MASTER)) &&
	(!(slevel & CHAN_OWNER))) {
      say_sender("You should have the +m flag on the channel (and be AUTHED) to use this command.");
      return;
    }
  }

  c = findchannel(channel);
  if(!c) {
    say_sender( "Couldn't find channel");
    return;
  }
  if(c->suspended) { say_sender("Channel is suspended"); return; }

  strncpy(limit,getword(a,&i),LIMITLEN);
  limit[LIMITLEN] = '\0';
  if(!strlen(limit)) {
    say_sender_("Current auto-limit is: %d", c->limit_change);
    if(db->globalauth >= 999) {
      say_sender_("%d %d %d %d %d %d %d", chanlimit_delay, c->limit_delay, 
		  c->limit_change, c->limit_last_joined, c->limit_next_check,
		  time(NULL));
    }
    return;
  }

  if((limit[0] != '#') || !isdigit(limit[1])) {
    say_sender("Argument should start with a '#' and a digit. (eg.  #10)");
    return;
  }

  if(sscanf(limit, "#%d", &l) == 1) {
    if(l > 500) l = 500;
    if(l < 2) l = 2;
    say_sender_( "Auto-limit is changed to: %d", l);
    addchanlimit_ch(c, l, 60);
  } else {
    say_sender("Argument should start with a '#' and a digit. (eg.  #10)");
  }
  return;
}
