/***************************************************************************
 *   Copyright (C) 2004 by Johnny Mast                                     *
 *   rave@linux                                                            *
 *                                                                         *
 *   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 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 this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "../global.h"



extern char *s_upercommand(char *string);
extern int msg_room (char *thisuser, char *room,const char *fmt, ...);
extern int msg_build ( int sock, int flags, const char *fmt, ... );
extern int s_strnlen(char *string, size_t max);


extern int irc_do_user(void);
extern int irc_do_nick(void);
extern int irc_do_users(void);
extern int irc_do_list(void); 
extern int irc_do_join(void); 
extern int irc_do_part(void); 
extern int irc_do_topic(void); 
extern int irc_do_version(void);
extern int irc_do_invite(void);
extern int irc_do_kick(void); 
extern int irc_do_names(char *room, int fd);
extern int irc_do_whois(void); 
extern int irc_do_who(void); 
extern int irc_do_away(void);
extern int irc_do_mode(void);
extern int irc_do_nmode(void);
extern int irc_do_privmsg(void);
extern int irc_do_notice(void);
extern int irc_do_quit(void);
extern int irc_names(void);
extern int irc_do_time(void);
extern int irc_do_info(void);	
extern int irc_do_vhost(void);	
extern int irc_do_ircop(void);
extern int irc_do_admin(void); 	
extern int irc_do_ping(struct __user *user,char *server);
extern int irc_do_pong(struct __user *user,char *server,char *what);
extern int irc_do_links(void);
extern int irc_do_ison(void);
extern int irc_do_whowas(void);
extern int irc_do_knock(void);
extern int irc_do_pass ( void );
extern int irc_do_service(void);
extern int irc_do_stats(void);
extern int irc_do_motd(void);
extern int irc_do_rules(void);
extern int irc_do_bugs(void);
extern int irc_do_services(void);


extern int irc_do_oper(void);
extern int irc_do_wallops(void);
extern int irc_do_locops(void);
extern int irc_do_globops(void);
extern int irc_do_netglobal(void);	
extern int irc_do_die(void);
extern int irc_do_restart(void);
extern int irc_do_setnick(void);
extern int irc_do_fjoin(char *room);
extern int irc_do_rehash(void);
extern int irc_do_bring(char *user, char *room);
extern int irc_do_kill(char *user,char *k_reason);
extern int irc_do_help(struct __user *_usr);
extern int irc_do_sethost(char *user, char *newhost);

extern int core_motd(struct __user *_usr, int type);
extern int new_who(char *user, char *ident, char *serv,time_t j_time);


struct __user *_usr ; 
char *msg;


extern struct _cm cmd[] = 
{ 

"PASS",     0, 1, 0, irc_do_pass, 
"USER",     0, 1, 0, irc_do_user,
"NICK",     0, 1, 0, irc_do_nick,
"USERS",    0, 1, 0, irc_do_users,
"LUSERS",   0, 1, 0, irc_do_users,
"LIST",     0, 1, 0, irc_do_list, 
"JOIN",     0, 1, 0, irc_do_join,
"PART",     0, 1, 0, irc_do_part,
"TOPIC",    0, 1, 0, irc_do_topic,
"VERSION",  0, 1, 0, irc_do_version,
"INVITE",   0, 1, 0, irc_do_invite,
"KICK",     0, 1, 0, irc_do_kick,
"NAMES",    0, 1, 0, irc_do_names,
"WHOWAS",   0, 1, 0, irc_do_whowas,
"WHOIS",    0, 1, 0, irc_do_whois,
"WHO",      0, 1, 0, irc_do_who,
"PASS",     0, 1, 0, irc_do_pass, 
"USER",     0, 1, 0, irc_do_user,
"NICK",     0, 1, 0, irc_do_nick,
"USERS",    0, 1, 0, irc_do_users,
"LUSERS",   0, 1, 0, irc_do_users,
"LIST",     0, 1, 0, irc_do_list, 
"JOIN",     0, 1, 0, irc_do_join,
"PART",     0, 1, 0, irc_do_part,
"TOPIC",    0, 1, 0, irc_do_topic,
"VERSION",  0, 1, 0, irc_do_version,
"INVITE",   0, 1, 0, irc_do_invite,
"KICK",     0, 1, 0, irc_do_kick,
"NAMES",    0, 1, 0, irc_do_names,
"WHOWAS",   0, 1, 0, irc_do_whowas,
"WHOIS",    0, 1, 0, irc_do_whois,
"WHO",      0, 1, 0, irc_do_who,
"AWAY",     0, 1, 0, irc_do_away,
"MODE",     0, 1, 0, irc_do_mode,
"NMODE",    0, 1, 0, irc_do_nmode,
"ISON",     0, 1, 0, irc_do_ison,
"KNOCK",    0, 1, 0, irc_do_knock,
"PRIVMSG",  0, 1, 0, irc_do_privmsg,
"NOTICE",   0, 1, 0, irc_do_notice,
"QUIT",     0, 1, 0, irc_do_quit,
"HELP",     0, 1, 0, irc_do_help,
"SERVERHELP", 0, 1, 0, irc_do_help,
"SERVICES", 0, 1, 0, irc_do_services,
"MOTD",     0, 1, 0, irc_do_motd,
"IRCDNAME", 0, 1, 0, irc_names,
"TIME",     0, 1, 0, irc_do_time,
"INFO",     0, 1, 0, irc_do_info,
"STATS",    0, 1, 0, irc_do_stats,
"RULES",    0, 1, 0, irc_do_rules,
"VHOST",    0, 1, 0, irc_do_vhost,
"BUGS",     0, 1, 0, irc_do_bugs,
"IRCOP",    0, 1, 0, irc_do_ircop,
"ADMIN",    0, 1, 0, irc_do_admin,
"PING",     0, 1, 0, irc_do_ping,
"PONG",     0, 1, 0, irc_do_pong,
"LINKS",    0, 1, 0, irc_do_links,
"OPER",     0, 1, 0, irc_do_oper,
"WALLOPS",  0, 1, 0, irc_do_wallops,
"LOCOPS",   0, 1, 0, irc_do_locops,
"GLOBOPS",  0, 1, 0, irc_do_globops,
"NETGLOBAL",0,1, 0, irc_do_netglobal,
"SETHOST",  0, 1, 0, irc_do_sethost,
"DIE",      0, 1, 0, irc_do_die,
"RESTART",  0, 1, 0, irc_do_restart,
"SETNICK",  0, 1, 0, irc_do_setnick,
"FJOIN",    0, 1, 0, irc_do_fjoin,
"REHASH",   0, 1, 0, irc_do_rehash,
"BRING",    0, 1, 0, irc_do_bring,
"KILL",     0, 1, 0, irc_do_kill,
 NULL,      0, 0, NULL

};

/*
**
**   _   _   _   _   _   _   _   _     _   _   _   _   _   _     _   _   _   _
**  / \ / \ / \ / \ / \ / \ / \ / \   / \ / \ / \ / \ / \ / \   / \ / \ / \ / \
** ( R | o | s | i | e | l | l | o ) ( S | o | u | r | c | e ) ( C | o | d | e )
**  \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/   \_/ \_/ \_/ \_/ \_/ \_/   \_/ \_/ \_/ \_/  
** 
**								GNU/GPL Licence
**				[ Why close the door when you can help eatch other out ? ] 
**
** Code by: rave
** Containt rave[at]rosiello[dot]org
**       _..-;
** )`--;'(O) F
**  ``--._ \> F
**       ) /} F
**      /_/ ;_[
**      |_[_; [
**      `\,_;_[
**        ; ; [\
**      _  : ;`|
**     /,).';'
**     \'.-`
**
**				This code should work on.
**					Unix and WIN32 
*/

int irc_command ( char * message, struct __user *us )
{
int i=0;
size_t size;


_usr = us;
msg = s_upercommand ( strdup(message) );
size = (size_t) s_strnlen (msg, MR);

while ( cmd[i].cmd )
{
	if ( (strncmp(msg, cmd[i].cmd,strlen(cmd[i].cmd))) == 0)
	{
		
	cmd[i].used ++;
	cmd[i].kb += size;


	if ( cmd[i].enabled == YES )
		return cmd[i].funct();

	} 

	++i;
}


return msg_build(_usr->fd,0,IRC_UNKNOW_COMMAND,_usr->Nick,msg);
}



