diff --git a/telegram-base.c b/telegram-base.c index 50a721f..857f08c 100644 --- a/telegram-base.c +++ b/telegram-base.c @@ -95,7 +95,9 @@ int read_pubkey_file (const char *name, struct rsa_pubkey *dst) { return 0; } - if (n_len != read (pubkey_fd, n_raw, n_len)) { + gint readret; + readret = read (pubkey_fd, n_raw, n_len); + if (readret <= 0 || (n_len != (guint) readret)) { free (n_raw); close (pubkey_fd); return 0; @@ -110,9 +112,7 @@ int read_pubkey_file (const char *name, struct rsa_pubkey *dst) { void read_state_file (struct tgl_state *TLS) { char *name = 0; - if (asprintf (&name, "%s/%s", TLS->base_path, "state") < 0) { - return; - } + name = g_strdup_printf("%s/%s", TLS->base_path, "state"); int state_file_fd = open (name, O_CREAT | O_RDWR, 0600); free (name); @@ -148,9 +148,7 @@ void write_state_file (struct tgl_state *TLS) { wseq = TLS->seq; wpts = TLS->pts; wqts = TLS->qts; wdate = TLS->date; char *name = 0; - if (asprintf (&name, "%s/%s", TLS->base_path, "state") < 0) { - return; - } + name = g_strdup_printf("%s/%s", TLS->base_path, "state"); int state_file_fd = open (name, O_CREAT | O_RDWR, 0600); free (name); @@ -210,9 +208,7 @@ void write_dc (struct tgl_dc *DC, void *extra) { void write_auth_file (struct tgl_state *TLS) { char *name = 0; - if (asprintf (&name, "%s/%s", TLS->base_path, "auth") < 0) { - return; - } + name = g_strdup_printf("%s/%s", TLS->base_path, "auth"); int auth_file_fd = open (name, O_CREAT | O_RDWR, 0600); free (name); if (auth_file_fd < 0) { return; } @@ -277,9 +273,7 @@ void empty_auth_file (struct tgl_state *TLS) { void read_auth_file (struct tgl_state *TLS) { char *name = 0; - if (asprintf (&name, "%s/%s", TLS->base_path, "auth") < 0) { - return; - } + name = g_strdup_printf("%s/%s", TLS->base_path, "auth"); int auth_file_fd = open (name, O_CREAT | O_RDWR, 0600); free (name); if (auth_file_fd < 0) { @@ -350,9 +344,7 @@ void write_secret_chat (tgl_peer_t *_P, void *extra) { void write_secret_chat_file (struct tgl_state *TLS) { char *name = 0; - if (asprintf (&name, "%s/%s", TLS->base_path, "secret") < 0) { - return; - } + name = g_strdup_printf("%s/%s", TLS->base_path, "secret"); int secret_chat_fd = open (name, O_CREAT | O_RDWR, 0600); free (name); assert (secret_chat_fd >= 0); @@ -414,9 +406,7 @@ void read_secret_chat (struct tgl_state *TLS, int fd, int v) { void read_secret_chat_file (struct tgl_state *TLS) { char *name = 0; - if (asprintf (&name, "%s/%s", TLS->base_path, "secret") < 0) { - return; - } + name = g_strdup_printf("%s/%s", TLS->base_path, "secret"); int secret_chat_fd = open (name, O_RDWR, 0600); free (name); diff --git a/telegram-purple.c b/telegram-purple.c index 6539f58..19c55fd 100644 --- a/telegram-purple.c +++ b/telegram-purple.c @@ -29,8 +29,10 @@ #include #include #include +#ifndef WIN32 #include #include +#endif #include "accountopt.h" #include "blist.h" @@ -47,6 +49,9 @@ #include "util.h" #include "eventloop.h" #include "request.h" +#ifdef WIN32 +#include "win32/win32dep.h" +#endif #include #include @@ -80,7 +85,11 @@ static void on_user_get_info (struct tgl_state *TLS, void *info_data, int succes const char *config_dir = "telegram-purple"; const char *user_pk_filename = "server.tglpub"; +#ifdef WIN32 +const char *pk_path = "server.tglpub"; +#else const char *pk_path = "/etc/telegram-purple/server.tglpub"; +#endif struct tgl_update_callback tgp_callback = { .logprintf = debug, @@ -574,8 +583,14 @@ static void tgprpl_login (PurpleAccount * acct) { debug ("base configuration path: '%s'", TLS->base_path); struct rsa_pubkey pubkey; - debug ("trying global pubkey at %s", pk_path); - gboolean global_pk_loaded = read_pubkey_file (pk_path, &pubkey); +#ifdef WIN32 + gchar *global_pk_path = g_strdup_printf("%s/%s", DATADIR, pk_path); +#else + gchar *global_pk_path = g_strdup(pk_path); +#endif + debug ("trying global pubkey at %s", global_pk_path); + gboolean global_pk_loaded = read_pubkey_file (global_pk_path, &pubkey); + g_free(global_pk_path); tgl_set_verbosity (TLS, 4); if (global_pk_loaded) { @@ -607,7 +622,7 @@ static void tgprpl_login (PurpleAccount * acct) { return; } } - + tgl_set_ev_base (TLS, conn); tgl_set_net_methods (TLS, &tgp_conn_methods); tgl_set_timer_methods (TLS, &tgp_timers); diff --git a/tgp-ft.c b/tgp-ft.c index 05cbfce..cda5192 100644 --- a/tgp-ft.c +++ b/tgp-ft.c @@ -63,7 +63,7 @@ static char *tgp_strdup_determine_filename (const char *mime, const char *captio type = "bin"; } } - return g_strdup_printf ("%lld.%s", ABS(hash), type); + return g_strdup_printf ("%" G_GINT64_MODIFIER "d.%s", ABS(hash), type); } static void tgprpl_xfer_recv_on_finished (struct tgl_state *TLS, void *_data, int success, const char *filename) { diff --git a/tgp-msg.c b/tgp-msg.c index 450827a..918084d 100644 --- a/tgp-msg.c +++ b/tgp-msg.c @@ -27,6 +27,25 @@ #include #include +#if !GLIB_CHECK_VERSION(2,30,0) +gchar * +g_utf8_substring (const gchar *str, + glong start_pos, + glong end_pos) +{ + gchar *start, *end, *out; + + start = g_utf8_offset_to_pointer (str, start_pos); + end = g_utf8_offset_to_pointer (start, end_pos - start_pos); + + out = g_malloc (end - start + 1); + memcpy (out, start, end - start); + out[end - start] = 0; + + return out; +} +#endif + #include "telegram-base.h" #include "tgp-structs.h" #include "tgp-msg.h" diff --git a/tgp-net.c b/tgp-net.c index 416790d..0ce2b9e 100644 --- a/tgp-net.c +++ b/tgp-net.c @@ -23,16 +23,20 @@ #include #include #include +#ifndef WIN32 #include #include #include -#include #include +#include +#include +#else +#include +#endif +#include #include #include #include -#include -#include #include #include @@ -259,7 +263,7 @@ static void net_on_connected (gpointer arg, gint fd, const gchar *error_message) if (fd == -1) { const char *msg = "Connection not possible, either your network or a Telegram data center is down, or the" - " Telegram network configuratio has changed."; + " Telegram network configuration has changed."; warning (msg); return; } @@ -403,7 +407,7 @@ static void try_write (struct connection *c) { delete_connection_buffer (b); } else { if (errno != EAGAIN && errno != EWOULDBLOCK) { - info ("fail_connection: write_error %m\n"); + info ("fail_connection: write_error %s\n", g_strerror(errno)); fail_connection (c); return; } else { @@ -481,7 +485,7 @@ static void try_read (struct connection *c) { c->in_tail = b; } else { if (errno != EAGAIN && errno != EWOULDBLOCK) { - debug ("fail_connection: read_error %m\n"); + debug ("fail_connection: read_error %s\n", strerror(errno)); fail_connection (c); return; } else {