diff options
Diffstat (limited to 'ssh/tomsg_clientlib.h')
-rw-r--r-- | ssh/tomsg_clientlib.h | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/ssh/tomsg_clientlib.h b/ssh/tomsg_clientlib.h new file mode 100644 index 0000000..f560728 --- /dev/null +++ b/ssh/tomsg_clientlib.h @@ -0,0 +1,168 @@ +#pragma once + +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + + +struct tomsg_client; + +enum tomsg_retval { + // Successful result + TOMSG_OK = 0, + + // Error codes + TOMSG_ERR_CONNECT, // Server refused connection + TOMSG_ERR_CLOSED, // Server connection unexpectedly closed + TOMSG_ERR_VERSION, // Server protocol version incompatible + TOMSG_ERR_TRANSPORT, // Error in the underlying SSH transport + TOMSG_ERR_SPACE, // Argument contained space or LF, while it could not + TOMSG_ERR_AGAIN, // (tomsg_next_event) no events for now, poll(2) and try again + TOMSG_ERR_PARSE, // (tomsg_next_event) could not parse line from server, line ignored + TOMSG_ERR_MEMORY, // Error allocating memory +}; + +const char* tomsg_strerror(enum tomsg_retval code); + +enum tomsg_retval tomsg_connect( + const char *hostname, int port, + struct tomsg_client **client // output +); + +// Will also free the client structure +void tomsg_close(struct tomsg_client *client); + +int tomsg_poll_fd(const struct tomsg_client *client); + +enum tomsg_event_type { + // Data in union fields indicated (but check 'error' first) + TOMSG_EV_REGISTER, // login + TOMSG_EV_LOGIN, // login + TOMSG_EV_LOGOUT, // - + TOMSG_EV_LIST_ROOMS, // list_rooms + TOMSG_EV_LIST_MEMBERS, // list_members + TOMSG_EV_CREATE_ROOM, // create_room + TOMSG_EV_INVITE, // join + TOMSG_EV_SEND, // send + TOMSG_EV_HISTORY, // history + TOMSG_EV_PING, // - + TOMSG_EV_IS_ONLINE, // is_online + TOMSG_EV_USER_ACTIVE, // user_active + TOMSG_EV_PUSH_ONLINE, // is_online + TOMSG_EV_PUSH_MESSAGE, // push_message + TOMSG_EV_PUSH_INVITE, // push_invite + TOMSG_EV_PUSH_JOIN, // join +}; + +struct history_message { + char *username; + int64_t timestamp; + int64_t msgid; + char *message; +}; + +struct tomsg_event { + enum tomsg_event_type type; + // If the server returned an error, 'error' is non-NULL; otherwise it is + // NULL, and the union fields describe the server's response. + // Actually, if 'error' is non-NULL, the union fields that can be + // filled with request data will already be filled. + char *error; + + union { + struct { + char *username; + } login; + struct { + int64_t count; + char **rooms; + } list_rooms; + struct { + char *room_name; + int64_t count; + char **members; + } list_members; + struct { + char *room_name; + } create_room; + struct { + char *room_name; + char *username; + } join; + struct { + int64_t tag; // the tag of the send request, returned by tomsg_send() + int64_t msgid; + } send; + struct { + char *room_name; + int64_t count; + struct history_message *messages; + } history; + struct { + char *username; + int64_t online_count; + } is_online; + struct { + bool active; + } user_active; + struct { + char *room_name; + struct history_message message; + } push_message; + struct { + char *room_name; + char *inviter; + } push_invite; + }; +}; + +void tomsg_event_nullify(struct tomsg_event event); + +// Will return TOMSG_ERR_AGAIN if no events are available at present. In that +// case, use poll(2) on the file descriptor from tomsg_poll_fd(). +// NOTE: when calling this function, you must call it as long as it gives +// TOMSG_OK, because there might be multiple messages in one batch. +enum tomsg_retval tomsg_next_event( + struct tomsg_client *client, + struct tomsg_event *event // output +); + +enum tomsg_retval tomsg_register( + struct tomsg_client *client, + const char *username, const char *password +); + +enum tomsg_retval tomsg_login( + struct tomsg_client *client, + const char *username, const char *password +); + +enum tomsg_retval tomsg_logout(struct tomsg_client *client); + +enum tomsg_retval tomsg_list_rooms(struct tomsg_client *client); + +enum tomsg_retval tomsg_list_members(struct tomsg_client *client, const char *room_name); + +enum tomsg_retval tomsg_create_room(struct tomsg_client *client); + +enum tomsg_retval tomsg_invite( + struct tomsg_client *client, const char *room_name, const char *username); + +// If 'tag' is not NULL, will write the message request tag to the referenced +// location. This tag will also be given in the TOMSG_EV_SEND response, so that +// the response can be linked to the original message. +enum tomsg_retval tomsg_send( + struct tomsg_client *client, const char *room_name, const char *message, + int64_t *tag // output +); + +// pass -1 to 'before_msgid' to return the latest 'count' +enum tomsg_retval tomsg_history( + struct tomsg_client *client, const char *room_name, + int64_t count, int64_t before_msgid); + +enum tomsg_retval tomsg_ping(struct tomsg_client *client); + +enum tomsg_retval tomsg_is_online(struct tomsg_client *client, const char *username); + +enum tomsg_retval tomsg_user_active(struct tomsg_client *client, bool active); |