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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s OP <channel>\nGives you ops on a channel which the bot is sitting on. The bot *must* be on the channel, and you must have a high enough level on that channel.\n"
	   , BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Gives you ops on a channel where the bot is.");
  dl->level = 1;
#if 0
  dl->func = m_op;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_op");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_op(char *a)
{
  int i;
  struct chanmod *cm;
  struct chanopts *c;
  char channel[CHANNELLEN+1];

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s op [%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 to type the channel");
    say_sender_( "/msg %s OP #channel", BOT_NAME);
    return;
  }

  i = userlevelonchan_d(sender_au->dbuser, channel);
  if(i & (CHAN_OPER | CHAN_MASTER | CHAN_OWNER)) { 
    c = findchannel(channel);
    if(c->suspended) { say_sender("Channel is suspended"); return; }
#ifdef AUTH_HISTORY
    if(c) {
      add_auth_history(c, sender_au->userid, sender_au->nick);
    }
#endif
    if(c) {
      cm = sender_au->chanmodes;
      while(cm) {
	if(!strcasecmp(cm->channel, c->channel)) break;
	cm = cm->next;
      }
      if(!cm) {
	/* ignore if user isn't in the channel */
	return;
      }
      putserver("%s M %s +o %s\n\r", yxx, c->channel, sender_au->yxx);
      c_op_set_modes(1, c->channel, sender_au);
    }
    say_sender("Done.");
  } else {
    say_sender("You don't have +o flag on that channel.");
  }
  return;
}
