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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s CHANGELEV <nick> [new auth level]\nChange/view the global auth level of a user - the level which applies to everything. This isn't a very useful feature really, but it may prove helpful at some later date. You can at most give someone an auth level 1 below your own.\n"\
	   "/msg %s CHANGELEV nick       View global auth level (if authed)\n"\
	   "/msg %s CHANGELEV #nick      View global auth level (if not authed)\n"\
	   "/msg %s CHANGELEV nick 100   Change global auth level to 100\n"
	   , BOT_NAME, BOT_NAME, BOT_NAME, BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Change/view someone's global auth level");
  dl->level = 100;
#if 0
  dl->func = m_changelev;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_changelev");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_changelev(char *a)
{
  int i,l;
  char victim[NICKLEN+2];
  char tmp[80];
  char *str;
  struct dbaseuser *v,*s;

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s changelev [%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;
  strncpy(victim,getword(a,&i),NICKLEN+1);
  victim[NICKLEN+1] = '\0';
  if(!strlen(victim)) {
    say_sender("No nickname was given.");
    return;
  }
  if (victim[0]!='#') {
    v=finduserfromnick(victim);
  } else {
    v=finduserfromname(victim+1);
  }

  if(!v) {
    say_sender_("Couldn't find that user. If the user isn't logged on, but is known by %s", BOT_NAME);
    say_sender_("Try: /msg %s CHANGELEV #nickname", BOT_NAME);
    return;
  }

  str = getword(a,&i);
  if(!strlen(str)) {
    sprintf(tmp, "Current level global auth level is : %d", v->globalauth);
    say_sender( tmp);
    return;
  }
  l=atoi(str);
  s = db;

  if (v==NULL) {
    say_sender("Couldn't find that person!");
    return;
  }
  if (s->globalauth <= v->globalauth) {
    wallop("%s (%s) FAILED to change level on %s (%d -> %d)", sender_au->nick, db->nick, v->nick, v->globalauth, l);
    say_sender("You can't change someone with a higher or equal level to your own!");
    return;
  }
  if (s->globalauth <= l) {
    say_sender("You can't give someone a higher or equal level to your own!");
    return;
  }
  
  if(v->globalauth >= 100) {
    if((l < 100) && (db->globalauth < 1000)) {
      wallop("%s (%s) FAILED to change level on %s (%d -> %d)", sender_au->nick, db->nick, v->nick, v->globalauth, l);
      say_sender("It's not possible to set the level below 100 for some reason.");
      log("m_changelev: Tried to set a low level. [%s] [%s]", db->nick, a);
      return;
    }
  }
  wallop("%s (%s) changed level on %s (%d -> %d)", sender_au->nick, db->nick, v->nick, v->globalauth, l);

  v->globalauth = l;
  say_sender("Done.");
}
