comparison src/online.c @ 279:1aa107c6b1e5

moved some code around
author markus schnalke <meillo@marmaro.de>
date Mon, 06 Dec 2010 17:46:24 -0300
parents fc1c6425c024
children f10a56dc7481
comparison
equal deleted inserted replaced
278:c35c59a36a2a 279:1aa107c6b1e5
1 /* MasqMail 1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth 2 Copyright (C) 1999-2001 Oliver Kurth
3 Copyright (C) 2008 markus schnalke <meillo@marmaro.de> 3 Copyright (C) 2008, 2010 markus schnalke <meillo@marmaro.de>
4 4
5 This program is free software; you can redistribute it and/or modify 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 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 7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 8 (at your option) any later version.
27 27
28 void 28 void
29 set_online_name(gchar * name) 29 set_online_name(gchar * name)
30 { 30 {
31 connection_name = g_strdup(name); 31 connection_name = g_strdup(name);
32 }
33
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];
41
42 err = stat(conf.online_file, &st);
43
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 }
52
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);
32 } 70 }
33 71
34 static gchar* 72 static gchar*
35 detect_online_pipe(const gchar * pipe) 73 detect_online_pipe(const gchar * pipe)
36 { 74 {
75 } 113 }
76 114
77 gchar* 115 gchar*
78 detect_online() 116 detect_online()
79 { 117 {
80 if (conf.online_detect == NULL) { 118 if (!conf.online_detect) {
81 return NULL; 119 return NULL;
82 } 120 }
83 121
84 if (strcmp(conf.online_detect, "file") == 0) { 122 if (strcmp(conf.online_detect, "file") == 0) {
85 DEBUG(3) debugf("online detection method 'file'\n"); 123 DEBUG(3) debugf("online detection method 'file'\n");
86 if (conf.online_file == NULL) { 124 if (!conf.online_file) {
87 logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n"); 125 logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n");
88 return NULL; 126 return NULL;
89 } 127 }
90 128 return detect_online_file(conf.online_file);
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 }
118 129
119 } else if (strcmp(conf.online_detect, "pipe") == 0) { 130 } else if (strcmp(conf.online_detect, "pipe") == 0) {
120 DEBUG(3) debugf("connection method 'pipe'\n"); 131 DEBUG(3) debugf("connection method 'pipe'\n");
121 if (conf.online_pipe) 132 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"); 133 logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n");
125 return NULL; 134 return NULL;
126 } 135 }
136 return detect_online_pipe(conf.online_pipe);
137
127 } else if (strcmp(conf.online_detect, "argument") == 0) { 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() */
128 return connection_name; 141 return connection_name;
129 } else { 142
130 DEBUG(3) debugf("no connection method selected\n");
131 } 143 }
132 144
145 DEBUG(3) debugf("unknown online detection method `%s'\n", conf.online_detect);
133 return NULL; 146 return NULL;
134 } 147 }