/*
 * glineresync.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"

#define MIN(x, y) (((x) < (y)) ? (x) : (y))

void m_glineresync(char *a);

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s GLINERESYNC [*pattern*]\n", BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Resync G-lines to all servers.");
  dl->level = 950;
#if 0
  dl->func = m_glineresync;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_glineresync");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_glineresync(char *a)
{
  ListElem *el;
  struct gline *g;
  int i, nr;
  char gline[GLINELEN+1];
  long durationl;
  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s glineresync [%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(gline, getword(a,&i), GLINELEN);
  gline[GLINELEN] = '\0';

  if(strlen(gline) < 1) {
    strcat(gline, "*");
  }

  nr = 0;
  el = GetHead(gline_list);
  while(el) {
    g = (struct gline *)el->data;
    if(!match(gline, g->gline)) {
      durationl = g->expire - time(NULL);
      if(durationl > 0) {
	putserver("%c%c GL * +%s %ld :%s\n\r", yxx[0], yxx[1], g->gline,
		  durationl,
		  g->reason);
      }
      //      say_sender_("dur=%ld %s %ld %ld", durationl, g->gline, g->expire, time(NULL));
      nr++;
    }
    el = GetSucc(el);
  }
  say_sender_("%d glines resynced", nr);
  return;
}
