Improve event handling-system

This commit is contained in:
mjentsch 2014-10-06 22:57:20 +02:00
parent 290a3251e6
commit 2f7f2068de
5 changed files with 216 additions and 129 deletions

View file

@ -938,9 +938,9 @@ void work_update (struct mtproto_connection *self, long long msg_id UU) {
{
peer_id_t chat_id = MK_CHAT (fetch_int (self));
peer_id_t id = MK_USER (fetch_int (self));
peer_t *C UU = user_chat_get (bl, chat_id);
peer_t *U UU = user_chat_get (bl, id);
event_update_user_typing(tg, U);
peer_t *C = user_chat_get (bl, chat_id);
peer_t *U = user_chat_get (bl, id);
event_update_chat_user_typing(tg, C, U, 0);
if (log_level >= 2) {
//print_start ();
//push_color (COLOR_YELLOW);
@ -1004,6 +1004,7 @@ void work_update (struct mtproto_connection *self, long long msg_id UU) {
fetch_skip_str (self);
fetch_skip_str (self);
}
event_update_user_name (tg, UC);
}
break;
case CODE_update_user_photo:
@ -1050,6 +1051,7 @@ void work_update (struct mtproto_connection *self, long long msg_id UU) {
fetch_file_location (self, &t);
}
}
event_update_user_photo(tg, UC);
fetch_bool (self);
}
break;
@ -1125,12 +1127,15 @@ void work_update (struct mtproto_connection *self, long long msg_id UU) {
}
//pop_color ();
//print_end ();
if (C) {
event_update_chat_participants(tg, C);
}
}
break;
case CODE_update_contact_registered:
{
peer_id_t user_id = MK_USER (fetch_int (self));
peer_t *U UU = user_chat_get (bl, user_id);
peer_t *U = user_chat_get (bl, user_id);
fetch_int (self); // date
//print_start ();
//push_color (COLOR_YELLOW);
@ -1140,6 +1145,7 @@ void work_update (struct mtproto_connection *self, long long msg_id UU) {
printf (" registered\n");
//pop_color ();
//print_end ();
event_update_user_registered(tg, U);
}
break;
case CODE_update_contact_link:
@ -1193,6 +1199,7 @@ void work_update (struct mtproto_connection *self, long long msg_id UU) {
s, location);
//pop_color ();
//print_end ();
event_update_auth_new(tg, location);
tfree_str (s);
tfree_str (location);
}
@ -1333,6 +1340,7 @@ void work_update (struct mtproto_connection *self, long long msg_id UU) {
printf ("\n");
//pop_color ();
//print_end ();
event_update_chat_add_participant(tg, C, user_id, inviter_id);
}
break;
case CODE_update_chat_participant_delete:
@ -1356,6 +1364,7 @@ void work_update (struct mtproto_connection *self, long long msg_id UU) {
printf (" deleted\n");
//pop_color ();
//print_end ();
event_update_chat_del_participant(tg, C, user_id, 0);
}
break;
case CODE_update_dc_options:

View file

@ -401,10 +401,53 @@ void tgprpl_login_on_connected(gpointer *data, gint fd, const gchar *error_messa
conn->mtp = telegram_add_proxy(tg, req, fd, conn);
}
void telegram_on_disconnected (struct telegram *tg)
void telegram_on_error (struct telegram *tg, const char *err)
{
debug ("telegram_on_disconnected()\n");
assert (0);
debug ("telegram_on_error()\n");
telegram_conn *conn = tg->extra;
purple_connection_error_reason(conn->gc, PURPLE_CONNECTION_ERROR_OTHER_ERROR,
err);
}
void on_update_user_name (struct telegram *tg, peer_t *user)
{
}
void on_update_user_photo (struct telegram *tg, peer_t *user)
{
}
void on_update_user_registered (struct telegram *tg, peer_t *user)
{
}
void on_update_chat_participants (struct telegram *tg, peer_t *chat)
{
}
void on_update_chat_add_participant (struct telegram *tg, peer_t *chat, peer_id_t user, peer_id_t inviter)
{
}
void on_update_chat_del_participant (struct telegram *tg, peer_t *chat, peer_id_t user, void *_ UU)
{
}
void on_update_chat_user_typing (struct telegram *tg, peer_t *chat, peer_t *user, void *_ UU)
{
}
void on_update_auth_new (struct telegram *tg, char *location)
{
}
struct telegram_config tgconf = {
@ -415,15 +458,29 @@ struct telegram_config tgconf = {
telegram_on_phone_registration,
telegram_on_client_registration,
telegram_on_ready,
telegram_on_disconnected,
telegram_on_error,
message_allocated_handler,
// user
peer_allocated_handler,
user_info_received_handler,
download_finished_handler,
on_new_user_status,
on_user_typing,
on_update_user_name,
on_update_user_photo,
on_update_user_registered,
// chat
on_update_chat_participants,
on_update_chat_add_participant,
on_update_chat_del_participant,
on_update_chat_user_typing,
// misc
on_update_auth_new,
message_allocated_handler,
download_finished_handler,
user_info_received_handler,
on_chat_joined
};

View file

@ -1865,7 +1865,7 @@ void end_load (struct telegram *instance, struct download *D) {
//update_prompt ();
close (D->fd);
debug ("Done: %s\n", D->name);
event_download_finished_handler(instance, D);
event_download_finished (instance, D);
instance->dl_curr = 0;
if (D->dc != telegram_get_working_dc(instance)->id) {
debug ("%d Not the working dc %d, closing...\n", D->dc,

View file

@ -15,29 +15,8 @@
#include "loop.h"
/*
* New message received
*/
void event_update_new_message(struct telegram *instance, struct message *M)
{
if (instance->config->on_msg_handler) {
instance->config->on_msg_handler (instance, M);
}
}
/*
* Peer allocated
*/
void event_peer_allocated(struct telegram *instance, void *peer)
{
if (instance->config->on_peer_allocated_handler) {
instance->config->on_peer_allocated_handler (instance, peer);
}
}
/*
* Peer user fetched full
* Events
*/
void event_user_info_received_handler(struct telegram *instance, struct tgl_user *peer, int show_info)
{
@ -46,42 +25,28 @@ void event_user_info_received_handler(struct telegram *instance, struct tgl_user
}
}
/*
* Download finished
*/
void event_download_finished_handler(struct telegram *instance, struct download *D)
{
if (instance->config->on_download_finished_handler) {
instance->config->on_download_finished_handler (instance, D);
}
}
DEFINE_EVENT_HANDLER (peer_allocated, void);
DEFINE_EVENT_HANDLER (update_user_status, void);
DEFINE_EVENT_HANDLER (update_user_typing, void);
/*
* User status changed
*/
void event_update_user_status (struct telegram *instance, void *peer)
{
if (instance->config->on_update_user_status_handler) {
instance->config->on_update_user_status_handler (instance, peer);
}
}
DEFINE_EVENT_HANDLER (update_user_name, peer_t);
DEFINE_EVENT_HANDLER (update_user_photo, peer_t);
DEFINE_EVENT_HANDLER (update_user_registered, peer_t);
/*
* User typing changed
*/
void event_update_user_typing (struct telegram *instance, void *peer)
{
if (instance->config->on_update_uesr_typing_handler) {
instance->config->on_update_uesr_typing_handler (instance, peer);
}
}
DEFINE_EVENT_HANDLER (update_chat_participants, peer_t);
DEFINE_EVENT_HANDLER_3(update_chat_add_participant, peer_t *, peer_id_t, peer_id_t);
DEFINE_EVENT_HANDLER_3(update_chat_del_participant, peer_t *, peer_id_t, void *);
DEFINE_EVENT_HANDLER_3(update_chat_user_typing, peer_t *, peer_t *, void *);
DEFINE_EVENT_HANDLER (update_auth_new, char);
DEFINE_EVENT_HANDLER (update_new_message, struct message);
DEFINE_EVENT_HANDLER (download_finished, struct download);
/**
* Calculate the configuration path for the given config file and the given instance
*
* @returns The full path to the configuration.
* @returns The full path to the configuration.
*
* NOTE: the returned string must be freed manually using gfree
*/
@ -93,7 +58,8 @@ char *telegram_get_config(struct telegram *instance, char *config)
/**
* Return whether the current client is registered.
*/
int telegram_is_registered(struct telegram *tg) {
int telegram_is_registered(struct telegram *tg)
{
return telegram_get_working_dc(tg)->has_auth;
}

View file

@ -109,6 +109,46 @@ struct proxy_request {
struct telegram;
struct download;
/*
* Events 1 arg
*/
#define DEFINE_EVENT_LISTENER(E_NAME, D_TYPE) void (*on_ ## E_NAME) (struct telegram *tg, D_TYPE *data);
#define DECLARE_EVENT_HANDLER(E_NAME, D_TYPE) \
void event_ ## E_NAME (struct telegram *tg, D_TYPE *data)
#define DEFINE_EVENT_HANDLER(E_NAME, D_TYPE) \
void event_ ## E_NAME (struct telegram *tg, D_TYPE *data) \
{ \
if (tg->config->on_ ## E_NAME) { \
tg->config->on_ ## E_NAME (tg, data); \
} else { \
warning ("Trying to execute non-existing event listener %s\n", "E_NAME"); \
} \
}
/*
* Events 3 args
*/
#define DEFINE_EVENT_LISTENER_3(E_NAME, D_TYPE, D_TYPE2, D_TYPE3) void (*on_ ## E_NAME) (struct telegram *tg, D_TYPE data, D_TYPE2 data2, D_TYPE3 data3);
#define DECLARE_EVENT_HANDLER_3(E_NAME, D_TYPE, D_TYPE2, D_TYPE3) \
void event_ ## E_NAME (struct telegram *tg, D_TYPE data, D_TYPE2 data2, D_TYPE3 data3)
#define DEFINE_EVENT_HANDLER_3(E_NAME, D_TYPE, D_TYPE2, D_TYPE3) \
void event_ ## E_NAME (struct telegram *tg, D_TYPE data, D_TYPE2 data2, D_TYPE3 data3) \
{ \
if (tg->config->on_ ## E_NAME) { \
tg->config->on_ ## E_NAME (tg, data, data2, data3); \
} else { \
warning ("Trying to execute non-existing event listener %s\n", "E_NAME"); \
} \
}
/**
* Contains all options and pointer to callback functions required by telegram
*/
@ -161,48 +201,105 @@ struct telegram_config {
/**
* A callback function that is called when telegram is disconnected
*/
void (*on_disconnected) (struct telegram *instance);
/**
* A callback function that is called when a new message was allocated. This is useful
* for adding new messages to the GUI.
*/
void (*on_msg_handler) (struct telegram *instance, struct message *M);
void (*on_error) (struct telegram *instance, const char *err);
/**
* A callback function that is called when a new peer was allocated. This is useful
* for populating the GUI with new peers.
*/
void (*on_peer_allocated_handler) (struct telegram *instance, void *peer);
DEFINE_EVENT_LISTENER(peer_allocated, void);
/**
* A callback function that is called when a user's status has changed
*/
DEFINE_EVENT_LISTENER(update_user_status, void);
/**
* A callback function that is called when a user starts or stops typing
*/
DEFINE_EVENT_LISTENER(update_user_typing, void);
DEFINE_EVENT_LISTENER(update_user_name, peer_t);
DEFINE_EVENT_LISTENER(update_user_photo, peer_t);
DEFINE_EVENT_LISTENER(update_user_registered, peer_t);
DEFINE_EVENT_LISTENER(update_chat_participants, peer_t);
/**
* A new user is added to a chat
*
* @param data1 The chat
* @param data2 The added user
* @param data3 The inviter
*/
DEFINE_EVENT_LISTENER_3(update_chat_add_participant, peer_t *, peer_id_t, peer_id_t);
/**
* A user is deleted from a chat
*
* @param data1 The chat
* @param data2 The added user
* @param data3 NULL
*/
DEFINE_EVENT_LISTENER_3(update_chat_del_participant, peer_t *, peer_id_t, void *);
/**
* A user in a chat is typing
*
* @param data1 The chat
* @param data2 The user
* @param data3 NULL
*/
DEFINE_EVENT_LISTENER_3(update_chat_user_typing, peer_t *, peer_t *, void *);
/**
* A new device was registered for @location
*
* @param tg
* @param location
*/
DEFINE_EVENT_LISTENER(update_auth_new, char);
/**
* A callback function that is called when a new message was allocated. This is useful
* for adding new messages to the GUI.
*/
DEFINE_EVENT_LISTENER(update_new_message, struct message);
/**
* A callback function that is called when a download is completed. This is useful
* for populating the GUI with new user photos.
*/
DEFINE_EVENT_LISTENER(download_finished, struct download);
/**
* A callback function that is called when a peer user info was received. This is useful
* for populating the GUI with new user photos.
*/
void (*on_user_info_received_handler) (struct telegram *instance, struct tgl_user *peer, int showInfo);
/**
* A callback function that is called when a download is completed. This is useful
* for populating the GUI with new user photos.
*/
void (*on_download_finished_handler) (struct telegram *instance, struct download *D);
/**
* A callback function that is called when a user's status has changed
*/
void (*on_update_user_status_handler) (struct telegram *instance, void *peer);
/**
* A callback function that is called when a user starts or stops typing
*/
void (*on_update_uesr_typing_handler) (struct telegram *instance, void *peer);
/**
* A callback function that is called when chat info is received
*/
void (*on_chat_info_received) (struct telegram *instance, peer_id_t chatid);
};
DECLARE_EVENT_HANDLER (peer_allocated, void);
DECLARE_EVENT_HANDLER (update_user_status, void);
DECLARE_EVENT_HANDLER (update_user_typing, void);
DECLARE_EVENT_HANDLER (update_user_name, peer_t);
DECLARE_EVENT_HANDLER (update_user_photo, peer_t);
DECLARE_EVENT_HANDLER (update_user_registered, peer_t);
DECLARE_EVENT_HANDLER (update_chat_participants, peer_t);
DECLARE_EVENT_HANDLER_3 (update_chat_add_participant, peer_t *, peer_id_t, peer_id_t);
DECLARE_EVENT_HANDLER_3 (update_chat_del_participant, peer_t *, peer_id_t, void *);
DECLARE_EVENT_HANDLER_3 (update_chat_user_typing, peer_t *, peer_t *, void *);
DECLARE_EVENT_HANDLER (update_auth_new, char);
DECLARE_EVENT_HANDLER (update_new_message, struct message);
DECLARE_EVENT_HANDLER (download_finished, struct download);
#define MSG_STORE_SIZE 10000
/**
@ -362,53 +459,11 @@ int network_verify_registration(const char *code, const char *sms_hash);
int network_verify_phone_registration(const char *code, const char *sms_hash,
const char *first, const char *last);
/**
* Retur if the current phone is registered in the given network.
*/
int network_phone_is_registered();
/**
* Return if the current client is registered.
*/
int network_client_is_registered();
/**
* Export the current registration to all available data centers
*/
void network_export_registration();
/**
* Fetch all unknown messages of the current session
*/
void session_get_difference();
/**
* Fetch all known contacts
*/
void session_update_contact_list();
/*
* Events
*/
void event_update_new_message(struct telegram *instance, struct message *M);
void event_update_user_status(struct telegram *instance, void *peer);
void event_update_user_typing(struct telegram *instance, void *peer);
/*
* Load known users and chats on connect
*/
void event_peer_allocated(struct telegram *instance, void *peer);
/*
* Load known users and chats on connect
*/
void event_user_info_received_handler(struct telegram *instance, struct tgl_user *peer, int showInfo);
/*
* Load known users and chats on connect
*/
void event_download_finished_handler(struct telegram *instance, struct download *D);
/**
* Set the connection after a proxy_request_cb
*