diff options
author | charmlesscoin <charmlesscoin@gmail.com> | 2013-08-06 20:58:42 -0400 |
---|---|---|
committer | charmlesscoin <charmlesscoin@gmail.com> | 2013-08-06 20:58:42 -0400 |
commit | c644ccd28782e9d74f5962a97a0973551fb7afe2 (patch) | |
tree | 42f892aea55a6394d5d6ff0dbace2214675cbd71 /testing/toxic/chat.c | |
parent | 95a3f69580749b8fcabf92cc2ace757d61fa34de (diff) | |
parent | d04f2d0e51931db5fbd8c672c44bb1e59fc58b79 (diff) |
Merge branch 'master' of git://github.com/irungentoo/ProjectTox-Core
Diffstat (limited to 'testing/toxic/chat.c')
-rw-r--r-- | testing/toxic/chat.c | 120 |
1 files changed, 63 insertions, 57 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index da1f9f4a..20c01620 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c | |||
@@ -16,41 +16,39 @@ | |||
16 | 16 | ||
17 | typedef struct { | 17 | typedef struct { |
18 | int friendnum; | 18 | int friendnum; |
19 | |||
20 | char line[256]; | 19 | char line[256]; |
21 | size_t pos; | 20 | size_t pos; |
22 | |||
23 | WINDOW* history; | 21 | WINDOW* history; |
24 | WINDOW* linewin; | 22 | WINDOW* linewin; |
25 | |||
26 | } ChatContext; | 23 | } ChatContext; |
27 | 24 | ||
28 | extern void fix_name(uint8_t* name); | 25 | extern int active_window; |
29 | void print_help(ChatContext* self); | ||
30 | void execute(ToxWindow* self, ChatContext* ctx, char* cmd); | ||
31 | 26 | ||
32 | static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { | 27 | extern void del_window(ToxWindow *w, int f_num); |
33 | ChatContext* ctx = (ChatContext*) self->x; | 28 | extern void fix_name(uint8_t *name); |
29 | void print_help(ChatContext *self); | ||
30 | void execute(ToxWindow *self, ChatContext *ctx, char *cmd); | ||
31 | |||
32 | static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len) | ||
33 | { | ||
34 | ChatContext *ctx = (ChatContext*) self->x; | ||
34 | uint8_t nick[MAX_NAME_LENGTH] = {0}; | 35 | uint8_t nick[MAX_NAME_LENGTH] = {0}; |
35 | |||
36 | time_t now; | 36 | time_t now; |
37 | time(&now); | 37 | time(&now); |
38 | struct tm * timeinfo; | 38 | struct tm * timeinfo; |
39 | timeinfo = localtime(&now); | 39 | timeinfo = localtime(&now); |
40 | 40 | ||
41 | if(ctx->friendnum != num) | 41 | if (ctx->friendnum != num) |
42 | return; | 42 | return; |
43 | 43 | ||
44 | getname(num, (uint8_t*) &nick); | 44 | getname(num, (uint8_t*) &nick); |
45 | |||
46 | msg[len-1] = '\0'; | 45 | msg[len-1] = '\0'; |
47 | nick[MAX_NAME_LENGTH-1] = '\0'; | 46 | nick[MAX_NAME_LENGTH-1] = '\0'; |
48 | |||
49 | fix_name(msg); | 47 | fix_name(msg); |
50 | fix_name(nick); | 48 | fix_name(nick); |
51 | 49 | ||
52 | wattron(ctx->history, COLOR_PAIR(2)); | 50 | wattron(ctx->history, COLOR_PAIR(2)); |
53 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 51 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
54 | wattroff(ctx->history, COLOR_PAIR(2)); | 52 | wattroff(ctx->history, COLOR_PAIR(2)); |
55 | wattron(ctx->history, COLOR_PAIR(4)); | 53 | wattron(ctx->history, COLOR_PAIR(4)); |
56 | wprintw(ctx->history, "%s: ", nick); | 54 | wprintw(ctx->history, "%s: ", nick); |
@@ -62,15 +60,14 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) | |||
62 | flash(); | 60 | flash(); |
63 | } | 61 | } |
64 | 62 | ||
65 | static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t len) { | 63 | static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) |
66 | ChatContext* ctx = (ChatContext*) self->x; | 64 | { |
67 | 65 | ChatContext *ctx = (ChatContext*) self->x; | |
68 | if(ctx->friendnum != num) | 66 | if (ctx->friendnum != num) |
69 | return; | 67 | return; |
70 | 68 | ||
71 | nick[len-1] = '\0'; | 69 | nick[len-1] = '\0'; |
72 | fix_name(nick); | 70 | fix_name(nick); |
73 | |||
74 | snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num); | 71 | snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num); |
75 | 72 | ||
76 | wattron(ctx->history, COLOR_PAIR(3)); | 73 | wattron(ctx->history, COLOR_PAIR(3)); |
@@ -78,7 +75,8 @@ static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t | |||
78 | wattroff(ctx->history, COLOR_PAIR(3)); | 75 | wattroff(ctx->history, COLOR_PAIR(3)); |
79 | } | 76 | } |
80 | 77 | ||
81 | static void chat_onStatusChange(ToxWindow* self, int num, uint8_t* status, uint16_t len) { | 78 | static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) |
79 | { | ||
82 | 80 | ||
83 | } | 81 | } |
84 | 82 | ||
@@ -87,45 +85,43 @@ int string_is_empty(char *string) | |||
87 | { | 85 | { |
88 | int rc = 0; | 86 | int rc = 0; |
89 | char *copy = strdup(string); | 87 | char *copy = strdup(string); |
90 | |||
91 | rc = ((strtok(copy, " ") == NULL) ? 1:0); | 88 | rc = ((strtok(copy, " ") == NULL) ? 1:0); |
92 | free(copy); | 89 | free(copy); |
93 | |||
94 | return rc; | 90 | return rc; |
95 | } | 91 | } |
96 | 92 | ||
97 | static void chat_onKey(ToxWindow* self, int key) { | 93 | static void chat_onKey(ToxWindow *self, int key) |
98 | ChatContext* ctx = (ChatContext*) self->x; | 94 | { |
99 | 95 | ChatContext *ctx = (ChatContext*) self->x; | |
100 | time_t now; | 96 | time_t now; |
101 | time(&now); | 97 | time(&now); |
102 | struct tm * timeinfo; | 98 | struct tm * timeinfo; |
103 | timeinfo = localtime(&now); | 99 | timeinfo = localtime(&now); |
104 | 100 | ||
105 | /* PRINTABLE characters: Add to line */ | 101 | /* Add printable characters to line */ |
106 | if(isprint(key)) { | 102 | if (isprint(key)) { |
107 | if(ctx->pos != sizeof(ctx->line)-1) { | 103 | if (ctx->pos != sizeof(ctx->line)-1) { |
108 | ctx->line[ctx->pos++] = key; | 104 | ctx->line[ctx->pos++] = key; |
109 | ctx->line[ctx->pos] = '\0'; | 105 | ctx->line[ctx->pos] = '\0'; |
110 | } | 106 | } |
111 | } | 107 | } |
112 | 108 | ||
113 | /* RETURN key: Execute command or print line */ | 109 | /* RETURN key: Execute command or print line */ |
114 | else if(key == '\n') { | 110 | else if (key == '\n') { |
115 | if (ctx->line[0] == '/') | 111 | if (ctx->line[0] == '/') |
116 | execute(self, ctx, ctx->line); | 112 | execute(self, ctx, ctx->line); |
117 | else { | 113 | else { |
118 | if(!string_is_empty(ctx->line)) { | 114 | if (!string_is_empty(ctx->line)) { |
119 | /* make sure the string has at least non-space character */ | 115 | /* make sure the string has at least non-space character */ |
120 | wattron(ctx->history, COLOR_PAIR(2)); | 116 | wattron(ctx->history, COLOR_PAIR(2)); |
121 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 117 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
122 | wattroff(ctx->history, COLOR_PAIR(2)); | 118 | wattroff(ctx->history, COLOR_PAIR(2)); |
123 | wattron(ctx->history, COLOR_PAIR(1)); | 119 | wattron(ctx->history, COLOR_PAIR(1)); |
124 | wprintw(ctx->history, "you: ", ctx->line); | 120 | wprintw(ctx->history, "you: ", ctx->line); |
125 | wattroff(ctx->history, COLOR_PAIR(1)); | 121 | wattroff(ctx->history, COLOR_PAIR(1)); |
126 | wprintw(ctx->history, "%s\n", ctx->line); | 122 | wprintw(ctx->history, "%s\n", ctx->line); |
127 | } | 123 | } |
128 | if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { | 124 | if (m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { |
129 | wattron(ctx->history, COLOR_PAIR(3)); | 125 | wattron(ctx->history, COLOR_PAIR(3)); |
130 | wprintw(ctx->history, " * Failed to send message.\n"); | 126 | wprintw(ctx->history, " * Failed to send message.\n"); |
131 | wattroff(ctx->history, COLOR_PAIR(3)); | 127 | wattroff(ctx->history, COLOR_PAIR(3)); |
@@ -136,29 +132,32 @@ static void chat_onKey(ToxWindow* self, int key) { | |||
136 | } | 132 | } |
137 | 133 | ||
138 | /* BACKSPACE key: Remove one character from line */ | 134 | /* BACKSPACE key: Remove one character from line */ |
139 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { | 135 | else if (key == 0x107 || key == 0x8 || key == 0x7f) { |
140 | if(ctx->pos != 0) { | 136 | if (ctx->pos != 0) { |
141 | ctx->line[--ctx->pos] = '\0'; | 137 | ctx->line[--ctx->pos] = '\0'; |
142 | } | 138 | } |
143 | } | 139 | } |
144 | } | 140 | } |
145 | 141 | ||
146 | void execute(ToxWindow* self, ChatContext* ctx, char* cmd) | 142 | void execute(ToxWindow *self, ChatContext *ctx, char *cmd) |
147 | { | 143 | { |
148 | if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { | 144 | if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { |
149 | wclear(self->window); | 145 | wclear(self->window); |
150 | wclear(ctx->history); | 146 | wclear(ctx->history); |
151 | } | 147 | } |
148 | |||
152 | else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) | 149 | else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) |
153 | print_help(ctx); | 150 | print_help(ctx); |
151 | |||
154 | else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { | 152 | else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { |
155 | endwin(); | 153 | endwin(); |
156 | exit(0); | 154 | exit(0); |
157 | } | 155 | } |
156 | |||
158 | else if (!strncmp(cmd, "/status ", strlen("/status "))) { | 157 | else if (!strncmp(cmd, "/status ", strlen("/status "))) { |
159 | char* msg; | 158 | char *msg; |
160 | msg = strchr(cmd, ' '); | 159 | msg = strchr(cmd, ' '); |
161 | if(msg == NULL) { | 160 | if (msg == NULL) { |
162 | wprintw(ctx->history, "Invalid syntax.\n"); | 161 | wprintw(ctx->history, "Invalid syntax.\n"); |
163 | return; | 162 | return; |
164 | } | 163 | } |
@@ -166,10 +165,11 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd) | |||
166 | m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1); | 165 | m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1); |
167 | wprintw(ctx->history, "Status set to: %s\n", msg); | 166 | wprintw(ctx->history, "Status set to: %s\n", msg); |
168 | } | 167 | } |
168 | |||
169 | else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { | 169 | else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { |
170 | char* nick; | 170 | char *nick; |
171 | nick = strchr(cmd, ' '); | 171 | nick = strchr(cmd, ' '); |
172 | if(nick == NULL) { | 172 | if (nick == NULL) { |
173 | wprintw(ctx->history, "Invalid syntax.\n"); | 173 | wprintw(ctx->history, "Invalid syntax.\n"); |
174 | return; | 174 | return; |
175 | } | 175 | } |
@@ -177,7 +177,8 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd) | |||
177 | setname((uint8_t*) nick, strlen(nick)+1); | 177 | setname((uint8_t*) nick, strlen(nick)+1); |
178 | wprintw(ctx->history, "Nickname set to: %s\n", nick); | 178 | wprintw(ctx->history, "Nickname set to: %s\n", nick); |
179 | } | 179 | } |
180 | else if(!strcmp(cmd, "/myid")) { | 180 | |
181 | else if (!strcmp(cmd, "/myid")) { | ||
181 | char id[32*2 + 1] = {0}; | 182 | char id[32*2 + 1] = {0}; |
182 | int i; | 183 | int i; |
183 | for (i = 0; i < 32; i++) { | 184 | for (i = 0; i < 32; i++) { |
@@ -187,58 +188,64 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd) | |||
187 | } | 188 | } |
188 | wprintw(ctx->history, "Your ID: %s\n", id); | 189 | wprintw(ctx->history, "Your ID: %s\n", id); |
189 | } | 190 | } |
191 | |||
192 | else if (strcmp(ctx->line, "/close") == 0) { | ||
193 | active_window = 0; // Go to prompt screen | ||
194 | int f_num = ctx->friendnum; | ||
195 | delwin(ctx->linewin); | ||
196 | del_window(self, f_num); | ||
197 | } | ||
198 | |||
190 | else | 199 | else |
191 | wprintw(ctx->history, "Invalid command.\n"); | 200 | wprintw(ctx->history, "Invalid command.\n"); |
192 | } | 201 | } |
193 | 202 | ||
194 | static void chat_onDraw(ToxWindow* self) { | 203 | static void chat_onDraw(ToxWindow *self) |
204 | { | ||
195 | curs_set(1); | 205 | curs_set(1); |
196 | int x, y; | 206 | int x, y; |
197 | ChatContext* ctx = (ChatContext*) self->x; | 207 | ChatContext *ctx = (ChatContext*) self->x; |
198 | |||
199 | getmaxyx(self->window, y, x); | 208 | getmaxyx(self->window, y, x); |
200 | |||
201 | (void) x; | 209 | (void) x; |
202 | if(y < 3) | 210 | if (y < 3) return; |
203 | return; | ||
204 | 211 | ||
205 | wclear(ctx->linewin); | 212 | wclear(ctx->linewin); |
206 | mvwhline(ctx->linewin, 0, 0, '_', COLS); | 213 | mvwhline(ctx->linewin, 0, 0, '_', COLS); |
207 | mvwprintw(self->window, y-1, 0, "%s\n", ctx->line); | 214 | mvwprintw(self->window, y-1, 0, "%s\n", ctx->line); |
208 | |||
209 | wrefresh(self->window); | 215 | wrefresh(self->window); |
210 | } | 216 | } |
211 | 217 | ||
212 | static void chat_onInit(ToxWindow* self) { | 218 | static void chat_onInit(ToxWindow *self) |
219 | { | ||
213 | int x, y; | 220 | int x, y; |
214 | ChatContext* ctx = (ChatContext*) self->x; | 221 | ChatContext *ctx = (ChatContext*) self->x; |
215 | |||
216 | getmaxyx(self->window, y, x); | 222 | getmaxyx(self->window, y, x); |
217 | |||
218 | ctx->history = subwin(self->window, y - 4, x, 0, 0); | 223 | ctx->history = subwin(self->window, y - 4, x, 0, 0); |
219 | scrollok(ctx->history, 1); | 224 | scrollok(ctx->history, 1); |
220 | |||
221 | ctx->linewin = subwin(self->window, 2, x, y - 3, 0); | 225 | ctx->linewin = subwin(self->window, 2, x, y - 3, 0); |
226 | print_help(ctx); | ||
222 | } | 227 | } |
223 | 228 | ||
224 | void print_help(ChatContext* self) { | 229 | void print_help(ChatContext *self) |
230 | { | ||
225 | wattron(self->history, COLOR_PAIR(2) | A_BOLD); | 231 | wattron(self->history, COLOR_PAIR(2) | A_BOLD); |
226 | wprintw(self->history, "\nCommands:\n"); | 232 | wprintw(self->history, "Commands:\n"); |
227 | wattroff(self->history, A_BOLD); | 233 | wattroff(self->history, A_BOLD); |
228 | 234 | ||
229 | wprintw(self->history, " /status <message> : Set your status\n"); | 235 | wprintw(self->history, " /status <message> : Set your status\n"); |
230 | wprintw(self->history, " /nick <nickname> : Set your nickname\n"); | 236 | wprintw(self->history, " /nick <nickname> : Set your nickname\n"); |
231 | wprintw(self->history, " /myid : Print your ID\n"); | 237 | wprintw(self->history, " /myid : Print your ID\n"); |
232 | wprintw(self->history, " /clear : Clear the screen\n"); | 238 | wprintw(self->history, " /clear : Clear the screen\n"); |
239 | wprintw(self->history, " /close : Close the current chat window\n"); | ||
233 | wprintw(self->history, " /quit or /exit : Exit program\n"); | 240 | wprintw(self->history, " /quit or /exit : Exit program\n"); |
234 | wprintw(self->history, " /help : Print this message again\n\n"); | 241 | wprintw(self->history, " /help : Print this message again\n\n"); |
235 | 242 | ||
236 | wattroff(self->history, COLOR_PAIR(2)); | 243 | wattroff(self->history, COLOR_PAIR(2)); |
237 | } | 244 | } |
238 | 245 | ||
239 | ToxWindow new_chat(int friendnum) { | 246 | ToxWindow new_chat(int friendnum) |
247 | { | ||
240 | ToxWindow ret; | 248 | ToxWindow ret; |
241 | |||
242 | memset(&ret, 0, sizeof(ret)); | 249 | memset(&ret, 0, sizeof(ret)); |
243 | 250 | ||
244 | ret.onKey = &chat_onKey; | 251 | ret.onKey = &chat_onKey; |
@@ -254,9 +261,8 @@ ToxWindow new_chat(int friendnum) { | |||
254 | 261 | ||
255 | snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); | 262 | snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); |
256 | 263 | ||
257 | ChatContext* x = calloc(1, sizeof(ChatContext)); | 264 | ChatContext *x = calloc(1, sizeof(ChatContext)); |
258 | x->friendnum = friendnum; | 265 | x->friendnum = friendnum; |
259 | |||
260 | ret.x = (void*) x; | 266 | ret.x = (void*) x; |
261 | return ret; | 267 | return ret; |
262 | } | 268 | } |