#!/usr/bin/env perl

# $Id: services.pl.in,v 1.5 2004/02/19 00:03:21 ircd Exp $

# Services commands for Hybrid 7 and ircd-ratbox 1.

# Creating a services module:
# 1.  Read in the configuration file
# 2.  Create m_services.c
# 3.  Print out instructions on building and installing

# Defaults
$conffile = "services.conf";
$srcdir = "../../contrib";

# Global variables
# Note:  For all the hashes, the command is the key
%nicks = ();
%userhosts = ();
%aliases = ();
$revision = '$Revision: 1.5 $';

# 1.  Read in the configuration file
open(CONF, "<$conffile");
while (<CONF>)
{
  chomp;
  if (/^#/)
  {
    next;
  }
  if (/^(\s)*$/)
  {
    next;
  }
  ($tcommand, $tnick, $tuhost, $talias) = split(',');
  $nicks{$tcommand} = $tnick;
  $userhosts{$tcommand} = $tuhost;
  if ($talias ne "NONE")
  {
    $aliases{$tcommand} = $talias;
  }
}
close(CONF);

# 2.  Create m_services.c
open(SOUT, ">${srcdir}/m_services.c");
print SOUT "/* m_services.c - Script generated services commands definition file\n";
print SOUT "** Do not edit this file\n";
print SOUT "*/\n\n#include \"stdinc.h\"\n#include \"handlers.h\"\n#include \"client.h\"\n#include \"common.h\"\n";
print SOUT "#include \"ircd.h\"\n#include \"irc_string.h\"\n#include \"numeric.h\"\n#include \"fdlist.h\"\n";
print SOUT "#include \"s_bsd.h\"\n#include \"s_conf.h\"\n#include \"s_log.h\"\n#include \"s_serv.h\"\n#include \"send.h\"\n";
print SOUT "#include \"msg.h\"\n#include \"parse.h\"\n#include \"modules.h\"\n\n";

foreach $k (keys %userhosts)
{
  print SOUT "#define SVS_${k}_NICK \"$nicks{$k}\"\n";
  print SOUT "#define SVS_${k}_USERSERV \"$userhosts{$k}\"\n";
}

print SOUT "\n";
print SOUT "char *reconstruct_parv(int parc, char *parv[]);\n\n";

foreach $k (keys %userhosts)
{
  print SOUT "static void m_$k(struct Client *client_p, struct Client *source_p, int parc, char *parv[]);\n";
}

print SOUT "\n";

foreach $k (keys %userhosts)
{
  print SOUT "struct Message ${k}_msgtab = {\n";
  print SOUT "  \"$k\", 0, 0, 2, 0, MFLG_SLOW, 0,\n";
  print SOUT "  {m_unregistered, m_$k, m_ignore, m_$k}\n";
  print SOUT "};\n\n";
}
foreach $k (keys %aliases)
{
  print SOUT "struct Message $aliases{$k}_msgtab = {\n";
  print SOUT "  \"$aliases{$k}\", 0, 0, 2, 0, MFLG_SLOW, 0,\n";
  print SOUT "  {m_unregistered, m_$k, m_ignore, m_$k}\n";
  print SOUT "};\n\n";
}

print SOUT "#ifndef STATIC_MODULES\nvoid _modinit(void)\n{\n";

foreach $k (keys %userhosts)
{
  print SOUT "  mod_add_cmd(\&${k}_msgtab);\n";
}
foreach $k (keys %aliases)
{
  print SOUT "  mod_add_cmd(\&$aliases{$k}_msgtab);\n";
}

print SOUT "}\n\nvoid _moddeinit(void)\n{\n";

foreach $k (keys %userhosts)
{
  print SOUT "  mod_del_cmd(\&${k}_msgtab);\n";
}
foreach $k (keys %aliases)
{
  print SOUT "  mod_del_cmd(\&$aliases{$k}_msgtab);\n";
}

print SOUT "}\nconst char *_version = \"$revision\";\n#endif\n\n";

print SOUT "char *reconstruct_parv(int parc, char *parv[])\n{\n";
print SOUT "  static char tmpbuf[BUFSIZE]; int i;\n\n";
print SOUT "  strlcpy(tmpbuf, parv[0], BUFSIZE);\n";
print SOUT "  for (i = 1; i < parc; i++)\n";
print SOUT "  {\n    strlcat(tmpbuf, \" \", BUFSIZE);\n";
print SOUT "    strlcat(tmpbuf, parv[i], BUFSIZE);\n  }\n";
print SOUT "  return tmpbuf;\n}\n\n";

foreach $k (keys %userhosts)
{
  print SOUT "static void m_$k(struct Client *client_p, struct Client *source_p, int parc, char *parv[])\n";
  print SOUT "{\n  struct Client *target_p;\n\n";
  print SOUT "  if ((target_p = find_person(SVS_${k}_NICK)))\n  {\n";
  print SOUT "    sendto_one(target_p, \":%s PRIVMSG %s :%s\", parv[0], SVS_${k}_USERSERV, reconstruct_parv(parc - 1, &parv[1]));\n";
  print SOUT "  }\n  else\n  {\n    sendto_one(source_p, form_str(ERR_NOSUCHNICK), me.name, parv[0], SVS_${k}_NICK);\n";
  print SOUT "  }\n}\n\n";
}

# Done!
close(SOUT);
