diff options
Diffstat (limited to 'testing/toxic/chat.c')
-rw-r--r-- | testing/toxic/chat.c | 97 |
1 files changed, 83 insertions, 14 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index a90bb2aa..ff7a1667 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)); |
@@ -97,41 +100,93 @@ static void chat_onKey(ToxWindow* self, int key) { | |||
97 | struct tm * timeinfo; | 100 | struct tm * timeinfo; |
98 | timeinfo = localtime(&now); | 101 | timeinfo = localtime(&now); |
99 | 102 | ||
103 | /* PRINTABLE characters: Add to line */ | ||
100 | if(isprint(key)) { | 104 | if(isprint(key)) { |
101 | |||
102 | if(ctx->pos != sizeof(ctx->line)-1) { | 105 | if(ctx->pos != sizeof(ctx->line)-1) { |
103 | ctx->line[ctx->pos++] = key; | 106 | ctx->line[ctx->pos++] = key; |
104 | ctx->line[ctx->pos] = '\0'; | 107 | ctx->line[ctx->pos] = '\0'; |
105 | } | 108 | } |
106 | } | 109 | } |
107 | 110 | ||
111 | /* RETURN key: Execute command or print line */ | ||
108 | else if(key == '\n') { | 112 | else if(key == '\n') { |
109 | if(!string_is_empty(ctx->line)) { | 113 | if (ctx->line[0] == '/') |
110 | /* make sure the string has at least non-space character */ | 114 | execute(self, ctx, ctx->line); |
111 | wattron(ctx->history, COLOR_PAIR(2)); | 115 | else { |
112 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 116 | if(!string_is_empty(ctx->line)) { |
113 | wattron(ctx->history, COLOR_PAIR(1)); | 117 | /* make sure the string has at least non-space character */ |
114 | wprintw(ctx->history, "you: ", ctx->line); | 118 | wattron(ctx->history, COLOR_PAIR(2)); |
115 | wattroff(ctx->history, COLOR_PAIR(1)); | 119 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
116 | wprintw(ctx->history, "%s\n", ctx->line); | 120 | wattroff(ctx->history, COLOR_PAIR(2)); |
117 | 121 | wattron(ctx->history, COLOR_PAIR(1)); | |
122 | wprintw(ctx->history, "you: ", ctx->line); | ||
123 | wattroff(ctx->history, COLOR_PAIR(1)); | ||
124 | wprintw(ctx->history, "%s\n", ctx->line); | ||
125 | } | ||
118 | if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { | 126 | if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { |
119 | wattron(ctx->history, COLOR_PAIR(3)); | 127 | wattron(ctx->history, COLOR_PAIR(3)); |
120 | wprintw(ctx->history, " * Failed to send message.\n"); | 128 | wprintw(ctx->history, " * Failed to send message.\n"); |
121 | wattroff(ctx->history, COLOR_PAIR(3)); | 129 | wattroff(ctx->history, COLOR_PAIR(3)); |
122 | } | 130 | } |
123 | |||
124 | ctx->line[0] = '\0'; | ||
125 | ctx->pos = 0; | ||
126 | } | 131 | } |
132 | ctx->line[0] = '\0'; | ||
133 | ctx->pos = 0; | ||
127 | } | 134 | } |
128 | 135 | ||
136 | /* BACKSPACE key: Remove one character from line */ | ||
129 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { | 137 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { |
130 | if(ctx->pos != 0) { | 138 | if(ctx->pos != 0) { |
131 | ctx->line[--ctx->pos] = '\0'; | 139 | ctx->line[--ctx->pos] = '\0'; |
132 | } | 140 | } |
133 | } | 141 | } |
142 | } | ||
134 | 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"); | ||
135 | } | 190 | } |
136 | 191 | ||
137 | static void chat_onDraw(ToxWindow* self) { | 192 | static void chat_onDraw(ToxWindow* self) { |
@@ -164,6 +219,21 @@ static void chat_onInit(ToxWindow* self) { | |||
164 | ctx->linewin = subwin(self->window, 2, x, y - 3, 0); | 219 | ctx->linewin = subwin(self->window, 2, x, y - 3, 0); |
165 | } | 220 | } |
166 | 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 | |||
167 | ToxWindow new_chat(int friendnum) { | 237 | ToxWindow new_chat(int friendnum) { |
168 | ToxWindow ret; | 238 | ToxWindow ret; |
169 | 239 | ||
@@ -186,6 +256,5 @@ ToxWindow new_chat(int friendnum) { | |||
186 | x->friendnum = friendnum; | 256 | x->friendnum = friendnum; |
187 | 257 | ||
188 | ret.x = (void*) x; | 258 | ret.x = (void*) x; |
189 | |||
190 | return ret; | 259 | return ret; |
191 | } | 260 | } |