/************************************************************************
 *   IRC - Internet Relay Chat, src/s_serv.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 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, 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.
 */

/*
 * $Id: m_watch.c,v 1.3 2003/06/14 13:55:51 tr-ircd Exp $ 
 */

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

static char buf[BUFSIZE];

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

#ifndef STATIC_MODULES

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

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

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

/*
 * RPL_NOWON   - Online at the moment (Succesfully added to WATCH-list)
 * RPL_NOWOFF  - Offline at the moement (Succesfully added to WATCH-list)
 * RPL_WATCHOFF   - Succesfully removed from WATCH-list.
 * ERR_TOOMANYWATCH - Take a guess :>  Too many WATCH entries.
 */
static void show_watch(aClient *cptr, char *name, int rpl1, int rpl2)
{
    aClient *acptr;

    if ((acptr = find_person(name)))
	send_me_numeric(cptr, rpl1, acptr->name, acptr->user->username,
			IsFake(acptr) ? acptr->user->fakehost : acptr->user->host,
			acptr->lasttime);
    else
	send_me_numeric(cptr, rpl2, name, "*", "*", 0);
}

/*
 * m_watch
 */
int m_watch(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    aClient *acptr;
    char *s, *p, *user;
    char def[2] = "l";

    if (parc < 2) {
	/*
	 * Default to 'l' - list who's currently online 
	 */
	parc = 2;
	parv[1] = def;
    }

    for (p = NULL, s = strtoken(&p, parv[1], ", "); s; s = strtoken(&p, NULL, ", ")) {
	if ((user = (char *) strchr(s, '!')))
	    *user++ = '\0';	/* Not used */

	/*
	 * Prefix of "+", they want to add a name to their WATCH
	 * list. 
	 */
	if (*s == '+') {
	    if (*(s + 1)) {
		if (sptr->watches >= MAXWATCH) {
		    send_me_numeric(sptr, ERR_TOOMANYWATCH, s + 1);
		    continue;
		}
		add_to_watch_hash_table(s + 1, sptr);
	    }
	    show_watch(sptr, s + 1, RPL_NOWON, RPL_NOWOFF);
	    continue;
	}

	/*
	 * Prefix of "-", coward wants to remove somebody from their
	 * WATCH list.  So do it. :-)
	 */
	if (*s == '-') {
	    del_from_watch_hash_table(s + 1, sptr);
	    show_watch(sptr, s + 1, RPL_WATCHOFF, RPL_WATCHOFF);
	    continue;
	}

	/*
	 * Fancy "C" or "c", they want to nuke their WATCH list and start
	 * over, so be it.
	 */
	if (*s == 'C' || *s == 'c') {
	    hash_del_watch_list(sptr);
	    continue;
	}

	/*
	 * Now comes the fun stuff, "S" or "s" returns a status report of
	 * their WATCH list.  I imagine this could be CPU intensive if its
	 * done alot, perhaps an auto-lag on this?
	 */
	if (*s == 'S' || *s == 's') {
    dlink_node *lp;
    aWatch *awptr;
    int count = 0;

	    /*
	     * Send a list of how many users they have on their WATCH list
	     * and how many WATCH lists they are on.
	     */
	    awptr = hash_get_watch(sptr->name);
	    if (awptr)
		for (lp = awptr->watched_by.head, count = 1; (lp = lp->next); count++);
	    send_me_numeric(sptr, RPL_WATCHSTAT, sptr->watches, count);

	    /*
	     * Send a list of everybody in their WATCH list. Be careful
	     * not to buffer overflow.
	     */
	    if ((lp = sptr->watchlist.head) == NULL) {
		send_me_numeric(sptr, RPL_ENDOFWATCHLIST, *s);
		continue;
	    }
	    *buf = '\0';
	    awptr = lp->data;
	    strlcpy_irc(buf, awptr->watchnick, NICKLEN);
	    count = strlen(parv[0]) + strlen(me.name) + 10 + strlen(buf);
	    while ((lp = lp->next)) {
		awptr = lp->data;
		if (count + strlen(awptr->watchnick) + 1 > BUFSIZE - 2) {
		    send_me_numeric(sptr, RPL_WATCHLIST, buf);
		    *buf = '\0';
		    count = strlen(parv[0]) + strlen(me.name) + 10;
		}
		strcat(buf, " ");
		strcat(buf, awptr->watchnick);
		count += (strlen(awptr->watchnick) + 1);
	    }
	    send_me_numeric(sptr, RPL_WATCHLIST, buf);
	    send_me_numeric(sptr, RPL_ENDOFWATCHLIST, *s);
	    continue;
	}

	/*
	 * Well that was fun, NOT.  Now they want a list of everybody in
	 * their WATCH list AND if they are online or offline? Sheesh,
	 * greedy arn't we?
	 */
	if (*s == 'L' || *s == 'l') {
    dlink_node *lp = sptr->watchlist.head;
    aWatch *awptr;

	    while (lp) {
		awptr = lp->data;
		if ((acptr = find_person(awptr->watchnick)))
		    send_me_numeric(sptr, RPL_NOWON, acptr->name, acptr->user->username,
				    IsFake(acptr) ? acptr->user->fakehost : acptr->user->host,
				    acptr->tsval);
		/*
		 * But actually, only show them offline if its a capital
		 * 'L' (full list wanted).
		 */
		else if (IsUpper(*s))
		    send_me_numeric(sptr, RPL_NOWOFF, awptr->watchnick, "*", "*", awptr->lasttime);
		lp = lp->next;
	    }
	    send_me_numeric(sptr, RPL_ENDOFWATCHLIST, *s);
	    continue;
	}
	/*
	 * Hmm.. unknown prefix character.. Ignore it. :-) 
	 */
    }

    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1