masqmail-0.2

view src/online.c @ 11:24872a9fe6e1

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