masqmail

annotate src/online.c @ 2:653aef34b225

removed distribution specific data
author meillo@marmaro.de
date Fri, 26 Sep 2008 21:26:36 +0200
parents
children 26e34ae9a3e3
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@0 21 #include "masqmail.h"
meillo@0 22 #include "mserver.h"
meillo@0 23 #include "peopen.h"
meillo@0 24
meillo@0 25 gchar *connection_name;
meillo@0 26
meillo@0 27 void set_online_name(gchar *name)
meillo@0 28 {
meillo@0 29 connection_name = g_strdup(name);
meillo@0 30 }
meillo@0 31
meillo@0 32 static
meillo@0 33 gchar *detect_online_pipe(const gchar *pipe)
meillo@0 34 {
meillo@0 35 pid_t pid;
meillo@0 36 void (*old_signal)(int);
meillo@0 37 int status;
meillo@0 38 FILE *in;
meillo@0 39 gchar *name = NULL;
meillo@0 40
meillo@0 41 old_signal = signal(SIGCHLD, SIG_DFL);
meillo@0 42
meillo@0 43 in = peopen(pipe, "r", environ, &pid);
meillo@0 44 if(in != NULL){
meillo@0 45 gchar output[256];
meillo@0 46 if(fgets(output, 255, in)){
meillo@0 47 g_strchomp(output);
meillo@0 48 name = g_strdup(output);
meillo@0 49 }
meillo@0 50 fclose(in);
meillo@0 51 waitpid(pid, &status, 0);
meillo@0 52 if(WEXITSTATUS(status) != EXIT_SUCCESS){
meillo@0 53 g_free(name);
meillo@0 54 name = NULL;
meillo@0 55 }
meillo@0 56 }else
meillo@0 57 logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno));
meillo@0 58
meillo@0 59 signal(SIGCHLD, old_signal);
meillo@0 60
meillo@0 61 return name;
meillo@0 62 }
meillo@0 63
meillo@0 64 gchar *detect_online()
meillo@0 65 {
meillo@0 66 if(conf.online_detect != NULL){
meillo@0 67 if(strcmp(conf.online_detect, "file") == 0){
meillo@0 68 DEBUG(3) debugf("online detection method 'file'\n");
meillo@0 69 if(conf.online_file != NULL){
meillo@0 70 struct stat st;
meillo@0 71 if(stat(conf.online_file, &st) == 0){
meillo@0 72 FILE *fptr = fopen(conf.online_file, "r");
meillo@0 73 if(fptr){
meillo@0 74 char buf[256];
meillo@0 75 fgets(buf, 256, fptr);
meillo@0 76 g_strchomp(buf);
meillo@0 77 fclose(fptr);
meillo@0 78 return g_strdup(buf);
meillo@0 79 }else{
meillo@0 80 logwrite(LOG_ALERT, "opening of %s failed: %s\n",
meillo@0 81 conf.online_file, strerror(errno));
meillo@0 82 return NULL;
meillo@0 83 }
meillo@0 84 }
meillo@0 85 else if(errno == ENOENT){
meillo@0 86 logwrite(LOG_NOTICE, "not online.\n");
meillo@0 87 return NULL;
meillo@0 88 }else{
meillo@0 89 logwrite(LOG_ALERT, "stat of %s failed: %s",
meillo@0 90 conf.online_file, strerror(errno));
meillo@0 91 return NULL;
meillo@0 92 }
meillo@0 93 }else
meillo@0 94 logwrite(LOG_ALERT,
meillo@0 95 "online detection mode is 'file', "
meillo@0 96 "but online_file is undefined\n");
meillo@0 97 #ifdef ENABLE_MSERVER
meillo@0 98 }else if(strcmp(conf.online_detect, "mserver") == 0){
meillo@0 99 DEBUG(3) debugf("connection method 'mserver'\n");
meillo@0 100 return mserver_detect_online(conf.mserver_iface);
meillo@0 101 #endif
meillo@0 102 }else if(strcmp(conf.online_detect, "pipe") == 0){
meillo@0 103 DEBUG(3) debugf("connection method 'pipe'\n");
meillo@0 104 if(conf.online_pipe)
meillo@0 105 return detect_online_pipe(conf.online_pipe);
meillo@0 106 else{
meillo@0 107 logwrite(LOG_ALERT,
meillo@0 108 "online detection mode is 'pipe', "
meillo@0 109 "but online_pipe is undefined\n");
meillo@0 110 return NULL;
meillo@0 111 }
meillo@0 112 }else if(strcmp(conf.online_detect, "argument") == 0){
meillo@0 113 return connection_name;
meillo@0 114 }else{
meillo@0 115 DEBUG(3) debugf("no connection method selected\n");
meillo@0 116 }
meillo@0 117 }
meillo@0 118 return NULL;
meillo@0 119 }