mirror of
https://github.com/Tha14/toxic.git
synced 2024-12-23 11:53:26 +01:00
code format/clean up
This commit is contained in:
parent
2fbe37f7cf
commit
5fd1a658bc
110
chat.c
110
chat.c
@ -16,38 +16,34 @@
|
||||
|
||||
typedef struct {
|
||||
int friendnum;
|
||||
|
||||
char line[256];
|
||||
size_t pos;
|
||||
|
||||
WINDOW* history;
|
||||
WINDOW* linewin;
|
||||
|
||||
} ChatContext;
|
||||
|
||||
extern int w_active;
|
||||
extern void del_window(ToxWindow *w, int f_num);
|
||||
extern void fix_name(uint8_t* name);
|
||||
void print_help(ChatContext* self);
|
||||
void execute(ToxWindow* self, ChatContext* ctx, char* cmd);
|
||||
extern int active_window;
|
||||
|
||||
static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) {
|
||||
ChatContext* ctx = (ChatContext*) self->x;
|
||||
extern void del_window(ToxWindow *w, int f_num);
|
||||
extern void fix_name(uint8_t *name);
|
||||
void print_help(ChatContext *self);
|
||||
void execute(ToxWindow *self, ChatContext *ctx, char *cmd);
|
||||
|
||||
static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len)
|
||||
{
|
||||
ChatContext *ctx = (ChatContext*) self->x;
|
||||
uint8_t nick[MAX_NAME_LENGTH] = {0};
|
||||
|
||||
time_t now;
|
||||
time(&now);
|
||||
struct tm * timeinfo;
|
||||
timeinfo = localtime(&now);
|
||||
|
||||
if(ctx->friendnum != num)
|
||||
if (ctx->friendnum != num)
|
||||
return;
|
||||
|
||||
getname(num, (uint8_t*) &nick);
|
||||
|
||||
msg[len-1] = '\0';
|
||||
nick[MAX_NAME_LENGTH-1] = '\0';
|
||||
|
||||
fix_name(msg);
|
||||
fix_name(nick);
|
||||
|
||||
@ -64,15 +60,14 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len)
|
||||
flash();
|
||||
}
|
||||
|
||||
static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t len) {
|
||||
ChatContext* ctx = (ChatContext*) self->x;
|
||||
|
||||
if(ctx->friendnum != num)
|
||||
static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len)
|
||||
{
|
||||
ChatContext *ctx = (ChatContext*) self->x;
|
||||
if (ctx->friendnum != num)
|
||||
return;
|
||||
|
||||
nick[len-1] = '\0';
|
||||
fix_name(nick);
|
||||
|
||||
snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num);
|
||||
|
||||
wattron(ctx->history, COLOR_PAIR(3));
|
||||
@ -80,7 +75,8 @@ static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t
|
||||
wattroff(ctx->history, COLOR_PAIR(3));
|
||||
}
|
||||
|
||||
static void chat_onStatusChange(ToxWindow* self, int num, uint8_t* status, uint16_t len) {
|
||||
static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@ -89,35 +85,33 @@ int string_is_empty(char *string)
|
||||
{
|
||||
int rc = 0;
|
||||
char *copy = strdup(string);
|
||||
|
||||
rc = ((strtok(copy, " ") == NULL) ? 1:0);
|
||||
free(copy);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
static void chat_onKey(ToxWindow* self, int key) {
|
||||
ChatContext* ctx = (ChatContext*) self->x;
|
||||
|
||||
static void chat_onKey(ToxWindow *self, int key)
|
||||
{
|
||||
ChatContext *ctx = (ChatContext*) self->x;
|
||||
time_t now;
|
||||
time(&now);
|
||||
struct tm * timeinfo;
|
||||
timeinfo = localtime(&now);
|
||||
|
||||
/* PRINTABLE characters: Add to line */
|
||||
if(isprint(key)) {
|
||||
if(ctx->pos != sizeof(ctx->line)-1) {
|
||||
/* Add printable characters to line */
|
||||
if (isprint(key)) {
|
||||
if (ctx->pos != sizeof(ctx->line)-1) {
|
||||
ctx->line[ctx->pos++] = key;
|
||||
ctx->line[ctx->pos] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
/* RETURN key: Execute command or print line */
|
||||
else if(key == '\n') {
|
||||
else if (key == '\n') {
|
||||
if (ctx->line[0] == '/')
|
||||
execute(self, ctx, ctx->line);
|
||||
else {
|
||||
if(!string_is_empty(ctx->line)) {
|
||||
if (!string_is_empty(ctx->line)) {
|
||||
/* make sure the string has at least non-space character */
|
||||
wattron(ctx->history, COLOR_PAIR(2));
|
||||
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
|
||||
@ -127,7 +121,7 @@ static void chat_onKey(ToxWindow* self, int key) {
|
||||
wattroff(ctx->history, COLOR_PAIR(1));
|
||||
wprintw(ctx->history, "%s\n", ctx->line);
|
||||
}
|
||||
if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) {
|
||||
if (m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) {
|
||||
wattron(ctx->history, COLOR_PAIR(3));
|
||||
wprintw(ctx->history, " * Failed to send message.\n");
|
||||
wattroff(ctx->history, COLOR_PAIR(3));
|
||||
@ -138,29 +132,32 @@ static void chat_onKey(ToxWindow* self, int key) {
|
||||
}
|
||||
|
||||
/* BACKSPACE key: Remove one character from line */
|
||||
else if(key == 0x107 || key == 0x8 || key == 0x7f) {
|
||||
if(ctx->pos != 0) {
|
||||
else if (key == 0x107 || key == 0x8 || key == 0x7f) {
|
||||
if (ctx->pos != 0) {
|
||||
ctx->line[--ctx->pos] = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void execute(ToxWindow* self, ChatContext* ctx, char* cmd)
|
||||
void execute(ToxWindow *self, ChatContext *ctx, char *cmd)
|
||||
{
|
||||
if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
|
||||
wclear(self->window);
|
||||
wclear(ctx->history);
|
||||
}
|
||||
|
||||
else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h"))
|
||||
print_help(ctx);
|
||||
|
||||
else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) {
|
||||
endwin();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
else if (!strncmp(cmd, "/status ", strlen("/status "))) {
|
||||
char* msg;
|
||||
char *msg;
|
||||
msg = strchr(cmd, ' ');
|
||||
if(msg == NULL) {
|
||||
if (msg == NULL) {
|
||||
wprintw(ctx->history, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
@ -168,10 +165,11 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd)
|
||||
m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1);
|
||||
wprintw(ctx->history, "Status set to: %s\n", msg);
|
||||
}
|
||||
|
||||
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
|
||||
char* nick;
|
||||
char *nick;
|
||||
nick = strchr(cmd, ' ');
|
||||
if(nick == NULL) {
|
||||
if (nick == NULL) {
|
||||
wprintw(ctx->history, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
@ -179,7 +177,8 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd)
|
||||
setname((uint8_t*) nick, strlen(nick)+1);
|
||||
wprintw(ctx->history, "Nickname set to: %s\n", nick);
|
||||
}
|
||||
else if(!strcmp(cmd, "/myid")) {
|
||||
|
||||
else if (!strcmp(cmd, "/myid")) {
|
||||
char id[32*2 + 1] = {0};
|
||||
int i;
|
||||
for (i = 0; i < 32; i++) {
|
||||
@ -189,48 +188,46 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd)
|
||||
}
|
||||
wprintw(ctx->history, "Your ID: %s\n", id);
|
||||
}
|
||||
|
||||
else if (strcmp(ctx->line, "/close") == 0) {
|
||||
w_active = 0; // Go to prompt screen
|
||||
active_window = 0; // Go to prompt screen
|
||||
int f_num = ctx->friendnum;
|
||||
delwin(ctx->linewin);
|
||||
del_window(self, f_num);
|
||||
}
|
||||
|
||||
else
|
||||
wprintw(ctx->history, "Invalid command.\n");
|
||||
}
|
||||
|
||||
static void chat_onDraw(ToxWindow* self) {
|
||||
static void chat_onDraw(ToxWindow *self)
|
||||
{
|
||||
curs_set(1);
|
||||
int x, y;
|
||||
ChatContext* ctx = (ChatContext*) self->x;
|
||||
|
||||
ChatContext *ctx = (ChatContext*) self->x;
|
||||
getmaxyx(self->window, y, x);
|
||||
|
||||
(void) x;
|
||||
if(y < 3)
|
||||
return;
|
||||
if (y < 3) return;
|
||||
|
||||
wclear(ctx->linewin);
|
||||
mvwhline(ctx->linewin, 0, 0, '_', COLS);
|
||||
mvwprintw(self->window, y-1, 0, "%s\n", ctx->line);
|
||||
|
||||
wrefresh(self->window);
|
||||
}
|
||||
|
||||
static void chat_onInit(ToxWindow* self) {
|
||||
static void chat_onInit(ToxWindow *self)
|
||||
{
|
||||
int x, y;
|
||||
ChatContext* ctx = (ChatContext*) self->x;
|
||||
|
||||
ChatContext *ctx = (ChatContext*) self->x;
|
||||
getmaxyx(self->window, y, x);
|
||||
|
||||
ctx->history = subwin(self->window, y - 4, x, 0, 0);
|
||||
scrollok(ctx->history, 1);
|
||||
|
||||
ctx->linewin = subwin(self->window, 2, x, y - 3, 0);
|
||||
print_help(ctx);
|
||||
}
|
||||
|
||||
void print_help(ChatContext* self) {
|
||||
void print_help(ChatContext *self)
|
||||
{
|
||||
wattron(self->history, COLOR_PAIR(2) | A_BOLD);
|
||||
wprintw(self->history, "Commands:\n");
|
||||
wattroff(self->history, A_BOLD);
|
||||
@ -246,9 +243,9 @@ void print_help(ChatContext* self) {
|
||||
wattroff(self->history, COLOR_PAIR(2));
|
||||
}
|
||||
|
||||
ToxWindow new_chat(int friendnum) {
|
||||
ToxWindow new_chat(int friendnum)
|
||||
{
|
||||
ToxWindow ret;
|
||||
|
||||
memset(&ret, 0, sizeof(ret));
|
||||
|
||||
ret.onKey = &chat_onKey;
|
||||
@ -264,9 +261,8 @@ ToxWindow new_chat(int friendnum) {
|
||||
|
||||
snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum);
|
||||
|
||||
ChatContext* x = calloc(1, sizeof(ChatContext));
|
||||
ChatContext *x = calloc(1, sizeof(ChatContext));
|
||||
x->friendnum = friendnum;
|
||||
|
||||
ret.x = (void*) x;
|
||||
return ret;
|
||||
}
|
||||
|
104
friendlist.c
104
friendlist.c
@ -15,60 +15,56 @@
|
||||
extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM];
|
||||
extern int add_window(ToxWindow w, int n);
|
||||
extern ToxWindow new_chat(int friendnum);
|
||||
extern int w_active;
|
||||
|
||||
extern int active_window;
|
||||
|
||||
typedef struct {
|
||||
uint8_t name[MAX_NAME_LENGTH];
|
||||
uint8_t status[MAX_USERSTATUS_LENGTH];
|
||||
int num;
|
||||
int chatwin;
|
||||
int num;
|
||||
int chatwin;
|
||||
} friend_t;
|
||||
|
||||
static friend_t friends[MAX_FRIENDS_NUM];
|
||||
static int num_friends = 0;
|
||||
static int num_selected = 0;
|
||||
|
||||
|
||||
void fix_name(uint8_t* name) {
|
||||
|
||||
// Remove all non alphanumeric characters.
|
||||
uint8_t* p = name;
|
||||
uint8_t* q = name;
|
||||
|
||||
void fix_name(uint8_t *name)
|
||||
{
|
||||
/* Remove all non alphanumeric characters */
|
||||
uint8_t *p = name;
|
||||
uint8_t *q = name;
|
||||
while(*p != 0) {
|
||||
if(isprint(*p)) {
|
||||
if (isprint(*p))
|
||||
*q++ = *p;
|
||||
}
|
||||
|
||||
p++;
|
||||
}
|
||||
|
||||
*q = 0;
|
||||
}
|
||||
|
||||
void friendlist_onMessage(ToxWindow* self, int num, uint8_t* str, uint16_t len) {
|
||||
|
||||
if(num >= num_friends)
|
||||
void friendlist_onMessage(ToxWindow *self, int num, uint8_t *str, uint16_t len)
|
||||
{
|
||||
if (num >= num_friends)
|
||||
return;
|
||||
|
||||
if(friends[num].chatwin == -1) {
|
||||
if (friends[num].chatwin == -1) {
|
||||
friends[num].chatwin = num;
|
||||
int i;
|
||||
/* Find first open slot to hold chat window */
|
||||
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) {
|
||||
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (WINDOW_STATUS[i] == -1) {
|
||||
WINDOW_STATUS[i] = num;
|
||||
add_window(new_chat(num), i);
|
||||
w_active = i;
|
||||
active_window = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void friendlist_onNickChange(ToxWindow* self, int num, uint8_t* str, uint16_t len) {
|
||||
|
||||
if(len >= MAX_NAME_LENGTH || num >= num_friends)
|
||||
void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
|
||||
{
|
||||
if (len >= MAX_NAME_LENGTH || num >= num_friends)
|
||||
return;
|
||||
|
||||
memcpy((char*) &friends[num].name, (char*) str, len);
|
||||
@ -76,9 +72,9 @@ void friendlist_onNickChange(ToxWindow* self, int num, uint8_t* str, uint16_t le
|
||||
fix_name(friends[num].name);
|
||||
}
|
||||
|
||||
void friendlist_onStatusChange(ToxWindow* self, int num, uint8_t* str, uint16_t len) {
|
||||
|
||||
if(len >= MAX_USERSTATUS_LENGTH || num >= num_friends)
|
||||
void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
|
||||
{
|
||||
if (len >= MAX_USERSTATUS_LENGTH || num >= num_friends)
|
||||
return;
|
||||
|
||||
memcpy((char*) &friends[num].status, (char*) str, len);
|
||||
@ -86,9 +82,9 @@ void friendlist_onStatusChange(ToxWindow* self, int num, uint8_t* str, uint16_t
|
||||
fix_name(friends[num].status);
|
||||
}
|
||||
|
||||
int friendlist_onFriendAdded(int num) {
|
||||
|
||||
if(num_friends == MAX_FRIENDS_NUM)
|
||||
int friendlist_onFriendAdded(int num)
|
||||
{
|
||||
if (num_friends == MAX_FRIENDS_NUM)
|
||||
return -1;
|
||||
|
||||
friends[num_friends].num = num;
|
||||
@ -99,34 +95,34 @@ int friendlist_onFriendAdded(int num) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void friendlist_onKey(ToxWindow* self, int key) {
|
||||
if(key == KEY_UP) {
|
||||
num_selected--;
|
||||
if (num_selected < 0)
|
||||
static void friendlist_onKey(ToxWindow *self, int key)
|
||||
{
|
||||
if (key == KEY_UP) {
|
||||
if (--num_selected < 0)
|
||||
num_selected = num_friends-1;
|
||||
}
|
||||
else if(key == KEY_DOWN) {
|
||||
if(num_friends != 0)
|
||||
else if (key == KEY_DOWN) {
|
||||
if (num_friends != 0)
|
||||
num_selected = (num_selected+1) % num_friends;
|
||||
}
|
||||
else if(key == '\n') {
|
||||
else if (key == '\n') {
|
||||
/* Jump to chat window if already open */
|
||||
if (friends[num_selected].chatwin != -1) {
|
||||
int i;
|
||||
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) {
|
||||
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (WINDOW_STATUS[i] == num_selected) {
|
||||
w_active = i;
|
||||
active_window = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}else {
|
||||
int i;
|
||||
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) {
|
||||
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (WINDOW_STATUS[i] == -1) {
|
||||
WINDOW_STATUS[i] = num_selected;
|
||||
friends[num_selected].chatwin = num_selected;
|
||||
add_window(new_chat(num_selected), i);
|
||||
w_active = i;
|
||||
active_window = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -134,13 +130,11 @@ static void friendlist_onKey(ToxWindow* self, int key) {
|
||||
}
|
||||
}
|
||||
|
||||
static void friendlist_onDraw(ToxWindow* self) {
|
||||
static void friendlist_onDraw(ToxWindow *self)
|
||||
{
|
||||
curs_set(0);
|
||||
size_t i;
|
||||
|
||||
werase(self->window);
|
||||
|
||||
if(num_friends == 0) {
|
||||
if (num_friends == 0) {
|
||||
wprintw(self->window, "Empty. Add some friends! :-)\n");
|
||||
}
|
||||
else {
|
||||
@ -150,12 +144,11 @@ static void friendlist_onDraw(ToxWindow* self) {
|
||||
}
|
||||
|
||||
wprintw(self->window, "\n");
|
||||
|
||||
for(i=0; i<num_friends; i++) {
|
||||
|
||||
if(i == num_selected) wattron(self->window, COLOR_PAIR(3));
|
||||
int i;
|
||||
for (i = 0; i < num_friends; ++i) {
|
||||
if (i == num_selected) wattron(self->window, COLOR_PAIR(3));
|
||||
wprintw(self->window, " [#%d] ", friends[i].num);
|
||||
if(i == num_selected) wattroff(self->window, COLOR_PAIR(3));
|
||||
if (i == num_selected) wattroff(self->window, COLOR_PAIR(3));
|
||||
|
||||
attron(A_BOLD);
|
||||
wprintw(self->window, "%s ", friends[i].name);
|
||||
@ -163,22 +156,21 @@ static void friendlist_onDraw(ToxWindow* self) {
|
||||
|
||||
wprintw(self->window, "(%s)\n", friends[i].status);
|
||||
}
|
||||
|
||||
wrefresh(self->window);
|
||||
}
|
||||
|
||||
void disable_chatwin(int f_num) {
|
||||
void disable_chatwin(int f_num)
|
||||
{
|
||||
friends[f_num].chatwin = -1;
|
||||
}
|
||||
|
||||
static void friendlist_onInit(ToxWindow* self) {
|
||||
static void friendlist_onInit(ToxWindow *self)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
ToxWindow new_friendlist() {
|
||||
ToxWindow ret;
|
||||
|
||||
memset(&ret, 0, sizeof(ret));
|
||||
|
||||
ret.onKey = &friendlist_onKey;
|
||||
@ -187,7 +179,7 @@ ToxWindow new_friendlist() {
|
||||
ret.onMessage = &friendlist_onMessage;
|
||||
ret.onNickChange = &friendlist_onNickChange;
|
||||
ret.onStatusChange = &friendlist_onStatusChange;
|
||||
strcpy(ret.title, "[friends]");
|
||||
|
||||
strcpy(ret.title, "[friends]");
|
||||
return ret;
|
||||
}
|
||||
|
217
main.c
217
main.c
@ -18,106 +18,108 @@ extern ToxWindow new_friendlist();
|
||||
|
||||
extern int friendlist_onFriendAdded(int num);
|
||||
extern void disable_chatwin(int f_num);
|
||||
extern int add_req(uint8_t* public_key); // XXX
|
||||
extern int add_req(uint8_t *public_key); // XXX
|
||||
|
||||
/* Holds status of chat windows */
|
||||
char WINDOW_STATUS[MAX_WINDOW_SLOTS];
|
||||
|
||||
char WINDOW_STATUS[MAX_WINDOW_SLOTS]; // Holds status of chat windows
|
||||
static ToxWindow windows[MAX_WINDOW_SLOTS];
|
||||
int w_num;
|
||||
int w_active;
|
||||
static ToxWindow* prompt;
|
||||
|
||||
// CALLBACKS START
|
||||
void on_request(uint8_t* public_key, uint8_t* data, uint16_t length) {
|
||||
size_t i;
|
||||
int n = add_req(public_key);
|
||||
int w_num;
|
||||
int active_window;
|
||||
|
||||
/* CALLBACKS START */
|
||||
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length)
|
||||
{
|
||||
int n = add_req(public_key);
|
||||
wprintw(prompt->window, "\nFriend request from:\n");
|
||||
|
||||
for(i=0; i<32; i++) {
|
||||
int i;
|
||||
for (i = 0; i < 32; ++i) {
|
||||
wprintw(prompt->window, "%02x", public_key[i] & 0xff);
|
||||
}
|
||||
|
||||
wprintw(prompt->window, "\n");
|
||||
|
||||
wprintw(prompt->window, "Use \"accept %d\" to accept it.\n", n);
|
||||
|
||||
for(i=0; i<MAX_WINDOW_SLOTS; i++) {
|
||||
if(windows[i].onFriendRequest != NULL)
|
||||
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (windows[i].onFriendRequest != NULL)
|
||||
windows[i].onFriendRequest(&windows[i], public_key, data, length);
|
||||
}
|
||||
}
|
||||
|
||||
void on_message(int friendnumber, uint8_t* string, uint16_t length) {
|
||||
size_t i;
|
||||
|
||||
void on_message(int friendnumber, uint8_t *string, uint16_t length)
|
||||
{
|
||||
wprintw(prompt->window, "\n(message) %d: %s\n", friendnumber, string);
|
||||
|
||||
for(i=0; i<MAX_WINDOW_SLOTS; i++) {
|
||||
if(windows[i].onMessage != NULL)
|
||||
int i;
|
||||
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (windows[i].onMessage != NULL)
|
||||
windows[i].onMessage(&windows[i], friendnumber, string, length);
|
||||
}
|
||||
}
|
||||
|
||||
void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) {
|
||||
size_t i;
|
||||
|
||||
void on_nickchange(int friendnumber, uint8_t *string, uint16_t length)
|
||||
{
|
||||
wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string);
|
||||
|
||||
for(i=0; i<MAX_WINDOW_SLOTS; i++) {
|
||||
if(windows[i].onNickChange != NULL)
|
||||
int i;
|
||||
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (windows[i].onNickChange != NULL)
|
||||
windows[i].onNickChange(&windows[i], friendnumber, string, length);
|
||||
}
|
||||
}
|
||||
|
||||
void on_statuschange(int friendnumber, USERSTATUS_KIND kind, uint8_t* string, uint16_t length) {
|
||||
size_t i;
|
||||
|
||||
void on_statuschange(int friendnumber, USERSTATUS_KIND kind, uint8_t *string, uint16_t length)
|
||||
{
|
||||
wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
|
||||
|
||||
for(i=0; i<MAX_WINDOW_SLOTS; i++) {
|
||||
if(windows[i].onStatusChange != NULL)
|
||||
int i;
|
||||
for (i=0; i<MAX_WINDOW_SLOTS; ++i) {
|
||||
if (windows[i].onStatusChange != NULL)
|
||||
windows[i].onStatusChange(&windows[i], friendnumber, string, length);
|
||||
}
|
||||
}
|
||||
|
||||
void on_friendadded(int friendnumber) {
|
||||
void on_friendadded(int friendnumber)
|
||||
{
|
||||
friendlist_onFriendAdded(friendnumber);
|
||||
}
|
||||
// CALLBACKS END
|
||||
/* CALLBACKS END */
|
||||
|
||||
static void init_term() {
|
||||
// Setup terminal.
|
||||
static void init_term()
|
||||
{
|
||||
/* Setup terminal */
|
||||
initscr();
|
||||
cbreak();
|
||||
keypad(stdscr, 1);
|
||||
noecho();
|
||||
timeout(100);
|
||||
|
||||
if(has_colors()) {
|
||||
if (has_colors()) {
|
||||
start_color();
|
||||
init_pair(1, COLOR_GREEN, COLOR_BLACK);
|
||||
init_pair(2, COLOR_CYAN, COLOR_BLACK);
|
||||
init_pair(3, COLOR_RED, COLOR_BLACK);
|
||||
init_pair(4, COLOR_BLUE, COLOR_BLACK);
|
||||
}
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
static void init_tox() {
|
||||
// Init core.
|
||||
static void init_tox()
|
||||
{
|
||||
/* Init core */
|
||||
initMessenger();
|
||||
|
||||
// Callbacks.
|
||||
/* Callbacks */
|
||||
m_callback_friendrequest(on_request);
|
||||
m_callback_friendmessage(on_message);
|
||||
m_callback_namechange(on_nickchange);
|
||||
m_callback_userstatus(on_statuschange);
|
||||
}
|
||||
|
||||
void init_window_status() {
|
||||
void init_window_status()
|
||||
{
|
||||
/* Default window values decrement from -2 */
|
||||
int i;
|
||||
for (i = 0; i < N_DEFAULT_WINS; i++)
|
||||
for (i = 0; i < N_DEFAULT_WINS; ++i)
|
||||
WINDOW_STATUS[i] = -(i+2);
|
||||
|
||||
int j;
|
||||
@ -125,15 +127,16 @@ void init_window_status() {
|
||||
WINDOW_STATUS[j] = -1;
|
||||
}
|
||||
|
||||
int add_window(ToxWindow w, int n) {
|
||||
if(w_num >= TOXWINDOWS_MAX_NUM)
|
||||
int add_window(ToxWindow w, int n)
|
||||
{
|
||||
if (w_num >= TOXWINDOWS_MAX_NUM)
|
||||
return -1;
|
||||
|
||||
if(LINES < 2)
|
||||
if (LINES < 2)
|
||||
return -1;
|
||||
|
||||
w.window = newwin(LINES - 2, COLS, 0, 0);
|
||||
if(w.window == NULL)
|
||||
if (w.window == NULL)
|
||||
return -1;
|
||||
|
||||
windows[n] = w;
|
||||
@ -143,10 +146,11 @@ int add_window(ToxWindow w, int n) {
|
||||
}
|
||||
|
||||
/* Deletes window w and cleans up */
|
||||
void del_window(ToxWindow *w, int f_num) {
|
||||
void del_window(ToxWindow *w, int f_num)
|
||||
{
|
||||
delwin(w->window);
|
||||
int i;
|
||||
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) {
|
||||
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (WINDOW_STATUS[i] == f_num) {
|
||||
WINDOW_STATUS[i] = -1;
|
||||
disable_chatwin(f_num);
|
||||
@ -157,11 +161,12 @@ void del_window(ToxWindow *w, int f_num) {
|
||||
refresh();
|
||||
}
|
||||
|
||||
static void init_windows() {
|
||||
static void init_windows()
|
||||
{
|
||||
w_num = 0;
|
||||
int n_prompt = 0;
|
||||
int n_friendslist = 1;
|
||||
if(add_window(new_prompt(), n_prompt) == -1
|
||||
if (add_window(new_prompt(), n_prompt) == -1
|
||||
|| add_window(new_friendlist(), n_friendslist) == -1) {
|
||||
fprintf(stderr, "add_window() failed.\n");
|
||||
endwin();
|
||||
@ -170,88 +175,79 @@ static void init_windows() {
|
||||
prompt = &windows[n_prompt];
|
||||
}
|
||||
|
||||
static void do_tox() {
|
||||
static void do_tox()
|
||||
{
|
||||
static bool dht_on = false;
|
||||
|
||||
if(!dht_on && DHT_isconnected()) {
|
||||
if (!dht_on && DHT_isconnected()) {
|
||||
dht_on = true;
|
||||
wprintw(prompt->window, "\nDHT connected!\n");
|
||||
}
|
||||
else if(dht_on && !DHT_isconnected()) {
|
||||
else if (dht_on && !DHT_isconnected()) {
|
||||
dht_on = false;
|
||||
wprintw(prompt->window, "\nDHT disconnected!\n");
|
||||
}
|
||||
|
||||
doMessenger();
|
||||
}
|
||||
|
||||
static void load_data(char *path) {
|
||||
FILE* fd;
|
||||
static void load_data(char *path)
|
||||
{
|
||||
FILE *fd;
|
||||
size_t len;
|
||||
uint8_t* buf;
|
||||
uint8_t *buf;
|
||||
|
||||
if((fd = fopen(path, "r")) != NULL) {
|
||||
if ((fd = fopen(path, "r")) != NULL) {
|
||||
fseek(fd, 0, SEEK_END);
|
||||
len = ftell(fd);
|
||||
fseek(fd, 0, SEEK_SET);
|
||||
|
||||
buf = malloc(len);
|
||||
|
||||
if(buf == NULL) {
|
||||
if (buf == NULL) {
|
||||
fprintf(stderr, "malloc() failed.\n");
|
||||
|
||||
fclose(fd);
|
||||
endwin();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if(fread(buf, len, 1, fd) != 1){
|
||||
if (fread(buf, len, 1, fd) != 1){
|
||||
fprintf(stderr, "fread() failed.\n");
|
||||
|
||||
free(buf);
|
||||
fclose(fd);
|
||||
endwin();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
Messenger_load(buf, len);
|
||||
}
|
||||
else {
|
||||
len = Messenger_size();
|
||||
buf = malloc(len);
|
||||
|
||||
if(buf == NULL) {
|
||||
if (buf == NULL) {
|
||||
fprintf(stderr, "malloc() failed.\n");
|
||||
endwin();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
Messenger_save(buf);
|
||||
|
||||
fd = fopen(path, "w");
|
||||
if(fd == NULL) {
|
||||
if (fd == NULL) {
|
||||
fprintf(stderr, "fopen() failed.\n");
|
||||
|
||||
free(buf);
|
||||
endwin();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if(fwrite(buf, len, 1, fd) != 1){
|
||||
if (fwrite(buf, len, 1, fd) != 1){
|
||||
fprintf(stderr, "fwrite() failed.\n");
|
||||
|
||||
free(buf);
|
||||
fclose(fd);
|
||||
endwin();
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
free(buf);
|
||||
fclose(fd);
|
||||
}
|
||||
|
||||
static void draw_bar() {
|
||||
static void draw_bar()
|
||||
{
|
||||
static int odd = 0;
|
||||
|
||||
attron(COLOR_PAIR(4));
|
||||
@ -265,21 +261,21 @@ static void draw_bar() {
|
||||
attroff(COLOR_PAIR(4) | A_BOLD);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < (MAX_WINDOW_SLOTS-1); i++) {
|
||||
for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) {
|
||||
if (WINDOW_STATUS[i] != -1) {
|
||||
if (i == w_active)
|
||||
if (i == active_window)
|
||||
attron(A_BOLD);
|
||||
|
||||
odd = (odd+1) % 10;
|
||||
if(windows[i].blink && (odd < 5)) {
|
||||
if (windows[i].blink && (odd < 5)) {
|
||||
attron(COLOR_PAIR(3));
|
||||
}
|
||||
|
||||
printw(" %s", windows[i].title);
|
||||
if(windows[i].blink && (odd < 5)) {
|
||||
if (windows[i].blink && (odd < 5)) {
|
||||
attron(COLOR_PAIR(3));
|
||||
}
|
||||
if(i == w_active) {
|
||||
if (i == active_window) {
|
||||
attroff(A_BOLD);
|
||||
}
|
||||
}
|
||||
@ -287,35 +283,37 @@ static void draw_bar() {
|
||||
refresh();
|
||||
}
|
||||
|
||||
void prepare_window(WINDOW* w) {
|
||||
void prepare_window(WINDOW *w)
|
||||
{
|
||||
mvwin(w, 0, 0);
|
||||
wresize(w, LINES-2, COLS);
|
||||
}
|
||||
|
||||
/* Shows next window when tab or back-tab is pressed */
|
||||
void set_active_window(int ch) {
|
||||
void set_active_window(int ch)
|
||||
{
|
||||
int f_inf = 0;
|
||||
int max = MAX_WINDOW_SLOTS-1;
|
||||
if (ch == '\t') {
|
||||
int i = (w_active + 1) % max;
|
||||
int i = (active_window + 1) % max;
|
||||
while (true) {
|
||||
if (WINDOW_STATUS[i] != -1) {
|
||||
w_active = i;
|
||||
active_window = i;
|
||||
return;
|
||||
}
|
||||
i = (i + 1) % max;
|
||||
if (f_inf++ > max) { // infinite loop check
|
||||
if (f_inf++ > max) { // infinite loop check
|
||||
endwin();
|
||||
clear();
|
||||
exit(2);
|
||||
}
|
||||
}
|
||||
}else {
|
||||
int i = w_active - 1;
|
||||
int i = active_window - 1;
|
||||
if (i < 0) i = max;
|
||||
while (true) {
|
||||
if (WINDOW_STATUS[i] != -1) {
|
||||
w_active = i;
|
||||
active_window = i;
|
||||
return;
|
||||
}
|
||||
if (--i < 0) i = max;
|
||||
@ -328,26 +326,26 @@ void set_active_window(int ch) {
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ch;
|
||||
int i = 0;
|
||||
int f_flag = 0;
|
||||
char *filename = "data";
|
||||
ToxWindow* a;
|
||||
|
||||
for(i = 0; i < argc; i++) {
|
||||
if (argv[i] == NULL){
|
||||
break;
|
||||
} else if(argv[i][0] == '-') {
|
||||
if(argv[i][1] == 'f') {
|
||||
if(argv[i + 1] != NULL)
|
||||
filename = argv[i + 1];
|
||||
else {
|
||||
f_flag = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
int i = 0;
|
||||
for (i = 0; i < argc; ++i) {
|
||||
if (argv[i] == NULL)
|
||||
break;
|
||||
else if (argv[i][0] == '-') {
|
||||
if (argv[i][1] == 'f') {
|
||||
if (argv[i + 1] != NULL)
|
||||
filename = argv[i + 1];
|
||||
else
|
||||
f_flag = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
init_term();
|
||||
init_tox();
|
||||
@ -355,7 +353,7 @@ int main(int argc, char* argv[]) {
|
||||
init_windows();
|
||||
init_window_status();
|
||||
|
||||
if(f_flag == -1) {
|
||||
if (f_flag == -1) {
|
||||
attron(COLOR_PAIR(3) | A_BOLD);
|
||||
wprintw(prompt->window, "You passed '-f' without giving an argument!\n"
|
||||
"defaulting to 'data' for a keyfile...\n");
|
||||
@ -363,24 +361,21 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
|
||||
while(true) {
|
||||
// Update tox.
|
||||
/* Update tox */
|
||||
do_tox();
|
||||
|
||||
// Draw.
|
||||
a = &windows[w_active];
|
||||
/* Draw */
|
||||
a = &windows[active_window];
|
||||
prepare_window(a->window);
|
||||
a->blink = false;
|
||||
draw_bar();
|
||||
a->onDraw(a);
|
||||
|
||||
// Handle input.
|
||||
/* Handle input */
|
||||
ch = getch();
|
||||
if(ch == '\t' || ch == KEY_BTAB)
|
||||
if (ch == '\t' || ch == KEY_BTAB)
|
||||
set_active_window(ch);
|
||||
else if(ch != ERR) {
|
||||
a->onKey(a, ch);
|
||||
}
|
||||
else if(ch != ERR) {
|
||||
else if (ch != ERR) {
|
||||
a->onKey(a, ch);
|
||||
}
|
||||
}
|
||||
|
288
prompt.c
288
prompt.c
@ -16,83 +16,78 @@ uint8_t pending_requests[256][CLIENT_ID_SIZE]; // XXX
|
||||
uint8_t num_requests=0; // XXX
|
||||
|
||||
extern void on_friendadded(int friendnumber);
|
||||
static void print_usage(ToxWindow* self);
|
||||
static void print_usage(ToxWindow *self);
|
||||
static char prompt_buf[256] = {0};
|
||||
static int prompt_buf_pos = 0;
|
||||
|
||||
// XXX:
|
||||
int add_req(uint8_t* public_key) {
|
||||
int add_req(uint8_t *public_key)
|
||||
{
|
||||
memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE);
|
||||
++num_requests;
|
||||
|
||||
return num_requests-1;
|
||||
}
|
||||
|
||||
// XXX: FIX
|
||||
unsigned char * hex_string_to_bin(char hex_string[])
|
||||
unsigned char *hex_string_to_bin(char hex_string[])
|
||||
{
|
||||
size_t len = strlen(hex_string);
|
||||
unsigned char *val = malloc(len);
|
||||
char *pos = hex_string;
|
||||
int i;
|
||||
for(i = 0; i < len; ++i, pos+=2)
|
||||
sscanf(pos,"%2hhx",&val[i]);
|
||||
return val;
|
||||
size_t len = strlen(hex_string);
|
||||
unsigned char *val = malloc(len);
|
||||
char *pos = hex_string;
|
||||
int i;
|
||||
for (i = 0; i < len; ++i, pos+=2)
|
||||
sscanf(pos,"%2hhx",&val[i]);
|
||||
return val;
|
||||
}
|
||||
|
||||
static char prompt_buf[256] = {0};
|
||||
static int prompt_buf_pos=0;
|
||||
|
||||
static void execute(ToxWindow* self, char* u_cmd) {
|
||||
int i;
|
||||
int newlines = 0;
|
||||
char cmd[256] = {0};
|
||||
for(i = 0; i < strlen(prompt_buf); i++)
|
||||
{
|
||||
static void execute(ToxWindow *self, char *u_cmd)
|
||||
{
|
||||
int newlines = 0;
|
||||
char cmd[256] = {0};
|
||||
int i;
|
||||
for (i = 0; i < strlen(prompt_buf); ++i) {
|
||||
if (u_cmd[i] == '\n')
|
||||
++newlines;
|
||||
else
|
||||
cmd[i - newlines] = u_cmd[i];
|
||||
}
|
||||
++newlines;
|
||||
else
|
||||
cmd[i - newlines] = u_cmd[i];
|
||||
}
|
||||
|
||||
if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) {
|
||||
if (!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) {
|
||||
endwin();
|
||||
exit(0);
|
||||
}
|
||||
else if(!strncmp(cmd, "connect ", strlen("connect "))) {
|
||||
char* ip;
|
||||
char* port;
|
||||
char* key;
|
||||
IP_Port dht;
|
||||
|
||||
ip = strchr(cmd, ' ');
|
||||
if(ip == NULL) {
|
||||
else if (!strncmp(cmd, "connect ", strlen("connect "))) {
|
||||
IP_Port dht;
|
||||
char *ip = strchr(cmd, ' ');
|
||||
if (ip == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
ip++;
|
||||
|
||||
port = strchr(ip, ' ');
|
||||
if(port == NULL) {
|
||||
char *port = strchr(ip, ' ');
|
||||
if (port == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
port[0] = 0;
|
||||
port++;
|
||||
|
||||
key = strchr(port, ' ');
|
||||
if(key == NULL) {
|
||||
char *key = strchr(port, ' ');
|
||||
if (key == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
key[0] = 0;
|
||||
key++;
|
||||
|
||||
if(atoi(port) == 0) {
|
||||
if (atoi(port) == 0) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
dht.port = htons(atoi(port));
|
||||
|
||||
uint32_t resolved_address = resolve_addr(ip);
|
||||
if (resolved_address == 0) {
|
||||
return;
|
||||
@ -103,49 +98,39 @@ static void execute(ToxWindow* self, char* u_cmd) {
|
||||
DHT_bootstrap(dht, binary_string);
|
||||
free(binary_string);
|
||||
}
|
||||
else if(!strncmp(cmd, "add ", strlen("add "))) {
|
||||
|
||||
else if (!strncmp(cmd, "add ", strlen("add "))) {
|
||||
uint8_t id_bin[32];
|
||||
size_t i;
|
||||
char xx[3];
|
||||
uint32_t x;
|
||||
|
||||
char* id;
|
||||
char* msg;
|
||||
int num;
|
||||
|
||||
id = strchr(cmd, ' ');
|
||||
if(id == NULL) {
|
||||
char *id = strchr(cmd, ' ');
|
||||
if (id == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
id++;
|
||||
|
||||
msg = strchr(id, ' ');
|
||||
if(msg != NULL) {
|
||||
char *msg = strchr(id, ' ');
|
||||
if (msg != NULL) {
|
||||
msg[0] = 0;
|
||||
msg++;
|
||||
}
|
||||
else msg = "";
|
||||
|
||||
if(strlen(id) != 2*32) {
|
||||
if (strlen(id) != 2*32) {
|
||||
wprintw(self->window, "Invalid ID length.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for(i=0; i<32; i++) {
|
||||
int i;
|
||||
for (i = 0; i < 32; ++i) {
|
||||
xx[0] = id[2*i];
|
||||
xx[1] = id[2*i+1];
|
||||
xx[2] = '\0';
|
||||
|
||||
if(sscanf(xx, "%02x", &x) != 1) {
|
||||
if (sscanf(xx, "%02x", &x) != 1) {
|
||||
wprintw(self->window, "Invalid ID.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
id_bin[i] = x;
|
||||
}
|
||||
|
||||
num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1);
|
||||
int num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1);
|
||||
switch (num) {
|
||||
case -1:
|
||||
wprintw(self->window, "Message is too long.\n");
|
||||
@ -168,178 +153,167 @@ static void execute(ToxWindow* self, char* u_cmd) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if(!strcmp(cmd, "clear")) {
|
||||
wclear(self->window);
|
||||
}
|
||||
else if(!strcmp(cmd, "help")) {
|
||||
wclear(self->window);
|
||||
print_usage(self);
|
||||
}
|
||||
else if(!strncmp(cmd, "status ", strlen("status "))) {
|
||||
char* msg;
|
||||
|
||||
msg = strchr(cmd, ' ');
|
||||
if(msg == NULL) {
|
||||
else if (!strcmp(cmd, "clear")) {
|
||||
wclear(self->window);
|
||||
}
|
||||
|
||||
else if (!strcmp(cmd, "help")) {
|
||||
wclear(self->window);
|
||||
print_usage(self);
|
||||
}
|
||||
|
||||
else if (!strncmp(cmd, "status ", strlen("status "))) {
|
||||
char *msg = strchr(cmd, ' ');
|
||||
if (msg == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
msg++;
|
||||
|
||||
m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1);
|
||||
wprintw(self->window, "Status set to: %s\n", msg);
|
||||
}
|
||||
else if(!strncmp(cmd, "nick ", strlen("nick "))) {
|
||||
char* nick;
|
||||
|
||||
nick = strchr(cmd, ' ');
|
||||
if(nick == NULL) {
|
||||
else if (!strncmp(cmd, "nick ", strlen("nick "))) {
|
||||
char *nick = strchr(cmd, ' ');
|
||||
if (nick == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
nick++;
|
||||
|
||||
setname((uint8_t*) nick, strlen(nick)+1);
|
||||
wprintw(self->window, "Nickname set to: %s\n", nick);
|
||||
}
|
||||
else if(!strcmp(cmd, "myid")) {
|
||||
|
||||
else if (!strcmp(cmd, "myid")) {
|
||||
char id[32*2 + 1] = {0};
|
||||
size_t i;
|
||||
|
||||
for(i=0; i<32; i++) {
|
||||
for (i = 0; i < 32; ++i) {
|
||||
char xx[3];
|
||||
snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff);
|
||||
strcat(id, xx);
|
||||
}
|
||||
|
||||
wprintw(self->window, "Your ID: %s\n", id);
|
||||
}
|
||||
else if(!strncmp(cmd, "accept ", strlen("accept "))) {
|
||||
char* id;
|
||||
int num;
|
||||
|
||||
id = strchr(cmd, ' ');
|
||||
if(id == NULL) {
|
||||
else if (!strncmp(cmd, "accept ", strlen("accept "))) {
|
||||
char *id = strchr(cmd, ' ');
|
||||
if (id == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
id++;
|
||||
|
||||
num = atoi(id);
|
||||
if(num >= num_requests) {
|
||||
int num = atoi(id);
|
||||
if (num >= num_requests) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
num = m_addfriend_norequest(pending_requests[num]);
|
||||
|
||||
if(num == -1) {
|
||||
if (num == -1)
|
||||
wprintw(self->window, "Failed to add friend.\n");
|
||||
}
|
||||
else {
|
||||
wprintw(self->window, "Friend accepted as: %d.\n", num);
|
||||
on_friendadded(num);
|
||||
}
|
||||
}
|
||||
else if(!strncmp(cmd, "msg ", strlen("msg "))) {
|
||||
char* id;
|
||||
char* msg;
|
||||
|
||||
id = strchr(cmd, ' ');
|
||||
|
||||
if(id == NULL) {
|
||||
else if (!strncmp(cmd, "msg ", strlen("msg "))) {
|
||||
char *id = strchr(cmd, ' ');
|
||||
if (id == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
id++;
|
||||
|
||||
msg = strchr(id, ' ');
|
||||
if(msg == NULL) {
|
||||
char *msg = strchr(++id, ' ');
|
||||
if (msg == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
msg[0] = 0;
|
||||
msg++;
|
||||
|
||||
if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) < 0) {
|
||||
if (m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) < 0)
|
||||
wprintw(self->window, "Error occurred while sending message.\n");
|
||||
}
|
||||
else {
|
||||
else
|
||||
wprintw(self->window, "Message successfully sent.\n");
|
||||
}
|
||||
}
|
||||
else {
|
||||
else
|
||||
wprintw(self->window, "Invalid command.\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void prompt_onKey(ToxWindow* self, int key) {
|
||||
// PRINTABLE characters: Add to line.
|
||||
if(isprint(key)) {
|
||||
if (prompt_buf_pos == (sizeof(prompt_buf) - 1)){
|
||||
wprintw(self->window, "\nToo Long.\n");
|
||||
prompt_buf_pos = 0;
|
||||
prompt_buf[0] = 0;
|
||||
}
|
||||
else if(!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS) && (prompt_buf_pos % (COLS - 3) == 0)) {
|
||||
prompt_buf[prompt_buf_pos++] = '\n';
|
||||
static void prompt_onKey(ToxWindow *self, int key)
|
||||
{
|
||||
/* Add printable characters to line */
|
||||
if (isprint(key)) {
|
||||
if (prompt_buf_pos == (sizeof(prompt_buf) - 1)) {
|
||||
wprintw(self->window, "\nToo Long.\n");
|
||||
prompt_buf_pos = 0;
|
||||
prompt_buf[0] = 0;
|
||||
}
|
||||
else if(!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS) && ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) {
|
||||
prompt_buf[prompt_buf_pos++] = '\n';
|
||||
else if (!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS)
|
||||
&& (prompt_buf_pos % (COLS - 3) == 0)) {
|
||||
prompt_buf[prompt_buf_pos++] = '\n';
|
||||
}
|
||||
else if (!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS)
|
||||
&& ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) {
|
||||
prompt_buf[prompt_buf_pos++] = '\n';
|
||||
}
|
||||
prompt_buf[prompt_buf_pos++] = key;
|
||||
prompt_buf[prompt_buf_pos] = 0;
|
||||
}
|
||||
|
||||
// RETURN key: execute command.
|
||||
else if(key == '\n') {
|
||||
/* RETURN key: execute command */
|
||||
else if (key == '\n') {
|
||||
wprintw(self->window, "\n");
|
||||
execute(self, prompt_buf);
|
||||
prompt_buf_pos = 0;
|
||||
prompt_buf[0] = 0;
|
||||
}
|
||||
|
||||
// BACKSPACE key: Remove one character from line.
|
||||
else if(key == 0x107 || key == 0x8 || key == 0x7f) {
|
||||
if(prompt_buf_pos != 0) {
|
||||
/* BACKSPACE key: Remove one character from line */
|
||||
else if (key == 0x107 || key == 0x8 || key == 0x7f) {
|
||||
if (prompt_buf_pos != 0) {
|
||||
prompt_buf[--prompt_buf_pos] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void prompt_onDraw(ToxWindow* self) {
|
||||
curs_set(1);
|
||||
int x, y;
|
||||
getyx(self->window, y, x);
|
||||
(void) x;
|
||||
int i;
|
||||
for (i = 0; i < (strlen(prompt_buf)); i++)
|
||||
{
|
||||
if ((prompt_buf[i] == '\n') && (y != 0))
|
||||
--y;
|
||||
}
|
||||
wattron(self->window, COLOR_PAIR(1));
|
||||
mvwprintw(self->window, y, 0, "# ");
|
||||
wattroff(self->window, COLOR_PAIR(1));
|
||||
mvwprintw(self->window, y, 2, "%s", prompt_buf);
|
||||
wclrtoeol(self->window);
|
||||
wrefresh(self->window);
|
||||
static void prompt_onDraw(ToxWindow *self)
|
||||
{
|
||||
curs_set(1);
|
||||
int x, y;
|
||||
getyx(self->window, y, x);
|
||||
(void) x;
|
||||
int i;
|
||||
for (i = 0; i < (strlen(prompt_buf)); ++i) {
|
||||
if ((prompt_buf[i] == '\n') && (y != 0))
|
||||
--y;
|
||||
}
|
||||
|
||||
wattron(self->window, COLOR_PAIR(1));
|
||||
mvwprintw(self->window, y, 0, "# ");
|
||||
wattroff(self->window, COLOR_PAIR(1));
|
||||
mvwprintw(self->window, y, 2, "%s", prompt_buf);
|
||||
wclrtoeol(self->window);
|
||||
wrefresh(self->window);
|
||||
}
|
||||
|
||||
static void print_usage(ToxWindow* self) {
|
||||
static void print_usage(ToxWindow *self)
|
||||
{
|
||||
wattron(self->window, COLOR_PAIR(2) | A_BOLD);
|
||||
wprintw(self->window, "Commands:\n");
|
||||
wattroff(self->window, A_BOLD);
|
||||
|
||||
wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n");
|
||||
wprintw(self->window, " add <id> <message> : Add friend\n");
|
||||
wprintw(self->window, " status <message> : Set your status\n");
|
||||
wprintw(self->window, " nick <nickname> : Set your nickname\n");
|
||||
wprintw(self->window, " accept <number> : Accept friend request\n");
|
||||
wprintw(self->window, " myid : Print your ID\n");
|
||||
wprintw(self->window, " quit/exit : Exit program\n");
|
||||
wprintw(self->window, " help : Print this message again\n");
|
||||
wprintw(self->window, " clear : Clear this window\n");
|
||||
|
||||
wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n");
|
||||
wprintw(self->window, " add <id> <message> : Add friend\n");
|
||||
wprintw(self->window, " status <message> : Set your status\n");
|
||||
wprintw(self->window, " nick <nickname> : Set your nickname\n");
|
||||
wprintw(self->window, " accept <number> : Accept friend request\n");
|
||||
wprintw(self->window, " myid : Print your ID\n");
|
||||
wprintw(self->window, " quit/exit : Exit program\n");
|
||||
wprintw(self->window, " help : Print this message again\n");
|
||||
wprintw(self->window, " clear : Clear this window\n");
|
||||
|
||||
wattron(self->window, A_BOLD);
|
||||
wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n");
|
||||
wattroff(self->window, A_BOLD);
|
||||
@ -347,22 +321,20 @@ static void print_usage(ToxWindow* self) {
|
||||
wattroff(self->window, COLOR_PAIR(2));
|
||||
}
|
||||
|
||||
static void prompt_onInit(ToxWindow* self) {
|
||||
static void prompt_onInit(ToxWindow *self)
|
||||
{
|
||||
scrollok(self->window, 1);
|
||||
|
||||
print_usage(self);
|
||||
wclrtoeol(self->window);
|
||||
}
|
||||
|
||||
ToxWindow new_prompt() {
|
||||
ToxWindow new_prompt()
|
||||
{
|
||||
ToxWindow ret;
|
||||
|
||||
memset(&ret, 0, sizeof(ret));
|
||||
|
||||
ret.onKey = &prompt_onKey;
|
||||
ret.onDraw = &prompt_onDraw;
|
||||
ret.onInit = &prompt_onInit;
|
||||
strcpy(ret.title, "[prompt]");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user