masqmail

changeset 279:1aa107c6b1e5

moved some code around
author markus schnalke <meillo@marmaro.de>
date Mon, 06 Dec 2010 17:46:24 -0300
parents c35c59a36a2a
children 72e377210d5e
files src/online.c
diffstat 1 files changed, 49 insertions(+), 36 deletions(-) [+]
line diff
     1.1 --- a/src/online.c	Mon Dec 06 17:41:50 2010 -0300
     1.2 +++ b/src/online.c	Mon Dec 06 17:46:24 2010 -0300
     1.3 @@ -1,6 +1,6 @@
     1.4  /*  MasqMail
     1.5      Copyright (C) 1999-2001 Oliver Kurth
     1.6 -    Copyright (C) 2008 markus schnalke <meillo@marmaro.de>
     1.7 +    Copyright (C) 2008, 2010 markus schnalke <meillo@marmaro.de>
     1.8  
     1.9      This program is free software; you can redistribute it and/or modify
    1.10      it under the terms of the GNU General Public License as published by
    1.11 @@ -32,6 +32,44 @@
    1.12  }
    1.13  
    1.14  static gchar*
    1.15 +detect_online_file(const gchar* file)
    1.16 +{
    1.17 +	struct stat st;
    1.18 +	int err;
    1.19 +	FILE *fptr;
    1.20 +	char buf[256];
    1.21 +
    1.22 +	err = stat(conf.online_file, &st);
    1.23 +
    1.24 +	if (err) {
    1.25 +		if (errno==ENOENT) {
    1.26 +			logwrite(LOG_NOTICE, "not online.\n");
    1.27 +			return NULL;
    1.28 +		}
    1.29 +		logwrite(LOG_ALERT, "stat of %s failed: %s\n", conf.online_file, strerror(errno));
    1.30 +		return NULL;
    1.31 +	}
    1.32 +
    1.33 +	fptr = fopen(conf.online_file, "r");
    1.34 +	if (!fptr) {
    1.35 +		logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno));
    1.36 +		return NULL;
    1.37 +	}
    1.38 +	if (fgets(buf, 256, fptr) == NULL) {
    1.39 +		logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file);
    1.40 +		fclose(fptr);
    1.41 +		return NULL;
    1.42 +	}
    1.43 +	g_strstrip(buf);  /* strip whitespace */
    1.44 +	fclose(fptr);
    1.45 +	if (strlen(buf) == 0) {
    1.46 +		logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file);
    1.47 +		return NULL;
    1.48 +	}
    1.49 +	return g_strdup(buf);
    1.50 +}
    1.51 +
    1.52 +static gchar*
    1.53  detect_online_pipe(const gchar * pipe)
    1.54  {
    1.55  	pid_t pid;
    1.56 @@ -77,58 +115,33 @@
    1.57  gchar*
    1.58  detect_online()
    1.59  {
    1.60 -	if (conf.online_detect == NULL) {
    1.61 +	if (!conf.online_detect) {
    1.62  		return NULL;
    1.63  	}
    1.64  
    1.65  	if (strcmp(conf.online_detect, "file") == 0) {
    1.66  		DEBUG(3) debugf("online detection method 'file'\n");
    1.67 -		if (conf.online_file == NULL) {
    1.68 +		if (!conf.online_file) {
    1.69  			logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n");
    1.70  			return NULL;
    1.71  		}
    1.72 -
    1.73 -		struct stat st;
    1.74 -		if (stat(conf.online_file, &st) == 0) {
    1.75 -			FILE *fptr = fopen(conf.online_file, "r");
    1.76 -			if (!fptr) {
    1.77 -				logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno));
    1.78 -				return NULL;
    1.79 -			}
    1.80 -			char buf[256];
    1.81 -			if (fgets(buf, 256, fptr) == NULL) {
    1.82 -				logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file);
    1.83 -				fclose(fptr);
    1.84 -				return NULL;
    1.85 -			}
    1.86 -			g_strchomp(g_strchug(buf));
    1.87 -			fclose(fptr);
    1.88 -			if (strlen(buf) == 0) {
    1.89 -				logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file);
    1.90 -				return NULL;
    1.91 -			}
    1.92 -			return g_strdup(buf);
    1.93 -		} else if (errno == ENOENT) {
    1.94 -			logwrite(LOG_NOTICE, "not online.\n");
    1.95 -			return NULL;
    1.96 -		} else {
    1.97 -			logwrite(LOG_ALERT, "stat of %s failed: %s\n", conf.online_file, strerror(errno));
    1.98 -			return NULL;
    1.99 -		}
   1.100 +		return detect_online_file(conf.online_file);
   1.101  
   1.102  	} else if (strcmp(conf.online_detect, "pipe") == 0) {
   1.103  		DEBUG(3) debugf("connection method 'pipe'\n");
   1.104 -		if (conf.online_pipe)
   1.105 -			return detect_online_pipe(conf.online_pipe);
   1.106 -		else {
   1.107 +		if (!conf.online_pipe) {
   1.108  			logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n");
   1.109  			return NULL;
   1.110  		}
   1.111 +		return detect_online_pipe(conf.online_pipe);
   1.112 +
   1.113  	} else if (strcmp(conf.online_detect, "argument") == 0) {
   1.114 +		DEBUG(3) debugf("online route literally defined\n");
   1.115 +		/* use the name set with set_online_name() */
   1.116  		return connection_name;
   1.117 -	} else {
   1.118 -		DEBUG(3) debugf("no connection method selected\n");
   1.119 +
   1.120  	}
   1.121  
   1.122 +	DEBUG(3) debugf("unknown online detection method `%s'\n", conf.online_detect);
   1.123  	return NULL;
   1.124  }