/*
 * invite.c (contributed by Rasmus Have, bigfoot@quakenet.eu.org)
 * (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_invite(char *a);

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s INVITE <channel>\nInvites you to 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,
	   "Invites you to a channel where the bot is.");
  dl->level = 1;
#if 0
  dl->func = m_invite;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_invite");
#endif
  return 1;
}

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

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

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s invite [%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 INVITE #channel", BOT_NAME);
    return;
  }

  c = findchannel(channel);

  /* Make sure the bot is in the channel */
  if(c && !c->suspended && c->bot_joined){
    i = userlevelonchan_d(sender_au->dbuser, channel); 
    /* 
     * Make sure the user has sufficient rights on the channel
     * Only ops, masters and owners have access to this
     */
    /* Perhaps voice ppl should have access too? CHAN_VOICE */
    if(i & (CHAN_OPER | CHAN_MASTER | CHAN_OWNER)) {
      /* Send the invite */
      putserver("%s I %s :%s\n\r", yxx, sender_au->nick, channel);
      say_sender("Done.");
    } else {
      say_sender("You don't have +o flag on that channel.");
    }
  }else{
    say_sender("Im not on that channel.");
  }
  return;
}
