From 74a84bdd3a4497f22b079570a1715fe6ea5ff981 Mon Sep 17 00:00:00 2001 From: "roberto@quantal64" Date: Wed, 26 Sep 2012 19:55:58 +0200 Subject: [PATCH] added support for mail subject in alarm_curl plugin --- plugins/alarm_curl/alarm_curl_plugin.c | 65 +++++++++++++++++++++----- 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/plugins/alarm_curl/alarm_curl_plugin.c b/plugins/alarm_curl/alarm_curl_plugin.c index dee57c4d..4bd3b4bc 100644 --- a/plugins/alarm_curl/alarm_curl_plugin.c +++ b/plugins/alarm_curl/alarm_curl_plugin.c @@ -3,13 +3,21 @@ extern struct uwsgi_server uwsgi; +struct uwsgi_alarm_curl_config { + char *arg; + char *subject; + char *to; +}; + struct uwsgi_alarm_curl_opt { char *name; CURLoption option; - void (*func)(CURL *, CURLoption, char *); + void (*func)(CURL *, CURLoption, char *, struct uwsgi_alarm_curl_config*); }; -static void uwsgi_alarm_curl_append(CURL *curl, CURLoption option, char *arg) { + +static void uwsgi_alarm_curl_to(CURL *curl, CURLoption option, char *arg, struct uwsgi_alarm_curl_config *uacc) { + uacc->to = arg; struct curl_slist *list = NULL; char *items = uwsgi_str(arg); char *ctx = NULL; @@ -21,18 +29,23 @@ static void uwsgi_alarm_curl_append(CURL *curl, CURLoption option, char *arg) { curl_easy_setopt(curl, option, list); } -static void uwsgi_alarm_curl_ssl(CURL *curl, CURLoption option, char *arg) { +static void uwsgi_alarm_curl_ssl(CURL *curl, CURLoption option, char *arg, struct uwsgi_alarm_curl_config *uacc) { curl_easy_setopt(curl, option, (long)CURLUSESSL_ALL); } -static void uwsgi_alarm_curl_int(CURL *curl, CURLoption option, char *arg) { +static void uwsgi_alarm_curl_int(CURL *curl, CURLoption option, char *arg, struct uwsgi_alarm_curl_config *uacc) { curl_easy_setopt(curl, option, atoi(arg)); } +static void uwsgi_alarm_curl_set_subject(CURL *curl, CURLoption option, char *arg, struct uwsgi_alarm_curl_config *uacc) { + uacc->subject = arg; +} + static struct uwsgi_alarm_curl_opt uaco[] = { {"url", CURLOPT_URL, NULL}, - {"mail_to", CURLOPT_MAIL_RCPT, uwsgi_alarm_curl_append }, + {"mail_to", CURLOPT_MAIL_RCPT, uwsgi_alarm_curl_to }, {"mail_from", CURLOPT_MAIL_FROM, NULL}, + {"subject", 0, uwsgi_alarm_curl_set_subject}, {"ssl", CURLOPT_USE_SSL, uwsgi_alarm_curl_ssl}, {"auth_user", CURLOPT_USERNAME, NULL}, {"auth_pass", CURLOPT_PASSWORD, NULL}, @@ -41,7 +54,7 @@ static struct uwsgi_alarm_curl_opt uaco[] = { {NULL, 0, NULL}, }; -static void uwsgi_alarm_curl_setopt(CURL *curl, char *opt) { +static void uwsgi_alarm_curl_setopt(CURL *curl, char *opt, struct uwsgi_alarm_curl_config *uacc) { static int first = 0; struct uwsgi_alarm_curl_opt *o = uaco; char *equal = strchr(opt,'='); @@ -57,7 +70,7 @@ static void uwsgi_alarm_curl_setopt(CURL *curl, char *opt) { while(o->name) { if (!strcmp(o->name, opt)) { if (o->func) { - o->func(curl, o->option, equal+1); + o->func(curl, o->option, equal+1, uacc); } else { curl_easy_setopt(curl, o->option, equal+1); @@ -74,13 +87,38 @@ static size_t uwsgi_alarm_curl_read_callback(void *ptr, size_t size, size_t nmem struct uwsgi_thread *ut = (struct uwsgi_thread *) userp; size_t full_size = size * nmemb; size_t remains = ut->len - ut->pos; + struct uwsgi_alarm_curl_config *uacc = ut->data; if (remains == 0) return 0; if (ut->custom0 == 0) { - memcpy(ptr, "\n", 1); + size_t newline = 0; + size_t required = 1; + char *addr = ptr; + if (uacc->to) required += 4 + strlen(uacc->to) + 1; + if (uacc->subject) required += 9 + strlen(uacc->subject) + 1; + if (required > full_size) goto skip; + + + if (uacc->to) { + memcpy(addr, "To: ", 4); addr+=4; + memcpy(addr, uacc->to, strlen(uacc->to)); addr += strlen(uacc->to); + *addr ++= '\n'; + newline = 1; + } + + if (uacc->subject) { + memcpy(addr, "Subject: ", 9); addr+=9; + memcpy(addr, uacc->subject, strlen(uacc->subject)); addr += strlen(uacc->subject); + *addr ++= '\n'; + newline = 1; + } +skip: + if (newline > 0) { + *addr = '\n'; + } ut->custom0 = 1; - return 1; + return required; } if (full_size < remains) { @@ -106,13 +144,14 @@ void uwsgi_alarm_curl_loop(struct uwsgi_thread *ut) { curl_easy_setopt(curl, CURLOPT_READFUNCTION, uwsgi_alarm_curl_read_callback); curl_easy_setopt(curl, CURLOPT_READDATA, ut); - char *opts = uwsgi_str(ut->data); + struct uwsgi_alarm_curl_config *uacc = (struct uwsgi_alarm_curl_config *) ut->data; + char *opts = uwsgi_str(uacc->arg); // fill curl options char *ctx = NULL; char *p = strtok_r(opts, ";", &ctx); while(p) { - uwsgi_alarm_curl_setopt(curl, uwsgi_str(p)); + uwsgi_alarm_curl_setopt(curl, uwsgi_str(p), uacc); p = strtok_r(NULL, ";", &ctx); } @@ -137,7 +176,9 @@ static void uwsgi_alarm_curl_init(struct uwsgi_alarm_instance *uai) { struct uwsgi_thread *ut = uwsgi_thread_new(uwsgi_alarm_curl_loop); if (!ut) return; uai->data_ptr = ut; - ut->data = uai->arg; + struct uwsgi_alarm_curl_config *uacc = uwsgi_calloc(sizeof(struct uwsgi_alarm_curl_config)); + uacc->arg = uai->arg; + ut->data = uacc; } // pipe the message into the thread;