masqmail-0.2
changeset 33:e1004fcc93c9
flattened conditional nesting
author | meillo@marmaro.de |
---|---|
date | Thu, 06 May 2010 13:31:57 +0200 |
parents | 941f755e2965 |
children | 8ea86ac25658 |
files | src/online.c |
diffstat | 1 files changed, 76 insertions(+), 68 deletions(-) [+] |
line diff
1.1 --- a/src/online.c Thu May 06 13:24:49 2010 +0200 1.2 +++ b/src/online.c Thu May 06 13:31:57 2010 +0200 1.3 @@ -44,28 +44,31 @@ 1.4 old_signal = signal(SIGCHLD, SIG_DFL); 1.5 1.6 in = peopen(pipe, "r", environ, &pid); 1.7 - if (in != NULL) { 1.8 - gchar output[256]; 1.9 - if (fgets(output, 255, in)) { 1.10 - g_strchomp(g_strchug(output)); 1.11 - if (strlen(output) == 0) { 1.12 - logwrite(LOG_ALERT, "only whitespace connection name\n"); 1.13 - name = NULL; 1.14 - } else { 1.15 - name = g_strdup(output); 1.16 - } 1.17 + if (in == NULL) { 1.18 + logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno)); 1.19 + signal(SIGCHLD, old_signal); 1.20 + return NULL; 1.21 + } 1.22 + 1.23 + gchar output[256]; 1.24 + if (fgets(output, 255, in)) { 1.25 + g_strchomp(g_strchug(output)); 1.26 + if (strlen(output) == 0) { 1.27 + logwrite(LOG_ALERT, "only whitespace connection name\n"); 1.28 + name = NULL; 1.29 } else { 1.30 - logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe); 1.31 - name = NULL; 1.32 + name = g_strdup(output); 1.33 } 1.34 - fclose(in); 1.35 - waitpid(pid, &status, 0); 1.36 - if (WEXITSTATUS(status) != EXIT_SUCCESS) { 1.37 - g_free(name); 1.38 - name = NULL; 1.39 - } 1.40 - } else 1.41 - logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno)); 1.42 + } else { 1.43 + logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe); 1.44 + name = NULL; 1.45 + } 1.46 + fclose(in); 1.47 + waitpid(pid, &status, 0); 1.48 + if (WEXITSTATUS(status) != EXIT_SUCCESS) { 1.49 + g_free(name); 1.50 + name = NULL; 1.51 + } 1.52 1.53 signal(SIGCHLD, old_signal); 1.54 1.55 @@ -75,58 +78,63 @@ 1.56 gchar* 1.57 detect_online() 1.58 { 1.59 - if (conf.online_detect != NULL) { 1.60 - if (strcmp(conf.online_detect, "file") == 0) { 1.61 - DEBUG(3) debugf("online detection method 'file'\n"); 1.62 - if (conf.online_file != NULL) { 1.63 - struct stat st; 1.64 - if (stat(conf.online_file, &st) == 0) { 1.65 - FILE *fptr = fopen(conf.online_file, "r"); 1.66 - if (fptr) { 1.67 - char buf[256]; 1.68 - if (fgets(buf, 256, fptr) == NULL) { 1.69 - logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file); 1.70 - fclose(fptr); 1.71 - return NULL; 1.72 - } 1.73 - g_strchomp(g_strchug(buf)); 1.74 - fclose(fptr); 1.75 - if (strlen(buf) == 0) { 1.76 - logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file); 1.77 - return NULL; 1.78 - } 1.79 - return g_strdup(buf); 1.80 - } else { 1.81 - logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno)); 1.82 - return NULL; 1.83 - } 1.84 - } else if (errno == ENOENT) { 1.85 - logwrite(LOG_NOTICE, "not online.\n"); 1.86 - return NULL; 1.87 - } else { 1.88 - logwrite(LOG_ALERT, "stat of %s failed: %s", conf.online_file, strerror(errno)); 1.89 - return NULL; 1.90 - } 1.91 - } else 1.92 - logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n"); 1.93 -#ifdef ENABLE_MSERVER 1.94 - } else if (strcmp(conf.online_detect, "mserver") == 0) { 1.95 - DEBUG(3) debugf("connection method 'mserver'\n"); 1.96 - return mserver_detect_online(conf.mserver_iface); 1.97 -#endif 1.98 - } else if (strcmp(conf.online_detect, "pipe") == 0) { 1.99 - DEBUG(3) debugf("connection method 'pipe'\n"); 1.100 - if (conf.online_pipe) 1.101 - return detect_online_pipe(conf.online_pipe); 1.102 - else { 1.103 - logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n"); 1.104 + if (conf.online_detect == NULL) { 1.105 + return NULL; 1.106 + } 1.107 + 1.108 + if (strcmp(conf.online_detect, "file") == 0) { 1.109 + DEBUG(3) debugf("online detection method 'file'\n"); 1.110 + if (conf.online_file != NULL) { 1.111 + logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n"); 1.112 + return NULL; 1.113 + } 1.114 + 1.115 + struct stat st; 1.116 + if (stat(conf.online_file, &st) == 0) { 1.117 + FILE *fptr = fopen(conf.online_file, "r"); 1.118 + if (!fptr) { 1.119 + logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno)); 1.120 return NULL; 1.121 } 1.122 - } else if (strcmp(conf.online_detect, "argument") == 0) { 1.123 - return connection_name; 1.124 + char buf[256]; 1.125 + if (fgets(buf, 256, fptr) == NULL) { 1.126 + logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file); 1.127 + fclose(fptr); 1.128 + return NULL; 1.129 + } 1.130 + g_strchomp(g_strchug(buf)); 1.131 + fclose(fptr); 1.132 + if (strlen(buf) == 0) { 1.133 + logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file); 1.134 + return NULL; 1.135 + } 1.136 + return g_strdup(buf); 1.137 + } else if (errno == ENOENT) { 1.138 + logwrite(LOG_NOTICE, "not online.\n"); 1.139 + return NULL; 1.140 } else { 1.141 - DEBUG(3) debugf("no connection method selected\n"); 1.142 + logwrite(LOG_ALERT, "stat of %s failed: %s", conf.online_file, strerror(errno)); 1.143 + return NULL; 1.144 } 1.145 + 1.146 +#ifdef ENABLE_MSERVER 1.147 + } else if (strcmp(conf.online_detect, "mserver") == 0) { 1.148 + DEBUG(3) debugf("connection method 'mserver'\n"); 1.149 + return mserver_detect_online(conf.mserver_iface); 1.150 +#endif 1.151 + } else if (strcmp(conf.online_detect, "pipe") == 0) { 1.152 + DEBUG(3) debugf("connection method 'pipe'\n"); 1.153 + if (conf.online_pipe) 1.154 + return detect_online_pipe(conf.online_pipe); 1.155 + else { 1.156 + logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n"); 1.157 + return NULL; 1.158 + } 1.159 + } else if (strcmp(conf.online_detect, "argument") == 0) { 1.160 + return connection_name; 1.161 + } else { 1.162 + DEBUG(3) debugf("no connection method selected\n"); 1.163 } 1.164 + 1.165 return NULL; 1.166 }