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>
|
|
|
|
|
|
|
|
#include "global_commands.h"
|
|
|
|
#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;
|
2015-03-26 03:56:45 +01:00
|
|
|
static TOX_USER_STATUS prev_status = TOX_USER_STATUS_NONE;
|
|
|
|
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
|
|
|
|
|
|
|
void lock_status ()
|
|
|
|
{
|
|
|
|
pthread_mutex_lock (&status_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock_status ()
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock (&status_lock);
|
|
|
|
}
|
2015-02-26 22:51:20 +01:00
|
|
|
|
|
|
|
static char *read_into_dyn_buffer (FILE *stream)
|
|
|
|
{
|
|
|
|
const char *input_ptr = NULL;
|
|
|
|
char *dyn_buffer = NULL;
|
|
|
|
int dyn_buffer_size = 1; /* account for the \0 */
|
|
|
|
|
2016-09-25 03:07:04 +02:00
|
|
|
while ((input_ptr = fgets (buffer, BUFFER_SIZE, stream)) != NULL) {
|
2015-02-26 22:51:20 +01:00
|
|
|
int length = dyn_buffer_size + strlen (input_ptr);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (dyn_buffer)
|
2016-09-25 03:07:04 +02:00
|
|
|
dyn_buffer = (char *) realloc (dyn_buffer, length);
|
2015-02-26 22:51:20 +01:00
|
|
|
else
|
2016-09-25 03:07:04 +02:00
|
|
|
dyn_buffer = (char *) malloc (length);
|
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
strcpy (dyn_buffer + dyn_buffer_size - 1, input_ptr);
|
|
|
|
dyn_buffer_size = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dyn_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *extract_socket_path (const char *info)
|
|
|
|
{
|
|
|
|
const char *search_str = " Socket";
|
|
|
|
const char *pos = strstr (info, search_str);
|
|
|
|
char *end = NULL;
|
2016-09-25 03:07:04 +02:00
|
|
|
char *path = NULL;
|
2015-02-26 22:51:20 +01:00
|
|
|
|
|
|
|
if (!pos)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pos += strlen (search_str);
|
|
|
|
pos = strchr (pos, PATH_SEP_C);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (!pos)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
end = strchr (pos, '\n');
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (!end)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*end = '\0';
|
|
|
|
end = strrchr (pos, '.');
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (!end)
|
|
|
|
return NULL;
|
|
|
|
|
2016-09-25 03:07:04 +02:00
|
|
|
path = (char *) malloc (end - pos + 1);
|
2015-02-26 22:51:20 +01:00
|
|
|
*end = '\0';
|
|
|
|
return strcpy (path, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int detect_gnu_screen ()
|
|
|
|
{
|
|
|
|
FILE *session_info_stream = NULL;
|
|
|
|
char *socket_name = NULL, *socket_path = NULL;
|
|
|
|
char *dyn_buffer = NULL;
|
|
|
|
|
|
|
|
socket_name = getenv ("STY");
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (!socket_name)
|
|
|
|
goto nomplex;
|
|
|
|
|
|
|
|
session_info_stream = popen ("env LC_ALL=C screen -ls", "r");
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (!session_info_stream)
|
|
|
|
goto nomplex;
|
|
|
|
|
|
|
|
dyn_buffer = read_into_dyn_buffer (session_info_stream);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (!dyn_buffer)
|
|
|
|
goto nomplex;
|
|
|
|
|
|
|
|
pclose (session_info_stream);
|
|
|
|
session_info_stream = NULL;
|
|
|
|
|
|
|
|
socket_path = extract_socket_path (dyn_buffer);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (!socket_path)
|
|
|
|
goto nomplex;
|
|
|
|
|
|
|
|
free (dyn_buffer);
|
|
|
|
dyn_buffer = NULL;
|
2015-08-27 09:38:08 +02:00
|
|
|
|
|
|
|
if (strlen(socket_path) + strlen(PATH_SEP_S) + strlen(socket_name) >= sizeof(mplex_data))
|
|
|
|
goto nomplex;
|
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
strcpy (mplex_data, socket_path);
|
|
|
|
strcat (mplex_data, PATH_SEP_S);
|
|
|
|
strcat (mplex_data, socket_name);
|
|
|
|
free (socket_path);
|
|
|
|
socket_path = NULL;
|
|
|
|
|
|
|
|
mplex = MPLEX_SCREEN;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
nomplex:
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (session_info_stream)
|
|
|
|
pclose (session_info_stream);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (dyn_buffer)
|
|
|
|
free (dyn_buffer);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-08-27 09:38:08 +02:00
|
|
|
if (socket_path)
|
|
|
|
free(socket_path);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int detect_tmux ()
|
|
|
|
{
|
|
|
|
char *tmux_env = getenv ("TMUX"), *pos;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (!tmux_env)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* find second separator */
|
|
|
|
pos = strrchr (tmux_env, ',');
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (!pos)
|
|
|
|
return 0;
|
|
|
|
|
2017-03-10 18:07:56 +01:00
|
|
|
/* store the session id for later use */
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static int detect_mplex ()
|
|
|
|
{
|
|
|
|
/* try screen, and if fails try tmux */
|
|
|
|
return detect_gnu_screen () || detect_tmux ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Detects gnu screen session attached/detached by examining permissions of
|
|
|
|
the session's unix socket.
|
|
|
|
*/
|
|
|
|
static int gnu_screen_is_detached ()
|
|
|
|
{
|
|
|
|
if (mplex != MPLEX_SCREEN)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
struct stat sb;
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (stat (mplex_data, &sb) != 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* execution permission (x) means attached */
|
|
|
|
return ! (sb.st_mode & S_IXUSR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Detects tmux attached/detached by getting session data and finding the
|
2017-03-10 18:07:56 +01:00
|
|
|
current session's entry.
|
|
|
|
*/
|
2015-02-26 22:51:20 +01:00
|
|
|
static int tmux_is_detached ()
|
|
|
|
{
|
|
|
|
if (mplex != MPLEX_TMUX)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
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;
|
2015-02-26 22:51:20 +01:00
|
|
|
const int numstr_len = strlen (mplex_data);
|
|
|
|
|
2017-03-10 18:07:56 +01:00
|
|
|
/* get the number of attached clients for each session */
|
|
|
|
session_info_stream = popen ("tmux list-sessions -F \"#{session_id} #{session_attached}\"", "r");
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (!session_info_stream)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
dyn_buffer = read_into_dyn_buffer (session_info_stream);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (!dyn_buffer)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
pclose (session_info_stream);
|
|
|
|
session_info_stream = NULL;
|
|
|
|
|
|
|
|
/* prepare search string, for finding the current session's entry */
|
2017-03-10 18:07:56 +01:00
|
|
|
search_str = (char *) malloc (numstr_len + 2);
|
2015-02-26 22:51:20 +01:00
|
|
|
search_str[0] = '\n';
|
|
|
|
strcpy (search_str + 1, mplex_data);
|
|
|
|
|
|
|
|
/* do the search */
|
2017-03-10 18:07:56 +01:00
|
|
|
if (strncmp (dyn_buffer, search_str + 1, numstr_len) == 0)
|
2015-02-26 22:51:20 +01:00
|
|
|
entry_pos = dyn_buffer;
|
|
|
|
else
|
|
|
|
entry_pos = strstr (dyn_buffer, search_str);
|
|
|
|
|
|
|
|
if (! entry_pos)
|
|
|
|
goto fail;
|
|
|
|
|
2017-03-10 18:07:56 +01:00
|
|
|
entry_pos = strchr (entry_pos, ' ') + 1;
|
|
|
|
detached = strncmp (entry_pos, "0\n", 2) == 0;
|
2015-02-26 22:51:20 +01:00
|
|
|
|
|
|
|
free (search_str);
|
|
|
|
search_str = NULL;
|
|
|
|
|
|
|
|
free (dyn_buffer);
|
|
|
|
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
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (session_info_stream)
|
|
|
|
pclose (session_info_stream);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01:00
|
|
|
if (dyn_buffer)
|
|
|
|
free (dyn_buffer);
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2015-02-26 22:51:20 +01: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.
|
|
|
|
*/
|
|
|
|
static int mplex_is_detached ()
|
|
|
|
{
|
|
|
|
return gnu_screen_is_detached () || tmux_is_detached ();
|
|
|
|
}
|
|
|
|
|
2015-03-11 23:38:07 +01:00
|
|
|
static void mplex_timer_handler (Tox *m)
|
2015-02-26 22:51:20 +01:00
|
|
|
{
|
2015-03-26 03:56:45 +01:00
|
|
|
TOX_USER_STATUS current_status, new_status;
|
2015-02-26 22:51:20 +01:00
|
|
|
const char *new_note;
|
|
|
|
|
|
|
|
if (mplex == MPLEX_NONE)
|
|
|
|
return;
|
|
|
|
|
2015-03-14 01:33:23 +01:00
|
|
|
int detached = mplex_is_detached ();
|
2015-03-11 23:38:07 +01:00
|
|
|
|
|
|
|
pthread_mutex_lock (&Winthread.lock);
|
2015-03-26 03:56:45 +01:00
|
|
|
current_status = tox_self_get_status (m);
|
2015-03-11 23:38:07 +01:00
|
|
|
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;
|
2015-03-11 23:38:07 +01:00
|
|
|
pthread_mutex_lock (&Winthread.lock);
|
2015-04-10 06:16:01 +02:00
|
|
|
size_t slen = tox_self_get_status_message_size(m);
|
2016-09-25 03:07:04 +02:00
|
|
|
tox_self_get_status_message (m, (uint8_t *) prev_note);
|
2015-04-10 06:16:01 +02:00
|
|
|
prev_note[slen] = '\0';
|
2015-03-11 23:38:07 +01:00
|
|
|
pthread_mutex_unlock (&Winthread.lock);
|
2015-02-26 22:51:20 +01:00
|
|
|
new_note = user_settings->mplex_away_note;
|
2016-09-25 03:07:04 +02:00
|
|
|
} else
|
2015-02-26 22:51:20 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
char argv[3][MAX_STR_SIZE];
|
|
|
|
strcpy (argv[0], "/status");
|
2015-03-26 03:56:45 +01:00
|
|
|
strcpy (argv[1], (new_status == TOX_USER_STATUS_AWAY ? "away" :
|
|
|
|
new_status == TOX_USER_STATUS_BUSY ? "busy" : "online"));
|
2015-02-26 22:51:20 +01:00
|
|
|
argv[2][0] = '\"';
|
|
|
|
strcpy (argv[2] + 1, new_note);
|
|
|
|
strcat (argv[2], "\"");
|
|
|
|
pthread_mutex_lock (&Winthread.lock);
|
2015-03-11 23:38:07 +01:00
|
|
|
cmd_status (prompt->chatwin->history, prompt, m, 2, argv);
|
2015-02-26 22:51:20 +01:00
|
|
|
pthread_mutex_unlock (&Winthread.lock);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int init_mplex_away_timer (Tox *m)
|
|
|
|
{
|
2015-02-26 22:51:20 +01:00
|
|
|
if (! detect_mplex ())
|
2015-03-11 23:38:07 +01:00
|
|
|
return 0;
|
2015-02-26 22:51:20 +01:00
|
|
|
|
|
|
|
if (! user_settings->mplex_away)
|
2015-03-11 23:38:07 +01:00
|
|
|
return 0;
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2015-03-11 23:38:07 +01:00
|
|
|
/* status access mutex */
|
|
|
|
if (pthread_mutex_init (&status_lock, NULL) != 0)
|
|
|
|
return -1;
|
2015-02-26 22:51:20 +01:00
|
|
|
|
2015-03-11 23:38:07 +01:00
|
|
|
if (pthread_create(&mplex_tid, NULL, mplex_timer_thread, (void *) m) != 0)
|
|
|
|
return -1;
|
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
|
|
|
}
|