summaryrefslogtreecommitdiff
path: root/testing/toxic/prompt.c
blob: 4a59cc7b841f62352ac58af194b375ddcc090699 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Toxic -- Tox Curses Client
 */

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <curses.h>

#include "../../core/Messenger.h"
#include "../../core/network.h"

#include "windows.h"

uint8_t pending_requests[256][CLIENT_ID_SIZE]; // XXX
uint8_t num_requests=0; // XXX

// XXX:
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[])
{
    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* cmd) {

  // quit/exit: Exit program.
  if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit")) {
    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) {
      return;
    }

    ip++;

    port = strchr(ip, ' ');
    if(port == NULL) {
      return;
    }

    port[0] = 0;
    port++;

    key = strchr(port, ' ');
    if(key == NULL) {
      return;
    }

    key[0] = 0;
    key++;

    if(atoi(port) == 0) {
      return;
    }

    wprintw(self->window, "ip=%s, port=%s, key=%s\n", ip, port, key);

    dht.port = htons(atoi(port));

    int resolved_address = resolve_addr(ip);
    if (resolved_address == -1) {
      return;
    }

    dht.ip.i = resolved_address;
    DHT_bootstrap(dht, hex_string_to_bin(key));
  }
  else if(!strncmp(cmd, "add ", strlen("add "))) {
    char* id;
    char* msg;
    int num;

    id = strchr(cmd, ' ');

    if(id == NULL) {
      return;
    }

    id++;

    msg = strchr(id, ' ');
    if(msg == NULL) {
      return;
    }

    msg[0] = 0;
    msg++;

    num = m_addfriend((uint8_t*) id, (uint8_t*) msg, strlen(msg)+1);
    wprintw(self->window, "Friend added as %d.\n", num);
  }
  else if(!strncmp(cmd, "status ", strlen("status "))) {
    char* msg;

    msg = strchr(cmd, ' ');
    if(msg == NULL) {
      return;
    }

    msg++;
    m_set_userstatus((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) {
      return;
    }

    nick++;
    setname((uint8_t*) nick, strlen(nick)+1);
    wprintw(self->window, "Nickname set to: %s.\n", nick);
  }
  else if(!strcmp(cmd, "myid")) {
    // XXX: Clean this up
    char idstring0[200];
    char idstring1[32][5];
    char idstring2[32][5];
    uint32_t i;

    for(i = 0; i < 32; i++) {
      if(self_public_key[i] < 16)
	strcpy(idstring1[i], "0");
      else 
	strcpy(idstring1[i], "");

      sprintf(idstring2[i], "%hhX", self_public_key[i]);
    }
    
    for (i=0; i<32; i++) {
      strcat(idstring0, idstring1[i]);
      strcat(idstring0, idstring2[i]);
    }

    wprintw(self->window, "%s\n", idstring0);
  }
  else if(!strncmp(cmd, "accept ", strlen("accept "))) {
    char* id;
    int num;

    id = strchr(cmd, ' ');
    if(id == NULL) {
      return;
    }
    id++;
   
    num = atoi(id);
    if(num >= num_requests) {
      return;
    }

    num = m_addfriend_norequest(pending_requests[num]);
    wprintw(self->window, "Friend accepted as: %d.\n", num);
  }
  else if(!strncmp(cmd, "msg ", strlen("msg "))) {
    char* id;
    char* msg;

    id = strchr(cmd, ' ');

    if(id == NULL) {
      return;
    }

    id++;

    msg = strchr(id, ' ');
    if(msg == NULL) {
      return;
    }

    msg[0] = 0;
    msg++;

    if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) != 1) {
      wprintw(self->window, "Error occurred while sending message.\n");
    }
    else {
      wprintw(self->window, "Message successfully sent.\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)) {
      return;
    }

    prompt_buf[prompt_buf_pos++] = key;
    prompt_buf[prompt_buf_pos] = 0;
  }

  // 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) {

    if(prompt_buf_pos != 0) {
      prompt_buf[--prompt_buf_pos] = 0;
    }
  }
}

static void prompt_onDraw(ToxWindow* self) {
  int x, y;

  mvwin(self->window,0,0);
  wresize(self->window, LINES-2, COLS);

  getyx(self->window, y, x);
  (void) x;

  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) {
  wattron(self->window, COLOR_PAIR(2) | A_BOLD);
  wprintw(self->window, "Usage:\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");
  wattroff(self->window, COLOR_PAIR(2));
}

static void prompt_onInit(ToxWindow* self) {
  scrollok(self->window, 1);

  print_usage(self);
  wclrtoeol(self->window);
}

ToxWindow new_prompt() {
  ToxWindow ret;

  ret.onKey = &prompt_onKey;
  ret.onDraw = &prompt_onDraw;
  ret.onInit = &prompt_onInit;
  strcpy(ret.title, "[prompt]");

  return ret;
}