diff --git a/Makefile.in b/Makefile.in index e710bd7..eb4650f 100644 --- a/Makefile.in +++ b/Makefile.in @@ -13,10 +13,10 @@ EXTRA_LIBS=@LIBS@ @EXTRA_LIBS@ LOCAL_LDFLAGS=-rdynamic -ggdb ${EXTRA_LIBS} 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 +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 +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 .SUFFIXES: diff --git a/interface.c b/interface.c index cd7635e..6af4dc0 100644 --- a/interface.c +++ b/interface.c @@ -49,6 +49,7 @@ // libpurple debugging-messages #include "debug.h" #include "purple-plugin/telegram-purple.h" +#include "msglog.h" #define ALLOW_MULT 1 char *default_prompt = "> "; @@ -1191,20 +1192,22 @@ void hexdump (int *in_ptr, int *in_end) { } void logprintf (const char *format, ...) { + va_list ap; + va_start (ap, format); + log_message(format, ap); + va_end (ap); + /* int x = 0; if (!prompt_was) { x = 1; print_start (); } printf (COLOR_GREY " *** "); - va_list ap; - va_start (ap, format); - vfprintf (stdout, format, ap); - va_end (ap); printf (COLOR_NORMAL); if (x) { print_end (); } + */ } int color_stack_pos; diff --git a/msglog.c b/msglog.c new file mode 100644 index 0000000..cbe1828 --- /dev/null +++ b/msglog.c @@ -0,0 +1,67 @@ +#include +#include + +#define COLOR_RED "\033[0;31m" +#define COLOR_REDB "\033[1;31m" +#define COLOR_NORMAL "\033[0m" +#define COLOR_GREEN "\033[32;1m" +#define COLOR_GREY "\033[37;1m" +#define COLOR_YELLOW "\033[33;1m" +#define COLOR_BLUE "\033[34;1m" +#define COLOR_MAGENTA "\033[35;1m" +#define COLOR_CYAN "\033[36;1m" +#define COLOR_LCYAN "\033[0;36m" + +#define COLOR_INVERSE "\033[7m" + +void log_printf(const char *format, ...) +{ + printf (COLOR_GREY " *** "); + va_list ap; + va_start (ap, format); + vfprintf (stdout, format, ap); + va_end (ap); + printf (COLOR_NORMAL); +} + +/** + * The callback that will log the given msg to the used target + */ +void (*log_cb)(const char*, ...) = log_printf; + +/** + * Set a custom logging callback to use instead of the regular + * printf to stdout + */ +void set_log_cb(void (*cb)(const char*, ...)) +{ + log_cb = cb; +} + +/** + * Log a message to the current message log + */ +void log_message(const char *format, ...) +{ + va_list ap; + va_start (ap, format); + log_cb(format, ap); + va_end (ap); +} + +/* +TODO: implement different log levels + +void log_debug(const char* format, ...) +{ +} + +void log_warning(const char* format, ...) +{ +} + +void log_error(const char* format, ...) +{ +} +*/ + diff --git a/msglog.h b/msglog.h new file mode 100644 index 0000000..07d74dc --- /dev/null +++ b/msglog.h @@ -0,0 +1,17 @@ +/** + * Set a custom logging callback to use instead of regular printing + * to stdout + */ +void set_log_cb(void (*cb)(const char*, ...)); + +/** + * Log a message to the current message log + */ +void log_message(const char *format, ...); + +/* +void log_debug(const char* format, ...); +void log_warning(const char* format, ...); +void log_error(const char* format, ...); +*/ + diff --git a/purple-plugin/Makefile b/purple-plugin/Makefile index a450ff2..87b5aca 100755 --- a/purple-plugin/Makefile +++ b/purple-plugin/Makefile @@ -4,7 +4,7 @@ EXTRA_LIBS = -lcrypto -lz -lm -lreadline LDFLAGSTG = -L/usr/local/lib CPPFLAGSTG = -I/usr/local/include -OBJECTSTG = ../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 +OBJECTSTG = ../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 INCLUDE = -I. -I.. # diff --git a/purple-plugin/telegram-purple.c b/purple-plugin/telegram-purple.c index f1ab3c5..4d73b4b 100644 --- a/purple-plugin/telegram-purple.c +++ b/purple-plugin/telegram-purple.c @@ -16,10 +16,13 @@ #include #include +#include + +// test // Libpurple Plugin Includes #include "notify.h" -#include "plugin.h" // NEEDED? +#include "plugin.h" #include "version.h" #include "account.h" #include "accountopt.h" @@ -37,13 +40,26 @@ // Telegram Includes #include +#include "msglog.h" // telegram-purple includes +#include "loop.h" #include "telegram-purple.h" - static PurplePlugin *_telegram_protocol = NULL; +/** + * Redirect the msglog of the telegram-cli application to the libpurple + * logger + */ +void tg_cli_log_cb(const char* format, ...) +{ + va_list ap; + va_start (ap, format); + purple_debug_info(PLUGIN_ID, format, ap); + va_end (ap); +} + /** * Returns the base icon name for the given buddy and account. * If buddy is NULL and the account is non-NULL, it will return the @@ -522,10 +538,13 @@ static PurplePluginProtocolInfo prpl_info = { static void tgprpl_init(PurplePlugin *plugin) { - PurpleAccountOption *option; + PurpleAccountOption *option; PurpleAccountUserSplit *split; GList *verification_values = NULL; + // intialise logging + set_log_cb(&tg_cli_log_cb); + // Required Verification-Key // split = purple_account_user_split_new("Verification key", NULL, '@'); // purple_account_user_split_set_reverse(split, FALSE);