-/* configfile.c -- Parsing the configuration file
- Copyright (C) 2003 Hugo Villeneuve */
-
+/*
+ * configfile.c -- Parsing the configuration file
+ *
+ * Copyright (C) 2003 Hugo Villeneuve <hugo@hugovil.com>
+ *
+ * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
#if HAVE_CONFIG_H
# include "config.h"
#include <unistd.h>
#include <stdio.h>
+#include <sys/stat.h>
#include <stdlib.h>
+#include <stdbool.h>
#if STDC_HEADERS
# include <string.h>
fprintf( stderr, "%s: Can't create file \"%s\"\n", PACKAGE, file );
exit( EXIT_FAILURE );
}
-
+
+ /* Changing permissions so that only the user can read/modify the file. */
+ status = chmod( file, S_IRUSR | S_IWUSR );
+ if( status < 0 ) {
+ fprintf( stderr, "%s: Can't set permission bits on file \"%s\"\n", PACKAGE, file );
+ exit( EXIT_FAILURE );
+ }
+
fprintf( fp, "# ~/.wmnotifyrc -- Default configuration file for wmnotify\n\n" );
fprintf( fp, "# Replace all 'xxxxxxxx' fields with your own settings.\n\n" );
fprintf( fp, "# Parameters preceded by a '#' character are optional.\n" );
static char *
-GetArguments( char *parameter, int number_of_arguments )
+GetArguments( char *parameter, bool single_argument )
{
char *token;
- if( number_of_arguments > 1 ) {
+ if( single_argument ) {
+ token = strtok( NULL, delimiter_single_arg );
+ }
+ else {
/* We search for a string terminated by either a '#' character (the rest of
the line is then a comment, which is simply ignored ), or the end of line
character '\n'. */
token = strtok( NULL, delimiter_multiple_arg );
}
- else {
- token = strtok( NULL, delimiter_single_arg );
- }
if( token == NULL ) {
fprintf( stderr, "%s: Missing argument for \"%s\" parameter in "
{
char line[LINE_BUFFER_LEN];
char *token;
- bool protocol_found = FALSE;
- bool server_found = FALSE;
- bool username_found = FALSE;
- bool password_found = FALSE;
+ bool protocol_found = false;
+ bool server_found = false;
+ bool username_found = false;
+ bool password_found = false;
const char *err_string = NULL;
/* Default values for optional parameters. */
wmnotify_infos.port = 110;
wmnotify_infos.mail_check_interval = 60; /* 1 minute interval. */
- wmnotify_infos.audible_notification = FALSE; /* Disabled. */
- wmnotify_infos.use_ssl = FALSE; /* Disabled. */
+ wmnotify_infos.audible_notification = false; /* Disabled. */
+ wmnotify_infos.use_ssl = false; /* Disabled. */
wmnotify_infos.mail_client_argv[0] = NULL; /* No default command. */
wmnotify_infos.audiofile[0] = '\0'; /* No default audio file. */
wmnotify_infos.volume = 100; /* 100% volume. */
}
if( STREQ( token, "protocol" ) ) {
- token = GetArguments( "protocol", 1 );
- if( STREQ( token, "POP3" ) == TRUE ) {
+ token = GetArguments( "protocol", true );
+ if( STREQ( token, "POP3" ) == true ) {
wmnotify_infos.protocol = POP3_PROTOCOL;
}
- else if( STREQ( token, "IMAP4" ) == TRUE ) {
+ else if( STREQ( token, "IMAP4" ) == true ) {
wmnotify_infos.protocol = IMAP4_PROTOCOL;
}
else {
exit( EXIT_FAILURE );
}
- protocol_found = TRUE;
+ protocol_found = true;
}
else if( STREQ( token, "use_ssl" ) ){
int number;
- token = GetArguments( "use_ssl", 1 );
+ token = GetArguments( "use_ssl", true );
number = GetNumber( token, "use_ssl" );
if( number == 0 ) {
- wmnotify_infos.use_ssl = FALSE;
+ wmnotify_infos.use_ssl = false;
}
else if( number == 1 ) {
- wmnotify_infos.use_ssl = TRUE;
+#if HAVE_SSL
+ wmnotify_infos.use_ssl = true;
+#else
+ fprintf( stderr, "%s error: You must compile %s with SSL support to\n" \
+ "set parameter 'use_ssl' to true in configuration file\n", PACKAGE, PACKAGE );
+ exit( EXIT_FAILURE );
+#endif
}
else {
- fprintf( stderr, "%s: Invalid value for for parameter 'enablebeep' in\n" \
+ fprintf( stderr, "%s: Invalid value for parameter 'use_ssl' in\n" \
"configuration file (must be 0 or 1): %d\n", PACKAGE, number );
exit( EXIT_FAILURE );
}
}
else if( STREQ( token, "server" ) ) {
- token = GetArguments( "server", 1 );
+ token = GetArguments( "server", true );
strncpy( wmnotify_infos.server_name, token, MAX_STR_LEN );
- server_found = TRUE;
+ server_found = true;
}
else if( STREQ( token, "port" ) ) {
- token = GetArguments( "port", 1 );
+ token = GetArguments( "port", true );
wmnotify_infos.port = (u_int16_t) GetNumber( token, "port" );
}
else if( STREQ( token, "username" ) ) {
- token = GetArguments( "username", 1 );
+ token = GetArguments( "username", true );
strncpy( wmnotify_infos.username, token, MAX_STR_LEN );
- username_found = TRUE;
+ username_found = true;
}
else if( STREQ( token, "password" ) ) {
- token = GetArguments( "password", 1 );
+ token = GetArguments( "password", true );
strncpy( wmnotify_infos.password, token, MAX_STR_LEN );
- password_found = TRUE;
+ password_found = true;
}
else if( STREQ( token, "mailcheckdelay" ) ) {
int delay; /* delay in minutes. */
- token = GetArguments( "mailcheckdelay", 1 );
+ token = GetArguments( "mailcheckdelay", true );
/* GetNumber() will exit if a negative number is entered. */
delay = GetNumber( token, "mailcheckdelay" );
if( delay == 0 ) {
wmnotify_infos.mail_check_interval = (unsigned int) delay * 60;
}
else if( STREQ( token, "mailclient" ) ) {
- token = GetArguments( "mailclient", 2 ); /* Multiple arguments */
+ token = GetArguments( "mailclient", false ); /* Multiple arguments */
strcpy( wmnotify_infos.mail_client_command, token );
ParseCommand( wmnotify_infos.mail_client_command,
wmnotify_infos.mail_client_argv );
else if( STREQ( token, "enablebeep" ) ){
int number;
- token = GetArguments( "enablebeep", 1 );
+ token = GetArguments( "enablebeep", true );
number = GetNumber( token, "enablebeep" );
if( number == 0 ) {
- wmnotify_infos.audible_notification = FALSE;
+ wmnotify_infos.audible_notification = false;
}
else if( number == 1 ) {
- wmnotify_infos.audible_notification = TRUE;
+ wmnotify_infos.audible_notification = true;
}
else {
fprintf( stderr, "%s: Invalid value for for parameter 'enablebeep' in\n" \
}
}
else if( STREQ( token, "audiofile" ) ) {
- token = GetArguments( "audiofile", 1 );
+ token = GetArguments( "audiofile", true );
/* Should check size before using strcpy(), or use strncopy() instead. */
strcpy( wmnotify_infos.audiofile, token );
}
else if( STREQ( token, "volume" ) ) {
- token = GetArguments( "volume", 1 );
+ token = GetArguments( "volume", true );
wmnotify_infos.volume = GetNumber( token, "volume" );
}
else {
}
}
- if( protocol_found == FALSE ) {
+ if( protocol_found == false ) {
err_string = "protocol";
}
- else if( server_found == FALSE ) {
+ else if( server_found == false ) {
err_string = "server";
}
- else if( username_found == FALSE ) {
+ else if( username_found == false ) {
err_string = "username";
}
- else if( password_found == FALSE ) {
+ else if( password_found == false ) {
err_string = "password";
}
else {