diff options
Diffstat (limited to 'testing/toxic/chat.c')
-rw-r--r-- | testing/toxic/chat.c | 63 |
1 files changed, 46 insertions, 17 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 | ||
27 | extern void fix_name(uint8_t* name); | 28 | extern void fix_name(uint8_t* name); |
28 | 29 | ||
29 | |||
30 | static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { | 30 | static 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 */ | ||
81 | int 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 | |||
74 | static void chat_onKey(ToxWindow* self, int key) { | 92 | static 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 | ||
108 | static void chat_onDraw(ToxWindow* self) { | 137 | static 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)); |