Makefile improved, added the log-save & verbose feature
[Cbot.git] / config.c
blob7a81ecd0f9054f3ab1d4493317aa64bb887cf85e
1 /* config.c - Functions which reads a config file to set the bot
2 * ===============================================================
4 * The `CONFIG_FILE' macro is set in `config.h', this macro
5 * sets the path where the config file is.
6 * ===============================================================
7 * Copyright (C) 2009 "Carlos RĂ­os Vera" <crosvera@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *******************************************************************/
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <errno.h>
30 #include "config.h"
32 struct _config *set_config( void ){
33 FILE *c_file = NULL;
34 struct _config *config = NULL;
35 char fline[512];
37 /* Now we open the `CONFIG_FILE'. If an error happend, show a
38 * message, and return NULL.
39 ****************************************************************/
40 c_file = fopen( CONFIG_FILE, "r" );
41 if( c_file == (FILE *)NULL ){
42 fprintf( stderr, "\n%s: %s\n", CONFIG_FILE, strerror( errno ) );
43 return (struct _config *)NULL;
46 config = (struct _config *) malloc( sizeof(struct _config) );
49 /* Now we read the `CONFIG_FILE' and store the info into
50 * the config structure.
51 ***************************************************************/
52 while( fgets(fline, sizeof(fline), c_file) ){
54 if( sscanf( fline, "server: %s\n", config->irc_server ) == 1 )
55 continue;
56 if( sscanf( fline, "port: %s\n", config->irc_port ) == 1 )
57 continue;
58 if( sscanf( fline, "channel: %s\n", config->irc_channel ) == 1 )
59 continue;
60 if( sscanf( fline, "nick: %s\n", config->irc_nick ) == 1)
61 continue;
62 if( sscanf( fline, "name: %[^\n]\n", config->irc_name ) == 1 )
63 continue;
64 if( sscanf( fline, "username: %[^\n]\n", config->username ) == 1 )
65 continue;
66 if( sscanf( fline, "master: %s\n", config->master ) == 1 )
67 continue;
68 if( sscanf( fline, "log-file: %[^\n]\n", config->log_file) == 1)
69 continue;
71 fclose(c_file);
73 if( strlen(config->irc_server) > 1 && strlen(config->irc_port) > 1
74 && strlen(config->irc_channel) > 1 && strlen(config->irc_nick) > 1
75 && strlen(config->irc_name) > 1 && strlen(config->username) > 1
76 && strlen(config->master) > 1 && strlen(config->log_file) > 1 )
77 {/* If there was no problems, go ahead !! */
78 return config;
81 /* troubles? */
82 free(config);
83 return (struct _config *)NULL;