meillo@0: /* MasqMail meillo@0: Copyright (C) 1999-2001 Oliver Kurth meillo@19: Copyright 2008 markus schnalke meillo@0: meillo@0: This program is free software; you can redistribute it and/or modify meillo@0: it under the terms of the GNU General Public License as published by meillo@0: the Free Software Foundation; either version 2 of the License, or meillo@0: (at your option) any later version. meillo@0: meillo@0: This program is distributed in the hope that it will be useful, meillo@0: but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@0: GNU General Public License for more details. meillo@0: meillo@0: You should have received a copy of the GNU General Public License meillo@0: along with this program; if not, write to the Free Software meillo@0: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. meillo@0: */ meillo@0: meillo@0: #include meillo@0: #include meillo@15: meillo@0: #include "masqmail.h" meillo@0: #include "mserver.h" meillo@0: #include "peopen.h" meillo@0: meillo@0: gchar *connection_name; meillo@0: meillo@10: void meillo@10: set_online_name(gchar * name) meillo@0: { meillo@10: connection_name = g_strdup(name); meillo@0: } meillo@0: meillo@10: static gchar* meillo@10: detect_online_pipe(const gchar * pipe) meillo@0: { meillo@10: pid_t pid; meillo@10: void (*old_signal) (int); meillo@10: int status; meillo@10: FILE *in; meillo@10: gchar *name = NULL; meillo@0: meillo@10: old_signal = signal(SIGCHLD, SIG_DFL); meillo@0: meillo@10: in = peopen(pipe, "r", environ, &pid); meillo@33: if (in == NULL) { meillo@33: logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno)); meillo@33: signal(SIGCHLD, old_signal); meillo@33: return NULL; meillo@33: } meillo@33: meillo@33: gchar output[256]; meillo@33: if (fgets(output, 255, in)) { meillo@33: g_strchomp(g_strchug(output)); meillo@33: if (strlen(output) == 0) { meillo@33: logwrite(LOG_ALERT, "only whitespace connection name\n"); meillo@33: name = NULL; meillo@18: } else { meillo@33: name = g_strdup(output); meillo@10: } meillo@33: } else { meillo@33: logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe); meillo@33: name = NULL; meillo@33: } meillo@33: fclose(in); meillo@33: waitpid(pid, &status, 0); meillo@33: if (WEXITSTATUS(status) != EXIT_SUCCESS) { meillo@33: g_free(name); meillo@33: name = NULL; meillo@33: } meillo@0: meillo@10: signal(SIGCHLD, old_signal); meillo@0: meillo@10: return name; meillo@0: } meillo@0: meillo@10: gchar* meillo@10: detect_online() meillo@0: { meillo@33: if (conf.online_detect == NULL) { meillo@33: return NULL; meillo@33: } meillo@33: meillo@33: if (strcmp(conf.online_detect, "file") == 0) { meillo@33: DEBUG(3) debugf("online detection method 'file'\n"); meillo@33: if (conf.online_file != NULL) { meillo@33: logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n"); meillo@33: return NULL; meillo@33: } meillo@33: meillo@33: struct stat st; meillo@33: if (stat(conf.online_file, &st) == 0) { meillo@33: FILE *fptr = fopen(conf.online_file, "r"); meillo@33: if (!fptr) { meillo@33: logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno)); meillo@10: return NULL; meillo@10: } meillo@33: char buf[256]; meillo@33: if (fgets(buf, 256, fptr) == NULL) { meillo@33: logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file); meillo@33: fclose(fptr); meillo@33: return NULL; meillo@33: } meillo@33: g_strchomp(g_strchug(buf)); meillo@33: fclose(fptr); meillo@33: if (strlen(buf) == 0) { meillo@33: logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file); meillo@33: return NULL; meillo@33: } meillo@33: return g_strdup(buf); meillo@33: } else if (errno == ENOENT) { meillo@33: logwrite(LOG_NOTICE, "not online.\n"); meillo@33: return NULL; meillo@10: } else { meillo@33: logwrite(LOG_ALERT, "stat of %s failed: %s", conf.online_file, strerror(errno)); meillo@33: return NULL; meillo@10: } meillo@33: meillo@33: #ifdef ENABLE_MSERVER meillo@33: } else if (strcmp(conf.online_detect, "mserver") == 0) { meillo@33: DEBUG(3) debugf("connection method 'mserver'\n"); meillo@33: return mserver_detect_online(conf.mserver_iface); meillo@33: #endif meillo@33: } else if (strcmp(conf.online_detect, "pipe") == 0) { meillo@33: DEBUG(3) debugf("connection method 'pipe'\n"); meillo@33: if (conf.online_pipe) meillo@33: return detect_online_pipe(conf.online_pipe); meillo@33: else { meillo@33: logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n"); meillo@33: return NULL; meillo@33: } meillo@33: } else if (strcmp(conf.online_detect, "argument") == 0) { meillo@33: return connection_name; meillo@33: } else { meillo@33: DEBUG(3) debugf("no connection method selected\n"); meillo@0: } meillo@33: meillo@0: return NULL; meillo@0: }