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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s RENCHAN <#oldchan> <#newchan>\nRename a channel from name #oldchan to #newchan\n", BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Rename a channel");
  dl->level = 950;
#if 0
  dl->func = m_renchan;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_renchan");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_renchan(char *a)
{
  int i, len, ok;
  char channel_old[CHANNELLEN+1],channel_new[CHANNELLEN+1];
  struct dbaseuser *d;
  struct chanpriv *p;
  struct chanopts *c;
  ListElem *el;

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s renamechan [%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_old,getword(a,&i),CHANNELLEN);
  channel_old[CHANNELLEN] = '\0';
  if(!strlen(channel_old)) {
    say_sender("You forgot the first channel parameter");
    say_sender_( "/msg %s RENCHAN #oldchan #newchan", BOT_NAME);
    return;
  }
  strncpy(channel_new,getword(a,&i),CHANNELLEN);
  channel_new[CHANNELLEN] = '\0';
  len = strlen(channel_new);
  if(!len) {
    say_sender("You forgot the second channel parameter");
    say_sender_( "/msg %s RENCHAN #oldchan #newchan", BOT_NAME);
    return;
  }

  c = findchannel(channel_old);
  if(!c) {
    say_sender( "Couldn't find that channel in channel database.");
    return;
  }
  if(c && c->suspended) { say_sender("Channel is suspended"); return; }

  ok = 1;
  for(i = 0; i<len; i++) {
    /* make sure channelname is correct */
    if(!isprint((int)(channel_new[i])) ||
       (channel_new[i]==':')) {
      ok = 0; break;
    }
  }
  if((channel_new[0]!='#') || !ok) {
    say_sender("Channel should start with '#' and only contain normal chars.");
    say_sender_( "/msg %s RENCHAN #oldchan #newchan", BOT_NAME);
    return;
  }

  /*
    couldn't find the channel for that user.
    Add it first in the linked list.
  */
  partchannel(c);
  Remove(c->h_elem);
  strncpy(c->channel,channel_new,CHANNELLEN);
  c->channel[CHANNELLEN] = '\0';
  AddElem(channels, c->h_elem);
  joinchannel(c);

  /* change info about Created By Operator... */
  c->created_by = db->userid;

  el = GetHead(dbuser_list);
  while(el) {
    d = (struct dbaseuser *)el->data;
    p = d->privs;
    while(p) {
      if(!strcasecmp(p->channel,channel_old)) {
	strncpy(p->channel, channel_new, CHANNELLEN);
	p->channel[CHANNELLEN] = '\0';
      }
      p = p->next;
    }
    el = GetSucc(el);
  }
  say_sender("Done.");
  return;
}
