Skip to content

Commit

Permalink
lib-imap-client: add callback to notify consumers about state changes
Browse files Browse the repository at this point in the history
Add a callback to notify imapc users about failures.  Currently, the only
failure defined is "authentication failed".
  • Loading branch information
Josef 'Jeff' Sipek authored and GitLab committed Feb 15, 2017
1 parent adb27e4 commit e883e44
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/lib-imap-client/imapc-client-private.h
Expand Up @@ -20,6 +20,9 @@ struct imapc_client {
imapc_untagged_callback_t *untagged_callback;
void *untagged_context;

imapc_state_change_callback_t *state_change_callback;
void *state_change_context;

ARRAY(struct imapc_client_connection *) conns;

struct ioloop *ioloop;
Expand Down
11 changes: 11 additions & 0 deletions src/lib-imap-client/imapc-client.c
Expand Up @@ -468,3 +468,14 @@ int imapc_client_create_temp_fd(struct imapc_client *client,
*path_r = str_c(path);
return fd;
}

void imapc_client_register_state_change_callback(struct imapc_client *client,
imapc_state_change_callback_t *cb,
void *context)
{
i_assert(client->state_change_callback == NULL);
i_assert(client->state_change_context == NULL);

client->state_change_callback = cb;
client->state_change_context = context;
}
11 changes: 11 additions & 0 deletions src/lib-imap-client/imapc-client.h
Expand Up @@ -146,12 +146,19 @@ struct imapc_untagged_reply {
void *untagged_box_context;
};

enum imapc_state_change_event {
IMAPC_STATE_CHANGE_AUTH_FAILED,
};

/* Called when tagged reply is received for command. */
typedef void imapc_command_callback_t(const struct imapc_command_reply *reply,
void *context);
/* Called each time untagged input is received. */
typedef void imapc_untagged_callback_t(const struct imapc_untagged_reply *reply,
void *context);
typedef void imapc_state_change_callback_t(void *context,
enum imapc_state_change_event event,
const char *error);

struct imapc_client *
imapc_client_init(const struct imapc_client_settings *set);
Expand Down Expand Up @@ -208,4 +215,8 @@ imapc_client_get_capabilities(struct imapc_client *client);
int imapc_client_create_temp_fd(struct imapc_client *client,
const char **path_r);

void imapc_client_register_state_change_callback(struct imapc_client *client,
imapc_state_change_callback_t *cb,
void *context);

#endif
7 changes: 7 additions & 0 deletions src/lib-imap-client/imapc-connection.c
Expand Up @@ -146,6 +146,13 @@ imapc_auth_failed(struct imapc_connection *conn,
const char *error)
{
i_error("imapc(%s): Authentication failed: %s", conn->name, error);

if (conn->client->state_change_callback == NULL)
return;

conn->client->state_change_callback(conn->client->state_change_context,
IMAPC_STATE_CHANGE_AUTH_FAILED,
error);
}

struct imapc_connection *
Expand Down

0 comments on commit e883e44

Please sign in to comment.