39 |
|
|
40 |
|
|
41 |
/* Private Prototypes */ |
/* Private Prototypes */ |
|
static void free_mail(Mail_T); |
|
42 |
static int count(char *, char *); |
static int count(char *, char *); |
43 |
static void copy_mail(Mail_T, Mail_T); |
static void copy_mail(Mail_T, Mail_T); |
44 |
|
static void replace(char **, char *, char *); |
45 |
static void smtp_alert(Process_T, int, char *, va_list); |
static void smtp_alert(Process_T, int, char *, va_list); |
46 |
static char *replace(char *, char *, char *); |
static void substitute(Mail_T*, char *name, char *event); |
|
static void substitute(Mail_T, char *name, char *event); |
|
47 |
|
|
48 |
/* Private Variables */ |
/* Private Variables */ |
49 |
static char desc[][STRLEN]= {"timed out", "restarted", "checksum error", |
static char desc[][STRLEN]= {"timed out", "restarted", "checksum error", |
157 |
/* ----------------------------------------------------------------- Private */ |
/* ----------------------------------------------------------------- Private */ |
158 |
|
|
159 |
|
|
160 |
/** |
/* |
161 |
* Send a smtp notification |
* Send a smtp notification |
162 |
*/ |
*/ |
163 |
static void smtp_alert(Process_T p, int event, char *optmsg, va_list ap) { |
static void smtp_alert(Process_T p, int event, char *optmsg, va_list ap) { |
191 |
|
|
192 |
copy_mail(tmp, m); |
copy_mail(tmp, m); |
193 |
|
|
194 |
if(optmsg) tmp->opt_message= format(optmsg, ap); |
if(optmsg) |
195 |
|
tmp->opt_message= format(optmsg, ap); |
196 |
|
|
197 |
substitute(tmp, p->name, desc[event]); |
substitute(&tmp, p->name, desc[event]); |
198 |
|
|
199 |
tmp->next= list; |
tmp->next= list; |
200 |
list= tmp; |
list= tmp; |
209 |
if(list) { |
if(list) { |
210 |
|
|
211 |
sendmail(list); |
sendmail(list); |
212 |
free_mail(list); |
gc_mail_list(&list); |
213 |
|
|
214 |
} |
} |
215 |
|
|
221 |
} |
} |
222 |
|
|
223 |
|
|
224 |
/** |
static void substitute(Mail_T *m, char *name, char *event) { |
|
* Substitute variables in the mail |
|
|
*/ |
|
|
static void substitute(Mail_T m, char *name, char *event) { |
|
225 |
|
|
226 |
char *now= get_ctime(); |
char *now= get_ctime(); |
227 |
char *host= get_localhostname(); |
char *host= get_localhostname(); |
228 |
|
|
229 |
m->from= replace(m->from, "$HOST", host); |
replace(&(*m)->from, "$HOST", host); |
230 |
m->subject= replace(m->subject, "$DATE", now); |
replace(&(*m)->subject, "$DATE", now); |
231 |
m->message= replace(m->message, "$DATE", now); |
replace(&(*m)->message, "$DATE", now); |
232 |
m->subject= replace(m->subject, "$HOST", host); |
replace(&(*m)->subject, "$HOST", host); |
233 |
m->message= replace(m->message, "$HOST", host); |
replace(&(*m)->message, "$HOST", host); |
234 |
m->subject= replace(m->subject, "$PROGRAM", name); |
replace(&(*m)->subject, "$PROGRAM", name); |
235 |
m->message= replace(m->message, "$PROGRAM", name); |
replace(&(*m)->message, "$PROGRAM", name); |
236 |
m->subject= replace(m->subject, "$EVENT", event); |
replace(&(*m)->subject, "$EVENT", event); |
237 |
m->message= replace(m->message, "$EVENT", event); |
replace(&(*m)->message, "$EVENT", event); |
238 |
|
|
239 |
free(now); |
free(now); |
240 |
free(host); |
free(host); |
242 |
} |
} |
243 |
|
|
244 |
|
|
245 |
/** |
static void replace(char **src, char *old, char *new) { |
|
* Replace all occurrences of old string with new string in src. |
|
|
*/ |
|
|
static char *replace(char *src, char *old, char *new) { |
|
246 |
|
|
247 |
int i= count(src, old); |
int i= count(*src, old); |
248 |
int d= strlen(new)-strlen(old); |
int d= strlen(new)-strlen(old); |
249 |
|
|
250 |
if(i==0) |
if(i==0) |
251 |
return src; |
return; |
252 |
if(d>0) |
if(d>0) |
253 |
d*= i; |
d*= i; |
254 |
else |
else |
257 |
{ |
{ |
258 |
char *p, *q; |
char *p, *q; |
259 |
int l= strlen(old); |
int l= strlen(old); |
260 |
char buf[strlen(src)+d+1]; |
char buf[strlen(*src)+d+1]; |
261 |
|
|
262 |
q= src; |
q= *src; |
263 |
*buf= '\0'; |
*buf= '\0'; |
264 |
|
|
265 |
while((p= strstr(q, old))) { |
while((p= strstr(q, old))) { |
273 |
} |
} |
274 |
|
|
275 |
strcat(buf, q); |
strcat(buf, q); |
276 |
free(src); |
free(*src); |
277 |
|
*src= xstrdup(buf); |
|
return xstrdup(buf); |
|
278 |
|
|
279 |
} |
} |
280 |
|
|
318 |
|
|
319 |
} |
} |
320 |
|
|
|
|
|
|
static void free_mail(Mail_T m) { |
|
|
|
|
|
if(m->next) |
|
|
free_mail(m->next); |
|
|
|
|
|
free(m->to); |
|
|
free(m->from); |
|
|
free(m->subject); |
|
|
free(m->message); |
|
|
free(m->opt_message); |
|
|
free(m); |
|
|
m= NULL; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|