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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s SUSPEND #channel <reason>\nSuspend a channel with a given reason. %s will leave the channel, but userlist and all other info will be intact during the suspension.\n"
	   , BOT_NAME, BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Suspend a channel.");
  dl->level = 990;
#if 0
  dl->func = m_suspend;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_suspend");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_suspend(char *a)
{
  int i, start;
  char reason_test[11];
  char reason[SUSPENDREASONLEN+1];
  char channel[CHANNELLEN+1];
  struct chanopts *c;
  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s suspend [%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 channelname");
    say_sender_( "/msg %s SUSPEND #channel", BOT_NAME);
    return;
  }
  c = findchannel(channel);
  if(!c) {
    say_sender( "Couldn't find that channel.");
    return;
  }
  if(c->suspended) {
    say_sender( "Channel is already suspended.");
    return;
  }

  start = i+1;
  strncpy(reason_test, getword(a,&i), 10);
  reason_test[10] = '\0';
  if(!strlen(reason_test)) {
    say_sender("No reason was given.");
    return;
  }
  
  strncpy(reason, &a[start], SUSPENDREASONLEN);
  reason[SUSPENDREASONLEN] = '\0';
  c->suspend_reason = Malloc(strlen(reason)+1);
  strcpy(c->suspend_reason, reason);
  c->suspend_who = db->userid;
  c->suspend_time = time(NULL);
  c->suspended = 1;

  /* Clear all channel modes to avoid loop setting limit, key etc */
  c->flags = CO_AUTO_JOIN;

  partchannel(c);
  say_sender("Done.");
  return;
}
