diff options
Diffstat (limited to 'store.c')
-rw-r--r-- | store.c | 28 |
1 files changed, 22 insertions, 6 deletions
@@ -11,12 +11,14 @@ #include <stdlib.h> #include <string.h> #include <errno.h> +#include <assert.h> /* Temp object between message callback and link callback */ struct say { long long msg_id; long long chat_id; char *text; + void (*cb)(bool); }; static void cb_link(bool successful, struct TdObject *result, struct TdError *error, void *cb_arg) { @@ -26,7 +28,7 @@ static void cb_link(bool successful, struct TdObject *result, struct TdError *er s->chat_id, s->msg_id, error ? error->message_ : "NULL", error ? error->code_ : 0); - goto cleanup; + goto f; } LOGDV("%s", s->text); struct TdMessageLink *link = (struct TdMessageLink *) result; @@ -40,7 +42,7 @@ static void cb_link(bool successful, struct TdObject *result, struct TdError *er goto sql_err; sql_err: LOGEV("Cannot insert: %s.", sqlite3_errstr(r)); - goto cleanup; + goto f; } if ((r = sqlite3_bind_text(stmt, 1, s->text, (int) strlen(s->text), NULL))) { goto sql_err; @@ -55,7 +57,15 @@ static void cb_link(bool successful, struct TdObject *result, struct TdError *er sqlite3_finalize(stmt); if (r != SQLITE_DONE && r != SQLITE_CONSTRAINT) { goto sql_err; + } else if (r == SQLITE_CONSTRAINT) { + goto f; + } else { + s->cb(true); + goto cleanup; } + assert(false); + f: + s->cb(false); goto cleanup; cleanup: free(s->text); @@ -95,23 +105,29 @@ static bool filter(struct TdMessage *msg) { return true; } -void store(struct TdMessage *msg) { - if (!filter(msg)) return; +void store(struct TdMessage *msg, void (*cb)(bool)) { + if (!filter(msg)) { + goto f; + f: + cb(false); + return; + } struct TdMessageText *text = (struct TdMessageText *) msg->content_; const char *t = text->text_->text_; struct say *s; if (!(s = malloc(sizeof(struct say)))) { LOGEV("Cannot allocate memory: %s.", strerror(errno)); - return; + goto f; } memset(s, 0, sizeof(struct say)); s->chat_id = msg->chat_id_; s->msg_id = msg->id_; + s->cb = cb; /* I'm just too lazy to manually free in each callback, so just strdup. */ if (!(s->text = strdup(t))) { LOGEV("Cannot allocate memory: %s.", strerror(errno)); free(s); - return; + goto f; } td_send(TdCreateObjectGetMessageLink(msg->chat_id_, msg->id_, |