/************************************************************************
 *   IRC - Internet Relay Chat, modules/m_away.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 "h.h"
#include "msg.h"
#include "numeric.h"
#include "s_conf.h"
#include "hook.h"

static struct Message _msgtab[] = {
    {MSG_AWAY, 0, MAXPARA, M_SLOW, 0L,
     m_unregistered, m_away, m_away, m_away, m_ignore}
};

static char *token = TOK1_AWAY;

static int hookid_call_m_private = 0;

#ifndef STATIC_MODULES

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

void _modinit(void)
{
    mod_add_cmd(_msgtab);
    tok1_msgtab[(u_char) *token].msg = _msgtab;
    hookid_call_m_private = hook_add_event("calling m_private");
}

void _moddeinit(void)
{
    mod_del_cmd(_msgtab);
    tok1_msgtab[(u_char) *token].msg = NULL;
    hook_del_event("calling m_private");
}
#else
void m_away_init(void)
{
    mod_add_cmd(_msgtab);
    tok1_msgtab[(u_char) *token].msg = _msgtab;
    hookid_call_m_private = hook_add_event("calling m_private");
}
#endif

static int private_more(aClient *cptr, aClient *sptr, int parc, char **parv)
{
    struct hook_data thisdata;
    char *myparv[3];
    myparv[0] = parv[0];
    myparv[1] = parv[0];
    myparv[2] = parv[1];

    thisdata.client_p = cptr;
    thisdata.source_p = sptr;
    thisdata.parc = 3;
    thisdata.parv = myparv;

    if (hook_call_event(hookid_call_m_private, &thisdata))
        return thisdata.check;
    return 0;
}

/***********************************************************************
         * m_away() - Added 14 Dec 1988 by jto.
 *            Not currently really working, I don't like this
 *            call at all...
 *            
 *            ...trying to make it work. I don't like it either,
 *            but perhaps it's worth the load it causes to net. 
 *            This requires flooding of the whole net like NICK,
 *            USER, MODE, etc messages...  --msa
 *            
 *            Added FLUD-style limiting for those lame scripts out there.
 ***********************************************************************/
/*
 * * m_away * parv[0] = sender prefix *       parv[1] = away message
 */

int m_away(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    char *away, *awy2 = parv[1];

    if (!(sptr->user)) {
	sendto_lev(DEBUG_LEV, "Got AWAY from null user, from %s (%s)", cptr->name, sptr->name);
	return 0;
    }

    if (private_more(sptr, cptr, parc, parv))
	return 0;

    away = sptr->user->away;

    if (ServerOpts.no_away_flood && MyClient(sptr)) {
	if ((sptr->last_away_time + ServerOpts.max_away_time) < NOW)
	    sptr->count_away = 0;
	sptr->last_away_time = NOW;
	sptr->count_away++;
    }

    if (parc < 2 || !*awy2) {
	if (away) {
	    MyFree(away);
	    sptr->user->away = NULL;
	    sendto_serv_butone(cptr, sptr, TOK1_AWAY, "");
	}

	if (MyConnect(sptr))
	    send_me_numeric(sptr, RPL_UNAWAY);
	return 0;
    }
    if (ServerOpts.no_away_flood && MyClient(sptr) && (sptr->count_away > ServerOpts.max_away_count)
	&& !IsAnOper(sptr)) {
	send_me_numeric(sptr, ERR_TOOMANYAWAY);
	return 0;
    }
    if (strlen(awy2) > (size_t) TOPICLEN)
	awy2[TOPICLEN] = '\0';

    sendto_serv_butone(cptr, sptr, TOK1_AWAY, ":%s ", parv[1]);

    if (away)
	MyFree(away);

    away = (char *) MyMalloc(strlen(awy2) + 1);
    strcpy(away, awy2);

    sptr->user->away = away;

    if (MyConnect(sptr))
	send_me_numeric(sptr, RPL_NOWAWAY);
    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1