summaryrefslogtreecommitdiff
path: root/testing/toxic
diff options
context:
space:
mode:
Diffstat (limited to 'testing/toxic')
-rw-r--r--testing/toxic/chat.c63
-rw-r--r--testing/toxic/main.c31
2 files changed, 73 insertions, 21 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c
index 1bfd94f5..a90bb2aa 100644
--- a/testing/toxic/chat.c
+++ b/testing/toxic/chat.c
@@ -7,6 +7,7 @@
7#include <string.h> 7#include <string.h>
8#include <stdint.h> 8#include <stdint.h>
9#include <ctype.h> 9#include <ctype.h>
10#include <time.h>
10 11
11#include "../../core/Messenger.h" 12#include "../../core/Messenger.h"
12#include "../../core/network.h" 13#include "../../core/network.h"
@@ -26,11 +27,15 @@ typedef struct {
26 27
27extern void fix_name(uint8_t* name); 28extern void fix_name(uint8_t* name);
28 29
29
30static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { 30static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) {
31 ChatContext* ctx = (ChatContext*) self->x; 31 ChatContext* ctx = (ChatContext*) self->x;
32 uint8_t nick[MAX_NAME_LENGTH] = {0}; 32 uint8_t nick[MAX_NAME_LENGTH] = {0};
33 33
34 time_t now;
35 time(&now);
36 struct tm * timeinfo;
37 timeinfo = localtime(&now);
38
34 if(ctx->friendnum != num) 39 if(ctx->friendnum != num)
35 return; 40 return;
36 41
@@ -42,10 +47,11 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len)
42 fix_name(msg); 47 fix_name(msg);
43 fix_name(nick); 48 fix_name(nick);
44 49
50 wattron(ctx->history, COLOR_PAIR(2));
51 wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
45 wattron(ctx->history, COLOR_PAIR(4)); 52 wattron(ctx->history, COLOR_PAIR(4));
46 wprintw(ctx->history, "%s: ", nick); 53 wprintw(ctx->history, "%s: ", nick);
47 wattroff(ctx->history, COLOR_PAIR(4)); 54 wattroff(ctx->history, COLOR_PAIR(4));
48
49 wprintw(ctx->history, "%s\n", msg); 55 wprintw(ctx->history, "%s\n", msg);
50 56
51 self->blink = true; 57 self->blink = true;
@@ -71,9 +77,26 @@ static void chat_onStatusChange(ToxWindow* self, int num, uint8_t* status, uint1
71 77
72} 78}
73 79
80/* check that the string has one non-space character */
81int string_is_empty(char *string)
82{
83 int rc = 0;
84 char *copy = strdup(string);
85
86 rc = ((strtok(copy, " ") == NULL) ? 1:0);
87 free(copy);
88
89 return rc;
90}
91
74static void chat_onKey(ToxWindow* self, int key) { 92static void chat_onKey(ToxWindow* self, int key) {
75 ChatContext* ctx = (ChatContext*) self->x; 93 ChatContext* ctx = (ChatContext*) self->x;
76 94
95 time_t now;
96 time(&now);
97 struct tm * timeinfo;
98 timeinfo = localtime(&now);
99
77 if(isprint(key)) { 100 if(isprint(key)) {
78 101
79 if(ctx->pos != sizeof(ctx->line)-1) { 102 if(ctx->pos != sizeof(ctx->line)-1) {
@@ -81,28 +104,34 @@ static void chat_onKey(ToxWindow* self, int key) {
81 ctx->line[ctx->pos] = '\0'; 104 ctx->line[ctx->pos] = '\0';
82 } 105 }
83 } 106 }
84 else if(key == '\n') {
85 wattron(ctx->history, COLOR_PAIR(1));
86 wprintw(ctx->history, "you: ", ctx->line);
87 wattroff(ctx->history, COLOR_PAIR(1));
88
89 wprintw(ctx->history, "%s\n", ctx->line);
90 107
91 if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { 108 else if(key == '\n') {
92 wattron(ctx->history, COLOR_PAIR(3)); 109 if(!string_is_empty(ctx->line)) {
93 wprintw(ctx->history, " * Failed to send message.\n"); 110 /* make sure the string has at least non-space character */
94 wattroff(ctx->history, COLOR_PAIR(3)); 111 wattron(ctx->history, COLOR_PAIR(2));
112 wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
113 wattron(ctx->history, COLOR_PAIR(1));
114 wprintw(ctx->history, "you: ", ctx->line);
115 wattroff(ctx->history, COLOR_PAIR(1));
116 wprintw(ctx->history, "%s\n", ctx->line);
117
118 if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) {
119 wattron(ctx->history, COLOR_PAIR(3));
120 wprintw(ctx->history, " * Failed to send message.\n");
121 wattroff(ctx->history, COLOR_PAIR(3));
122 }
123
124 ctx->line[0] = '\0';
125 ctx->pos = 0;
95 } 126 }
96
97 ctx->line[0] = '\0';
98 ctx->pos = 0;
99 } 127 }
128
100 else if(key == 0x107 || key == 0x8 || key == 0x7f) { 129 else if(key == 0x107 || key == 0x8 || key == 0x7f) {
101 if(ctx->pos != 0) { 130 if(ctx->pos != 0) {
102 ctx->line[--ctx->pos] = '\0'; 131 ctx->line[--ctx->pos] = '\0';
103 } 132 }
104 } 133 }
105 134
106} 135}
107 136
108static void chat_onDraw(ToxWindow* self) { 137static void chat_onDraw(ToxWindow* self) {
@@ -150,7 +179,7 @@ ToxWindow new_chat(int friendnum) {
150 uint8_t nick[MAX_NAME_LENGTH] = {0}; 179 uint8_t nick[MAX_NAME_LENGTH] = {0};
151 getname(friendnum, (uint8_t*) &nick); 180 getname(friendnum, (uint8_t*) &nick);
152 fix_name(nick); 181 fix_name(nick);
153 182
154 snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); 183 snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum);
155 184
156 ChatContext* x = calloc(1, sizeof(ChatContext)); 185 ChatContext* x = calloc(1, sizeof(ChatContext));
diff --git a/testing/toxic/main.c b/testing/toxic/main.c
index 391b0b39..3b45a89f 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -170,12 +170,12 @@ static void do_tox() {
170 doMessenger(); 170 doMessenger();
171} 171}
172 172
173static void load_data() { 173static void load_data(char *path) {
174 FILE* fd; 174 FILE* fd;
175 size_t len; 175 size_t len;
176 uint8_t* buf; 176 uint8_t* buf;
177 177
178 if((fd = fopen("data", "r")) != NULL) { 178 if((fd = fopen(path, "r")) != NULL) {
179 fseek(fd, 0, SEEK_END); 179 fseek(fd, 0, SEEK_END);
180 len = ftell(fd); 180 len = ftell(fd);
181 fseek(fd, 0, SEEK_SET); 181 fseek(fd, 0, SEEK_SET);
@@ -213,7 +213,7 @@ static void load_data() {
213 213
214 Messenger_save(buf); 214 Messenger_save(buf);
215 215
216 fd = fopen("data", "w"); 216 fd = fopen(path, "w");
217 if(fd == NULL) { 217 if(fd == NULL) {
218 fprintf(stderr, "fopen() failed.\n"); 218 fprintf(stderr, "fopen() failed.\n");
219 219
@@ -282,13 +282,36 @@ void prepare_window(WINDOW* w) {
282 282
283int main(int argc, char* argv[]) { 283int main(int argc, char* argv[]) {
284 int ch; 284 int ch;
285 int i = 0;
286 int f_flag = 0;
287 char *filename = "data";
285 ToxWindow* a; 288 ToxWindow* a;
286 289
290 for(i = 0; i < argc; i++) {
291 if(argv[i][0] == '-') {
292 if(argv[i][1] == 'f') {
293 if(argv[i + 1] != NULL)
294 filename = argv[i + 1];
295 else {
296 f_flag = -1;
297 }
298 }
299 }
300 }
301
287 init_term(); 302 init_term();
288 init_tox(); 303 init_tox();
289 load_data(); 304 load_data(filename);
290 init_windows(); 305 init_windows();
291 306
307 if(f_flag == -1) {
308 attron(COLOR_PAIR(3) | A_BOLD);
309 wprintw(prompt->window, "You passed '-f' without giving an argument!\n"
310 "defaulting to 'data' for a keyfile...\n");
311 attroff(COLOR_PAIR(3) | A_BOLD);
312 }
313
314
292 while(true) { 315 while(true) {
293 // Update tox. 316 // Update tox.
294 do_tox(); 317 do_tox();