masqmail

view src/online.c @ 239:31ee44f45787

refactored alias.c heavily especially substituted the loop-based alias_expand() with a recursive approach. Now alias_expand() wraps alias_one() which recursively expands aliases. In principle the ``data processing'' is the same but now it's clearer structured and thus easier to understand IMO. The loop might have been faster but I don't care for speed -- the most simple solution is the best. It's fast enough, that is sufficient.
author markus schnalke <meillo@marmaro.de>
date Mon, 25 Oct 2010 15:35:28 -0300
parents 5b621742b2e7
children fc1c6425c024
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
3 Copyright (C) 2008 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_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 logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno));
48 signal(SIGCHLD, old_signal);
49 return NULL;
50 }
52 gchar output[256];
53 if (fgets(output, 255, in)) {
54 g_strchomp(g_strchug(output));
55 if (strlen(output) == 0) {
56 logwrite(LOG_ALERT, "only whitespace connection name\n");
57 name = NULL;
58 } else {
59 name = g_strdup(output);
60 }
61 } else {
62 logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe);
63 name = NULL;
64 }
65 fclose(in);
66 waitpid(pid, &status, 0);
67 if (WEXITSTATUS(status) != EXIT_SUCCESS) {
68 g_free(name);
69 name = NULL;
70 }
72 signal(SIGCHLD, old_signal);
74 return name;
75 }
77 gchar*
78 detect_online()
79 {
80 if (conf.online_detect == NULL) {
81 return NULL;
82 }
84 if (strcmp(conf.online_detect, "file") == 0) {
85 DEBUG(3) debugf("online detection method 'file'\n");
86 if (conf.online_file == NULL) {
87 logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n");
88 return NULL;
89 }
91 struct stat st;
92 if (stat(conf.online_file, &st) == 0) {
93 FILE *fptr = fopen(conf.online_file, "r");
94 if (!fptr) {
95 logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno));
96 return NULL;
97 }
98 char buf[256];
99 if (fgets(buf, 256, fptr) == NULL) {
100 logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file);
101 fclose(fptr);
102 return NULL;
103 }
104 g_strchomp(g_strchug(buf));
105 fclose(fptr);
106 if (strlen(buf) == 0) {
107 logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file);
108 return NULL;
109 }
110 return g_strdup(buf);
111 } else if (errno == ENOENT) {
112 logwrite(LOG_NOTICE, "not online.\n");
113 return NULL;
114 } else {
115 logwrite(LOG_ALERT, "stat of %s failed: %s\n", conf.online_file, strerror(errno));
116 return NULL;
117 }
119 } else if (strcmp(conf.online_detect, "pipe") == 0) {
120 DEBUG(3) debugf("connection method 'pipe'\n");
121 if (conf.online_pipe)
122 return detect_online_pipe(conf.online_pipe);
123 else {
124 logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n");
125 return NULL;
126 }
127 } else if (strcmp(conf.online_detect, "argument") == 0) {
128 return connection_name;
129 } else {
130 DEBUG(3) debugf("no connection method selected\n");
131 }
133 return NULL;
134 }