Makefile improved, added the log-save & verbose feature
[Cbot.git] / irc_cmd.c
blob8850e69bcc0e7ebe9fe004bb51a007b486b7c27c
1 /* irc_cmd.c - Functions which allows parse and execute
2 * a set of commands into the IRC bot.
3 * =====================================================
5 * Copyright (C) 2009 "Carlos RĂ­os Vera" <crosvera@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 **********************************************************/
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
27 #include "irc_cmd.h"
28 #include "config.h"
29 #include "log.h"
31 static struct irc_msg *master_command( const char *raw_msg, struct _config *config ){
33 char *ptr = NULL, *msg_to_send = NULL;
34 struct irc_msg *IRCmsg = NULL;
36 /* Did `config->master'said something important like a command to the bot ?
37 * ***REMEMBER*** these commands must be sent via a PRIVMSG to the bot from
38 * the `config->master'.
39 ***************************************************************************/
40 char *PRIVMSG = (char *)calloc( 1 +
41 strlen("PRIVMSG ") + strlen(config->irc_nick), sizeof(char) );
43 sprintf( PRIVMSG, "PRIVMSG %s ", config->irc_nick );
44 int privmsg_len = strlen(PRIVMSG);
46 if( strncmp(PRIVMSG, raw_msg, privmsg_len) == 0 ){
47 /* What command did `config->master` sent ? */
49 /** the !say command? **/
50 ptr = strstr( &raw_msg[privmsg_len], ":!say " );
51 if( ptr != NULL ){
52 IRCmsg = (struct irc_msg *)malloc( sizeof(struct irc_msg) );
54 msg_to_send = (char *)calloc( 1 +
55 strlen(&ptr[6]) + strlen("PRIVMSG :") + strlen(config->irc_channel) , sizeof(char) );
56 sprintf(msg_to_send, "PRIVMSG %s :%s", config->irc_channel, &ptr[6]);
58 IRCmsg->type = NORMAL_MSG;
59 IRCmsg->msg = msg_to_send;
60 IRCmsg->msg_log = msg_to_send;
62 return IRCmsg;
63 }/*** END !say COMMAND */
65 /** The !quit command? **/
66 ptr = strstr( &raw_msg[privmsg_len], ":!quit");
67 if( ptr != NULL ){
68 IRCmsg = (struct irc_msg *)malloc( sizeof(struct irc_msg) );
70 IRCmsg->type = EXIT;
71 IRCmsg->msg = "QUIT";
72 IRCmsg->msg_log = "EXIT command recived from the master.... BYE!\n";
74 return IRCmsg;
75 }/*** END !quit COMMAND **/
77 /** The !raw command? **/
78 ptr = strstr( &raw_msg[privmsg_len], ":!raw");
79 if( ptr != NULL ){
80 IRCmsg = (struct irc_msg *)malloc( sizeof(struct irc_msg) );
82 IRCmsg->type = NORMAL_MSG;
83 IRCmsg->msg = &ptr[6];
84 IRCmsg->msg_log = &ptr[6];
86 return IRCmsg;
87 }/** END !raw COMMAND **/
89 /** The !fjoin command? **/
90 ptr = strstr( &raw_msg[privmsg_len], ":!fjoin");
91 if( ptr != NULL ){
92 IRCmsg = (struct irc_msg *)malloc( sizeof(struct irc_msg) );
94 msg_to_send = (char *)calloc( 1 + strlen("JOIN \n") + strlen(config->irc_channel), sizeof(char) );
95 sprintf( msg_to_send, "JOIN %s\n", config->irc_channel);
97 IRCmsg->type = NORMAL_MSG;
98 IRCmsg->msg = msg_to_send;
99 IRCmsg->msg_log = "Forcing join into the channel\n";
101 return IRCmsg;
102 }/** END !rjoin command **/
106 return (struct irc_msg *)NULL;
109 struct irc_msg *irc_parser( const char *raw_entry, struct _config *config ){
110 struct irc_msg *IRCmsg = NULL;
111 IRCmsg = (struct irc_msg *)malloc( sizeof(struct irc_msg) );
113 char *msg_to_send = NULL, *ptr = NULL;
116 if( *raw_entry == ':' ){
117 /* Did the `config->master' said something? */
118 if( strncmp(&raw_entry[1], config->master, strlen(config->master)) == 0 ){
119 /* Now we search the possition after the first ' ' in `raw_entry'
120 * because that is the location of the message. */
121 ptr = strchr( raw_entry, ' ' );
122 ptr++;
124 return master_command( ptr, config );
127 }else{
128 /* Search the location after the first ' ' in `raw_entry' */
129 ptr = strchr( raw_entry, ' ' );
130 ptr++;
132 /***PING RESPONSE***/
133 if( strncmp( raw_entry, "PING", 4 ) == 0 ){
134 msg_to_send = (char *)calloc(4 + strlen(&raw_entry[5]), sizeof(char));
135 sprintf( msg_to_send, "PONG %s", &raw_entry[5] );
136 IRCmsg->type = NORMAL_MSG;
137 IRCmsg->msg = msg_to_send;
138 IRCmsg->msg_log = msg_to_send;
140 return IRCmsg;
143 /***KILL RESPONSE***/
144 char *kill_msg = NULL;
145 kill_msg = (char *)calloc(7 + strlen(config->irc_nick), sizeof(char));
146 sprintf( kill_msg, "KILL %s\n", config->irc_nick);
147 if( strncmp(kill_msg, ptr, strlen(kill_msg) ) == 0 ){
148 char *msg2log = "\n*** The bot was killed, time to re-connect!!***\n";
149 IRCmsg->type = EXIT_RESTART;
150 IRCmsg->msg = NULL;
151 IRCmsg->msg_log = msg2log;
153 return IRCmsg;
157 return NULL;