Mercurial > masqmail
annotate src/online.c @ 21:4f4a57f9e8c1
Added tag 0.2.21-4 for changeset af25f5c39d90
author | meillo@marmaro.de |
---|---|
date | Thu, 04 Dec 2008 09:45:12 +0100 |
parents | 7354c2e0eb31 |
children | e1004fcc93c9 |
rev | line source |
---|---|
0 | 1 /* MasqMail |
2 Copyright (C) 1999-2001 Oliver Kurth | |
19
7354c2e0eb31
added myself to AUTHORS and the source files I improved
meillo@marmaro.de
parents:
18
diff
changeset
|
3 Copyright 2008 markus schnalke <meillo@marmaro.de> |
0 | 4 |
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. | |
9 | |
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. | |
14 | |
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 */ | |
19 | |
20 #include <sys/stat.h> | |
21 #include <sys/wait.h> | |
15 | 22 |
0 | 23 #include "masqmail.h" |
24 #include "mserver.h" | |
25 #include "peopen.h" | |
26 | |
27 gchar *connection_name; | |
28 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
29 void |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
30 set_online_name(gchar * name) |
0 | 31 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
32 connection_name = g_strdup(name); |
0 | 33 } |
34 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
35 static gchar* |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
36 detect_online_pipe(const gchar * pipe) |
0 | 37 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
38 pid_t pid; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
39 void (*old_signal) (int); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
40 int status; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
41 FILE *in; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
42 gchar *name = NULL; |
0 | 43 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
44 old_signal = signal(SIGCHLD, SIG_DFL); |
0 | 45 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
46 in = peopen(pipe, "r", environ, &pid); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
47 if (in != NULL) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
48 gchar output[256]; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
49 if (fgets(output, 255, in)) { |
18
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
50 g_strchomp(g_strchug(output)); |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
51 if (strlen(output) == 0) { |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
52 logwrite(LOG_ALERT, "only whitespace connection name\n"); |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
53 name = NULL; |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
54 } else { |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
55 name = g_strdup(output); |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
56 } |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
57 } else { |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
58 logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe); |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
59 name = NULL; |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
60 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
61 fclose(in); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
62 waitpid(pid, &status, 0); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
63 if (WEXITSTATUS(status) != EXIT_SUCCESS) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
64 g_free(name); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
65 name = NULL; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
66 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
67 } else |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
68 logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno)); |
0 | 69 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
70 signal(SIGCHLD, old_signal); |
0 | 71 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
72 return name; |
0 | 73 } |
74 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
75 gchar* |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
76 detect_online() |
0 | 77 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
78 if (conf.online_detect != NULL) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
79 if (strcmp(conf.online_detect, "file") == 0) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
80 DEBUG(3) debugf("online detection method 'file'\n"); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
81 if (conf.online_file != NULL) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
82 struct stat st; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
83 if (stat(conf.online_file, &st) == 0) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
84 FILE *fptr = fopen(conf.online_file, "r"); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
85 if (fptr) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
86 char buf[256]; |
18
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
87 if (fgets(buf, 256, fptr) == NULL) { |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
88 logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file); |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
89 fclose(fptr); |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
90 return NULL; |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
91 } |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
92 g_strchomp(g_strchug(buf)); |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
93 fclose(fptr); |
18
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
94 if (strlen(buf) == 0) { |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
95 logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file); |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
96 return NULL; |
99c09ed776c1
fixed empty or only-whitespace connection names
meillo@marmaro.de
parents:
15
diff
changeset
|
97 } |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
98 return g_strdup(buf); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
99 } else { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
100 logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno)); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
101 return NULL; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
102 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
103 } else if (errno == ENOENT) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
104 logwrite(LOG_NOTICE, "not online.\n"); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
105 return NULL; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
106 } else { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
107 logwrite(LOG_ALERT, "stat of %s failed: %s", conf.online_file, strerror(errno)); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
108 return NULL; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
109 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
110 } else |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
111 logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n"); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
112 #ifdef ENABLE_MSERVER |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
113 } else if (strcmp(conf.online_detect, "mserver") == 0) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
114 DEBUG(3) debugf("connection method 'mserver'\n"); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
115 return mserver_detect_online(conf.mserver_iface); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
116 #endif |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
117 } else if (strcmp(conf.online_detect, "pipe") == 0) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
118 DEBUG(3) debugf("connection method 'pipe'\n"); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
119 if (conf.online_pipe) |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
120 return detect_online_pipe(conf.online_pipe); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
121 else { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
122 logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n"); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
123 return NULL; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
124 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
125 } else if (strcmp(conf.online_detect, "argument") == 0) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
126 return connection_name; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
127 } else { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
128 DEBUG(3) debugf("no connection method selected\n"); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
129 } |
0 | 130 } |
131 return NULL; | |
132 } |