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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "Show/Set the level of debug messages.\n\
/msg %s DEBUGLEVEL          Show debug level.\n\
/msg %s DEBUGLEVEL [0-10]   Set debug level.\n", BOT_NAME, BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Show/Set the level of debug messages.");
  dl->level = 100;
#if 0
  dl->func = m_debuglevel;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_debuglevel");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_debuglevel(char *a)
{
  int i,l;
  char *str;

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s debuglevel [%s]\n", db->nick, a);
  } else
    return;
  if(!is_oper()) {
    say_sender("You are not an Oper.");
    log("To: %s Is not an Oper\n", db->nick);
    return;
  }
  i=0;
  str = getword(a,&i);
  if(!strlen(str)) {
    say_sender_("Your debuglevel level is set to %d", 
		((sender_au->oper & OPER_DEBUG_MASK) >> OPER_DEBUG_BIT));
    return;
  }
  if(!isdigit(str[0])) {
    say_sender("Argument must be a digit");
    return;
  }
  l = atoi(str);
  if(l >= 10) l = 10;

  sender_au->oper &= ~OPER_DEBUG_MASK;
  sender_au->oper |= ((l << OPER_DEBUG_BIT) & OPER_DEBUG_MASK);

  say_sender_("Your debug level is now: %d",
	     ((sender_au->oper & OPER_DEBUG_MASK) >> OPER_DEBUG_BIT));
  return;
}
