masqmail

annotate src/online.c @ 15:f671821d8222

code beautifying; 0 -> \0 if appropriate
author meillo@marmaro.de
date Thu, 06 Nov 2008 09:18:38 +0100
parents 26e34ae9a3e3
children 99c09ed776c1
rev   line source
meillo@0 1 /* MasqMail
meillo@0 2 Copyright (C) 1999-2001 Oliver Kurth
meillo@0 3
meillo@0 4 This program is free software; you can redistribute it and/or modify
meillo@0 5 it under the terms of the GNU General Public License as published by
meillo@0 6 the Free Software Foundation; either version 2 of the License, or
meillo@0 7 (at your option) any later version.
meillo@0 8
meillo@0 9 This program is distributed in the hope that it will be useful,
meillo@0 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@0 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@0 12 GNU General Public License for more details.
meillo@0 13
meillo@0 14 You should have received a copy of the GNU General Public License
meillo@0 15 along with this program; if not, write to the Free Software
meillo@0 16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 17 */
meillo@0 18
meillo@0 19 #include <sys/stat.h>
meillo@0 20 #include <sys/wait.h>
meillo@15 21
meillo@0 22 #include "masqmail.h"
meillo@0 23 #include "mserver.h"
meillo@0 24 #include "peopen.h"
meillo@0 25
meillo@0 26 gchar *connection_name;
meillo@0 27
meillo@10 28 void
meillo@10 29 set_online_name(gchar * name)
meillo@0 30 {
meillo@10 31 connection_name = g_strdup(name);
meillo@0 32 }
meillo@0 33
meillo@10 34 static gchar*
meillo@10 35 detect_online_pipe(const gchar * pipe)
meillo@0 36 {
meillo@10 37 pid_t pid;
meillo@10 38 void (*old_signal) (int);
meillo@10 39 int status;
meillo@10 40 FILE *in;
meillo@10 41 gchar *name = NULL;
meillo@0 42
meillo@10 43 old_signal = signal(SIGCHLD, SIG_DFL);
meillo@0 44
meillo@10 45 in = peopen(pipe, "r", environ, &pid);
meillo@10 46 if (in != NULL) {
meillo@10 47 gchar output[256];
meillo@10 48 if (fgets(output, 255, in)) {
meillo@10 49 g_strchomp(output);
meillo@10 50 name = g_strdup(output);
meillo@10 51 }
meillo@10 52 fclose(in);
meillo@10 53 waitpid(pid, &status, 0);
meillo@10 54 if (WEXITSTATUS(status) != EXIT_SUCCESS) {
meillo@10 55 g_free(name);
meillo@10 56 name = NULL;
meillo@10 57 }
meillo@10 58 } else
meillo@10 59 logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno));
meillo@0 60
meillo@10 61 signal(SIGCHLD, old_signal);
meillo@0 62
meillo@10 63 return name;
meillo@0 64 }
meillo@0 65
meillo@10 66 gchar*
meillo@10 67 detect_online()
meillo@0 68 {
meillo@10 69 if (conf.online_detect != NULL) {
meillo@10 70 if (strcmp(conf.online_detect, "file") == 0) {
meillo@10 71 DEBUG(3) debugf("online detection method 'file'\n");
meillo@10 72 if (conf.online_file != NULL) {
meillo@10 73 struct stat st;
meillo@10 74 if (stat(conf.online_file, &st) == 0) {
meillo@10 75 FILE *fptr = fopen(conf.online_file, "r");
meillo@10 76 if (fptr) {
meillo@10 77 char buf[256];
meillo@10 78 fgets(buf, 256, fptr);
meillo@10 79 g_strchomp(buf);
meillo@10 80 fclose(fptr);
meillo@10 81 return g_strdup(buf);
meillo@10 82 } else {
meillo@10 83 logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno));
meillo@10 84 return NULL;
meillo@10 85 }
meillo@10 86 } else if (errno == ENOENT) {
meillo@10 87 logwrite(LOG_NOTICE, "not online.\n");
meillo@10 88 return NULL;
meillo@10 89 } else {
meillo@10 90 logwrite(LOG_ALERT, "stat of %s failed: %s", conf.online_file, strerror(errno));
meillo@10 91 return NULL;
meillo@10 92 }
meillo@10 93 } else
meillo@10 94 logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n");
meillo@10 95 #ifdef ENABLE_MSERVER
meillo@10 96 } else if (strcmp(conf.online_detect, "mserver") == 0) {
meillo@10 97 DEBUG(3) debugf("connection method 'mserver'\n");
meillo@10 98 return mserver_detect_online(conf.mserver_iface);
meillo@10 99 #endif
meillo@10 100 } else if (strcmp(conf.online_detect, "pipe") == 0) {
meillo@10 101 DEBUG(3) debugf("connection method 'pipe'\n");
meillo@10 102 if (conf.online_pipe)
meillo@10 103 return detect_online_pipe(conf.online_pipe);
meillo@10 104 else {
meillo@10 105 logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n");
meillo@10 106 return NULL;
meillo@10 107 }
meillo@10 108 } else if (strcmp(conf.online_detect, "argument") == 0) {
meillo@10 109 return connection_name;
meillo@10 110 } else {
meillo@10 111 DEBUG(3) debugf("no connection method selected\n");
meillo@10 112 }
meillo@0 113 }
meillo@0 114 return NULL;
meillo@0 115 }