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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s SETTOPIC <channel> <topic>\nSets both the current and default topics on the channel to that specified.\n"
	   , BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Changes the default channel topic");
  dl->level = 1;
#if 0
  dl->func = m_settopic;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_settopic");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_settopic(char *a)
{
  int start, i = 0;
  char channel[CHANNELLEN+1];
  char topic_test[11];
  struct chanopts *c;
  int oper;

  strncpy(channel,getword(a,&i),CHANNELLEN);
  channel[CHANNELLEN] = '\0';
  if(!strlen(channel)) {
    say_sender("You forgot the #channel parameter");
    say_sender_("/msg %s SETTOPIC #channel this is my new topic", BOT_NAME);
    return;
  }
  c = findchannel(channel);
  if(!c) {
    say_sender("No such channel registered!");
    return;
  }
  if(c && c->suspended && !is_oper()) { say_sender("Channel is suspended"); return; }

  oper = is_oper();

  if((oper && (sender_au->auth>=950)) ||
     (userlevelonchan_d(sender_au->dbuser, channel) & (CHAN_TOPIC|CHAN_MASTER|CHAN_OWNER))) {
    start = i+1;
    strncpy(topic_test, getword(a,&i), 10);
    topic_test[10] = '\0';

    if(!strlen(topic_test)) {
      say_sender( "Current topic is:");
      say_sender( c->topic);
      say_sender_( "To unset force-topic type:  /msg %s CHANFLAGS #channel -f", BOT_NAME);
      return;
    }

    strncpy(c->topic, &a[start], TOPICLEN);
    c->topic[TOPICLEN] = '\0';
    settopic(c, c->topic);
    if(sender_au->dbuser) {
      log("From: %s settopic [%s]\n", sender_au->dbuser->nick, c->topic);
    }
    say_sender("Done.");
  } else {
    say_sender("You don't have +t flag on that channel.");
  }
  return;
}
