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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s REJOIN #channel\nRejoin a channel. Could only be used for a registered channel, and could be used if %s have lost op due to a netsplit.\n", BOT_NAME, BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Rejoin a channel.");
  dl->level = 950;
#if 0
  dl->func = m_rejoin;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_rejoin");
#endif
  return 1;
}

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

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

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

  /*
    couldn't find the channel for that user.
    Add it first in the linked list.
  */
  c = findchannel(channel);
  if(!c) {
    say_sender( "Couldn't find that channel in channel database.");
    return;
  }
  if(c->suspended) { say_sender("Channel is suspended"); return; }

  /* found the channel */
  partchannel(c);
  joinchannel(c);
  say_sender("Done.");
  return;
}
