2015-02-26 22:51:20 +01:00
|
|
|
/* term_mplex.c
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Toxic All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Toxic.
|
|
|
|
*
|
|
|
|
* Toxic 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Toxic 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 Toxic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits.h> /* PATH_MAX */
|
|
|
|
#include <stdio.h> /* fgets, popen, pclose */
|
|
|
|
#include <stdlib.h> /* malloc, realloc, free, getenv */
|
2015-03-11 23:38:07 +01:00
|
|
|
#include <string.h> /* strlen, strcpy, strstr, strchr, strrchr, strcat, strncmp */
|
2015-02-26 22:51:20 +01:00
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <tox/tox.h>
|
|
|
|
|
2018-10-08 19:47:08 +02:00
|
|
|
#include "execute.h"
|
2015-02-26 22:51:20 +01:00
|
|
|
#include "windows.h"
|
|
|
|
#include "term_mplex.h"
|
|
|
|
#include "toxic.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
extern struct ToxWindow *prompt;
|
|
|
|
extern struct user_settings *user_settings;
|
|
|
|
extern struct Winthread Winthread;
|
|
|
|
|
|
|
|
#if defined(PATH_MAX) && PATH_MAX > 512
|
|
|
|
#define BUFFER_SIZE PATH_MAX
|
|
|
|
#else
|
|
|
|
#define BUFFER_SIZE 512
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define PATH_SEP_S "/"
|
|
|
|
#define PATH_SEP_C '/'
|
|
|
|
|
2016-09-25 03:07:04 +02:00
|
|
|
typedef enum {
|
2015-02-26 22:51:20 +01:00
|
|
|
MPLEX_NONE,
|
|
|
|
MPLEX_SCREEN,
|
|
|
|
MPLEX_TMUX,
|
|
|
|
} mplex_status;
|
|
|
|
|
|
|
|
/* used for:
|
|
|
|
- storing screen socket name
|
|
|
|
- storing tmux session number in string form */
|
|
|
|
static char mplex_data [BUFFER_SIZE];
|
|
|
|
|
|
|
|
static char buffer [BUFFER_SIZE];
|
|
|
|
|
2015-03-14 01:33:23 +01:00
|
|
|
/* Differentiates between mplex auto-away and manual-away */
|
|
|
|
static bool auto_away_active = false;
|
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
static mplex_status mplex = MPLEX_NONE;
|
2018-10-10 05:06:57 +02:00
|
|
|
static Tox_User_Status prev_status = TOX_USER_STATUS_NONE;
|
2015-03-26 03:56:45 +01:00
|
|
|
static char prev_note [TOX_MAX_STATUS_MESSAGE_LENGTH] = "";
|
2015-03-11 23:38:07 +01:00
|
|
|
|
|
|
|
/* mutex for access to status data, for sync between:
|
|
|
|
- user command /status from ncurses thread
|
|
|
|
- auto-away POSIX timer, which runs from a separate thread
|
|
|
|
after init, should be accessed only by cmd_status()
|
|
|
|
*/
|
2015-03-14 01:33:23 +01:00
|
|
|
static pthread_mutex_t status_lock;
|
|
|
|
static pthread_t mplex_tid;
|
2015-03-11 23:38:07 +01:00
|
|
|
|
2018-09-08 19:23:07 +02:00
|
|
|
void lock_status(void)
|
2015-03-11 23:38:07 +01:00
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
pthread_mutex_lock(&status_lock);
|
2015-03-11 23:38:07 +01:00
|
|
|
}
|
|
|
|
|
2018-09-08 19:23:07 +02:00
|
|
|
void unlock_status(void)
|
2015-03-11 23:38:07 +01:00
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
pthread_mutex_unlock(&status_lock);
|
2015-03-11 23:38:07 +01:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
static char *read_into_dyn_buffer(FILE *stream)
|
2015-02-26 22:51:20 +01:00
|
|
|
{
|
|
|
|
const char *input_ptr = NULL;
|
|
|
|
char *dyn_buffer = NULL;
|
|
|
|
int dyn_buffer_size = 1; /* account for the \0 */
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
while ((input_ptr = fgets(buffer, BUFFER_SIZE, stream)) != NULL) {
|
|
|
|
int length = dyn_buffer_size + strlen(input_ptr);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (dyn_buffer) {
|
2020-11-03 00:08:54 +01:00
|
|
|
char *tmp = realloc(dyn_buffer, length);
|
|
|
|
|
|
|
|
if (tmp == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dyn_buffer = tmp;
|
2018-07-18 17:33:16 +02:00
|
|
|
} else {
|
2020-11-03 00:08:54 +01:00
|
|
|
dyn_buffer = malloc(length);
|
|
|
|
|
|
|
|
if (dyn_buffer == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
strcpy(dyn_buffer + dyn_buffer_size - 1, input_ptr);
|
2015-02-26 22:51:20 +01:00
|
|
|
dyn_buffer_size = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dyn_buffer;
|
|
|
|
}
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
static char *extract_socket_path(const char *info)
|
2015-02-26 22:51:20 +01:00
|
|
|
{
|
|
|
|
const char *search_str = " Socket";
|
2018-07-18 17:33:16 +02:00
|
|
|
const char *pos = strstr(info, search_str);
|
2015-02-26 22:51:20 +01:00
|
|
|
char *end = NULL;
|
2016-09-25 03:07:04 +02:00
|
|
|
char *path = NULL;
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!pos) {
|
2015-02-26 22:51:20 +01:00
|
|
|
return NULL;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
pos += strlen(search_str);
|
|
|
|
pos = strchr(pos, PATH_SEP_C);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!pos) {
|
2015-02-26 22:51:20 +01:00
|
|
|
return NULL;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
end = strchr(pos, '\n');
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!end) {
|
2015-02-26 22:51:20 +01:00
|
|
|
return NULL;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
|
|
|
*end = '\0';
|
2018-07-18 17:33:16 +02:00
|
|
|
end = strrchr(pos, '.');
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!end) {
|
2015-02-26 22:51:20 +01:00
|
|
|
return NULL;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
path = (char *) malloc(end - pos + 1);
|
2015-02-26 22:51:20 +01:00
|
|
|
*end = '\0';
|
2018-07-18 17:33:16 +02:00
|
|
|
return strcpy(path, pos);
|
2015-02-26 22:51:20 +01:00
|
|
|
}
|
|
|
|
|
2018-09-08 19:23:07 +02:00
|
|
|
static int detect_gnu_screen(void)
|
2015-02-26 22:51:20 +01:00
|
|
|
{
|
|
|
|
FILE *session_info_stream = NULL;
|
|
|
|
char *socket_name = NULL, *socket_path = NULL;
|
|
|
|
char *dyn_buffer = NULL;
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
socket_name = getenv("STY");
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!socket_name) {
|
2015-02-26 22:51:20 +01:00
|
|
|
goto nomplex;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
session_info_stream = popen("env LC_ALL=C screen -ls", "r");
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!session_info_stream) {
|
2015-02-26 22:51:20 +01:00
|
|
|
goto nomplex;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
dyn_buffer = read_into_dyn_buffer(session_info_stream);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!dyn_buffer) {
|
2015-02-26 22:51:20 +01:00
|
|
|
goto nomplex;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
pclose(session_info_stream);
|
2015-02-26 22:51:20 +01:00
|
|
|
session_info_stream = NULL;
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
socket_path = extract_socket_path(dyn_buffer);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!socket_path) {
|
2015-02-26 22:51:20 +01:00
|
|
|
goto nomplex;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
free(dyn_buffer);
|
2015-02-26 22:51:20 +01:00
|
|
|
dyn_buffer = NULL;
|
2015-08-27 09:38:08 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (strlen(socket_path) + strlen(PATH_SEP_S) + strlen(socket_name) >= sizeof(mplex_data)) {
|
2015-08-27 09:38:08 +02:00
|
|
|
goto nomplex;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-08-27 09:38:08 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
strcpy(mplex_data, socket_path);
|
|
|
|
strcat(mplex_data, PATH_SEP_S);
|
|
|
|
strcat(mplex_data, socket_name);
|
|
|
|
free(socket_path);
|
2015-02-26 22:51:20 +01:00
|
|
|
socket_path = NULL;
|
|
|
|
|
|
|
|
mplex = MPLEX_SCREEN;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
nomplex:
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (session_info_stream) {
|
|
|
|
pclose(session_info_stream);
|
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (dyn_buffer) {
|
|
|
|
free(dyn_buffer);
|
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (socket_path) {
|
2015-08-27 09:38:08 +02:00
|
|
|
free(socket_path);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-08 19:23:07 +02:00
|
|
|
static int detect_tmux(void)
|
2015-02-26 22:51:20 +01:00
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
char *tmux_env = getenv("TMUX"), *pos;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!tmux_env) {
|
2015-02-26 22:51:20 +01:00
|
|
|
return 0;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
|
|
|
/* find second separator */
|
2018-07-18 17:33:16 +02:00
|
|
|
pos = strrchr(tmux_env, ',');
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!pos) {
|
2015-02-26 22:51:20 +01:00
|
|
|
return 0;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2017-03-10 18:07:56 +01:00
|
|
|
/* store the session id for later use */
|
2018-07-18 17:33:16 +02:00
|
|
|
snprintf(mplex_data, sizeof(mplex_data), "$%s", pos + 1);
|
2015-02-26 22:51:20 +01:00
|
|
|
mplex = MPLEX_TMUX;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Checks whether a terminal multiplexer (mplex) is present, and finds
|
|
|
|
its unix socket.
|
|
|
|
|
|
|
|
GNU screen and tmux are supported.
|
|
|
|
|
|
|
|
Returns 1 if present, 0 otherwise. This value can be used to determine
|
|
|
|
whether an auto-away detection timer is needed.
|
|
|
|
*/
|
2018-09-08 19:23:07 +02:00
|
|
|
static int detect_mplex(void)
|
2015-02-26 22:51:20 +01:00
|
|
|
{
|
|
|
|
/* try screen, and if fails try tmux */
|
2018-07-18 17:33:16 +02:00
|
|
|
return detect_gnu_screen() || detect_tmux();
|
2015-02-26 22:51:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Detects gnu screen session attached/detached by examining permissions of
|
|
|
|
the session's unix socket.
|
|
|
|
*/
|
2018-09-08 19:23:07 +02:00
|
|
|
static int gnu_screen_is_detached(void)
|
2015-02-26 22:51:20 +01:00
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (mplex != MPLEX_SCREEN) {
|
2015-02-26 22:51:20 +01:00
|
|
|
return 0;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
|
|
|
struct stat sb;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (stat(mplex_data, &sb) != 0) {
|
2015-02-26 22:51:20 +01:00
|
|
|
return 0;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
|
|
|
/* execution permission (x) means attached */
|
2018-07-18 17:33:16 +02:00
|
|
|
return !(sb.st_mode & S_IXUSR);
|
2015-02-26 22:51:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Detects tmux attached/detached by getting session data and finding the
|
2017-03-10 18:07:56 +01:00
|
|
|
current session's entry.
|
|
|
|
*/
|
2018-09-08 19:23:07 +02:00
|
|
|
static int tmux_is_detached(void)
|
2015-02-26 22:51:20 +01:00
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (mplex != MPLEX_TMUX) {
|
2015-02-26 22:51:20 +01:00
|
|
|
return 0;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
|
|
|
FILE *session_info_stream = NULL;
|
|
|
|
char *dyn_buffer = NULL, *search_str = NULL;
|
2017-03-10 18:07:56 +01:00
|
|
|
char *entry_pos;
|
|
|
|
int detached;
|
2018-07-18 17:33:16 +02:00
|
|
|
const int numstr_len = strlen(mplex_data);
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2017-03-10 18:07:56 +01:00
|
|
|
/* get the number of attached clients for each session */
|
2018-07-18 17:33:16 +02:00
|
|
|
session_info_stream = popen("tmux list-sessions -F \"#{session_id} #{session_attached}\"", "r");
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!session_info_stream) {
|
2015-02-26 22:51:20 +01:00
|
|
|
goto fail;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
dyn_buffer = read_into_dyn_buffer(session_info_stream);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!dyn_buffer) {
|
2015-02-26 22:51:20 +01:00
|
|
|
goto fail;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
pclose(session_info_stream);
|
2015-02-26 22:51:20 +01:00
|
|
|
session_info_stream = NULL;
|
|
|
|
|
|
|
|
/* prepare search string, for finding the current session's entry */
|
2018-07-18 17:33:16 +02:00
|
|
|
search_str = (char *) malloc(numstr_len + 2);
|
2015-02-26 22:51:20 +01:00
|
|
|
search_str[0] = '\n';
|
2018-07-18 17:33:16 +02:00
|
|
|
strcpy(search_str + 1, mplex_data);
|
2015-02-26 22:51:20 +01:00
|
|
|
|
|
|
|
/* do the search */
|
2018-07-18 17:33:16 +02:00
|
|
|
if (strncmp(dyn_buffer, search_str + 1, numstr_len) == 0) {
|
2015-02-26 22:51:20 +01:00
|
|
|
entry_pos = dyn_buffer;
|
2018-07-18 17:33:16 +02:00
|
|
|
} else {
|
|
|
|
entry_pos = strstr(dyn_buffer, search_str);
|
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (! entry_pos) {
|
2015-02-26 22:51:20 +01:00
|
|
|
goto fail;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
entry_pos = strchr(entry_pos, ' ') + 1;
|
|
|
|
detached = strncmp(entry_pos, "0\n", 2) == 0;
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
free(search_str);
|
2015-02-26 22:51:20 +01:00
|
|
|
search_str = NULL;
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
free(dyn_buffer);
|
2015-02-26 22:51:20 +01:00
|
|
|
dyn_buffer = NULL;
|
|
|
|
|
2017-03-10 18:07:56 +01:00
|
|
|
return detached;
|
2015-02-26 22:51:20 +01:00
|
|
|
|
|
|
|
fail:
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (session_info_stream) {
|
|
|
|
pclose(session_info_stream);
|
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (dyn_buffer) {
|
|
|
|
free(dyn_buffer);
|
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (search_str) {
|
|
|
|
free(search_str);
|
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Checks whether there is a terminal multiplexer present, but in detached
|
|
|
|
state. Returns 1 if detached, 0 if attached or if there is no terminal
|
|
|
|
multiplexer.
|
|
|
|
|
|
|
|
If detect_mplex_socket() failed to find a mplex, there is no need to call
|
|
|
|
this function. If it did find one, this function can be used to periodically
|
|
|
|
sample its state and update away status according to attached/detached state
|
|
|
|
of the mplex.
|
|
|
|
*/
|
2018-09-08 19:23:07 +02:00
|
|
|
static int mplex_is_detached(void)
|
2015-02-26 22:51:20 +01:00
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
return gnu_screen_is_detached() || tmux_is_detached();
|
2015-02-26 22:51:20 +01:00
|
|
|
}
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
static void mplex_timer_handler(Tox *m)
|
2015-02-26 22:51:20 +01:00
|
|
|
{
|
2018-10-10 05:06:57 +02:00
|
|
|
Tox_User_Status current_status, new_status;
|
2015-02-26 22:51:20 +01:00
|
|
|
const char *new_note;
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (mplex == MPLEX_NONE) {
|
2015-02-26 22:51:20 +01:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
int detached = mplex_is_detached();
|
2015-03-11 23:38:07 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
|
|
|
current_status = tox_self_get_status(m);
|
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2016-09-25 03:07:04 +02:00
|
|
|
if (auto_away_active && current_status == TOX_USER_STATUS_AWAY && !detached) {
|
2015-03-14 01:33:23 +01:00
|
|
|
auto_away_active = false;
|
2015-02-26 22:51:20 +01:00
|
|
|
new_status = prev_status;
|
|
|
|
new_note = prev_note;
|
2016-09-25 03:07:04 +02:00
|
|
|
} else if (current_status == TOX_USER_STATUS_NONE && detached) {
|
2015-03-14 01:33:23 +01:00
|
|
|
auto_away_active = true;
|
2015-02-26 22:51:20 +01:00
|
|
|
prev_status = current_status;
|
2015-03-26 03:56:45 +01:00
|
|
|
new_status = TOX_USER_STATUS_AWAY;
|
2018-07-18 17:33:16 +02:00
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
2015-04-10 06:16:01 +02:00
|
|
|
size_t slen = tox_self_get_status_message_size(m);
|
2018-07-18 17:33:16 +02:00
|
|
|
tox_self_get_status_message(m, (uint8_t *) prev_note);
|
2015-04-10 06:16:01 +02:00
|
|
|
prev_note[slen] = '\0';
|
2018-07-18 17:33:16 +02:00
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
2015-02-26 22:51:20 +01:00
|
|
|
new_note = user_settings->mplex_away_note;
|
2018-07-18 17:33:16 +02:00
|
|
|
} else {
|
2015-02-26 22:51:20 +01:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-10-08 19:47:08 +02:00
|
|
|
char status_str[MAX_STR_SIZE];
|
|
|
|
char note_str[MAX_STR_SIZE];
|
|
|
|
const char *status = new_status == TOX_USER_STATUS_AWAY ? "away" :
|
2019-12-08 05:17:02 +01:00
|
|
|
new_status == TOX_USER_STATUS_BUSY ? "busy" : "online";
|
2018-10-08 19:47:08 +02:00
|
|
|
snprintf(status_str, sizeof(status_str), "/status %s", status);
|
|
|
|
snprintf(note_str, sizeof(status_str), "/note %s", new_note);
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
2018-10-08 19:47:08 +02:00
|
|
|
execute(prompt->chatwin->history, prompt, m, status_str, GLOBAL_COMMAND_MODE);
|
|
|
|
execute(prompt->chatwin->history, prompt, m, note_str, GLOBAL_COMMAND_MODE);
|
2018-07-18 17:33:16 +02:00
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
2015-02-26 22:51:20 +01:00
|
|
|
}
|
|
|
|
|
2015-03-11 23:38:07 +01:00
|
|
|
/* Time in seconds between calls to mplex_timer_handler */
|
|
|
|
#define MPLEX_TIMER_INTERVAL 5
|
|
|
|
|
|
|
|
void *mplex_timer_thread(void *data)
|
2015-02-26 22:51:20 +01:00
|
|
|
{
|
2015-03-11 23:38:07 +01:00
|
|
|
Tox *m = (Tox *) data;
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2015-03-11 23:38:07 +01:00
|
|
|
while (true) {
|
|
|
|
sleep(MPLEX_TIMER_INTERVAL);
|
|
|
|
mplex_timer_handler(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
int init_mplex_away_timer(Tox *m)
|
2015-03-11 23:38:07 +01:00
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (! detect_mplex()) {
|
2015-03-11 23:38:07 +01:00
|
|
|
return 0;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (! user_settings->mplex_away) {
|
2015-03-11 23:38:07 +01:00
|
|
|
return 0;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2015-03-11 23:38:07 +01:00
|
|
|
/* status access mutex */
|
2018-07-18 17:33:16 +02:00
|
|
|
if (pthread_mutex_init(&status_lock, NULL) != 0) {
|
2015-03-11 23:38:07 +01:00
|
|
|
return -1;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (pthread_create(&mplex_tid, NULL, mplex_timer_thread, (void *) m) != 0) {
|
2015-03-11 23:38:07 +01:00
|
|
|
return -1;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2015-03-11 23:38:07 +01:00
|
|
|
return 0;
|
2015-02-26 22:51:20 +01:00
|
|
|
}
|