Makefile improved, added the log-save & verbose feature
[Cbot.git] / b_socket.c
blob2ec6c1b810a79077b1b01bdd28d38c96a9eec2de
1 /* b_socket.c - cbot socket set up
2 * ======================================================
3 * Copyright (C) 2009 "Carlos RĂ­os Vera" <crosvera@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ********************************************************/
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netdb.h>
27 #include <arpa/inet.h>
28 #include <unistd.h>
30 #include "b_socket.h"
31 #include "config.h"
32 #include "irc_cmd.h"
33 #include "log.h"
35 int set_socket( struct _config *config ){
36 if( config == NULL ){
37 printlog("***ERROR*** There was an error with the configuration file.");
38 return ERROR_EXIT;
41 char buffer[1025];
42 struct addrinfo hints, *res;
43 int remote_socket;
45 memset( &hints, 0, sizeof hints );
46 hints.ai_family = AF_UNSPEC;
47 hints.ai_socktype = SOCK_STREAM;
49 /* Get the `config->irc_server' adress */
50 getaddrinfo( config->irc_server, config->irc_port, &hints, &res );
52 /* Make the socket */
53 remote_socket = socket( res->ai_family, res->ai_socktype, res->ai_protocol);
55 /* connect! */
56 connect( remote_socket, res->ai_addr, res->ai_addrlen );
59 /*** SET NICK ***/
60 char NICK[25];
61 sprintf( NICK, "NICK %s\n", config->irc_nick);
62 send( remote_socket, NICK, strlen(NICK), 0 );
64 sleep(1);
66 /*** SET USER ***/
67 char USER[256];
68 sprintf( USER, "USER %s %s haha :%s\n", config->username, config->irc_server, config->irc_name );
69 send( remote_socket, USER, strlen(USER), 0 );
71 //sleep(1);
73 /*** JOIN INTO THE `config->irc_channel' ***/
74 char JOIN[128];
75 sprintf( JOIN, "JOIN %s\n", config->irc_channel );
76 send( remote_socket, JOIN, strlen(JOIN), 0 );
78 struct irc_msg *IRCmsg;
80 for( ;; ){
82 if( recv( remote_socket, buffer, 1024, 0 ) > 0 )
83 printlog(buffer);//printf( "%s", buffer );
85 IRCmsg = irc_parser( buffer, config );
87 if( IRCmsg != NULL ){
88 if(IRCmsg->type == EXIT_RESTART){
89 send( remote_socket, IRCmsg->msg, strlen(IRCmsg->msg), 0);
90 //printf("$BOT > %s", IRCmsg->msg_log);
91 printlog( IRCmsg->msg_log );
92 /* CLOSE, FREE & EXIT! */
93 close( remote_socket );
94 freeaddrinfo( res );
95 free( config );
96 free(IRCmsg);
97 return IRC_RECONNECT;
100 if(IRCmsg->type == NORMAL_MSG){
101 send( remote_socket, IRCmsg->msg, strlen(IRCmsg->msg), 0 );
102 //printf("$BOT > %s", IRCmsg->msg_log);
103 printlog( IRCmsg->msg_log );
106 if(IRCmsg->type == EXIT){
107 if( IRCmsg->msg != NULL )
108 send( remote_socket, IRCmsg->msg, strlen(IRCmsg->msg), 0 );
110 printlog(IRCmsg->msg_log);
111 /* CLOSE, FREE & EXIT! */
112 close( remote_socket );
113 freeaddrinfo( res );
114 free( config );
115 free(IRCmsg);
117 return SUCCESS_EXIT;
120 free(IRCmsg);
123 /* Clean the buffer */
124 memset( buffer, 0, sizeof(buffer) );
128 /* CLOSE & FREE!! */
129 close( remote_socket );
130 freeaddrinfo( res );
131 free( config );
133 return SUCCESS_EXIT;