diff options
Diffstat (limited to 'testing/toxic/chat.c')
-rw-r--r-- | testing/toxic/chat.c | 115 |
1 files changed, 101 insertions, 14 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index 7cae1c0a..936eb868 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c | |||
@@ -26,6 +26,8 @@ typedef struct { | |||
26 | } ChatContext; | 26 | } ChatContext; |
27 | 27 | ||
28 | extern void fix_name(uint8_t* name); | 28 | extern void fix_name(uint8_t* name); |
29 | void print_help(ChatContext* self); | ||
30 | void execute(ToxWindow* self, ChatContext* ctx, char* cmd); | ||
29 | 31 | ||
30 | static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { | 32 | static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { |
31 | ChatContext* ctx = (ChatContext*) self->x; | 33 | ChatContext* ctx = (ChatContext*) self->x; |
@@ -49,6 +51,7 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) | |||
49 | 51 | ||
50 | wattron(ctx->history, COLOR_PAIR(2)); | 52 | wattron(ctx->history, COLOR_PAIR(2)); |
51 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 53 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
54 | wattroff(ctx->history, COLOR_PAIR(2)); | ||
52 | wattron(ctx->history, COLOR_PAIR(4)); | 55 | wattron(ctx->history, COLOR_PAIR(4)); |
53 | wprintw(ctx->history, "%s: ", nick); | 56 | wprintw(ctx->history, "%s: ", nick); |
54 | wattroff(ctx->history, COLOR_PAIR(4)); | 57 | wattroff(ctx->history, COLOR_PAIR(4)); |
@@ -77,6 +80,18 @@ static void chat_onStatusChange(ToxWindow* self, int num, uint8_t* status, uint1 | |||
77 | 80 | ||
78 | } | 81 | } |
79 | 82 | ||
83 | /* check that the string has one non-space character */ | ||
84 | int string_is_empty(char *string) | ||
85 | { | ||
86 | int rc = 0; | ||
87 | char *copy = strdup(string); | ||
88 | |||
89 | rc = ((strtok(copy, " ") == NULL) ? 1:0); | ||
90 | free(copy); | ||
91 | |||
92 | return rc; | ||
93 | } | ||
94 | |||
80 | static void chat_onKey(ToxWindow* self, int key) { | 95 | static void chat_onKey(ToxWindow* self, int key) { |
81 | ChatContext* ctx = (ChatContext*) self->x; | 96 | ChatContext* ctx = (ChatContext*) self->x; |
82 | 97 | ||
@@ -85,36 +100,93 @@ static void chat_onKey(ToxWindow* self, int key) { | |||
85 | struct tm * timeinfo; | 100 | struct tm * timeinfo; |
86 | timeinfo = localtime(&now); | 101 | timeinfo = localtime(&now); |
87 | 102 | ||
103 | /* PRINTABLE characters: Add to line */ | ||
88 | if(isprint(key)) { | 104 | if(isprint(key)) { |
89 | |||
90 | if(ctx->pos != sizeof(ctx->line)-1) { | 105 | if(ctx->pos != sizeof(ctx->line)-1) { |
91 | ctx->line[ctx->pos++] = key; | 106 | ctx->line[ctx->pos++] = key; |
92 | ctx->line[ctx->pos] = '\0'; | 107 | ctx->line[ctx->pos] = '\0'; |
93 | } | 108 | } |
94 | } | 109 | } |
110 | |||
111 | /* RETURN key: Execute command or print line */ | ||
95 | else if(key == '\n') { | 112 | else if(key == '\n') { |
96 | wattron(ctx->history, COLOR_PAIR(2)); | 113 | if (ctx->line[0] == '/') |
97 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 114 | execute(self, ctx, ctx->line); |
98 | wattron(ctx->history, COLOR_PAIR(1)); | 115 | else { |
99 | wprintw(ctx->history, "you: ", ctx->line); | 116 | if(!string_is_empty(ctx->line)) { |
100 | wattroff(ctx->history, COLOR_PAIR(1)); | 117 | /* make sure the string has at least non-space character */ |
101 | wprintw(ctx->history, "%s\n", ctx->line); | 118 | wattron(ctx->history, COLOR_PAIR(2)); |
102 | 119 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | |
103 | if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { | 120 | wattroff(ctx->history, COLOR_PAIR(2)); |
104 | wattron(ctx->history, COLOR_PAIR(3)); | 121 | wattron(ctx->history, COLOR_PAIR(1)); |
105 | wprintw(ctx->history, " * Failed to send message.\n"); | 122 | wprintw(ctx->history, "you: ", ctx->line); |
106 | wattroff(ctx->history, COLOR_PAIR(3)); | 123 | wattroff(ctx->history, COLOR_PAIR(1)); |
124 | wprintw(ctx->history, "%s\n", ctx->line); | ||
125 | } | ||
126 | if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { | ||
127 | wattron(ctx->history, COLOR_PAIR(3)); | ||
128 | wprintw(ctx->history, " * Failed to send message.\n"); | ||
129 | wattroff(ctx->history, COLOR_PAIR(3)); | ||
130 | } | ||
107 | } | 131 | } |
108 | |||
109 | ctx->line[0] = '\0'; | 132 | ctx->line[0] = '\0'; |
110 | ctx->pos = 0; | 133 | ctx->pos = 0; |
111 | } | 134 | } |
135 | |||
136 | /* BACKSPACE key: Remove one character from line */ | ||
112 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { | 137 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { |
113 | if(ctx->pos != 0) { | 138 | if(ctx->pos != 0) { |
114 | ctx->line[--ctx->pos] = '\0'; | 139 | ctx->line[--ctx->pos] = '\0'; |
115 | } | 140 | } |
116 | } | 141 | } |
142 | } | ||
117 | 143 | ||
144 | void execute(ToxWindow* self, ChatContext* ctx, char* cmd) | ||
145 | { | ||
146 | if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { | ||
147 | wclear(self->window); | ||
148 | wclear(ctx->history); | ||
149 | } | ||
150 | else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) | ||
151 | print_help(ctx); | ||
152 | else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { | ||
153 | endwin(); | ||
154 | exit(0); | ||
155 | } | ||
156 | else if (!strncmp(cmd, "/status ", strlen("/status "))) { | ||
157 | char* msg; | ||
158 | msg = strchr(cmd, ' '); | ||
159 | if(msg == NULL) { | ||
160 | wprintw(ctx->history, "Invalid syntax.\n"); | ||
161 | return; | ||
162 | } | ||
163 | msg++; | ||
164 | m_set_userstatus((uint8_t*) msg, strlen(msg)+1); | ||
165 | wprintw(ctx->history, "Status set to: %s\n", msg); | ||
166 | } | ||
167 | else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { | ||
168 | char* nick; | ||
169 | nick = strchr(cmd, ' '); | ||
170 | if(nick == NULL) { | ||
171 | wprintw(ctx->history, "Invalid syntax.\n"); | ||
172 | return; | ||
173 | } | ||
174 | nick++; | ||
175 | setname((uint8_t*) nick, strlen(nick)+1); | ||
176 | wprintw(ctx->history, "Nickname set to: %s\n", nick); | ||
177 | } | ||
178 | else if(!strcmp(cmd, "/myid")) { | ||
179 | char id[32*2 + 1] = {0}; | ||
180 | int i; | ||
181 | for (i = 0; i < 32; i++) { | ||
182 | char xx[3]; | ||
183 | snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); | ||
184 | strcat(id, xx); | ||
185 | } | ||
186 | wprintw(ctx->history, "Your ID: %s\n", id); | ||
187 | } | ||
188 | else | ||
189 | wprintw(ctx->history, "Invalid command.\n"); | ||
118 | } | 190 | } |
119 | 191 | ||
120 | static void chat_onDraw(ToxWindow* self) { | 192 | static void chat_onDraw(ToxWindow* self) { |
@@ -147,6 +219,21 @@ static void chat_onInit(ToxWindow* self) { | |||
147 | ctx->linewin = subwin(self->window, 2, x, y - 3, 0); | 219 | ctx->linewin = subwin(self->window, 2, x, y - 3, 0); |
148 | } | 220 | } |
149 | 221 | ||
222 | void print_help(ChatContext* self) { | ||
223 | wattron(self->history, COLOR_PAIR(2) | A_BOLD); | ||
224 | wprintw(self->history, "\nCommands:\n"); | ||
225 | wattroff(self->history, A_BOLD); | ||
226 | |||
227 | wprintw(self->history, " /status <message> : Set your status\n"); | ||
228 | wprintw(self->history, " /nick <nickname> : Set your nickname\n"); | ||
229 | wprintw(self->history, " /myid : Print your ID\n"); | ||
230 | wprintw(self->history, " /clear : Clear the screen\n"); | ||
231 | wprintw(self->history, " /quit or /exit : Exit program\n"); | ||
232 | wprintw(self->history, " /help : Print this message again\n\n"); | ||
233 | |||
234 | wattroff(self->history, COLOR_PAIR(2)); | ||
235 | } | ||
236 | |||
150 | ToxWindow new_chat(int friendnum) { | 237 | ToxWindow new_chat(int friendnum) { |
151 | ToxWindow ret; | 238 | ToxWindow ret; |
152 | 239 | ||
@@ -169,6 +256,6 @@ ToxWindow new_chat(int friendnum) { | |||
169 | x->friendnum = friendnum; | 256 | x->friendnum = friendnum; |
170 | 257 | ||
171 | ret.x = (void*) x; | 258 | ret.x = (void*) x; |
172 | 259 | free(x); | |
173 | return ret; | 260 | return ret; |
174 | } | 261 | } |