email-json: factorize the code

pull/1667/head
Eric Leblond 10 years ago
parent a7ef0c05ba
commit 4c0f8803e7

@ -83,6 +83,7 @@ static json_t* JsonEmailJsonArrayFromCommaList(const uint8_t *val, size_t len)
char *savep = NULL; char *savep = NULL;
char *p; char *p;
char *to_line = BytesToString((uint8_t *)val, len); char *to_line = BytesToString((uint8_t *)val, len);
if (likely(to_line != NULL)) {
p = strtok_r(to_line, ",", &savep); p = strtok_r(to_line, ",", &savep);
json_array_append_new(ajs, json_string(p)); json_array_append_new(ajs, json_string(p));
while ((p = strtok_r(NULL, ",", &savep)) != NULL) { while ((p = strtok_r(NULL, ",", &savep)) != NULL) {
@ -90,6 +91,7 @@ static json_t* JsonEmailJsonArrayFromCommaList(const uint8_t *val, size_t len)
} }
SCFree(to_line); SCFree(to_line);
} }
}
return ajs; return ajs;
} }
@ -224,52 +226,20 @@ json_t *JsonEmailLogJsonData(const Flow *f, void *state, void *vtx, uint64_t tx_
} }
/* To: */ /* To: */
char *to_line = NULL;
field = MimeDecFindField(entity, "to"); field = MimeDecFindField(entity, "to");
if (field != NULL) { if (field != NULL) {
json_t *js_to = json_array(); json_t *ajs = JsonEmailJsonArrayFromCommaList(field->value, field->value_len);
if (likely(js_to != NULL)) { if (ajs) {
to_line = BytesToString((uint8_t *)field->value, json_object_set_new(sjs, "to", ajs);
(size_t)field->value_len);
if (likely(to_line != NULL)) {
char *savep = NULL;
char *p;
//printf("to_line:: TO: \"%s\" (%d)\n", to_line, strlen(to_line));
p = strtok_r(to_line, ",", &savep);
//printf("got another addr: \"%s\"\n", p);
json_array_append_new(js_to, json_string(p));
while ((p = strtok_r(NULL, ",", &savep)) != NULL) {
//printf("got another addr: \"%s\"\n", p);
json_array_append_new(js_to, json_string(&p[strspn(p, " ")]));
}
SCFree(to_line);
}
json_object_set_new(sjs, "to", js_to);
} }
} }
/* Cc: */ /* Cc: */
char *cc_line = NULL;
field = MimeDecFindField(entity, "cc"); field = MimeDecFindField(entity, "cc");
if (field != NULL) { if (field != NULL) {
json_t *js_cc = json_array(); json_t *ajs = JsonEmailJsonArrayFromCommaList(field->value, field->value_len);
if (likely(js_cc != NULL)) { if (ajs) {
cc_line = BytesToString((uint8_t *)field->value, json_object_set_new(sjs, "cc", ajs);
(size_t)field->value_len);
if (likely(cc_line != NULL)) {
char *savep = NULL;
char *p;
//printf("cc_line:: CC: \"%s\" (%d)\n", to_line, strlen(to_line));
p = strtok_r(cc_line, ",", &savep);
//printf("got another addr: \"%s\"\n", p);
json_array_append_new(js_cc, json_string(p));
while ((p = strtok_r(NULL, ",", &savep)) != NULL) {
//printf("got another addr: \"%s\"\n", p);
json_array_append_new(js_cc, json_string(&p[strspn(p, " ")]));
}
SCFree(cc_line);
}
json_object_set_new(sjs, "cc", js_cc);
} }
} }

Loading…
Cancel
Save