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 ********************************************************/
24 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include <arpa/inet.h>
35 int set_socket( struct _config
*config
){
37 printlog("***ERROR*** There was an error with the configuration file.");
42 struct addrinfo hints
, *res
;
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
);
53 remote_socket
= socket( res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
56 connect( remote_socket
, res
->ai_addr
, res
->ai_addrlen
);
61 sprintf( NICK
, "NICK %s\n", config
->irc_nick
);
62 send( remote_socket
, NICK
, strlen(NICK
), 0 );
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 );
73 /*** JOIN INTO THE `config->irc_channel' ***/
75 sprintf( JOIN
, "JOIN %s\n", config
->irc_channel
);
76 send( remote_socket
, JOIN
, strlen(JOIN
), 0 );
78 struct irc_msg
*IRCmsg
;
82 if( recv( remote_socket
, buffer
, 1024, 0 ) > 0 )
83 printlog(buffer
);//printf( "%s", buffer );
85 IRCmsg
= irc_parser( buffer
, config
);
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
);
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
);
123 /* Clean the buffer */
124 memset( buffer
, 0, sizeof(buffer
) );
129 close( remote_socket
);