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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s TRUSTADD <host> <#999> [<duration> s/h/d/w/m/y] <reason>\nAdd host as trusted for many users.\n", BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Add host as trusted for many users");
  dl->level = 950;
#if 0
  dl->func = m_trustadd;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_trustadd");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_trustadd(char *a)
{
  char tmp[512];
  char time_str[80];
  int start;
  int i, nr;
  int dur_len;
  int rlen;
  long durationl;
  char duration[21];
  char reason_test[REASONLEN+1];
  char reason[REASONLEN+1];
  struct tm *tm_;
  time_t time_;
  ListElem *el;

  char nrusers[HOSTLEN+1];
  char host[HOSTLEN+1];
  struct trusted *tr;

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s trustadd [%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(host, getword(a,&i), HOSTLEN);
  host[HOSTLEN] = '\0';

  if(strlen(host) < 1) {
    say_sender( "You forgot to specify host");
    say_sender_( "/msg %s TRUSTADD <host> <#999> [<duration> s/h/d/w/m/y] <reason>", BOT_NAME);
    return;
  }

  if(strchr(host, '*')) {
    say_sender("Hostname shouldn't contain *");
    return;
  }

  strncpy(nrusers, getword(a,&i), HOSTLEN);
  nrusers[HOSTLEN] = '\0';
  
  if(nrusers[0] != '#' && !isdigit(nrusers[0])) {
    say_sender( "Second parameter should be a number");
    say_sender_( "/msg %s TRUSTADD <host> <#999> [<duration> s/h/d/w/m/y] <reason>", BOT_NAME);
    return;
  }

  nr = 0;
  if(nrusers[0] == '#') nr++;
  nr = atoi(&nrusers[nr]);

  strncpy(duration, getword(a,&i), 20);
  duration[20] = '\0';
  durationl = atol(duration);
  dur_len = strlen(duration);

  if(dur_len && !isdigit(duration[dur_len-1])) {
    switch(duration[dur_len-1]) {
    case 'h':
      durationl *= 3600;
      break;
    case 'd':
      durationl *= 24*3600;
      break;
    case 'w':
      durationl *= 7*24*3600;
      break;
    case 'm':
      durationl *= 30*24*3600;
      break;
    case 'y':
      durationl *= 365*24*3600;
      break;
    default:
      break;
    }
  }
  if(durationl == 0) {
    say_sender("No duration length was given.");
    return;
  }
  sprintf(duration, "%ld", durationl);


  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], REASONLEN);
  rlen = MIN(strlen(&a[start]), REASONLEN);
  reason[rlen] = '\0';

  tr = trusted_add(host, nr, 0, reason, sender_au->userid);
  if(!tr) {
    el = GetHead(trusted_list);
    while(el) {
      tr = (struct trusted *)(el->data);
      if(!strcasecmp(host, tr->host)) {
	break;
      }
      el = GetSucc(el);
    }
    if(!el) {
      say_sender("Couldn't add the trusted host for some reason.");
      return;
    }

    time_ = durationl + time(NULL);
    tr->expire = time_;

    tm_ = localtime(&time_);
    strftime(time_str, 60, "%c", tm_);
    sprintf(tmp, "TRUSTADD %s %d (before %d) user%s, expires %s (set by %s)\n", host, 
	    nr, tr->nr_users, ((nr==1) ? "" : "s"), time_str, sender_au->dbuser->nick);
    wallop(tmp);
    log(tmp);

    tr->placedby = sender_au->userid;
    tr->nr_users = nr;
    return;
  }

  time_ = durationl + time(NULL);
  tr->expire = time_;

  tm_ = localtime(&time_);
  strftime(time_str, 60, "%c", tm_);
  sprintf(tmp, "TRUSTADD %s %d user%s, expires %s (set by %s)\n", host, 
	  nr, ((nr==1) ? "" : "s"), time_str, sender_au->dbuser->nick);
  wallop(tmp);
  log(tmp);
  return;
}
