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