masqmail
view src/online.c @ 291:95d536599fd7
updated ChangeLog, NEWS and date/version in the man pages
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Wed, 08 Dec 2010 18:06:44 -0300 |
parents | fc1c6425c024 |
children | f10a56dc7481 |
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
3 Copyright (C) 2008, 2010 markus schnalke <meillo@marmaro.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
20 #include <sys/stat.h>
21 #include <sys/wait.h>
23 #include "masqmail.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_file(const gchar* file)
36 {
37 struct stat st;
38 int err;
39 FILE *fptr;
40 char buf[256];
42 err = stat(conf.online_file, &st);
44 if (err) {
45 if (errno==ENOENT) {
46 logwrite(LOG_NOTICE, "not online.\n");
47 return NULL;
48 }
49 logwrite(LOG_ALERT, "stat of %s failed: %s\n", conf.online_file, strerror(errno));
50 return NULL;
51 }
53 fptr = fopen(conf.online_file, "r");
54 if (!fptr) {
55 logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno));
56 return NULL;
57 }
58 if (fgets(buf, 256, fptr) == NULL) {
59 logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file);
60 fclose(fptr);
61 return NULL;
62 }
63 g_strstrip(buf); /* strip whitespace */
64 fclose(fptr);
65 if (strlen(buf) == 0) {
66 logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file);
67 return NULL;
68 }
69 return g_strdup(buf);
70 }
72 static gchar*
73 detect_online_pipe(const gchar * pipe)
74 {
75 pid_t pid;
76 void (*old_signal) (int);
77 int status;
78 FILE *in;
79 gchar *name = NULL;
81 old_signal = signal(SIGCHLD, SIG_DFL);
83 in = peopen(pipe, "r", environ, &pid);
84 if (in == NULL) {
85 logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno));
86 signal(SIGCHLD, old_signal);
87 return NULL;
88 }
90 gchar output[256];
91 if (fgets(output, 255, in)) {
92 g_strchomp(g_strchug(output));
93 if (strlen(output) == 0) {
94 logwrite(LOG_ALERT, "only whitespace connection name\n");
95 name = NULL;
96 } else {
97 name = g_strdup(output);
98 }
99 } else {
100 logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe);
101 name = NULL;
102 }
103 fclose(in);
104 waitpid(pid, &status, 0);
105 if (WEXITSTATUS(status) != 0) {
106 g_free(name);
107 name = NULL;
108 }
110 signal(SIGCHLD, old_signal);
112 return name;
113 }
115 gchar*
116 detect_online()
117 {
118 if (!conf.online_detect) {
119 return NULL;
120 }
122 if (strcmp(conf.online_detect, "file") == 0) {
123 DEBUG(3) debugf("online detection method 'file'\n");
124 if (!conf.online_file) {
125 logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n");
126 return NULL;
127 }
128 return detect_online_file(conf.online_file);
130 } else if (strcmp(conf.online_detect, "pipe") == 0) {
131 DEBUG(3) debugf("connection method 'pipe'\n");
132 if (!conf.online_pipe) {
133 logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n");
134 return NULL;
135 }
136 return detect_online_pipe(conf.online_pipe);
138 } else if (strcmp(conf.online_detect, "argument") == 0) {
139 DEBUG(3) debugf("online route literally defined\n");
140 /* use the name set with set_online_name() */
141 return connection_name;
143 }
145 DEBUG(3) debugf("unknown online detection method `%s'\n", conf.online_detect);
146 return NULL;
147 }