diff options
author | Jfreegman <Jfreegman@gmail.com> | 2013-08-04 04:42:17 -0400 |
---|---|---|
committer | Jfreegman <Jfreegman@gmail.com> | 2013-08-04 04:42:17 -0400 |
commit | b513c3d352a9939884e54975cca9008c8b46a3e1 (patch) | |
tree | e23a0914e00a29e14fa96ee0d0b56040aca6d568 /testing | |
parent | 357c02ac2231d1f7844ff74427c6f6bbb7adc6ca (diff) |
added command functionality to chat windows and a few minor improvements
Diffstat (limited to 'testing')
-rw-r--r-- | testing/toxic/chat.c | 85 | ||||
-rw-r--r-- | testing/toxic/prompt.c | 9 |
2 files changed, 77 insertions, 17 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index a90bb2aa..72fa5d4b 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; |
@@ -92,24 +94,19 @@ int string_is_empty(char *string) | |||
92 | static void chat_onKey(ToxWindow* self, int key) { | 94 | static void chat_onKey(ToxWindow* self, int key) { |
93 | ChatContext* ctx = (ChatContext*) self->x; | 95 | ChatContext* ctx = (ChatContext*) self->x; |
94 | 96 | ||
95 | time_t now; | 97 | /* PRINTABLE characters: Add to line */ |
96 | time(&now); | ||
97 | struct tm * timeinfo; | ||
98 | timeinfo = localtime(&now); | ||
99 | |||
100 | if(isprint(key)) { | 98 | if(isprint(key)) { |
101 | |||
102 | if(ctx->pos != sizeof(ctx->line)-1) { | 99 | if(ctx->pos != sizeof(ctx->line)-1) { |
103 | ctx->line[ctx->pos++] = key; | 100 | ctx->line[ctx->pos++] = key; |
104 | ctx->line[ctx->pos] = '\0'; | 101 | ctx->line[ctx->pos] = '\0'; |
105 | } | 102 | } |
106 | } | 103 | } |
107 | 104 | ||
105 | /* RETURN key: Execute command or print line */ | ||
108 | else if(key == '\n') { | 106 | else if(key == '\n') { |
109 | if(!string_is_empty(ctx->line)) { | 107 | if (ctx->line[0] == '/') |
110 | /* make sure the string has at least non-space character */ | 108 | execute(self, ctx, ctx->line); |
111 | wattron(ctx->history, COLOR_PAIR(2)); | 109 | else { |
112 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | ||
113 | wattron(ctx->history, COLOR_PAIR(1)); | 110 | wattron(ctx->history, COLOR_PAIR(1)); |
114 | wprintw(ctx->history, "you: ", ctx->line); | 111 | wprintw(ctx->history, "you: ", ctx->line); |
115 | wattroff(ctx->history, COLOR_PAIR(1)); | 112 | wattroff(ctx->history, COLOR_PAIR(1)); |
@@ -120,18 +117,65 @@ static void chat_onKey(ToxWindow* self, int key) { | |||
120 | wprintw(ctx->history, " * Failed to send message.\n"); | 117 | wprintw(ctx->history, " * Failed to send message.\n"); |
121 | wattroff(ctx->history, COLOR_PAIR(3)); | 118 | wattroff(ctx->history, COLOR_PAIR(3)); |
122 | } | 119 | } |
123 | |||
124 | ctx->line[0] = '\0'; | ||
125 | ctx->pos = 0; | ||
126 | } | 120 | } |
121 | ctx->line[0] = '\0'; | ||
122 | ctx->pos = 0; | ||
127 | } | 123 | } |
128 | 124 | ||
125 | /* BACKSPACE key: Remove one character from line */ | ||
129 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { | 126 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { |
130 | if(ctx->pos != 0) { | 127 | if(ctx->pos != 0) { |
131 | ctx->line[--ctx->pos] = '\0'; | 128 | ctx->line[--ctx->pos] = '\0'; |
132 | } | 129 | } |
133 | } | 130 | } |
131 | } | ||
134 | 132 | ||
133 | void execute(ToxWindow* self, ChatContext* ctx, char* cmd) | ||
134 | { | ||
135 | if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { | ||
136 | wclear(self->window); | ||
137 | wclear(ctx->history); | ||
138 | } | ||
139 | else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) | ||
140 | print_help(ctx); | ||
141 | else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { | ||
142 | endwin(); | ||
143 | exit(0); | ||
144 | } | ||
145 | else if (!strncmp(cmd, "/status ", strlen("/status "))) { | ||
146 | char* msg; | ||
147 | msg = strchr(cmd, ' '); | ||
148 | if(msg == NULL) { | ||
149 | wprintw(ctx->history, "Invalid syntax.\n"); | ||
150 | return; | ||
151 | } | ||
152 | msg++; | ||
153 | m_set_userstatus((uint8_t*) msg, strlen(msg)+1); | ||
154 | wprintw(ctx->history, "Status set to: %s\n", msg); | ||
155 | } | ||
156 | else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { | ||
157 | char* nick; | ||
158 | nick = strchr(cmd, ' '); | ||
159 | if(nick == NULL) { | ||
160 | wprintw(ctx->history, "Invalid syntax.\n"); | ||
161 | return; | ||
162 | } | ||
163 | nick++; | ||
164 | setname((uint8_t*) nick, strlen(nick)+1); | ||
165 | wprintw(ctx->history, "Nickname set to: %s\n", nick); | ||
166 | } | ||
167 | else if(!strcmp(cmd, "/myid")) { | ||
168 | char id[32*2 + 1] = {0}; | ||
169 | int i; | ||
170 | for (i = 0; i < 32; i++) { | ||
171 | char xx[3]; | ||
172 | snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); | ||
173 | strcat(id, xx); | ||
174 | } | ||
175 | wprintw(ctx->history, "Your ID: %s\n", id); | ||
176 | } | ||
177 | else | ||
178 | wprintw(ctx->history, "Invalid command.\n"); | ||
135 | } | 179 | } |
136 | 180 | ||
137 | static void chat_onDraw(ToxWindow* self) { | 181 | static void chat_onDraw(ToxWindow* self) { |
@@ -164,6 +208,21 @@ static void chat_onInit(ToxWindow* self) { | |||
164 | ctx->linewin = subwin(self->window, 2, x, y - 3, 0); | 208 | ctx->linewin = subwin(self->window, 2, x, y - 3, 0); |
165 | } | 209 | } |
166 | 210 | ||
211 | void print_help(ChatContext* self) { | ||
212 | wattron(self->history, COLOR_PAIR(2) | A_BOLD); | ||
213 | wprintw(self->history, "\nCommands:\n"); | ||
214 | wattroff(self->history, A_BOLD); | ||
215 | |||
216 | wprintw(self->history, " /status <message> : Set your status\n"); | ||
217 | wprintw(self->history, " /nick <nickname> : Set your nickname\n"); | ||
218 | wprintw(self->history, " /myid : Print your ID\n"); | ||
219 | wprintw(self->history, " /clear : Clear the screen\n"); | ||
220 | wprintw(self->history, " /quit or /exit : Exit program\n"); | ||
221 | wprintw(self->history, " /help : Print this message again\n\n"); | ||
222 | |||
223 | wattroff(self->history, COLOR_PAIR(2)); | ||
224 | } | ||
225 | |||
167 | ToxWindow new_chat(int friendnum) { | 226 | ToxWindow new_chat(int friendnum) { |
168 | ToxWindow ret; | 227 | ToxWindow ret; |
169 | 228 | ||
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index a47238af..365aee49 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c | |||
@@ -148,7 +148,7 @@ static void execute(ToxWindow* self, char* cmd) { | |||
148 | wprintw(self->window, "Friend request already sent.\n"); | 148 | wprintw(self->window, "Friend request already sent.\n"); |
149 | break; | 149 | break; |
150 | case -5: | 150 | case -5: |
151 | wprintw(self->window, "[i] Undefined error when adding friend.\n"); | 151 | wprintw(self->window, "Undefined error when adding friend.\n"); |
152 | break; | 152 | break; |
153 | default: | 153 | default: |
154 | wprintw(self->window, "Friend added as %d.\n", num); | 154 | wprintw(self->window, "Friend added as %d.\n", num); |
@@ -178,12 +178,13 @@ static void execute(ToxWindow* self, char* cmd) { | |||
178 | 178 | ||
179 | nick = strchr(cmd, ' '); | 179 | nick = strchr(cmd, ' '); |
180 | if(nick == NULL) { | 180 | if(nick == NULL) { |
181 | wprintw(self->window, "Invalid syntax.\n"); | ||
181 | return; | 182 | return; |
182 | } | 183 | } |
183 | nick++; | 184 | nick++; |
184 | 185 | ||
185 | setname((uint8_t*) nick, strlen(nick)+1); | 186 | setname((uint8_t*) nick, strlen(nick)+1); |
186 | wprintw(self->window, "Nickname set to: %s.\n", nick); | 187 | wprintw(self->window, "Nickname set to: %s\n", nick); |
187 | } | 188 | } |
188 | else if(!strcmp(cmd, "myid")) { | 189 | else if(!strcmp(cmd, "myid")) { |
189 | char id[32*2 + 1] = {0}; | 190 | char id[32*2 + 1] = {0}; |
@@ -195,7 +196,7 @@ static void execute(ToxWindow* self, char* cmd) { | |||
195 | strcat(id, xx); | 196 | strcat(id, xx); |
196 | } | 197 | } |
197 | 198 | ||
198 | wprintw(self->window, "%s\n", id); | 199 | wprintw(self->window, "Your ID: %s\n", id); |
199 | } | 200 | } |
200 | else if(!strncmp(cmd, "accept ", strlen("accept "))) { | 201 | else if(!strncmp(cmd, "accept ", strlen("accept "))) { |
201 | char* id; | 202 | char* id; |
@@ -256,7 +257,7 @@ static void execute(ToxWindow* self, char* cmd) { | |||
256 | wclear(self->window); | 257 | wclear(self->window); |
257 | } | 258 | } |
258 | else { | 259 | else { |
259 | wprintw(self->window, "Invalid syntax.\n"); | 260 | wprintw(self->window, "Invalid command.\n"); |
260 | } | 261 | } |
261 | } | 262 | } |
262 | 263 | ||