/************************************************************************
 *   IRC - Internet Relay Chat, modules/m_dccallow.c
 *
 *   Copyright (C) 2000-2003 TR-IRCD Development
 *
 *   Copyright (C) 1990 Jarkko Oikarinen and
 *                      University of Oulu, Co Center
 *
 *   See file AUTHORS in IRC package for additional names of
 *   the programmers.
 *
 *   This program is free softwmare; 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, 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 this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "struct.h"
#include "common.h"
#include "sys.h"
#include "numeric.h"
#include "msg.h"
#include "channel.h"
#include "h.h"

static struct Message _msgtab[] = {
    {MSG_DCCALLOW, 0, 1, M_SLOW, 0L,
     m_unregistered, m_dccallow, m_dccallow, m_ignore, m_ignore}
};

#ifndef STATIC_MODULES

char *_version = "$Revision: 1.5 $";

void _modinit(void)
{
    mod_add_cmd(_msgtab);
}

void _moddeinit(void)
{
    mod_del_cmd(_msgtab);
}
#else
void m_dccallow_init(void)
{
    mod_add_cmd(_msgtab);
}
#endif

int m_dccallow(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    dlink_node *lp, *next_lp;
    char *p, *s;
    char *cn;
    aClient *acptr;
    int didlist = 0, didanything = 0;

    if (!MyClient(sptr))
	return 0;		/* don't accept dccallows from servers or
				 * clients that aren't mine.. */

    if (parc < 2) {
	send_me_notice(sptr,
		       ":No command specified for DCCALLOW. Type /quote help dccallow for more information.");
	return 0;
    }

    for (p = NULL, s = strtoken(&p, parv[1], ", "); s; s = strtoken(&p, NULL, ", ")) {
	if (*s == '+') {
	    didanything++;
	    cn = s + 1;
	    if (*cn == '\0')
		continue;

	    acptr = find_person(cn);
	    if (!acptr) {
		send_me_numeric(sptr, ERR_NOSUCHNICK, cn);
		continue;
	    }
	    if (acptr == sptr)
		continue;
	    add_dccallow(sptr, acptr);
	} else if (*s == '-') {
	    didanything++;
	    cn = s + 1;
	    if (*cn == '\0')
		continue;

	    acptr = find_person(cn);
	    if (!acptr) {
		send_me_numeric(sptr, ERR_NOSUCHNICK, cn);
		continue;
	    }
	    if (acptr == sptr)
		continue;
	    del_dccallow(sptr, acptr);
	} else {
	    if (!didlist && irc_strncmp(s, "list", 4) == 0) {
		didanything++;
		didlist++;
		sendto_one(sptr,
			   ":%C %N %s :The following users are on your dcc allow list:",
			   &me, RPL_DCCINFO, sptr->name);
		for (lp = sptr->user->dccallow.head; lp; lp = next_lp) {
		    next_lp = lp->next;
		    acptr = lp->data;
		    if (!acptr || !IsPerson(acptr)) {
		 	del_dccallow(sptr, acptr);
			continue;
		    }
		    sendto_one(sptr, ":%C %N %s :%s (%s@%s)", &me,
			       RPL_DCCLIST, sptr->name, acptr->name, acptr->user->username,
			       IsFake(acptr) ? acptr->user->fakehost : acptr->user->host);

		}
		send_me_numeric(sptr, RPL_ENDOFDCCLIST, s);
	    }
	}
    }

    if (!didanything) {
	send_me_notice(sptr,
		       ":Invalid syntax for DCCALLOW. Type /quote help dccallow for more information.");
	return 0;
    }

    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1