summaryrefslogtreecommitdiff
path: root/testing/toxic/friendlist.c
blob: 2e46f124020ffd2a6abe2ef4e3d1942ad28de928 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * 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"
#include "friendlist.h"


typedef struct {
    uint8_t name[MAX_NAME_LENGTH];
    uint8_t status[MAX_STATUSMESSAGE_LENGTH];
    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;

    while (*p != 0) {
        if (isprint(*p))
            *q++ = *p;

        p++;
    }

    *q = 0;
}

void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str, uint16_t len)
{
    if (num >= num_friends)
        return;

    if (friends[num].chatwin == -1) {
        friends[num].chatwin = add_window(m, new_chat(m, num));
    }
}

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);
    friends[num].name[len] = 0;
    fix_name(friends[num].name);
}

void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
{
    if (len >= MAX_STATUSMESSAGE_LENGTH || num >= num_friends)
        return;

    memcpy((char *) &friends[num].status, (char *) str, len);
    friends[num].status[len] = 0;
    fix_name(friends[num].status);
}

int friendlist_onFriendAdded(Messenger *m, int num)
{
    if (num_friends == MAX_FRIENDS_NUM)
        return -1;

    friends[num_friends].num = num;
    getname(m, num, friends[num_friends].name);
    strcpy((char *) friends[num_friends].name, "unknown");
    strcpy((char *) friends[num_friends].status, "unknown");
    friends[num_friends++].chatwin = -1;
    return 0;
}

static void friendlist_onKey(ToxWindow *self, Messenger *m, int key)
{
    if (key == KEY_UP) {
        if (--num_selected < 0)
            num_selected = num_friends - 1;
    } else if (key == KEY_DOWN) {
        if (num_friends != 0)
            num_selected = (num_selected + 1) % num_friends;
    } else if (key == '\n') {
        /* Jump to chat window if already open */
        if (friends[num_selected].chatwin != -1) {
            set_active_window(friends[num_selected].chatwin);
        } else {
            friends[num_selected].chatwin = add_window(m, new_chat(m, num_selected));
        }
    }
}

static void friendlist_onDraw(ToxWindow *self, Messenger *m)
{
    curs_set(0);
    werase(self->window);

    if (num_friends == 0) {
        wprintw(self->window, "Empty. Add some friends! :-)\n");
    } else {
        wattron(self->window, COLOR_PAIR(2) | A_BOLD);
        wprintw(self->window, "Open chat with.. (up/down keys, enter)\n");
        wattroff(self->window, COLOR_PAIR(2) | A_BOLD);
    }

    wprintw(self->window, "\n");
    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));

        attron(A_BOLD);
        wprintw(self->window, "%s ", friends[i].name);
        attroff(A_BOLD);

        wprintw(self->window, "(%s)\n", friends[i].status);
    }

    wrefresh(self->window);
}

void disable_chatwin(int f_num)
{
    friends[f_num].chatwin = -1;
}

static void friendlist_onInit(ToxWindow *self, Messenger *m)
{

}

ToxWindow new_friendlist()
{
    ToxWindow ret;
    memset(&ret, 0, sizeof(ret));

    ret.onKey = &friendlist_onKey;
    ret.onDraw = &friendlist_onDraw;
    ret.onInit = &friendlist_onInit;
    ret.onMessage = &friendlist_onMessage;
    ret.onAction = &friendlist_onMessage;    // Action has identical behaviour to message
    ret.onNickChange = &friendlist_onNickChange;
    ret.onStatusChange = &friendlist_onStatusChange;

    strcpy(ret.title, "[friends]");
    return ret;
}