2013-07-31 03:47:40 +08:00
|
|
|
/*
|
|
|
|
* Toxic -- Tox Curses Client
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <curses.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "../../core/Messenger.h"
|
|
|
|
#include "../../core/network.h"
|
|
|
|
|
|
|
|
#include "windows.h"
|
|
|
|
|
2013-08-05 13:57:29 +08:00
|
|
|
extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM];
|
2013-08-13 05:34:46 +08:00
|
|
|
extern int add_window(Messenger *m, ToxWindow w, int n);
|
2013-08-11 11:24:11 +08:00
|
|
|
extern ToxWindow new_chat(Messenger *m, int friendnum);
|
2013-08-07 06:27:51 +08:00
|
|
|
|
|
|
|
extern int active_window;
|
2013-07-31 03:47:40 +08:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t name[MAX_NAME_LENGTH];
|
2013-08-08 06:12:59 +08:00
|
|
|
uint8_t status[MAX_STATUSMESSAGE_LENGTH];
|
2013-08-07 06:27:51 +08:00
|
|
|
int num;
|
|
|
|
int chatwin;
|
2013-07-31 03:47:40 +08:00
|
|
|
} friend_t;
|
|
|
|
|
|
|
|
static friend_t friends[MAX_FRIENDS_NUM];
|
|
|
|
static int num_friends = 0;
|
2013-08-01 01:20:03 +08:00
|
|
|
static int num_selected = 0;
|
2013-07-31 03:47:40 +08:00
|
|
|
|
2013-08-07 06:27:51 +08:00
|
|
|
void fix_name(uint8_t *name)
|
|
|
|
{
|
|
|
|
/* Remove all non alphanumeric characters */
|
|
|
|
uint8_t *p = name;
|
|
|
|
uint8_t *q = name;
|
2013-07-31 03:47:40 +08:00
|
|
|
while(*p != 0) {
|
2013-08-07 06:27:51 +08:00
|
|
|
if (isprint(*p))
|
2013-07-31 03:47:40 +08:00
|
|
|
*q++ = *p;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
*q = 0;
|
|
|
|
}
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str, uint16_t len)
|
2013-08-07 06:27:51 +08:00
|
|
|
{
|
|
|
|
if (num >= num_friends)
|
2013-08-01 02:20:16 +08:00
|
|
|
return;
|
|
|
|
|
2013-08-07 06:27:51 +08:00
|
|
|
if (friends[num].chatwin == -1) {
|
2013-08-06 04:04:06 +08:00
|
|
|
friends[num].chatwin = num;
|
|
|
|
int i;
|
|
|
|
/* Find first open slot to hold chat window */
|
2013-08-07 06:27:51 +08:00
|
|
|
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
|
2013-08-06 04:04:06 +08:00
|
|
|
if (WINDOW_STATUS[i] == -1) {
|
|
|
|
WINDOW_STATUS[i] = num;
|
2013-08-13 05:34:46 +08:00
|
|
|
add_window(m, new_chat(m, num), i);
|
2013-08-07 06:27:51 +08:00
|
|
|
active_window = i;
|
2013-08-06 04:04:06 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-08-06 06:07:08 +08:00
|
|
|
}
|
2013-08-01 02:20:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:27:51 +08:00
|
|
|
void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
|
|
|
|
{
|
|
|
|
if (len >= MAX_NAME_LENGTH || num >= num_friends)
|
2013-08-01 01:20:03 +08:00
|
|
|
return;
|
2013-07-31 03:47:40 +08:00
|
|
|
|
|
|
|
memcpy((char*) &friends[num].name, (char*) str, len);
|
|
|
|
friends[num].name[len] = 0;
|
|
|
|
fix_name(friends[num].name);
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:27:51 +08:00
|
|
|
void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
|
|
|
|
{
|
2013-08-08 06:12:59 +08:00
|
|
|
if (len >= MAX_STATUSMESSAGE_LENGTH || num >= num_friends)
|
2013-08-01 01:20:03 +08:00
|
|
|
return;
|
2013-07-31 03:47:40 +08:00
|
|
|
|
|
|
|
memcpy((char*) &friends[num].status, (char*) str, len);
|
|
|
|
friends[num].status[len] = 0;
|
|
|
|
fix_name(friends[num].status);
|
|
|
|
}
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
int friendlist_onFriendAdded(Messenger *m, int num)
|
2013-08-07 06:27:51 +08:00
|
|
|
{
|
|
|
|
if (num_friends == MAX_FRIENDS_NUM)
|
2013-07-31 03:47:40 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
friends[num_friends].num = num;
|
2013-08-11 11:24:11 +08:00
|
|
|
getname(m, num, friends[num_friends].name);
|
2013-07-31 03:47:40 +08:00
|
|
|
strcpy((char*) friends[num_friends].name, "unknown");
|
|
|
|
strcpy((char*) friends[num_friends].status, "unknown");
|
2013-08-05 13:57:29 +08:00
|
|
|
friends[num_friends++].chatwin = -1;
|
2013-07-31 03:47:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
static void friendlist_onKey(ToxWindow *self, Messenger *m, int key)
|
2013-08-07 06:27:51 +08:00
|
|
|
{
|
|
|
|
if (key == KEY_UP) {
|
|
|
|
if (--num_selected < 0)
|
2013-08-06 06:07:08 +08:00
|
|
|
num_selected = num_friends-1;
|
2013-08-01 01:20:03 +08:00
|
|
|
}
|
2013-08-07 06:27:51 +08:00
|
|
|
else if (key == KEY_DOWN) {
|
|
|
|
if (num_friends != 0)
|
2013-08-01 01:20:03 +08:00
|
|
|
num_selected = (num_selected+1) % num_friends;
|
|
|
|
}
|
2013-08-07 06:27:51 +08:00
|
|
|
else if (key == '\n') {
|
2013-08-05 13:57:29 +08:00
|
|
|
/* Jump to chat window if already open */
|
|
|
|
if (friends[num_selected].chatwin != -1) {
|
|
|
|
int i;
|
2013-08-07 06:27:51 +08:00
|
|
|
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
|
2013-08-05 13:57:29 +08:00
|
|
|
if (WINDOW_STATUS[i] == num_selected) {
|
2013-08-07 06:27:51 +08:00
|
|
|
active_window = i;
|
2013-08-05 13:57:29 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
int i;
|
2013-08-07 06:27:51 +08:00
|
|
|
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
|
2013-08-05 13:57:29 +08:00
|
|
|
if (WINDOW_STATUS[i] == -1) {
|
|
|
|
WINDOW_STATUS[i] = num_selected;
|
2013-08-06 04:04:06 +08:00
|
|
|
friends[num_selected].chatwin = num_selected;
|
2013-08-13 05:34:46 +08:00
|
|
|
add_window(m, new_chat(m, num_selected), i);
|
2013-08-07 06:27:51 +08:00
|
|
|
active_window = i;
|
2013-08-05 13:57:29 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-01 01:20:03 +08:00
|
|
|
}
|
2013-07-31 03:47:40 +08:00
|
|
|
}
|
|
|
|
|
2013-08-07 06:27:51 +08:00
|
|
|
static void friendlist_onDraw(ToxWindow *self)
|
|
|
|
{
|
2013-08-03 09:36:01 +08:00
|
|
|
curs_set(0);
|
2013-08-06 01:14:44 +08:00
|
|
|
werase(self->window);
|
2013-08-07 06:27:51 +08:00
|
|
|
if (num_friends == 0) {
|
2013-07-31 03:47:40 +08:00
|
|
|
wprintw(self->window, "Empty. Add some friends! :-)\n");
|
|
|
|
}
|
2013-08-01 01:20:03 +08:00
|
|
|
else {
|
|
|
|
wattron(self->window, COLOR_PAIR(2) | A_BOLD);
|
2013-08-01 02:20:16 +08:00
|
|
|
wprintw(self->window, "Open chat with.. (up/down keys, enter)\n");
|
|
|
|
wattroff(self->window, COLOR_PAIR(2) | A_BOLD);
|
2013-08-01 01:20:03 +08:00
|
|
|
}
|
2013-07-31 03:47:40 +08:00
|
|
|
|
|
|
|
wprintw(self->window, "\n");
|
2013-08-07 06:27:51 +08:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < num_friends; ++i) {
|
|
|
|
if (i == num_selected) wattron(self->window, COLOR_PAIR(3));
|
2013-08-01 02:20:16 +08:00
|
|
|
wprintw(self->window, " [#%d] ", friends[i].num);
|
2013-08-07 06:27:51 +08:00
|
|
|
if (i == num_selected) wattroff(self->window, COLOR_PAIR(3));
|
2013-07-31 03:47:40 +08:00
|
|
|
|
|
|
|
attron(A_BOLD);
|
|
|
|
wprintw(self->window, "%s ", friends[i].name);
|
|
|
|
attroff(A_BOLD);
|
|
|
|
|
|
|
|
wprintw(self->window, "(%s)\n", friends[i].status);
|
|
|
|
}
|
|
|
|
wrefresh(self->window);
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:27:51 +08:00
|
|
|
void disable_chatwin(int f_num)
|
|
|
|
{
|
2013-08-05 13:57:29 +08:00
|
|
|
friends[f_num].chatwin = -1;
|
|
|
|
}
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
static void friendlist_onInit(ToxWindow *self, Messenger *m)
|
2013-08-07 06:27:51 +08:00
|
|
|
{
|
2013-07-31 03:47:40 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ToxWindow new_friendlist() {
|
|
|
|
ToxWindow ret;
|
2013-08-01 01:20:03 +08:00
|
|
|
memset(&ret, 0, sizeof(ret));
|
|
|
|
|
2013-07-31 03:47:40 +08:00
|
|
|
ret.onKey = &friendlist_onKey;
|
|
|
|
ret.onDraw = &friendlist_onDraw;
|
|
|
|
ret.onInit = &friendlist_onInit;
|
2013-08-01 02:20:16 +08:00
|
|
|
ret.onMessage = &friendlist_onMessage;
|
2013-08-09 03:01:33 +08:00
|
|
|
ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message
|
2013-08-01 01:20:03 +08:00
|
|
|
ret.onNickChange = &friendlist_onNickChange;
|
|
|
|
ret.onStatusChange = &friendlist_onStatusChange;
|
2013-07-31 03:47:40 +08:00
|
|
|
|
2013-08-07 06:27:51 +08:00
|
|
|
strcpy(ret.title, "[friends]");
|
2013-07-31 03:47:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|