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