masqmail-0.2

diff src/online.c @ 0:08114f7dcc23

this is masqmail-0.2.21 from oliver kurth
author meillo@marmaro.de
date Fri, 26 Sep 2008 17:05:23 +0200
parents
children 26e34ae9a3e3
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/online.c	Fri Sep 26 17:05:23 2008 +0200
     1.3 @@ -0,0 +1,119 @@
     1.4 +/*  MasqMail
     1.5 +    Copyright (C) 1999-2001 Oliver Kurth
     1.6 +
     1.7 +    This program is free software; you can redistribute it and/or modify
     1.8 +    it under the terms of the GNU General Public License as published by
     1.9 +    the Free Software Foundation; either version 2 of the License, or
    1.10 +    (at your option) any later version.
    1.11 +
    1.12 +    This program is distributed in the hope that it will be useful,
    1.13 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 +    GNU General Public License for more details.
    1.16 +
    1.17 +    You should have received a copy of the GNU General Public License
    1.18 +    along with this program; if not, write to the Free Software
    1.19 +    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    1.20 +*/
    1.21 +
    1.22 +#include <sys/stat.h>
    1.23 +#include <sys/wait.h>
    1.24 +#include "masqmail.h"
    1.25 +#include "mserver.h"
    1.26 +#include "peopen.h"
    1.27 +
    1.28 +gchar *connection_name;
    1.29 +
    1.30 +void set_online_name(gchar *name)
    1.31 +{
    1.32 +  connection_name = g_strdup(name);
    1.33 +}
    1.34 +
    1.35 +static
    1.36 +gchar *detect_online_pipe(const gchar *pipe)
    1.37 +{
    1.38 +  pid_t pid;
    1.39 +  void (*old_signal)(int);
    1.40 +  int status;
    1.41 +  FILE *in;
    1.42 +  gchar *name = NULL;
    1.43 +
    1.44 +  old_signal = signal(SIGCHLD, SIG_DFL);
    1.45 +
    1.46 +  in = peopen(pipe, "r", environ, &pid);
    1.47 +  if(in != NULL){
    1.48 +    gchar output[256];
    1.49 +    if(fgets(output, 255, in)){
    1.50 +      g_strchomp(output);
    1.51 +      name = g_strdup(output);
    1.52 +    }
    1.53 +    fclose(in);
    1.54 +    waitpid(pid, &status, 0);
    1.55 +    if(WEXITSTATUS(status) != EXIT_SUCCESS){
    1.56 +      g_free(name);
    1.57 +      name = NULL;
    1.58 +    }
    1.59 +  }else
    1.60 +    logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno));
    1.61 +
    1.62 +  signal(SIGCHLD, old_signal);
    1.63 +
    1.64 +  return name;
    1.65 +}
    1.66 +
    1.67 +gchar *detect_online()
    1.68 +{
    1.69 +  if(conf.online_detect != NULL){
    1.70 +    if(strcmp(conf.online_detect, "file") == 0){
    1.71 +      DEBUG(3) debugf("online detection method 'file'\n");
    1.72 +      if(conf.online_file != NULL){
    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 +	    char buf[256];
    1.78 +	    fgets(buf, 256, fptr);
    1.79 +	    g_strchomp(buf);
    1.80 +	    fclose(fptr);
    1.81 +	    return g_strdup(buf);
    1.82 +	  }else{
    1.83 +	    logwrite(LOG_ALERT, "opening of %s failed: %s\n",
    1.84 +		     conf.online_file, strerror(errno));
    1.85 +	    return NULL;
    1.86 +	  }
    1.87 +	}
    1.88 +	else if(errno == ENOENT){
    1.89 +	  logwrite(LOG_NOTICE, "not online.\n");
    1.90 +	  return NULL;
    1.91 +	}else{
    1.92 +	  logwrite(LOG_ALERT, "stat of %s failed: %s",
    1.93 +		   conf.online_file, strerror(errno));
    1.94 +	  return NULL;
    1.95 +	}
    1.96 +      }else
    1.97 +	logwrite(LOG_ALERT,
    1.98 +		 "online detection mode is 'file', "
    1.99 +		 "but online_file is undefined\n");
   1.100 +#ifdef ENABLE_MSERVER
   1.101 +    }else if(strcmp(conf.online_detect, "mserver") == 0){
   1.102 +      DEBUG(3) debugf("connection method 'mserver'\n");
   1.103 +      return mserver_detect_online(conf.mserver_iface);
   1.104 +#endif
   1.105 +    }else if(strcmp(conf.online_detect, "pipe") == 0){
   1.106 +      DEBUG(3) debugf("connection method 'pipe'\n");
   1.107 +      if(conf.online_pipe)
   1.108 +	return detect_online_pipe(conf.online_pipe);
   1.109 +      else{
   1.110 +	logwrite(LOG_ALERT,
   1.111 +		 "online detection mode is 'pipe', "
   1.112 +		 "but online_pipe is undefined\n");
   1.113 +	return NULL;
   1.114 +      }
   1.115 +    }else if(strcmp(conf.online_detect, "argument") == 0){
   1.116 +      return connection_name;
   1.117 +    }else{
   1.118 +      DEBUG(3) debugf("no connection method selected\n");
   1.119 +    }
   1.120 +  }
   1.121 +  return NULL;
   1.122 +}