2013-07-30 21:47:40 +02:00
|
|
|
/*
|
|
|
|
* Toxic -- Tox Curses Client
|
|
|
|
*/
|
|
|
|
|
2013-08-23 09:50:04 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-30 21:47:40 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
2013-08-28 20:47:47 +02:00
|
|
|
#include <stdlib.h>
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2013-08-24 04:15:15 +02:00
|
|
|
#include <tox/tox.h>
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2013-08-13 00:50:43 +02:00
|
|
|
#include "friendlist.h"
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2013-08-28 11:46:09 +02:00
|
|
|
extern char *DATA_FILE;
|
2013-09-07 01:59:45 +02:00
|
|
|
extern ToxWindow *prompt;
|
|
|
|
|
2013-07-30 21:47:40 +02:00
|
|
|
typedef struct {
|
2013-08-23 23:03:44 +02:00
|
|
|
uint8_t name[TOX_MAX_NAME_LENGTH];
|
2013-09-18 23:30:35 +02:00
|
|
|
uint16_t namelength;
|
2013-09-02 06:18:20 +02:00
|
|
|
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH];
|
2013-09-10 10:04:13 +02:00
|
|
|
uint16_t statusmsg_len;
|
2013-08-16 19:11:09 +02:00
|
|
|
int num;
|
|
|
|
int chatwin;
|
2013-09-05 03:25:59 +02:00
|
|
|
bool active;
|
|
|
|
bool online;
|
|
|
|
TOX_USERSTATUS status;
|
2013-07-30 21:47:40 +02:00
|
|
|
} friend_t;
|
|
|
|
|
|
|
|
static friend_t friends[MAX_FRIENDS_NUM];
|
|
|
|
static int num_friends = 0;
|
2013-07-31 19:20:03 +02:00
|
|
|
static int num_selected = 0;
|
2013-07-30 21:47:40 +02:00
|
|
|
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-09-05 03:25:59 +02:00
|
|
|
if (num < 0 || num >= num_friends)
|
2013-08-16 19:11:09 +02:00
|
|
|
return;
|
|
|
|
|
2013-09-04 08:05:36 +02:00
|
|
|
if (friends[num].chatwin == -1)
|
2013-09-07 01:59:45 +02:00
|
|
|
friends[num].chatwin = add_window(m, new_chat(m, prompt, friends[num].num));
|
2013-07-31 20:20:16 +02:00
|
|
|
}
|
|
|
|
|
2013-09-05 03:25:59 +02:00
|
|
|
void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status)
|
|
|
|
{
|
|
|
|
if (num < 0 || num >= num_friends)
|
|
|
|
return;
|
|
|
|
|
2013-09-15 22:38:38 +02:00
|
|
|
friends[num].online = status == 1 ? true : false;
|
2013-09-05 03:25:59 +02:00
|
|
|
}
|
|
|
|
|
2013-09-09 07:08:06 +02:00
|
|
|
void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-09-05 03:25:59 +02:00
|
|
|
if (len >= TOX_MAX_NAME_LENGTH || num < 0 || num >= num_friends)
|
2013-08-16 19:11:09 +02:00
|
|
|
return;
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
memcpy((char *) &friends[num].name, (char *) str, len);
|
2013-09-18 01:11:23 +02:00
|
|
|
friends[num].namelength = len;
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
2013-09-05 03:25:59 +02:00
|
|
|
void friendlist_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status)
|
|
|
|
{
|
|
|
|
if (num < 0 || num >= num_friends)
|
|
|
|
return;
|
|
|
|
|
|
|
|
friends[num].status = status;
|
|
|
|
}
|
|
|
|
|
2013-09-03 05:27:34 +02:00
|
|
|
void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-09-05 03:25:59 +02:00
|
|
|
if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num < 0 || num >= num_friends)
|
2013-08-16 19:11:09 +02:00
|
|
|
return;
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2013-09-10 10:04:13 +02:00
|
|
|
friends[num].statusmsg_len = len;
|
2013-09-09 06:56:47 +02:00
|
|
|
memcpy((char *) &friends[num].statusmsg, (char *) str, len);
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
int friendlist_onFriendAdded(Tox *m, int num)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-28 20:47:47 +02:00
|
|
|
if (num_friends < 0 || num_friends >= MAX_FRIENDS_NUM)
|
2013-08-16 19:11:09 +02:00
|
|
|
return -1;
|
|
|
|
|
2013-08-28 11:46:09 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i <= num_friends; ++i) {
|
|
|
|
if (!friends[i].active) {
|
|
|
|
friends[i].num = num;
|
|
|
|
friends[i].active = true;
|
|
|
|
friends[i].chatwin = -1;
|
2013-09-05 03:25:59 +02:00
|
|
|
friends[i].online = false;
|
2013-09-05 07:34:23 +02:00
|
|
|
friends[i].status = TOX_USERSTATUS_NONE;
|
2013-09-18 01:11:23 +02:00
|
|
|
friends[i].namelength = tox_getname(m, num, friends[i].name);
|
2013-09-04 03:31:50 +02:00
|
|
|
|
2013-09-18 01:11:23 +02:00
|
|
|
if (friends[i].namelength == -1 || friends[i].name[0] == '\0') {
|
2013-09-09 12:54:36 +02:00
|
|
|
strcpy((char *) friends[i].name, UNKNOWN_NAME);
|
2013-09-18 01:11:23 +02:00
|
|
|
friends[i].namelength = strlen(UNKNOWN_NAME) + 1;
|
|
|
|
}
|
2013-09-04 03:31:50 +02:00
|
|
|
|
2013-08-28 11:46:09 +02:00
|
|
|
if (i == num_friends)
|
|
|
|
++num_friends;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
2013-09-04 23:22:16 +02:00
|
|
|
static void select_friend(Tox *m, wint_t key)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-28 11:46:09 +02:00
|
|
|
if (num_friends < 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int n = num_selected;
|
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
if (key == KEY_UP) {
|
2013-09-04 23:22:16 +02:00
|
|
|
while (--n != num_selected) {
|
|
|
|
if (n < 0) n = num_friends - 1;
|
2013-08-28 11:46:09 +02:00
|
|
|
if (friends[n].active) {
|
|
|
|
num_selected = n;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
} else if (key == KEY_DOWN) {
|
2013-09-06 06:56:55 +02:00
|
|
|
while (++n != num_selected) {
|
|
|
|
n = n % num_friends;
|
2013-08-28 11:46:09 +02:00
|
|
|
if (friends[n].active) {
|
|
|
|
num_selected = n;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-09-04 23:22:16 +02:00
|
|
|
} else return; /* Bad key input */
|
|
|
|
|
|
|
|
/* If we reach this something is wrong */
|
|
|
|
endwin();
|
|
|
|
tox_kill(m);
|
2013-09-12 07:33:41 +02:00
|
|
|
fprintf(stderr, "select_friend() failed. Aborting...\n");
|
2013-09-11 06:02:27 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-08-28 11:46:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key)
|
|
|
|
{
|
|
|
|
tox_delfriend(m, f_num);
|
|
|
|
memset(&(friends[f_num]), 0, sizeof(friend_t));
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
2013-08-28 20:47:47 +02:00
|
|
|
for (i = num_friends; i > 0; --i) {
|
2013-08-28 11:53:47 +02:00
|
|
|
if (friends[i-1].active)
|
2013-08-28 11:46:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
num_friends = i;
|
2013-09-05 07:34:23 +02:00
|
|
|
|
|
|
|
store_data(m, DATA_FILE);
|
2013-09-04 23:22:16 +02:00
|
|
|
select_friend(m, KEY_DOWN);
|
2013-08-28 11:46:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key)
|
|
|
|
{
|
|
|
|
if (key == KEY_UP || key == KEY_DOWN) {
|
2013-09-04 23:22:16 +02:00
|
|
|
select_friend(m, key);
|
2013-08-16 19:11:09 +02:00
|
|
|
} else if (key == '\n') {
|
|
|
|
/* Jump to chat window if already open */
|
|
|
|
if (friends[num_selected].chatwin != -1) {
|
2013-08-17 10:00:19 +02:00
|
|
|
set_active_window(friends[num_selected].chatwin);
|
2013-08-16 19:11:09 +02:00
|
|
|
} else {
|
2013-09-07 01:59:45 +02:00
|
|
|
friends[num_selected].chatwin = add_window(m, new_chat(m, prompt, friends[num_selected].num));
|
2013-09-06 00:24:58 +02:00
|
|
|
set_active_window(friends[num_selected].chatwin);
|
2013-08-05 07:57:29 +02:00
|
|
|
}
|
2013-08-28 11:46:09 +02:00
|
|
|
} else if (key == 0x107 || key == 0x8 || key == 0x7f)
|
|
|
|
delete_friend(m, self, num_selected, key);
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
static void friendlist_onDraw(ToxWindow *self, Tox *m)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
curs_set(0);
|
|
|
|
werase(self->window);
|
|
|
|
|
|
|
|
if (num_friends == 0) {
|
|
|
|
wprintw(self->window, "Empty. Add some friends! :-)\n");
|
|
|
|
} else {
|
2013-09-05 06:47:33 +02:00
|
|
|
wattron(self->window, COLOR_PAIR(CYAN) | A_BOLD);
|
2013-09-04 03:31:50 +02:00
|
|
|
wprintw(self->window, " Open chat with up/down keys and enter.\n");
|
|
|
|
wprintw(self->window, " Delete friends with the backspace key.\n\n");
|
2013-09-05 06:47:33 +02:00
|
|
|
wattroff(self->window, COLOR_PAIR(CYAN) | A_BOLD);
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_friends; ++i) {
|
2013-08-28 11:53:47 +02:00
|
|
|
if (friends[i].active) {
|
2013-08-28 11:46:09 +02:00
|
|
|
if (i == num_selected)
|
2013-09-02 04:11:47 +02:00
|
|
|
wprintw(self->window, " > ");
|
|
|
|
else
|
|
|
|
wprintw(self->window, " ");
|
2013-09-08 09:18:34 +02:00
|
|
|
|
2013-09-05 03:25:59 +02:00
|
|
|
if (friends[i].online) {
|
|
|
|
TOX_USERSTATUS status = friends[i].status;
|
2013-09-05 06:47:33 +02:00
|
|
|
int colour = WHITE;
|
2013-09-02 04:11:47 +02:00
|
|
|
|
|
|
|
switch(status) {
|
|
|
|
case TOX_USERSTATUS_NONE:
|
2013-09-05 06:47:33 +02:00
|
|
|
colour = GREEN;
|
2013-09-02 04:11:47 +02:00
|
|
|
break;
|
|
|
|
case TOX_USERSTATUS_AWAY:
|
2013-09-05 06:47:33 +02:00
|
|
|
colour = YELLOW;
|
2013-09-02 04:11:47 +02:00
|
|
|
break;
|
|
|
|
case TOX_USERSTATUS_BUSY:
|
2013-09-05 06:47:33 +02:00
|
|
|
colour = RED;
|
2013-09-02 04:11:47 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-04 03:31:50 +02:00
|
|
|
wprintw(self->window, "[");
|
2013-09-05 07:34:23 +02:00
|
|
|
wattron(self->window, COLOR_PAIR(colour) | A_BOLD);
|
2013-09-04 03:31:50 +02:00
|
|
|
wprintw(self->window, "O");
|
2013-09-05 07:34:23 +02:00
|
|
|
wattroff(self->window, COLOR_PAIR(colour) | A_BOLD);
|
2013-09-10 10:04:13 +02:00
|
|
|
wprintw(self->window, "]%s (", friends[i].name);
|
|
|
|
|
|
|
|
int x, y;
|
|
|
|
getmaxyx(self->window, y, x);
|
|
|
|
|
2013-09-13 08:02:49 +02:00
|
|
|
/* Reset friends[i].statusmsg on window resize */
|
|
|
|
if (x != self->x) {
|
|
|
|
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
|
|
|
|
tox_copy_statusmessage(m, friends[i].num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
|
|
|
|
snprintf(friends[i].statusmsg, sizeof(friends[i].statusmsg), "%s", statusmsg);
|
2013-09-15 22:38:38 +02:00
|
|
|
friends[i].statusmsg_len = tox_get_statusmessage_size(m, self->num);
|
2013-09-13 08:02:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self->x = x;
|
|
|
|
|
|
|
|
/* Truncate note if it doesn't fit on one line */
|
|
|
|
uint16_t maxlen = x - getcurx(self->window) - 2;
|
2013-09-10 10:04:13 +02:00
|
|
|
if (friends[i].statusmsg_len > maxlen) {
|
|
|
|
friends[i].statusmsg[maxlen] = '\0';
|
|
|
|
friends[i].statusmsg_len = maxlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
wprintw(self->window, "%s)\n", friends[i].statusmsg);
|
2013-09-02 04:11:47 +02:00
|
|
|
} else {
|
2013-09-09 07:08:06 +02:00
|
|
|
wprintw(self->window, "[O]%s\n", friends[i].name);
|
2013-09-02 04:11:47 +02:00
|
|
|
}
|
2013-08-28 11:46:09 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
2013-09-05 03:25:59 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
wrefresh(self->window);
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
void disable_chatwin(int f_num)
|
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
friends[f_num].chatwin = -1;
|
2013-08-05 07:57:29 +02:00
|
|
|
}
|
|
|
|
|
2013-09-18 01:15:29 +02:00
|
|
|
/* Returns the respective friend number of name. Returns -1 on no match */
|
2013-09-18 01:11:23 +02:00
|
|
|
int get_friendnum(uint8_t *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int len = strlen(name);
|
|
|
|
|
|
|
|
for (i = 0; i < num_friends; ++i) {
|
|
|
|
if (strncmp(friends[i].name, name, len) == 0)
|
|
|
|
return friends[i].num;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
static void friendlist_onInit(ToxWindow *self, Tox *m)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-07-30 21:47:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-17 10:00:19 +02:00
|
|
|
ToxWindow new_friendlist()
|
2013-08-16 19:11:09 +02:00
|
|
|
{
|
|
|
|
ToxWindow ret;
|
|
|
|
memset(&ret, 0, sizeof(ret));
|
|
|
|
|
|
|
|
ret.onKey = &friendlist_onKey;
|
|
|
|
ret.onDraw = &friendlist_onDraw;
|
|
|
|
ret.onInit = &friendlist_onInit;
|
|
|
|
ret.onMessage = &friendlist_onMessage;
|
2013-09-05 03:25:59 +02:00
|
|
|
ret.onConnectionChange = &friendlist_onConnectionChange;
|
2013-08-16 19:11:09 +02:00
|
|
|
ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message
|
|
|
|
ret.onNickChange = &friendlist_onNickChange;
|
2013-09-05 03:25:59 +02:00
|
|
|
ret.onStatusChange = &friendlist_onStatusChange;
|
2013-09-03 05:27:34 +02:00
|
|
|
ret.onStatusMessageChange = &friendlist_onStatusMessageChange;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-09-06 00:24:58 +02:00
|
|
|
strcpy(ret.name, "friends");
|
2013-08-16 19:11:09 +02:00
|
|
|
return ret;
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|