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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s OPCHAN <channel> <nick>\nThis sets mode +o <nick> on <channel> - i.e. it makes you a chanop.\n", BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN, "Gives you +o on a channel.");
  dl->level = 900;
#if 0
  dl->func = m_opchan;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_opchan");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_opchan(char *a)
{
  char pyxx[YXXLEN+1];
  char nick[NICKLEN+1],channel[CHANNELLEN+1];
  struct activeuser *u;
  int i;

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s opchan [%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(channel,getword(a,&i),CHANNELLEN);
  channel[CHANNELLEN] = '\0';
  if(!strlen(channel)) {
    say_sender("You forgot to type the channel!");
    say_sender_("Type: /msg %s OPCHAN #channel nick", BOT_NAME);
    return;
  }

  strncpy(nick,getword(a,&i),NICKLEN);
  nick[NICKLEN] = '\0';
  if(!strlen(nick)) {
    say_sender("You forgot to type the nickname!");
    say_sender_("Type: /msg %s OPCHAN #channel nick", BOT_NAME);
    return;
  }

  if((u = nick_to_yxx(nick, pyxx))) {
    putserver("%c%c M %s +o %s\n\r", yxx[0], yxx[1], channel, pyxx);
    c_op_set_modes(1, channel, u);
  } else {
    say_sender( "Couldn't find that user.");
    return;
  }
  say_sender( "Done.");
  return;
}
