annotate src/online.c @ 282:ba53e648906f

-q and -qo are non-exclusive -qo without arg is already included in -q (-qXXX is something completely different)
author markus schnalke <meillo@marmaro.de>
date Tue, 07 Dec 2010 14:07:25 -0300
parents 1aa107c6b1e5
children f10a56dc7481
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
1 /* MasqMail
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
2 Copyright (C) 1999-2001 Oliver Kurth
279
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
3 Copyright (C) 2008, 2010 markus schnalke <meillo@marmaro.de>
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
4
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
7 the Free Software Foundation; either version 2 of the License, or
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
8 (at your option) any later version.
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
9
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
13 GNU General Public License for more details.
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
14
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
16 along with this program; if not, write to the Free Software
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
18 */
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
19
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
20 #include <sys/stat.h>
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
21 #include <sys/wait.h>
15
f671821d8222 code beautifying; 0 -> \0 if appropriate
meillo@marmaro.de
parents: 10
diff changeset
22
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
23 #include "masqmail.h"
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
24 #include "peopen.h"
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
25
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
26 gchar *connection_name;
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
27
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
28 void
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
29 set_online_name(gchar * name)
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
30 {
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
31 connection_name = g_strdup(name);
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
32 }
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
33
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
34 static gchar*
279
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
35 detect_online_file(const gchar* file)
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
36 {
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
37 struct stat st;
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
38 int err;
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
39 FILE *fptr;
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
40 char buf[256];
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
41
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
42 err = stat(conf.online_file, &st);
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
43
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
44 if (err) {
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
45 if (errno==ENOENT) {
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
46 logwrite(LOG_NOTICE, "not online.\n");
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
47 return NULL;
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
48 }
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
49 logwrite(LOG_ALERT, "stat of %s failed: %s\n", conf.online_file, strerror(errno));
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
50 return NULL;
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
51 }
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
52
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
53 fptr = fopen(conf.online_file, "r");
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
54 if (!fptr) {
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
55 logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno));
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
56 return NULL;
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
57 }
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
58 if (fgets(buf, 256, fptr) == NULL) {
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
59 logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file);
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
60 fclose(fptr);
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
61 return NULL;
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
62 }
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
63 g_strstrip(buf); /* strip whitespace */
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
64 fclose(fptr);
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
65 if (strlen(buf) == 0) {
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
66 logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file);
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
67 return NULL;
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
68 }
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
69 return g_strdup(buf);
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
70 }
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
71
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
72 static gchar*
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
73 detect_online_pipe(const gchar * pipe)
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
74 {
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
75 pid_t pid;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
76 void (*old_signal) (int);
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
77 int status;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
78 FILE *in;
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
79 gchar *name = NULL;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
80
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
81 old_signal = signal(SIGCHLD, SIG_DFL);
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
82
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
83 in = peopen(pipe, "r", environ, &pid);
33
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
84 if (in == NULL) {
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
85 logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno));
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
86 signal(SIGCHLD, old_signal);
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
87 return NULL;
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
88 }
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
89
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
90 gchar output[256];
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
91 if (fgets(output, 255, in)) {
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
92 g_strchomp(g_strchug(output));
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
93 if (strlen(output) == 0) {
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
94 logwrite(LOG_ALERT, "only whitespace connection name\n");
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
95 name = NULL;
18
99c09ed776c1 fixed empty or only-whitespace connection names
meillo@marmaro.de
parents: 15
diff changeset
96 } else {
33
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
97 name = g_strdup(output);
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
98 }
33
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
99 } else {
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
100 logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe);
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
101 name = NULL;
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
102 }
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
103 fclose(in);
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
104 waitpid(pid, &status, 0);
262
fc1c6425c024 s/EXIT_SUCCESS/0/ && s/EXIT_FAILURE/1/
markus schnalke <meillo@marmaro.de>
parents: 208
diff changeset
105 if (WEXITSTATUS(status) != 0) {
33
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
106 g_free(name);
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
107 name = NULL;
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
108 }
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
109
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
110 signal(SIGCHLD, old_signal);
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
111
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
112 return name;
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
113 }
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
114
10
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
115 gchar*
26e34ae9a3e3 changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents: 0
diff changeset
116 detect_online()
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
117 {
279
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
118 if (!conf.online_detect) {
33
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
119 return NULL;
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
120 }
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
121
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
122 if (strcmp(conf.online_detect, "file") == 0) {
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
123 DEBUG(3) debugf("online detection method 'file'\n");
279
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
124 if (!conf.online_file) {
33
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
125 logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n");
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
126 return NULL;
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
127 }
279
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
128 return detect_online_file(conf.online_file);
33
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
129
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
130 } else if (strcmp(conf.online_detect, "pipe") == 0) {
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
131 DEBUG(3) debugf("connection method 'pipe'\n");
279
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
132 if (!conf.online_pipe) {
33
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
133 logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n");
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
134 return NULL;
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
135 }
279
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
136 return detect_online_pipe(conf.online_pipe);
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
137
33
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
138 } else if (strcmp(conf.online_detect, "argument") == 0) {
279
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
139 DEBUG(3) debugf("online route literally defined\n");
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
140 /* use the name set with set_online_name() */
33
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
141 return connection_name;
279
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
142
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
143 }
33
e1004fcc93c9 flattened conditional nesting
meillo@marmaro.de
parents: 19
diff changeset
144
279
1aa107c6b1e5 moved some code around
markus schnalke <meillo@marmaro.de>
parents: 262
diff changeset
145 DEBUG(3) debugf("unknown online detection method `%s'\n", conf.online_detect);
0
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
146 return NULL;
08114f7dcc23 this is masqmail-0.2.21 from oliver kurth
meillo@marmaro.de
parents:
diff changeset
147 }