2013-10-23 10:26:17 -04:00
|
|
|
/*
|
|
|
|
This file is part of telegram-client.
|
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright Vitaly Valtman 2013
|
|
|
|
*/
|
2014-01-13 08:05:25 -05:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2013-11-09 17:47:19 -05:00
|
|
|
#include "config.h"
|
2014-01-13 08:05:25 -05:00
|
|
|
#endif
|
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
2013-11-09 17:47:19 -05:00
|
|
|
#if (READLINE == GNU)
|
2013-10-03 08:38:25 -04:00
|
|
|
#include <readline/readline.h>
|
2013-11-09 17:47:19 -05:00
|
|
|
#else
|
|
|
|
#include <editline/readline.h>
|
|
|
|
#endif
|
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <fcntl.h>
|
2014-02-03 07:25:09 -05:00
|
|
|
|
|
|
|
#ifdef HAVE_EXECINFO_H
|
2013-10-11 16:52:20 -04:00
|
|
|
#include <execinfo.h>
|
2014-01-30 06:49:26 -05:00
|
|
|
#endif
|
2013-10-11 16:52:20 -04:00
|
|
|
#include <signal.h>
|
2014-02-05 06:07:58 -05:00
|
|
|
#ifdef HAVE_LIBCONFIG
|
2013-11-08 13:58:28 -05:00
|
|
|
#include <libconfig.h>
|
2013-11-10 15:35:08 -05:00
|
|
|
#endif
|
2013-10-03 08:38:25 -04:00
|
|
|
|
2014-02-04 11:35:16 -05:00
|
|
|
#include "telegram.h"
|
2013-10-03 08:38:25 -04:00
|
|
|
#include "loop.h"
|
2013-10-11 16:52:20 -04:00
|
|
|
#include "mtproto-client.h"
|
2013-11-11 13:35:31 -05:00
|
|
|
#include "interface.h"
|
2014-01-10 06:32:57 -05:00
|
|
|
#include "tools.h"
|
2013-10-03 08:38:25 -04:00
|
|
|
|
2014-01-11 19:43:29 -05:00
|
|
|
#ifdef USE_LUA
|
|
|
|
# include "lua-tg.h"
|
|
|
|
#endif
|
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
#define PROGNAME "telegram-client"
|
|
|
|
#define VERSION "0.01"
|
|
|
|
|
2014-02-04 11:35:16 -05:00
|
|
|
#define CONFIG_DIRECTORY "." PROG_NAME
|
2013-11-08 13:58:28 -05:00
|
|
|
#define CONFIG_FILE "config"
|
|
|
|
#define AUTH_KEY_FILE "auth"
|
|
|
|
#define STATE_FILE "state"
|
|
|
|
#define SECRET_CHAT_FILE "secret"
|
|
|
|
#define DOWNLOADS_DIRECTORY "downloads"
|
2013-11-12 16:43:42 -05:00
|
|
|
#define BINLOG_FILE "binlog"
|
2013-10-03 08:38:25 -04:00
|
|
|
|
|
|
|
#define CONFIG_DIRECTORY_MODE 0700
|
|
|
|
|
|
|
|
#define DEFAULT_CONFIG_CONTENTS \
|
|
|
|
"# This is an empty config file\n" \
|
|
|
|
"# Feel free to put something here\n"
|
|
|
|
|
|
|
|
char *default_username;
|
|
|
|
char *auth_token;
|
2013-10-25 15:50:10 -04:00
|
|
|
int msg_num_mode;
|
2013-11-08 13:58:28 -05:00
|
|
|
char *config_filename;
|
|
|
|
char *prefix;
|
|
|
|
int test_dc;
|
|
|
|
char *auth_file_name;
|
|
|
|
char *state_file_name;
|
|
|
|
char *secret_chat_file_name;
|
|
|
|
char *downloads_directory;
|
|
|
|
char *config_directory;
|
2013-11-12 16:52:30 -05:00
|
|
|
char *binlog_file_name;
|
2013-11-11 16:59:36 -05:00
|
|
|
int binlog_enabled;
|
2013-11-15 11:14:25 -05:00
|
|
|
extern int log_level;
|
2013-11-29 18:15:10 -05:00
|
|
|
int sync_from_start;
|
2013-12-24 18:42:05 -05:00
|
|
|
int allow_weak_random;
|
2013-10-03 08:38:25 -04:00
|
|
|
|
|
|
|
void set_default_username (const char *s) {
|
|
|
|
if (default_username) {
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree_str (default_username);
|
2013-10-03 08:38:25 -04:00
|
|
|
}
|
2014-01-10 06:32:57 -05:00
|
|
|
default_username = tstrdup (s);
|
2013-10-03 08:38:25 -04:00
|
|
|
}
|
|
|
|
|
2013-12-02 12:19:08 -05:00
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
/* {{{ TERMINAL */
|
2013-12-02 12:19:08 -05:00
|
|
|
static struct termios term_in, term_out;
|
|
|
|
static int term_set_in;
|
|
|
|
static int term_set_out;
|
2013-10-03 08:38:25 -04:00
|
|
|
|
|
|
|
void get_terminal_attributes (void) {
|
2013-12-02 12:19:08 -05:00
|
|
|
if (tcgetattr (STDIN_FILENO, &term_in) < 0) {
|
|
|
|
} else {
|
|
|
|
term_set_in = 1;
|
|
|
|
}
|
|
|
|
if (tcgetattr (STDOUT_FILENO, &term_out) < 0) {
|
|
|
|
} else {
|
|
|
|
term_set_out = 1;
|
2013-10-03 08:38:25 -04:00
|
|
|
}
|
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
|
|
|
|
void set_terminal_attributes (void) {
|
2013-12-02 12:19:08 -05:00
|
|
|
if (term_set_in) {
|
|
|
|
if (tcsetattr (STDIN_FILENO, 0, &term_in) < 0) {
|
|
|
|
perror ("tcsetattr()");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (term_set_out) {
|
|
|
|
if (tcsetattr (STDOUT_FILENO, 0, &term_out) < 0) {
|
|
|
|
perror ("tcsetattr()");
|
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
}
|
2013-10-03 08:38:25 -04:00
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
char *get_home_directory (void) {
|
2014-01-10 06:40:28 -05:00
|
|
|
static char *home_directory = NULL;
|
|
|
|
if (home_directory != NULL) {
|
|
|
|
return home_directory;
|
|
|
|
}
|
2013-10-03 08:38:25 -04:00
|
|
|
struct passwd *current_passwd;
|
|
|
|
uid_t user_id;
|
|
|
|
setpwent ();
|
|
|
|
user_id = getuid ();
|
|
|
|
while ((current_passwd = getpwent ())) {
|
|
|
|
if (current_passwd->pw_uid == user_id) {
|
2014-01-10 06:40:28 -05:00
|
|
|
home_directory = tstrdup (current_passwd->pw_dir);
|
|
|
|
break;
|
2013-10-03 08:38:25 -04:00
|
|
|
}
|
|
|
|
}
|
2014-01-10 06:40:28 -05:00
|
|
|
endpwent ();
|
|
|
|
if (home_directory == NULL) {
|
|
|
|
home_directory = tstrdup (".");
|
|
|
|
}
|
|
|
|
return home_directory;
|
2013-10-03 08:38:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char *get_config_directory (void) {
|
|
|
|
char *config_directory;
|
2014-01-10 09:37:56 -05:00
|
|
|
tasprintf (&config_directory, "%s/" CONFIG_DIRECTORY, get_home_directory ());
|
2013-10-03 08:38:25 -04:00
|
|
|
return config_directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *get_config_filename (void) {
|
|
|
|
return config_filename;
|
|
|
|
}
|
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
char *get_auth_key_filename (void) {
|
2013-11-08 13:58:28 -05:00
|
|
|
return auth_file_name;
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
|
2013-11-04 12:34:27 -05:00
|
|
|
char *get_state_filename (void) {
|
2013-11-08 13:58:28 -05:00
|
|
|
return state_file_name;
|
2013-11-04 12:34:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
char *get_secret_chat_filename (void) {
|
2013-11-08 13:58:28 -05:00
|
|
|
return secret_chat_file_name;
|
2013-11-04 12:34:27 -05:00
|
|
|
}
|
|
|
|
|
2013-11-08 13:58:28 -05:00
|
|
|
char *get_downloads_directory (void) {
|
2013-10-03 08:38:25 -04:00
|
|
|
return downloads_directory;
|
|
|
|
}
|
|
|
|
|
2013-11-12 16:52:30 -05:00
|
|
|
char *get_binlog_file_name (void) {
|
|
|
|
return binlog_file_name;
|
|
|
|
}
|
|
|
|
|
2013-11-08 13:58:28 -05:00
|
|
|
char *make_full_path (char *s) {
|
|
|
|
if (*s != '/') {
|
|
|
|
char *t = s;
|
2014-01-10 09:37:56 -05:00
|
|
|
tasprintf (&s, "%s/%s", get_home_directory (), s);
|
|
|
|
tfree_str (t);
|
2013-11-08 13:58:28 -05:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-01-10 10:04:27 -05:00
|
|
|
void check_type_sizes (void) {
|
|
|
|
if (sizeof (int) != 4u) {
|
|
|
|
logprintf ("sizeof (int) isn't equal 4.\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
if (sizeof (char) != 1u) {
|
|
|
|
logprintf ("sizeof (char) isn't equal 1.\n");
|
2014-01-10 07:30:56 -05:00
|
|
|
exit (1);
|
|
|
|
}
|
2014-01-10 10:04:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void running_for_first_time (void) {
|
|
|
|
check_type_sizes ();
|
2013-11-08 13:58:28 -05:00
|
|
|
if (config_filename) {
|
|
|
|
return; // Do not create custom config file
|
|
|
|
}
|
2014-01-10 09:37:56 -05:00
|
|
|
tasprintf (&config_filename, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, CONFIG_FILE);
|
2013-11-08 13:58:28 -05:00
|
|
|
config_filename = make_full_path (config_filename);
|
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
int config_file_fd;
|
|
|
|
char *config_directory = get_config_directory ();
|
2013-11-08 13:58:28 -05:00
|
|
|
//char *downloads_directory = get_downloads_directory ();
|
2013-10-03 08:38:25 -04:00
|
|
|
|
2013-11-08 13:58:28 -05:00
|
|
|
if (!mkdir (config_directory, CONFIG_DIRECTORY_MODE)) {
|
2013-10-03 08:38:25 -04:00
|
|
|
printf ("[%s] created\n", config_directory);
|
|
|
|
}
|
|
|
|
|
2014-01-10 09:37:56 -05:00
|
|
|
tfree_str (config_directory);
|
2014-01-10 06:46:46 -05:00
|
|
|
config_directory = NULL;
|
2013-10-03 08:38:25 -04:00
|
|
|
// see if config file is there
|
2014-02-04 11:35:16 -05:00
|
|
|
if (access (config_filename, R_OK) != 0) {
|
2013-10-03 08:38:25 -04:00
|
|
|
// config file missing, so touch it
|
2013-10-26 15:57:22 -04:00
|
|
|
config_file_fd = open (config_filename, O_CREAT | O_RDWR, 0600);
|
2013-10-03 12:10:53 -04:00
|
|
|
if (config_file_fd == -1) {
|
|
|
|
perror ("open[config_file]");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2013-10-03 08:38:25 -04:00
|
|
|
if (write (config_file_fd, DEFAULT_CONFIG_CONTENTS, strlen (DEFAULT_CONFIG_CONTENTS)) <= 0) {
|
2013-10-03 12:10:53 -04:00
|
|
|
perror ("write[config_file]");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2013-10-03 08:38:25 -04:00
|
|
|
close (config_file_fd);
|
2013-11-08 13:58:28 -05:00
|
|
|
/*int auth_file_fd = open (get_auth_key_filename (), O_CREAT | O_RDWR, 0600);
|
2013-10-11 16:52:20 -04:00
|
|
|
int x = -1;
|
|
|
|
assert (write (auth_file_fd, &x, 4) == 4);
|
|
|
|
close (auth_file_fd);
|
|
|
|
|
2013-11-08 13:58:28 -05:00
|
|
|
printf ("[%s] created\n", config_filename);*/
|
2013-10-03 08:38:25 -04:00
|
|
|
|
|
|
|
/* create downloads directory */
|
2013-11-08 13:58:28 -05:00
|
|
|
/*if (mkdir (downloads_directory, 0755) !=0) {
|
2013-10-03 12:10:53 -04:00
|
|
|
perror ("creating download directory");
|
|
|
|
exit (EXIT_FAILURE);
|
2013-11-08 13:58:28 -05:00
|
|
|
}*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-05 06:07:58 -05:00
|
|
|
#ifdef HAVE_LIBCONFIG
|
2013-11-08 13:58:28 -05:00
|
|
|
void parse_config_val (config_t *conf, char **s, char *param_name, const char *default_name, const char *path) {
|
|
|
|
static char buf[1000];
|
|
|
|
int l = 0;
|
|
|
|
if (prefix) {
|
|
|
|
l = strlen (prefix);
|
|
|
|
memcpy (buf, prefix, l);
|
|
|
|
buf[l ++] = '.';
|
|
|
|
}
|
|
|
|
*s = 0;
|
|
|
|
const char *r = 0;
|
|
|
|
strcpy (buf + l, param_name);
|
|
|
|
config_lookup_string (conf, buf, &r);
|
|
|
|
if (r) {
|
|
|
|
if (path) {
|
2014-01-10 09:37:56 -05:00
|
|
|
tasprintf (s, "%s/%s", path, r);
|
2013-11-08 13:58:28 -05:00
|
|
|
} else {
|
2014-01-10 06:32:57 -05:00
|
|
|
*s = tstrdup (r);
|
2013-11-08 13:58:28 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (path) {
|
2014-01-10 09:37:56 -05:00
|
|
|
tasprintf (s, "%s/%s", path, default_name);
|
2013-11-08 13:58:28 -05:00
|
|
|
} else {
|
2014-01-10 06:32:57 -05:00
|
|
|
*s = tstrdup (default_name);
|
2013-10-03 12:10:53 -04:00
|
|
|
}
|
2013-10-03 08:38:25 -04:00
|
|
|
}
|
2013-11-08 13:58:28 -05:00
|
|
|
}
|
2013-10-03 08:38:25 -04:00
|
|
|
|
2013-11-08 13:58:28 -05:00
|
|
|
void parse_config (void) {
|
|
|
|
config_filename = make_full_path (config_filename);
|
|
|
|
|
|
|
|
config_t conf;
|
|
|
|
config_init (&conf);
|
|
|
|
if (config_read_file (&conf, config_filename) != CONFIG_TRUE) {
|
|
|
|
fprintf (stderr, "Can not read config '%s': error '%s' on the line %d\n", config_filename, config_error_text (&conf), config_error_line (&conf));
|
|
|
|
exit (2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!prefix) {
|
|
|
|
config_lookup_string (&conf, "default_profile", (void *)&prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char buf[1000];
|
|
|
|
int l = 0;
|
|
|
|
if (prefix) {
|
|
|
|
l = strlen (prefix);
|
|
|
|
memcpy (buf, prefix, l);
|
|
|
|
buf[l ++] = '.';
|
|
|
|
}
|
|
|
|
test_dc = 0;
|
|
|
|
strcpy (buf + l, "test");
|
|
|
|
config_lookup_bool (&conf, buf, &test_dc);
|
|
|
|
|
2013-11-15 11:14:25 -05:00
|
|
|
strcpy (buf + l, "log_level");
|
2013-12-02 10:11:05 -05:00
|
|
|
long long t = log_level;
|
|
|
|
config_lookup_int (&conf, buf, (void *)&t);
|
|
|
|
log_level = t;
|
2013-11-15 11:14:25 -05:00
|
|
|
|
2013-11-08 13:58:28 -05:00
|
|
|
if (!msg_num_mode) {
|
|
|
|
strcpy (buf + l, "msg_num");
|
|
|
|
config_lookup_bool (&conf, buf, &msg_num_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_config_val (&conf, &config_directory, "config_directory", CONFIG_DIRECTORY, 0);
|
|
|
|
config_directory = make_full_path (config_directory);
|
|
|
|
|
|
|
|
parse_config_val (&conf, &auth_file_name, "auth_file", AUTH_KEY_FILE, config_directory);
|
|
|
|
parse_config_val (&conf, &state_file_name, "state_file", STATE_FILE, config_directory);
|
|
|
|
parse_config_val (&conf, &secret_chat_file_name, "secret", SECRET_CHAT_FILE, config_directory);
|
|
|
|
parse_config_val (&conf, &downloads_directory, "downloads", DOWNLOADS_DIRECTORY, config_directory);
|
2013-11-12 16:43:42 -05:00
|
|
|
parse_config_val (&conf, &binlog_file_name, "binlog", BINLOG_FILE, config_directory);
|
2013-11-08 13:58:28 -05:00
|
|
|
|
2013-11-11 16:59:36 -05:00
|
|
|
strcpy (buf + l, "binlog_enabled");
|
|
|
|
config_lookup_bool (&conf, buf, &binlog_enabled);
|
|
|
|
|
2013-11-08 13:58:28 -05:00
|
|
|
if (!mkdir (config_directory, CONFIG_DIRECTORY_MODE)) {
|
|
|
|
printf ("[%s] created\n", config_directory);
|
|
|
|
}
|
|
|
|
if (!mkdir (downloads_directory, CONFIG_DIRECTORY_MODE)) {
|
|
|
|
printf ("[%s] created\n", downloads_directory);
|
|
|
|
}
|
2013-10-03 08:38:25 -04:00
|
|
|
}
|
2013-11-09 17:47:19 -05:00
|
|
|
#else
|
|
|
|
void parse_config (void) {
|
|
|
|
printf ("libconfig not enabled\n");
|
2014-01-10 09:37:56 -05:00
|
|
|
tasprintf (&auth_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, AUTH_KEY_FILE);
|
|
|
|
tasprintf (&state_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, STATE_FILE);
|
|
|
|
tasprintf (&secret_chat_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, SECRET_CHAT_FILE);
|
|
|
|
tasprintf (&downloads_directory, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, DOWNLOADS_DIRECTORY);
|
|
|
|
tasprintf (&binlog_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, BINLOG_FILE);
|
2013-11-09 17:47:19 -05:00
|
|
|
}
|
|
|
|
#endif
|
2013-10-03 08:38:25 -04:00
|
|
|
|
|
|
|
void inner_main (void) {
|
|
|
|
loop ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void usage (void) {
|
2014-02-27 04:17:07 -05:00
|
|
|
printf ("%s Usage\n", PROGNAME);
|
|
|
|
|
|
|
|
printf (" -h this help list\n");
|
|
|
|
printf (" -u username specify username\n");
|
|
|
|
printf (" -k public-key specify server public key\n");
|
|
|
|
printf (" -v verbose\n");
|
|
|
|
printf (" -l [1-3] log level\n");
|
|
|
|
printf (" -L log-file log net file\n");
|
|
|
|
printf (" -N message num mode\n");
|
|
|
|
printf (" -c config-file specify config file\n");
|
|
|
|
printf (" -p prefix specify prefix\n");
|
|
|
|
printf (" -R register mode\n");
|
|
|
|
printf (" -f sync from start\n");
|
|
|
|
printf (" -B enable binlog\n");
|
|
|
|
printf (" -E disable auto accept\n");
|
|
|
|
printf (" -w allow weak random\n");
|
|
|
|
printf (" -s specify lua script\n");
|
|
|
|
printf (" -W wait dialog list\n");
|
|
|
|
printf ("\n");
|
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2013-10-11 16:52:20 -04:00
|
|
|
extern char *rsa_public_key_name;
|
|
|
|
extern int verbosity;
|
|
|
|
extern int default_dc_num;
|
|
|
|
|
2013-12-18 10:21:49 -05:00
|
|
|
char *log_net_file;
|
|
|
|
FILE *log_net_f;
|
|
|
|
|
2013-11-21 14:35:49 -05:00
|
|
|
int register_mode;
|
2013-12-18 10:21:49 -05:00
|
|
|
int disable_auto_accept;
|
2014-02-03 10:50:57 -05:00
|
|
|
int wait_dialog_list;
|
2014-01-11 19:43:29 -05:00
|
|
|
|
|
|
|
char *lua_file;
|
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
void args_parse (int argc, char **argv) {
|
|
|
|
int opt = 0;
|
2014-02-03 10:50:57 -05:00
|
|
|
while ((opt = getopt (argc, argv, "u:hk:vn:Nc:p:l:RfBL:Es:wW")) != -1) {
|
2013-10-03 08:38:25 -04:00
|
|
|
switch (opt) {
|
|
|
|
case 'u':
|
|
|
|
set_default_username (optarg);
|
|
|
|
break;
|
2013-10-11 16:52:20 -04:00
|
|
|
case 'k':
|
2014-01-10 06:32:57 -05:00
|
|
|
rsa_public_key_name = tstrdup (optarg);
|
2013-10-11 16:52:20 -04:00
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbosity ++;
|
|
|
|
break;
|
2013-10-25 15:50:10 -04:00
|
|
|
case 'N':
|
|
|
|
msg_num_mode ++;
|
|
|
|
break;
|
2013-11-08 13:58:28 -05:00
|
|
|
case 'c':
|
2014-01-10 06:32:57 -05:00
|
|
|
config_filename = tstrdup (optarg);
|
2013-11-08 13:58:28 -05:00
|
|
|
break;
|
|
|
|
case 'p':
|
2014-01-10 06:32:57 -05:00
|
|
|
prefix = tstrdup (optarg);
|
2013-11-08 13:58:28 -05:00
|
|
|
assert (strlen (prefix) <= 100);
|
|
|
|
break;
|
2013-11-15 11:14:25 -05:00
|
|
|
case 'l':
|
|
|
|
log_level = atoi (optarg);
|
|
|
|
break;
|
2013-11-21 14:35:49 -05:00
|
|
|
case 'R':
|
|
|
|
register_mode = 1;
|
|
|
|
break;
|
2013-11-29 18:15:10 -05:00
|
|
|
case 'f':
|
|
|
|
sync_from_start = 1;
|
|
|
|
break;
|
2013-12-03 06:44:14 -05:00
|
|
|
case 'B':
|
|
|
|
binlog_enabled = 1;
|
|
|
|
break;
|
2013-12-18 10:21:49 -05:00
|
|
|
case 'L':
|
|
|
|
if (log_net_file) {
|
|
|
|
usage ();
|
|
|
|
}
|
2014-01-10 06:32:57 -05:00
|
|
|
log_net_file = tstrdup (optarg);
|
2013-12-18 10:21:49 -05:00
|
|
|
log_net_f = fopen (log_net_file, "a");
|
|
|
|
assert (log_net_f);
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
disable_auto_accept = 1;
|
|
|
|
break;
|
2013-12-24 18:42:05 -05:00
|
|
|
case 'w':
|
|
|
|
allow_weak_random = 1;
|
|
|
|
break;
|
2014-01-11 19:43:29 -05:00
|
|
|
case 's':
|
|
|
|
lua_file = tstrdup (optarg);
|
|
|
|
break;
|
2014-02-03 10:50:57 -05:00
|
|
|
case 'W':
|
|
|
|
wait_dialog_list = 1;
|
|
|
|
break;
|
2013-10-03 08:38:25 -04:00
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-03 07:25:09 -05:00
|
|
|
#ifdef HAVE_EXECINFO_H
|
2013-10-11 16:52:20 -04:00
|
|
|
void print_backtrace (void) {
|
|
|
|
void *buffer[255];
|
|
|
|
const int calls = backtrace (buffer, sizeof (buffer) / sizeof (void *));
|
|
|
|
backtrace_symbols_fd (buffer, calls, 1);
|
|
|
|
}
|
2014-01-30 06:01:16 -05:00
|
|
|
#else
|
|
|
|
void print_backtrace (void) {
|
2014-02-05 06:07:58 -05:00
|
|
|
if (write (1, "No libexec. Backtrace disabled\n", 32) < 0) {
|
|
|
|
// Sad thing
|
|
|
|
}
|
2014-01-30 06:01:16 -05:00
|
|
|
}
|
|
|
|
#endif
|
2013-10-11 16:52:20 -04:00
|
|
|
|
2014-02-03 07:25:09 -05:00
|
|
|
void sig_segv_handler (int signum __attribute__ ((unused))) {
|
2013-10-11 16:52:20 -04:00
|
|
|
set_terminal_attributes ();
|
2014-02-03 10:26:43 -05:00
|
|
|
if (write (1, "SIGSEGV received\n", 18) < 0) {
|
|
|
|
// Sad thing
|
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
print_backtrace ();
|
2014-02-03 07:25:09 -05:00
|
|
|
exit (EXIT_FAILURE);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
|
2014-02-03 07:25:09 -05:00
|
|
|
void sig_abrt_handler (int signum __attribute__ ((unused))) {
|
|
|
|
set_terminal_attributes ();
|
2014-02-03 10:26:43 -05:00
|
|
|
if (write (1, "SIGABRT received\n", 18) < 0) {
|
|
|
|
// Sad thing
|
|
|
|
}
|
2014-02-03 07:25:09 -05:00
|
|
|
print_backtrace ();
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
int main (int argc, char **argv) {
|
2014-02-03 07:25:09 -05:00
|
|
|
signal (SIGSEGV, sig_segv_handler);
|
|
|
|
signal (SIGABRT, sig_abrt_handler);
|
2013-11-15 11:14:25 -05:00
|
|
|
|
|
|
|
log_level = 10;
|
2013-11-08 13:58:28 -05:00
|
|
|
|
|
|
|
args_parse (argc, argv);
|
2013-10-25 05:28:29 -04:00
|
|
|
printf (
|
2013-10-25 05:32:10 -04:00
|
|
|
"Telegram-client version " TG_VERSION ", Copyright (C) 2013 Vitaly Valtman\n"
|
2013-10-25 05:28:29 -04:00
|
|
|
"Telegram-client comes with ABSOLUTELY NO WARRANTY; for details type `show_license'.\n"
|
|
|
|
"This is free software, and you are welcome to redistribute it\n"
|
|
|
|
"under certain conditions; type `show_license' for details.\n"
|
|
|
|
);
|
2013-11-08 13:58:28 -05:00
|
|
|
running_for_first_time ();
|
|
|
|
parse_config ();
|
|
|
|
|
2013-10-25 05:28:29 -04:00
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
get_terminal_attributes ();
|
|
|
|
|
2014-01-11 19:43:29 -05:00
|
|
|
#ifdef USE_LUA
|
|
|
|
if (lua_file) {
|
|
|
|
lua_init (lua_file);
|
|
|
|
}
|
|
|
|
#endif
|
2013-10-03 08:38:25 -04:00
|
|
|
|
|
|
|
inner_main ();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|