/*
 * Copyright (C) 2005 BarkerJr
 *
 * This module is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Anope; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include "module.h"
#define VERSION "1.0.0"

/*
 * ircd_ab2508
 *   by BarkerJr <http://barkerjr.net>
 *
 * This module has been written to block IRC-Orks which message ab2508.
 *
 * It has been tested on SolidIRCd 3.4.6 (based on Bahamut) and Anope 1.7.7svn.
 */

/* Akill Reason. */
#define REASON "IRC-Ork"

/* Akill time in seconds (if applicable).  Minimum is 60.  Maximum is 2147483647.
 * 604800 = 1 week */
#define AKILL_TIME 604800

/* LagServ information */
#define s_ab2508 "ab2508"
#define AB2508_FULL "Do not message me or you will be banned"

/* Run in gebug mode */
#undef DEBUG_MODE



/* End of configuration */



#if AKILL_TIME < 60
  #define AKILL_TIME 60
#elif AKILL_TIME > 2147483647
  #define AKILL_TIME 2147483647
#endif

#if defined (IRC_SOLID) || defined (IRC_BAHAMUT)
  #define USE_IP
#endif

#ifdef USE_IP
int gotConnect(char*, int, char**);
#endif
int gotPrivmsg(char*, int, char**);

int AnopeInit(int argc, char **argv)
{
  Message *msg;
  int reply;
#ifdef USE_IP
  msg = createMessage("NICK", gotConnect);
  reply = moduleAddMessage(msg, MOD_TAIL);
  if (reply != MOD_ERR_OK)
  {
    alog("ircd_ab2508 error adding message \"NICK\": %i", reply);
    return MOD_STOP;
  }
#endif
  msg = createMessage(send_token("PRIVMSG", "!"), gotPrivmsg);
  reply = moduleAddMessage(msg, MOD_TAIL);
  if (reply != MOD_ERR_OK)
  {
    alog("ircd_ab2508 error adding message \"%s\": %i", send_token("PRIVMSG", "!"), reply);
    return MOD_STOP;
  }
  anope_cmd_bot_nick(s_ab2508, ServiceUser, ServiceHost, AB2508_FULL, "+i");
  moduleAddAuthor("BarkerJr");
  moduleAddVersion(VERSION);
  alog("ircd_ab2508 v%s by BarkerJr loaded", VERSION);
#if AKILL_TIME < 3600
  alog("ircd_ab2508 akills expire after %u minutes", AKILL_TIME / 60);
#elif AKILL_TIME == 3600
  alog("ircd_ab2508 akills expire after 1 hour");
#elif AKILL_TIME < 86400
  alog("ircd_ab2508 akills expire after %.1f hours", AKILL_TIME / 3600.0);
#elif AKILL_TIME == 86400
  alog("ircd_ab2508 akills expire after 1 day");
#else
  alog("ircd_ab2508 akills expire after %.1f days", AKILL_TIME / 86400.0);
#endif
  return MOD_CONT;
}

void AnopeFini(void)
{
  anope_cmd_quit(s_ab2508, "Help!  I'm being unloaded!");
}

#ifdef USE_IP
int gotConnect(char *source, int ac, char **av)
{
  if (ac < 2) return MOD_CONT;
  if (*source) return MOD_CONT;
  {
    User *u;
    if (!(u = finduser(av[0]))) return MOD_CONT; /* Not a user */
    {
      int reply;
      char ipbuf[16];
      struct in_addr addr;

      addr.s_addr = htonl(strtoul(av[8], NULL, 10));
      ntoa(addr, ipbuf, sizeof(ipbuf));
  #ifdef DEBUG_MODE
      alog("ircd_ab2508 storing %s for %s", ipbuf, av[0]);
  #endif
      reply = moduleAddData(&u->moduleData, "ip", ipbuf);

      if (reply != MOD_ERR_OK)
        alog("ircd_storeip error storing IP for %s: %i", av[0], reply);
    }
  }
  return MOD_CONT;
}
#endif

int gotPrivmsg(char *source, int ac, char **av)
{
  if (*av[0] == '#') return MOD_CONT; /* Channel Notice */
  if (strcmp(av[0], s_ab2508)) return MOD_CONT; /* Not for me */
  {
    User *u;
    if (!(u = finduser(source))) return MOD_CONT; /* Not a user */
    if (is_oper(u))
    {
      anope_cmd_notice2(s_ab2508, u->nick, "You're lucky to be an oper :)");
      return MOD_CONT;
    }
    {
      char *mask;
      char *host;
#ifdef USE_IP
      if (!(host = moduleGetData(&u->moduleData, "ip")))
#endif
        host = u->host;
      if (!(mask = (char *)malloc(strlen(host) + 3))) /* Out of memory */
      {
        alog("ircd_ab2508 error allocating memory to akill %s", u->nick);
        return MOD_CONT;
      }
      sprintf(mask, "*@%s", host);
#ifdef DEBUG_MODE
      alog("%s: Sending akill for %s", s_ab2508, mask);
#endif
      add_akill(NULL, mask, s_ab2508, time(NULL) + AKILL_TIME, REASON);
      free(mask);
      alog("%s: Akilling %s!%s@%s [%s] for messaging me", s_ab2508, u->nick, u->username, u->host, u->realname);
    }
  }
  return MOD_STOP;
}
