diff --git a/Makefile.in b/Makefile.in index f2abddb..6e37204 100644 --- a/Makefile.in +++ b/Makefile.in @@ -17,8 +17,7 @@ LINK_FLAGS=${LDFLAGS} ${LOCAL_LDFLAGS} HEADERS= ${srcdir}/constants.h ${srcdir}/include.h ${srcdir}/interface.h ${srcdir}/LICENSE.h ${srcdir}/loop.h ${srcdir}/mtproto-client.h ${srcdir}/mtproto-common.h ${srcdir}/net.h ${srcdir}/no-preview.h ${srcdir}/queries.h ${srcdir}/structures.h ${srcdir}/telegram.h ${srcdir}/tree.h ${srcdir}/config.h ${srcdir}/binlog.h ${srcdir}/tools.h ${srcdir}/lua-tg.h ${srcdir}/msglog.h INCLUDE=-I. -I${srcdir} CC=@CC@ -OBJECTS=main.o loop.o interface.o net.o mtproto-common.o mtproto-client.o queries.o structures.o binlog.o tools.o lua-tg.o msglog.o tg-cli.o - +OBJECTS=main.o loop.o interface.o net.o mtproto-common.o mtproto-client.o queries.o structures.o binlog.o tools.o lua-tg.o msglog.o telegram.o .SUFFIXES: .SUFFIXES: .c .h .o @@ -107,7 +106,7 @@ uninstall: $(PRPL_LIBNAME) .PHONY: run run: install - pidgin -d | grep 'telegram\|plugin' + pidgin -d | grep 'telegram\|plugin\|proxy' clean: rm -rf *.so *.a *.o telegram config.log config.status $(PRPL_C_OBJS) $(PRPL_LIBNAME) > /dev/null || echo "all clean" diff --git a/purple-plugin/telegram-purple.c b/purple-plugin/telegram-purple.c index 6502299..f4f9ac3 100644 --- a/purple-plugin/telegram-purple.c +++ b/purple-plugin/telegram-purple.c @@ -44,7 +44,7 @@ #include "util.h" // Telegram Includes -#include +#include "telegram.h" #include "msglog.h" #include "mtproto-client.h" #include "mtproto-common.h" @@ -90,7 +90,7 @@ void chat_allocated_handler(peer_t *chat); */ static const char *tgprpl_list_icon(PurpleAccount * acct, PurpleBuddy * buddy) { - purple_debug_info(PLUGIN_ID, "tgrpl_list_icon()\n"); + //purple_debug_info(PLUGIN_ID, "tgrpl_list_icon()\n"); return "telegram"; } @@ -145,7 +145,7 @@ static void tgprpl_login(PurpleAccount * acct) const char *username = purple_account_get_username(acct); const char *code = purple_account_get_string(acct, "verification_key", NULL); - const char *hash = purple_account_get_string(acct, "verification_hash", NULL); + const char *hash = purple_account_get_string(acct, "verification_hash", NULL); const char *hostname = purple_account_get_string(acct, "server", TELEGRAM_TEST_SERVER); // const char *verificationType = purple_account_get_string(acct, "verification_type", TELEGRAM_AUTH_MODE_SMS); // int port = purple_account_get_int(acct, "port", TELEGRAM_DEFAULT_PORT); @@ -304,8 +304,11 @@ static void tgprpl_close(PurpleConnection * gc) static int tgprpl_send_im(PurpleConnection * gc, const char *who, const char *message, PurpleMessageFlags flags) { purple_debug_info(PLUGIN_ID, "tgprpl_send_im()\n"); - - return -1; + PurpleBuddy *b = purple_find_buddy(_pa, who); + peer_id_t *peer = purple_buddy_get_protocol_data(b); + do_send_message(*peer, message, strlen(message)); + // TODO: error handling + return 1; } /** diff --git a/structures.c b/structures.c index d7465bf..78b75e8 100644 --- a/structures.c +++ b/structures.c @@ -33,8 +33,6 @@ #include "queries.h" #include "binlog.h" -#include "tg-cli.h" - #define sha1 SHA1 static int id_cmp (struct message *M1, struct message *M2); diff --git a/tg-cli.c b/telegram.c similarity index 59% rename from tg-cli.c rename to telegram.c index b73eff8..0fdb88d 100755 --- a/tg-cli.c +++ b/telegram.c @@ -1,6 +1,6 @@ #include #include "mtproto-common.h" -#include "tg-cli.h" +#include "telegram.h" #include "msglog.h" /* @@ -37,23 +37,39 @@ void event_user_allocated(peer_t *user) { * Chat allocated */ void (*on_chat_allocated_handler)(peer_t *chat); -void on_chat_allocated(void (*handler)(peer_t *chat)) { +void on_chat_allocated(void (*handler)(peer_t *chat)) +{ on_chat_allocated_handler = handler; } -void event_chat_allocated(peer_t *chat) { +void event_chat_allocated(peer_t *chat) +{ if (on_chat_allocated_handler) { on_chat_allocated_handler(chat); } } +/* + * Callback to create proxy connections + */ +void (*proxy_connection_source)(const char *host, int port, void (*on_connection_created)(int fd)) = NULL; +void *proxy_connection_data = NULL; +void set_proxy_connection_source (void (*connection_source)(const char *host, int port, + void (*on_connection_created)(int fd)), void* data) +{ + proxy_connection_source = connection_source; + proxy_connection_data = data; +} + // template //void (*on_blarg_handler)(type); -//void on_blarg(void (*handler)(type)) { +//void on_blarg(void (*handler)(type)) +//{ // on_blarg_handler = handler; //} -//void event_blarg(type) { -// if (on_blarg_handler) { +//void event_blarg(type) +//{ +// if (on_blarg_handler) +// { // on_blarg_handler(type); // } //} - diff --git a/telegram.h b/telegram.h index d72da12..69882c4 100644 --- a/telegram.h +++ b/telegram.h @@ -1,24 +1,189 @@ -/* - This file is part of telegram-client. +/* + * libtelegram + * =========== + * + * Telegram library based on the telegram cli application by vysheng (see https://github.com/vysheng/tg) + */ - Telegram-client is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 2 of the License, or - (at your option) any later version. - - Telegram-client is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this telegram-client. If not, see . - - Copyright Vitaly Valtman 2013 -*/ #define MAX_DC_NUM 9 #define MAX_PEER_NUM 100000 #ifndef PROG_NAME #define PROG_NAME "telegram" #endif + +#include +#include "net.h" +#include "mtproto-common.h" +#include "structures.h" + + +/** + * A telegram session + * + * Contains all globals from the telegram-cli application and should + * be passed to + */ +struct telegram { + + /* + * Read and save the configuration files into this directory + * + * Every distinct account needs its own configuration directory, that + * will be used to store encryption data and the protocol state for this + * specific user + */ + char *config_dir; + + /* + * Reserved for custom protocol data + */ + void *protocol_data; + + /* + * Events and Callbacks + */ + // TODO: Insert definitions for all function pointers for event and logging + + /* + * Internal protocol state + */ + // TODO: Insert *all* global variables from the telegram-implementation + +}; + +/* + * Constructor + */ +void telegram_create( /* struct telegram, struct configuration config */ ); + // TODO: Initiate a new telegram instance + +/* + * Destructor + */ +void telegram_destroy(struct telegram instance); + // TODO: Write clean-up functions to free all allocated resources of this session + +// Export functions for plugins + +int tg_login (); + +void running_for_first_time (); +void parse_config (); +void store_config (); +void read_auth_file (); + +/** + * Connect to the telegram network with the given configuration + */ +void network_connect(); + +/** + * Request a registration code + */ +char* network_request_registration(); + +/** + * Verify the registration with the given registration code + */ +int network_verify_registration(const char *code, const char *sms_hash); + +/** + * Verify the registration with the given registration code + */ +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 on_update_new_message(void (*on_msg)(struct message *M)); +void event_update_new_message(struct message *M); + +void on_update_user_typing(); +void on_update_chat_user_typing(); +void on_update_user_status(); +void on_update_user_name(); +void on_update_user_photo(); +void on_update_chat_participants(); + +/* + * Load known users and chats on connect + */ +void on_user_allocated(); + +void on_user_allocated(void (*handler)(peer_t *user)); +void event_user_allocated(peer_t *user); + +void on_chat_allocated(void (*handler)(peer_t *chat)); +void event_chat_allocated(peer_t *chat); + +// template +//void on_blarg(void (*on_msg)(struct message *M)); +//void event_blarg(struct message *M); + +void on_chat_allocated(); + +/** + * Set a function to use as a handle to read from a network resource + * instead of the regular socket read function + */ +void set_net_read_cb(int (*cb)(int fd, void *buff, size_t size)); + +/** + * Set a function to use as handle to write to a newtork resource + * instead of the regular socket write function + */ +void set_net_write_cb(int (*cb)(int fd, const void *buff, size_t size)); + +/** + * The current proxy connection source. + */ +extern void (*proxy_connection_source)(const char *host, int port, void (*on_connection_created)(int fd)); + +/** + * The connection data passed to the connection source. + */ +extern void *proxy_connection_data; + +/** + * Set an alternative connection_source which is used to create open connections instead of the + * regular function. + * + * @param connection_source Called when a new connection is needed. A connection source must accept + * host and port and pass a valid file descriptor to an open TCP-Socket to the + * callback function on_connection_created + * @param data Additional connection data, that will be passed to the callback and may be + * needed for establishing the connection. + */ +void set_proxy_connection_source (void (*connection_source)(const char *host, int port, void (*on_connection_created)(int fd)), void* data); + +/** + * ? + */ +void set_default_username (); diff --git a/tg-cli.h b/tg-cli.h deleted file mode 100755 index bb2ddf8..0000000 --- a/tg-cli.h +++ /dev/null @@ -1,115 +0,0 @@ -#include -#include "net.h" -#include "mtproto-common.h" -#include "structures.h" - -// Export functions for plugins - -int tg_login (); - -void running_for_first_time (); -void parse_config (); -void store_config (); -void read_auth_file (); - -/** - * Connect to the telegram network with the given configuration - */ -void network_connect (); - -/** - * Request a registration code - */ -char* network_request_registration(); - -/** - * Verify the registration with the given registration code - */ -int network_verify_registration(const char *code, const char *sms_hash); - -/** - * Verify the registration with the given registration code - */ -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 on_update_new_message(void (*on_msg)(struct message *M)); -void event_update_new_message(struct message *M); - -void on_update_user_typing(); -void on_update_chat_user_typing(); -void on_update_user_status(); -void on_update_user_name(); -void on_update_user_photo(); -void on_update_chat_participants(); - -/* - * Load known users and chats on connect - */ -void on_user_allocated(); - -void on_user_allocated(void (*handler)(peer_t *user)); -void event_user_allocated(peer_t *user); - -void on_chat_allocated(void (*handler)(peer_t *chat)); -void event_chat_allocated(peer_t *chat); - -// template -//void on_blarg(void (*on_msg)(struct message *M)); -//void event_blarg(struct message *M); - -void on_chat_allocated(); - -/** - * Set a function to use as a handle to read from a network resource - * instead of the regular socket read function - */ -void set_net_read_cb(int (*cb)(int fd, void *buff, size_t size)); - -/** - * Set a function to use as handle to write to a newtork resource - * instead of the regular socket write function - */ -void set_net_write_cb(int (*cb)(int fd, const void *buff, size_t size)); - -/** - * Set a function to use as handle to create new connections instead of the regular - * socket write function - */ -void set_dc_ensure_session_cb (void (*dc_ens_sess)(struct dc *DC, void (*on_session_ready)(void))); - -/** - * ? - */ -void set_default_username (); -// Settings -#define AUTH_MODE_SMS "sms" -#define AUTH_MODE_PHONE "phone"