2013-10-23 10:26:17 -04:00
|
|
|
/*
|
2014-08-21 18:02:43 -04:00
|
|
|
This file is part of telegram-cli.
|
2013-10-23 10:26:17 -04:00
|
|
|
|
2014-08-21 18:02:43 -04:00
|
|
|
Telegram-cli is free software: you can redistribute it and/or modify
|
2013-10-23 10:26:17 -04:00
|
|
|
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.
|
|
|
|
|
2014-08-21 18:02:43 -04:00
|
|
|
Telegram-cli is distributed in the hope that it will be useful,
|
2013-10-23 10:26:17 -04:00
|
|
|
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
|
2014-08-21 18:02:43 -04:00
|
|
|
along with this telegram-cli. If not, see <http://www.gnu.org/licenses/>.
|
2013-10-23 10:26:17 -04:00
|
|
|
|
2014-08-21 18:02:43 -04:00
|
|
|
Copyright Vitaly Valtman 2013-2014
|
2013-10-23 10:26:17 -04:00
|
|
|
*/
|
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
|
2014-09-23 10:39:28 -04:00
|
|
|
#ifdef EVENT_V2
|
|
|
|
#include <event2/event.h>
|
|
|
|
#include <event2/bufferevent.h>
|
|
|
|
#include <event2/buffer.h>
|
|
|
|
#else
|
|
|
|
#include <event.h>
|
|
|
|
#include "event-old.h"
|
|
|
|
#endif
|
2013-11-09 17:47:19 -05:00
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
#include <sys/stat.h>
|
2014-09-22 18:23:40 -04:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2013-10-03 08:38:25 -04:00
|
|
|
#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-09-09 14:05:56 -04:00
|
|
|
#include <grp.h>
|
2014-09-20 18:22:35 -04:00
|
|
|
#include <arpa/inet.h>
|
2014-10-02 04:14:55 -04:00
|
|
|
#include <netinet/in.h>
|
2014-09-09 14:05:56 -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-11-11 13:35:31 -05:00
|
|
|
#include "interface.h"
|
2014-11-12 09:38:44 -05:00
|
|
|
#include <tgl/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
|
|
|
|
|
2014-11-12 09:38:44 -05:00
|
|
|
#include <tgl/tgl.h>
|
2014-08-13 08:56:55 -04:00
|
|
|
|
2014-08-13 21:31:24 -04:00
|
|
|
#define PROGNAME "telegram-cli"
|
|
|
|
#define VERSION "0.07"
|
2013-10-03 08:38:25 -04:00
|
|
|
|
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
|
|
|
|
2014-09-22 18:23:40 -04:00
|
|
|
|
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"
|
|
|
|
|
2014-08-14 18:16:01 -04:00
|
|
|
int verbosity;
|
2013-10-25 15:50:10 -04:00
|
|
|
int msg_num_mode;
|
2014-09-23 08:58:49 -04:00
|
|
|
char *default_username;
|
2013-11-08 13:58:28 -05:00
|
|
|
char *config_filename;
|
|
|
|
char *prefix;
|
|
|
|
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;
|
2014-09-23 08:58:49 -04:00
|
|
|
char *lua_file;
|
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;
|
2014-08-24 20:11:41 -04:00
|
|
|
int disable_colors;
|
2014-08-26 14:26:00 -04:00
|
|
|
int readline_disabled;
|
2014-09-09 13:05:36 -04:00
|
|
|
int disable_output;
|
2014-09-18 09:57:23 -04:00
|
|
|
int reset_authorization;
|
2014-09-20 18:22:35 -04:00
|
|
|
int port;
|
2014-10-11 20:27:34 -04:00
|
|
|
int use_ids;
|
2014-10-02 14:54:06 -04:00
|
|
|
char *start_command;
|
2013-10-03 08:38:25 -04:00
|
|
|
|
2014-10-24 13:43:08 -04:00
|
|
|
struct tgl_state *TLS;
|
|
|
|
|
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
|
|
|
/* }}} */
|
|
|
|
|
2014-02-25 09:43:21 -05:00
|
|
|
int str_empty (char *str) {
|
|
|
|
return ((str == NULL) || (strlen(str) < 1));
|
|
|
|
}
|
|
|
|
|
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;
|
2014-02-25 09:43:21 -05:00
|
|
|
home_directory = getenv("TELEGRAM_HOME");
|
|
|
|
if (!str_empty (home_directory)) { return tstrdup (home_directory); }
|
|
|
|
home_directory = getenv("HOME");
|
|
|
|
if (!str_empty (home_directory)) { return tstrdup (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 ();
|
2014-02-25 09:43:21 -05:00
|
|
|
if (str_empty (home_directory)) { home_directory = tstrdup ("."); }
|
2014-01-10 06:40:28 -05:00
|
|
|
return home_directory;
|
2013-10-03 08:38:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char *get_config_directory (void) {
|
|
|
|
char *config_directory;
|
2014-02-25 09:43:21 -05:00
|
|
|
config_directory = getenv("TELEGRAM_CONFIG_DIR");
|
|
|
|
if (!str_empty (config_directory)) { return tstrdup (config_directory); }
|
2014-10-10 05:13:37 -04:00
|
|
|
// XDG: http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
|
|
|
config_directory = getenv("XDG_CONFIG_HOME");
|
|
|
|
if (!str_empty (config_directory)) {
|
|
|
|
tasprintf (&config_directory, "%s/" PROG_NAME, config_directory);
|
|
|
|
// :TODO: someone check whether it could be required to pass tasprintf
|
|
|
|
// a tstrdup()ed config_directory instead; works for me without.
|
|
|
|
// should work b/c this scope's lifespan encompasses tasprintf()
|
|
|
|
return 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 ();
|
2014-10-10 07:30:17 -04:00
|
|
|
if (!str_empty (config_filename)) {
|
2013-11-08 13:58:28 -05:00
|
|
|
return; // Do not create custom config file
|
|
|
|
}
|
2014-10-10 07:30:17 -04:00
|
|
|
if (str_empty (config_directory)) {
|
|
|
|
config_directory = get_config_directory ();
|
|
|
|
}
|
|
|
|
tasprintf (&config_filename, "%s/%s", config_directory, CONFIG_FILE);
|
2013-11-08 13:58:28 -05:00
|
|
|
config_filename = make_full_path (config_filename);
|
2014-10-10 07:42:10 -04:00
|
|
|
if (!disable_output) {
|
|
|
|
printf ("I: config dir=[%s]\n", config_directory);
|
|
|
|
}
|
2014-10-10 07:30:17 -04:00
|
|
|
// printf ("I: config file=[%s]\n", config_filename);
|
2013-11-08 13:58:28 -05:00
|
|
|
|
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)) {
|
2014-09-09 13:05:36 -04:00
|
|
|
if (!disable_output) {
|
|
|
|
printf ("[%s] created\n", config_directory);
|
|
|
|
}
|
2013-10-03 08:38:25 -04:00
|
|
|
}
|
|
|
|
|
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]");
|
2014-02-25 09:43:21 -05:00
|
|
|
printf ("I: config_file=[%s]\n", config_filename);
|
2013-10-03 12:10:53 -04:00
|
|
|
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) {
|
2014-09-29 10:38:06 -04:00
|
|
|
if (path && *r != '/') {
|
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 {
|
2014-08-20 23:24:52 -04:00
|
|
|
if (path && default_name) {
|
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-08-21 11:38:51 -04:00
|
|
|
*s = default_name ? tstrdup (default_name) : 0;
|
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) {
|
2014-09-09 11:10:01 -04:00
|
|
|
//config_filename = make_full_path (config_filename);
|
2013-11-08 13:58:28 -05:00
|
|
|
|
|
|
|
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 ++] = '.';
|
|
|
|
}
|
2014-08-13 08:56:55 -04:00
|
|
|
|
2014-08-13 11:55:16 -04:00
|
|
|
int test_mode = 0;
|
2013-11-08 13:58:28 -05:00
|
|
|
strcpy (buf + l, "test");
|
2014-08-13 11:55:16 -04:00
|
|
|
config_lookup_bool (&conf, buf, &test_mode);
|
|
|
|
if (test_mode) {
|
2014-10-24 13:43:08 -04:00
|
|
|
tgl_set_test_mode (TLS);
|
2014-08-13 11:55:16 -04:00
|
|
|
}
|
2013-11-08 13:58:28 -05:00
|
|
|
|
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, &downloads_directory, "downloads", DOWNLOADS_DIRECTORY, config_directory);
|
|
|
|
|
2014-08-21 11:38:51 -04:00
|
|
|
if (!lua_file) {
|
|
|
|
parse_config_val (&conf, &lua_file, "lua_script", 0, config_directory);
|
|
|
|
}
|
|
|
|
|
2013-11-11 16:59:36 -05:00
|
|
|
strcpy (buf + l, "binlog_enabled");
|
|
|
|
config_lookup_bool (&conf, buf, &binlog_enabled);
|
2014-08-29 09:33:15 -04:00
|
|
|
|
2014-09-18 06:11:23 -04:00
|
|
|
int pfs_enabled = 0;
|
2014-08-29 09:33:15 -04:00
|
|
|
strcpy (buf + l, "pfs_enabled");
|
|
|
|
config_lookup_bool (&conf, buf, &pfs_enabled);
|
|
|
|
if (pfs_enabled) {
|
2014-10-24 13:43:08 -04:00
|
|
|
tgl_enable_pfs (TLS);
|
2014-08-29 09:33:15 -04:00
|
|
|
}
|
2014-08-13 21:31:24 -04:00
|
|
|
|
|
|
|
if (binlog_enabled) {
|
2014-08-22 07:45:16 -04:00
|
|
|
parse_config_val (&conf, &binlog_file_name, "binlog", BINLOG_FILE, config_directory);
|
2014-10-24 13:43:08 -04:00
|
|
|
tgl_set_binlog_mode (TLS, 1);
|
|
|
|
tgl_set_binlog_path (TLS, binlog_file_name);
|
2014-08-13 21:31:24 -04:00
|
|
|
} else {
|
2014-10-24 13:43:08 -04:00
|
|
|
tgl_set_binlog_mode (TLS, 0);
|
2014-08-22 07:45:16 -04:00
|
|
|
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);
|
|
|
|
//tgl_set_auth_file_path (auth_file_name);
|
2014-08-13 21:31:24 -04:00
|
|
|
}
|
2014-10-24 13:43:08 -04:00
|
|
|
tgl_set_download_directory (TLS, downloads_directory);
|
2013-11-11 16:59:36 -05:00
|
|
|
|
2013-11-08 13:58:28 -05:00
|
|
|
if (!mkdir (config_directory, CONFIG_DIRECTORY_MODE)) {
|
2014-09-09 13:05:36 -04:00
|
|
|
if (!disable_output) {
|
|
|
|
printf ("[%s] created\n", config_directory);
|
|
|
|
}
|
2013-11-08 13:58:28 -05:00
|
|
|
}
|
|
|
|
if (!mkdir (downloads_directory, CONFIG_DIRECTORY_MODE)) {
|
2014-09-09 13:05:36 -04:00
|
|
|
if (!disable_output) {
|
|
|
|
printf ("[%s] created\n", downloads_directory);
|
|
|
|
}
|
2013-11-08 13:58:28 -05:00
|
|
|
}
|
2014-09-23 08:58:49 -04:00
|
|
|
config_destroy (&conf);
|
2013-10-03 08:38:25 -04:00
|
|
|
}
|
2013-11-09 17:47:19 -05:00
|
|
|
#else
|
|
|
|
void parse_config (void) {
|
2014-09-09 13:05:36 -04:00
|
|
|
if (!disable_output) {
|
|
|
|
printf ("libconfig not enabled\n");
|
|
|
|
}
|
2014-01-10 09:37:56 -05:00
|
|
|
tasprintf (&downloads_directory, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, DOWNLOADS_DIRECTORY);
|
2014-08-13 21:31:24 -04:00
|
|
|
|
|
|
|
if (binlog_enabled) {
|
2014-08-22 07:45:16 -04:00
|
|
|
tasprintf (&binlog_file_name, "%s/%s/%s", get_home_directory (), CONFIG_DIRECTORY, BINLOG_FILE);
|
2014-10-24 13:43:08 -04:00
|
|
|
tgl_set_binlog_mode (TLS, 1);
|
|
|
|
tgl_set_binlog_path (TLS, binlog_file_name);
|
2014-08-13 21:31:24 -04:00
|
|
|
} else {
|
2014-10-24 13:43:08 -04:00
|
|
|
tgl_set_binlog_mode (TLS, 0);
|
2014-08-22 07:45:16 -04:00
|
|
|
//tgl_set_auth_file_path (auth_file_name;
|
|
|
|
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);
|
2014-08-13 21:31:24 -04:00
|
|
|
}
|
2014-10-24 13:43:08 -04:00
|
|
|
tgl_set_download_directory (TLS, downloads_directory);
|
2014-10-02 15:20:03 -04:00
|
|
|
if (!mkdir (downloads_directory, CONFIG_DIRECTORY_MODE)) {
|
|
|
|
if (!disable_output) {
|
|
|
|
printf ("[%s] created\n", downloads_directory);
|
|
|
|
}
|
|
|
|
}
|
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);
|
2014-08-24 20:11:41 -04:00
|
|
|
|
|
|
|
printf (" -u specify username (would not be asked during authorization)\n");
|
|
|
|
printf (" -k specify location of public key (possible multiple entries)\n");
|
|
|
|
printf (" -v increase verbosity (0-ERROR 1-WARNIN 2-NOTICE 3+-DEBUG-levels)\n");
|
|
|
|
printf (" -N message num mode\n");
|
|
|
|
#ifdef HAVE_LIBCONFIG
|
|
|
|
printf (" -c config file name\n");
|
|
|
|
printf (" -p use specified profile\n");
|
|
|
|
#else
|
|
|
|
printf (" -B enable binlog\n");
|
|
|
|
#endif
|
|
|
|
printf (" -l log level\n");
|
|
|
|
printf (" -f during authorization fetch all messages since registration\n");
|
2014-10-02 07:00:37 -04:00
|
|
|
printf (" -E disable auto accept of encrypted chats\n");
|
2014-08-24 20:11:41 -04:00
|
|
|
#ifdef USE_LUA
|
|
|
|
printf (" -s lua script file\n");
|
|
|
|
#endif
|
|
|
|
printf (" -W send dialog_list query and wait for answer before reading input\n");
|
|
|
|
printf (" -C disable color output\n");
|
2014-08-26 14:26:00 -04:00
|
|
|
printf (" -R disable readline\n");
|
2014-09-09 11:01:27 -04:00
|
|
|
printf (" -d daemon mode\n");
|
|
|
|
printf (" -L <log-name> log file name\n");
|
2014-09-09 14:05:56 -04:00
|
|
|
printf (" -U <user-name> change uid after start\n");
|
|
|
|
printf (" -G <group-name> change gid after start\n");
|
2014-09-09 13:05:36 -04:00
|
|
|
printf (" -D disable output\n");
|
2014-09-20 18:22:35 -04:00
|
|
|
printf (" -P <port> port to listen for input commands\n");
|
2014-09-22 18:23:40 -04:00
|
|
|
printf (" -S <socket-name> unix socket to create\n");
|
2014-10-02 14:54:06 -04:00
|
|
|
printf (" -e <commands> make commands end exit\n");
|
2014-10-11 20:27:34 -04:00
|
|
|
printf (" -I use user and chat IDs in updates instead of names\n");
|
2014-02-27 04:17:07 -05:00
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2014-08-14 14:03:33 -04:00
|
|
|
//extern char *rsa_public_key_name;
|
|
|
|
//extern int default_dc_num;
|
2013-10-11 16:52:20 -04:00
|
|
|
|
2014-09-09 11:01:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-09-09 11:01:27 -04:00
|
|
|
char *logname;
|
|
|
|
int daemonize;
|
|
|
|
|
|
|
|
|
|
|
|
void reopen_logs (void) {
|
|
|
|
int fd;
|
|
|
|
fflush (stdout);
|
|
|
|
fflush (stderr);
|
|
|
|
if ((fd = open ("/dev/null", O_RDWR, 0)) != -1) {
|
|
|
|
dup2 (fd, 0);
|
|
|
|
dup2 (fd, 1);
|
|
|
|
dup2 (fd, 2);
|
|
|
|
if (fd > 2) {
|
|
|
|
close (fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (logname && (fd = open (logname, O_WRONLY|O_APPEND|O_CREAT, 0640)) != -1) {
|
|
|
|
dup2 (fd, 1);
|
|
|
|
dup2 (fd, 2);
|
|
|
|
if (fd > 2) {
|
|
|
|
close (fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void sigusr1_handler (const int sig) {
|
|
|
|
fprintf(stderr, "got SIGUSR1, rotate logs.\n");
|
|
|
|
reopen_logs ();
|
|
|
|
signal (SIGUSR1, sigusr1_handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sighup_handler (const int sig) {
|
|
|
|
fprintf(stderr, "got SIGHUP.\n");
|
|
|
|
signal (SIGHUP, sighup_handler);
|
|
|
|
}
|
|
|
|
|
2014-09-09 14:05:56 -04:00
|
|
|
char *set_user_name;
|
|
|
|
char *set_group_name;
|
|
|
|
|
|
|
|
int change_user_group () {
|
|
|
|
char *username = set_user_name;
|
|
|
|
char *groupname = set_group_name;
|
|
|
|
struct passwd *pw;
|
|
|
|
/* lose root privileges if we have them */
|
|
|
|
if (getuid() == 0 || geteuid() == 0) {
|
|
|
|
if (username == 0 || *username == '\0') {
|
|
|
|
username = "telegramd";
|
|
|
|
}
|
|
|
|
if ((pw = getpwnam (username)) == 0) {
|
|
|
|
fprintf (stderr, "change_user_group: can't find the user %s to switch to\n", username);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
gid_t gid = pw->pw_gid;
|
|
|
|
if (setgroups (1, &gid) < 0) {
|
|
|
|
fprintf (stderr, "change_user_group: failed to clear supplementary groups list: %m\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupname) {
|
|
|
|
struct group *g = getgrnam (groupname);
|
|
|
|
if (g == NULL) {
|
|
|
|
fprintf (stderr, "change_user_group: can't find the group %s to switch to\n", groupname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
gid = g->gr_gid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setgid (gid) < 0) {
|
|
|
|
fprintf (stderr, "change_user_group: setgid (%d) failed. %m\n", (int) gid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setuid (pw->pw_uid) < 0) {
|
|
|
|
fprintf (stderr, "change_user_group: failed to assume identity of user %s\n", username);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2014-01-11 19:43:29 -05:00
|
|
|
|
2014-09-22 18:23:40 -04:00
|
|
|
char *unix_socket;
|
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
void args_parse (int argc, char **argv) {
|
2014-10-24 13:43:08 -04:00
|
|
|
TLS = tgl_state_alloc ();
|
2013-10-03 08:38:25 -04:00
|
|
|
int opt = 0;
|
2014-10-11 20:27:34 -04:00
|
|
|
while ((opt = getopt (argc, argv, "u:hk:vNl:fEwWCRdL:DU:G:qP:S:e:I"
|
2014-08-24 20:11:41 -04:00
|
|
|
#ifdef HAVE_LIBCONFIG
|
|
|
|
"c:p:"
|
|
|
|
#else
|
|
|
|
"B"
|
|
|
|
#endif
|
|
|
|
#ifdef USE_LUA
|
2014-08-26 12:30:30 -04:00
|
|
|
"s:"
|
2014-08-24 20:11:41 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
)) != -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-08-14 14:03:33 -04:00
|
|
|
//rsa_public_key_name = tstrdup (optarg);
|
2014-10-24 13:43:08 -04:00
|
|
|
tgl_set_rsa_key (TLS, optarg);
|
2013-10-11 16:52:20 -04:00
|
|
|
break;
|
|
|
|
case 'v':
|
2014-10-24 13:43:08 -04:00
|
|
|
tgl_incr_verbosity (TLS);
|
2014-08-14 18:16:01 -04:00
|
|
|
verbosity ++;
|
2013-10-11 16:52:20 -04:00
|
|
|
break;
|
2013-10-25 15:50:10 -04:00
|
|
|
case 'N':
|
|
|
|
msg_num_mode ++;
|
|
|
|
break;
|
2014-08-24 20:11:41 -04:00
|
|
|
#ifdef HAVE_LIBCONFIG
|
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;
|
2014-08-24 20:11:41 -04:00
|
|
|
#else
|
|
|
|
case 'B':
|
|
|
|
binlog_enabled = 1;
|
|
|
|
break;
|
|
|
|
#endif
|
2013-11-15 11:14:25 -05:00
|
|
|
case 'l':
|
|
|
|
log_level = atoi (optarg);
|
|
|
|
break;
|
2013-11-29 18:15:10 -05:00
|
|
|
case 'f':
|
|
|
|
sync_from_start = 1;
|
|
|
|
break;
|
2013-12-18 10:21:49 -05:00
|
|
|
case 'E':
|
|
|
|
disable_auto_accept = 1;
|
|
|
|
break;
|
2013-12-24 18:42:05 -05:00
|
|
|
case 'w':
|
|
|
|
allow_weak_random = 1;
|
|
|
|
break;
|
2014-08-24 20:11:41 -04:00
|
|
|
#ifdef USE_LUA
|
2014-01-11 19:43:29 -05:00
|
|
|
case 's':
|
2014-08-26 12:30:30 -04:00
|
|
|
lua_file = strdup (optarg);
|
2014-01-11 19:43:29 -05:00
|
|
|
break;
|
2014-08-24 20:11:41 -04:00
|
|
|
#endif
|
2014-02-03 10:50:57 -05:00
|
|
|
case 'W':
|
|
|
|
wait_dialog_list = 1;
|
|
|
|
break;
|
2014-08-24 20:11:41 -04:00
|
|
|
case 'C':
|
|
|
|
disable_colors ++;
|
|
|
|
break;
|
2014-08-26 14:26:00 -04:00
|
|
|
case 'R':
|
|
|
|
readline_disabled ++;
|
|
|
|
break;
|
2014-09-09 11:01:27 -04:00
|
|
|
case 'd':
|
|
|
|
daemonize ++;
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
logname = optarg;
|
|
|
|
break;
|
2014-09-09 14:05:56 -04:00
|
|
|
case 'U':
|
|
|
|
set_user_name = optarg;
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
set_group_name = optarg;
|
|
|
|
break;
|
2014-09-09 13:05:36 -04:00
|
|
|
case 'D':
|
|
|
|
disable_output ++;
|
|
|
|
break;
|
2014-09-18 09:57:23 -04:00
|
|
|
case 'q':
|
|
|
|
reset_authorization ++;
|
|
|
|
break;
|
2014-09-20 18:22:35 -04:00
|
|
|
case 'P':
|
|
|
|
port = atoi (optarg);
|
|
|
|
break;
|
2014-09-22 18:23:40 -04:00
|
|
|
case 'S':
|
|
|
|
unix_socket = optarg;
|
|
|
|
break;
|
2014-10-02 14:54:06 -04:00
|
|
|
case 'e':
|
|
|
|
start_command = optarg;
|
|
|
|
break;
|
2014-10-11 20:27:34 -04:00
|
|
|
case 'I':
|
|
|
|
use_ids ++;
|
|
|
|
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-09-22 18:23:40 -04:00
|
|
|
int sfd;
|
|
|
|
int usfd;
|
|
|
|
|
|
|
|
void termination_signal_handler (int signum) {
|
|
|
|
if (!readline_disabled) {
|
|
|
|
rl_free_line_state ();
|
|
|
|
rl_cleanup_after_signal ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write (1, "SIGNAL received\n", 18) < 0) {
|
2014-02-03 10:26:43 -05:00
|
|
|
// Sad thing
|
|
|
|
}
|
2014-09-22 18:23:40 -04:00
|
|
|
|
|
|
|
if (unix_socket) {
|
|
|
|
unlink (unix_socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usfd > 0) {
|
|
|
|
close (usfd);
|
|
|
|
}
|
|
|
|
if (sfd > 0) {
|
|
|
|
close (sfd);
|
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
print_backtrace ();
|
2014-09-22 18:23:40 -04:00
|
|
|
|
2014-02-03 07:25:09 -05:00
|
|
|
exit (EXIT_FAILURE);
|
2013-10-11 16:52:20 -04:00
|
|
|
}
|
|
|
|
|
2014-09-22 18:23:40 -04:00
|
|
|
volatile int sigterm_cnt;
|
|
|
|
|
|
|
|
void sig_term_handler (int signum __attribute__ ((unused))) {
|
|
|
|
signal (signum, termination_signal_handler);
|
|
|
|
//set_terminal_attributes ();
|
|
|
|
if (write (1, "SIGTERM/SIGINT received\n", 25) < 0) {
|
2014-02-03 10:26:43 -05:00
|
|
|
// Sad thing
|
|
|
|
}
|
2014-10-24 13:43:08 -04:00
|
|
|
if (TLS && TLS->ev_base) {
|
|
|
|
event_base_loopbreak (TLS->ev_base);
|
2014-09-23 10:39:28 -04:00
|
|
|
}
|
2014-09-22 18:23:40 -04:00
|
|
|
sigterm_cnt ++;
|
2014-02-03 07:25:09 -05:00
|
|
|
}
|
2013-10-11 16:52:20 -04:00
|
|
|
|
2014-09-22 18:23:40 -04:00
|
|
|
void do_halt (int error) {
|
|
|
|
if (!readline_disabled) {
|
|
|
|
rl_free_line_state ();
|
|
|
|
rl_cleanup_after_signal ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write (1, "halt\n", 5) < 0) {
|
|
|
|
// Sad thing
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unix_socket) {
|
|
|
|
unlink (unix_socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usfd > 0) {
|
|
|
|
close (usfd);
|
|
|
|
}
|
|
|
|
if (sfd > 0) {
|
|
|
|
close (sfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit (error ? EXIT_FAILURE : EXIT_SUCCESS);
|
|
|
|
}
|
2014-09-20 18:22:35 -04:00
|
|
|
|
2013-10-03 08:38:25 -04:00
|
|
|
int main (int argc, char **argv) {
|
2014-09-22 18:23:40 -04:00
|
|
|
signal (SIGSEGV, termination_signal_handler);
|
|
|
|
signal (SIGABRT, termination_signal_handler);
|
|
|
|
signal (SIGBUS, termination_signal_handler);
|
|
|
|
signal (SIGQUIT, termination_signal_handler);
|
|
|
|
signal (SIGFPE, termination_signal_handler);
|
|
|
|
|
|
|
|
signal (SIGPIPE, SIG_IGN);
|
|
|
|
|
|
|
|
signal (SIGTERM, sig_term_handler);
|
|
|
|
signal (SIGINT, sig_term_handler);
|
|
|
|
|
|
|
|
rl_catch_signals = 0;
|
|
|
|
|
2013-11-15 11:14:25 -05:00
|
|
|
|
|
|
|
log_level = 10;
|
2013-11-08 13:58:28 -05:00
|
|
|
|
|
|
|
args_parse (argc, argv);
|
2014-09-20 18:22:35 -04:00
|
|
|
|
|
|
|
change_user_group ();
|
|
|
|
|
|
|
|
if (port > 0) {
|
|
|
|
struct sockaddr_in serv_addr;
|
|
|
|
|
|
|
|
sfd = socket (AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sfd < 0) {
|
|
|
|
perror ("socket");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset (&serv_addr, 0, sizeof (serv_addr));
|
|
|
|
|
|
|
|
serv_addr.sin_family = AF_INET;
|
|
|
|
serv_addr.sin_addr.s_addr = htonl (0x7f000001);
|
|
|
|
serv_addr.sin_port = htons (port);
|
|
|
|
|
|
|
|
if (bind (sfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) {
|
|
|
|
perror ("bind");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2014-09-22 18:23:40 -04:00
|
|
|
listen (sfd, 5);
|
2014-09-20 18:22:35 -04:00
|
|
|
} else {
|
|
|
|
sfd = -1;
|
|
|
|
}
|
2014-09-22 18:23:40 -04:00
|
|
|
|
|
|
|
if (unix_socket) {
|
|
|
|
assert (strlen (unix_socket) < 100);
|
|
|
|
struct sockaddr_un serv_addr;
|
|
|
|
|
|
|
|
usfd = socket (AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (usfd < 0) {
|
|
|
|
perror ("socket");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset (&serv_addr, 0, sizeof (serv_addr));
|
|
|
|
|
|
|
|
serv_addr.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
snprintf (serv_addr.sun_path, 108, "%s", unix_socket);
|
|
|
|
|
|
|
|
if (bind (usfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) {
|
|
|
|
perror ("bind");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
listen (usfd, 5);
|
|
|
|
} else {
|
|
|
|
usfd = -1;
|
|
|
|
}
|
2014-09-09 11:01:27 -04:00
|
|
|
|
|
|
|
if (daemonize) {
|
|
|
|
signal (SIGHUP, sighup_handler);
|
|
|
|
reopen_logs ();
|
|
|
|
}
|
2014-09-09 17:01:22 -04:00
|
|
|
signal (SIGUSR1, sigusr1_handler);
|
|
|
|
|
2014-09-09 13:05:36 -04:00
|
|
|
if (!disable_output) {
|
|
|
|
printf (
|
|
|
|
"Telegram-cli version " TGL_VERSION ", Copyright (C) 2013-2014 Vitaly Valtman\n"
|
|
|
|
"Telegram-cli 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 ();
|
|
|
|
|
2014-10-24 13:43:08 -04:00
|
|
|
tgl_set_rsa_key (TLS, "/etc/" PROG_NAME "/server.pub");
|
|
|
|
tgl_set_rsa_key (TLS, "tg-server.pub");
|
2014-08-21 18:02:43 -04:00
|
|
|
|
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;
|
|
|
|
}
|