0
|
1 #include <stdio.h> |
|
2 #include <stdlib.h> |
|
3 #include <string.h> |
|
4 #include <ncurses.h> |
|
5 #include <panel.h> |
|
6 #include <time.h> |
|
7 #include <ctype.h> |
|
8 |
|
9 #include "screen.h" |
|
10 #include "utils.h" |
|
11 #include "buddies.h" |
|
12 #include "parsecfg.h" |
|
13 #include "lang.h" |
|
14 #include "server.h" |
|
15 |
|
16 #include "list.h" |
|
17 |
|
18 /* Definicion de tipos */ |
|
19 #define window_entry(n) list_entry(n, window_entry_t, list) |
|
20 |
|
21 typedef struct _window_entry_t { |
|
22 WINDOW *win; |
|
23 PANEL *panel; |
|
24 char *name; |
|
25 int nlines; |
|
26 char **texto; |
|
27 struct list_head list; |
|
28 } window_entry_t; |
|
29 |
|
30 LIST_HEAD(window_list); |
|
31 |
|
32 /* Variables globales a SCREEN.C */ |
3
|
33 static WINDOW *rosterWnd, *chatWnd, *inputWnd; |
|
34 static WINDOW *logWnd, *logWnd_border; |
|
35 static PANEL *rosterPanel, *chatPanel, *inputPanel; |
2
|
36 static PANEL *logPanel, *logPanel_border; |
3
|
37 static int maxY, maxX; |
|
38 static window_entry_t *currentWindow; |
0
|
39 |
1
|
40 char inputLine[INPUTLINE_LENGTH]; |
|
41 char *ptr_inputline; |
|
42 |
0
|
43 |
|
44 /* Funciones */ |
|
45 |
|
46 int scr_WindowHeight(WINDOW * win) |
|
47 { |
|
48 int x, y; |
|
49 getmaxyx(win, y, x); |
|
50 return x; |
|
51 } |
|
52 |
1
|
53 void scr_draw_box(WINDOW * win, int y, int x, int height, int width, |
|
54 int Color, chtype box, chtype border) |
0
|
55 { |
|
56 int i, j; |
|
57 |
|
58 wattrset(win, COLOR_PAIR(Color)); |
|
59 for (i = 0; i < height; i++) { |
|
60 wmove(win, y + i, x); |
|
61 for (j = 0; j < width; j++) |
|
62 if (!i && !j) |
|
63 waddch(win, border | ACS_ULCORNER); |
|
64 else if (i == height - 1 && !j) |
|
65 waddch(win, border | ACS_LLCORNER); |
|
66 else if (!i && j == width - 1) |
|
67 waddch(win, box | ACS_URCORNER); |
|
68 else if (i == height - 1 && j == width - 1) |
|
69 waddch(win, box | ACS_LRCORNER); |
|
70 else if (!i) |
|
71 waddch(win, border | ACS_HLINE); |
|
72 else if (i == height - 1) |
|
73 waddch(win, box | ACS_HLINE); |
|
74 else if (!j) |
|
75 waddch(win, border | ACS_VLINE); |
|
76 else if (j == width - 1) |
|
77 waddch(win, box | ACS_VLINE); |
|
78 else |
|
79 waddch(win, box | ' '); |
|
80 } |
|
81 } |
|
82 |
|
83 int FindColor(char *name) |
|
84 { |
|
85 if (!strcmp(name, "default")) |
|
86 return -1; |
|
87 if (!strcmp(name, "black")) |
|
88 return COLOR_BLACK; |
|
89 if (!strcmp(name, "red")) |
|
90 return COLOR_RED; |
|
91 if (!strcmp(name, "green")) |
|
92 return COLOR_GREEN; |
|
93 if (!strcmp(name, "yellow")) |
|
94 return COLOR_YELLOW; |
|
95 if (!strcmp(name, "blue")) |
|
96 return COLOR_BLUE; |
|
97 if (!strcmp(name, "magenta")) |
|
98 return COLOR_MAGENTA; |
|
99 if (!strcmp(name, "cyan")) |
|
100 return COLOR_CYAN; |
|
101 if (!strcmp(name, "white")) |
|
102 return COLOR_WHITE; |
|
103 |
|
104 return -1; |
|
105 } |
|
106 |
|
107 void ParseColors(void) |
|
108 { |
|
109 char *colors[11] = { |
|
110 "", "", |
|
111 "borderlines", |
|
112 "jidonlineselected", |
|
113 "jidonline", |
|
114 "jidofflineselected", |
|
115 "jidoffline", |
|
116 "text", |
|
117 NULL |
|
118 }; |
|
119 |
|
120 char *tmp = malloc(1024); |
|
121 char *color1; |
|
122 char *background = cfg_read("color_background"); |
|
123 char *backselected = cfg_read("color_backselected"); |
|
124 int i = 0; |
|
125 |
|
126 while (colors[i]) { |
|
127 sprintf(tmp, "color_%s", colors[i]); |
|
128 color1 = cfg_read(tmp); |
|
129 |
|
130 switch (i + 1) { |
|
131 case 1: |
|
132 init_pair(1, COLOR_BLACK, COLOR_WHITE); |
|
133 break; |
|
134 case 2: |
|
135 init_pair(2, COLOR_WHITE, COLOR_BLACK); |
|
136 break; |
|
137 case 3: |
|
138 init_pair(3, FindColor(color1), FindColor(background)); |
|
139 break; |
|
140 case 4: |
|
141 init_pair(4, FindColor(color1), FindColor(backselected)); |
|
142 break; |
|
143 case 5: |
|
144 init_pair(5, FindColor(color1), FindColor(background)); |
|
145 break; |
|
146 case 6: |
|
147 init_pair(6, FindColor(color1), FindColor(backselected)); |
|
148 break; |
|
149 case 7: |
|
150 init_pair(7, FindColor(color1), FindColor(background)); |
|
151 break; |
|
152 case 8: |
|
153 init_pair(8, FindColor(color1), FindColor(background)); |
|
154 break; |
|
155 } |
|
156 i++; |
|
157 } |
|
158 } |
|
159 |
|
160 |
|
161 window_entry_t *scr_CreatePanel(char *title, int x, int y, int lines, |
3
|
162 int cols, int dont_show) |
0
|
163 { |
|
164 window_entry_t *tmp = calloc(1, sizeof(window_entry_t)); |
|
165 |
|
166 tmp->win = newwin(lines, cols, y, x); |
|
167 tmp->panel = new_panel(tmp->win); |
|
168 tmp->name = (char *) calloc(1, 1024); |
|
169 strncpy(tmp->name, title, 1024); |
9
|
170 |
0
|
171 scr_draw_box(tmp->win, 0, 0, lines, cols, COLOR_GENERAL, 0, 0); |
|
172 mvwprintw(tmp->win, 0, (cols - (2 + strlen(title))) / 2, " %s ", title); |
3
|
173 if (!dont_show) { |
|
174 currentWindow = tmp; |
|
175 } else { |
9
|
176 if (currentWindow) |
|
177 top_panel(currentWindow->panel); |
|
178 else |
|
179 top_panel(chatPanel); |
3
|
180 } |
9
|
181 |
0
|
182 list_add_tail(&tmp->list, &window_list); |
|
183 update_panels(); |
|
184 |
|
185 return tmp; |
|
186 } |
|
187 |
|
188 |
1
|
189 void scr_CreatePopup(char *title, char *texto, int corte, int type, |
|
190 char *returnstring) |
0
|
191 { |
|
192 WINDOW *popupWin; |
|
193 PANEL *popupPanel; |
|
194 |
|
195 int lineas = 0; |
|
196 int cols = 0; |
|
197 |
|
198 char **submsgs; |
|
199 int n = 0; |
|
200 int i; |
|
201 |
|
202 char *instr = (char *) calloc(1, 1024); |
|
203 |
|
204 /* fprintf(stderr, "\r\n%d", lineas); */ |
|
205 |
|
206 submsgs = ut_SplitMessage(texto, &n, corte); |
|
207 |
|
208 switch (type) { |
|
209 case 1: |
|
210 case 0: |
|
211 lineas = n + 4; |
|
212 break; |
|
213 } |
|
214 |
|
215 cols = corte + 3; |
|
216 popupWin = newwin(lineas, cols, (maxY - lineas) / 2, (maxX - cols) / 2); |
|
217 popupPanel = new_panel(popupWin); |
|
218 |
|
219 /*ATENCION!!! Colorear el popup ?? |
|
220 / box (popupWin, 0, 0); */ |
|
221 scr_draw_box(popupWin, 0, 0, lineas, cols, COLOR_POPUP, 0, 0); |
|
222 mvwprintw(popupWin, 0, (cols - (2 + strlen(title))) / 2, " %s ", title); |
|
223 |
|
224 for (i = 0; i < n; i++) |
|
225 mvwprintw(popupWin, i + 1, 2, "%s", submsgs[i]); |
|
226 |
|
227 |
|
228 for (i = 0; i < n; i++) |
|
229 free(submsgs[i]); |
|
230 free(submsgs); |
|
231 |
|
232 switch (type) { |
|
233 case 0: |
|
234 mvwprintw(popupWin, n + 2, |
|
235 (cols - (2 + strlen(i18n("Press any key")))) / 2, |
|
236 i18n("Press any key")); |
|
237 update_panels(); |
|
238 doupdate(); |
|
239 getch(); |
|
240 break; |
|
241 case 1: |
|
242 { |
|
243 char ch; |
|
244 int scroll = 0; |
|
245 int input_x = 0; |
|
246 |
|
247 wmove(popupWin, 3, 1); |
|
248 wrefresh(popupWin); |
|
249 keypad(popupWin, TRUE); |
|
250 while ((ch = getch()) != '\n') { |
|
251 switch (ch) { |
|
252 case 0x09: |
|
253 case KEY_UP: |
|
254 case KEY_DOWN: |
|
255 break; |
|
256 case KEY_RIGHT: |
|
257 case KEY_LEFT: |
|
258 break; |
|
259 case KEY_BACKSPACE: |
|
260 case 127: |
|
261 if (input_x || scroll) { |
|
262 /* wattrset (popupWin, 0); */ |
|
263 if (!input_x) { |
|
264 scroll = scroll < cols - 3 ? 0 : scroll - (cols - 3); |
|
265 wmove(popupWin, 3, 1); |
|
266 for (i = 0; i < cols; i++) |
|
267 waddch |
|
268 (popupWin, |
|
269 instr |
|
270 [scroll |
|
271 + input_x + i] ? instr[scroll + input_x + i] : ' '); |
|
272 input_x = strlen(instr) - scroll; |
|
273 } else |
|
274 input_x--; |
|
275 instr[scroll + input_x] = '\0'; |
|
276 mvwaddch(popupWin, 3, input_x + 1, ' '); |
|
277 wmove(popupWin, 3, input_x + 1); |
|
278 wrefresh(popupWin); |
|
279 } |
|
280 default: |
|
281 if ( /*ch<0x100 && */ isprint(ch) || ch == '�' |
|
282 || ch == '�') { |
|
283 if (scroll + input_x < 1024) { |
|
284 instr[scroll + input_x] = ch; |
|
285 instr[scroll + input_x + 1] = '\0'; |
|
286 if (input_x == cols - 3) { |
|
287 scroll++; |
|
288 wmove(popupWin, 3, 1); |
|
289 for (i = 0; i < cols - 3; i++) |
|
290 waddch(popupWin, instr[scroll + i]); |
|
291 } else { |
|
292 wmove(popupWin, 3, 1 + input_x++); |
|
293 waddch(popupWin, ch); |
|
294 } |
|
295 wrefresh(popupWin); |
|
296 } else { |
|
297 flash(); |
|
298 } |
|
299 } |
|
300 } |
|
301 } |
|
302 } |
|
303 if (returnstring != NULL) |
|
304 strcpy(returnstring, instr); |
|
305 break; |
|
306 } |
|
307 |
|
308 del_panel(popupPanel); |
|
309 delwin(popupWin); |
|
310 update_panels(); |
|
311 doupdate(); |
|
312 free(instr); |
2
|
313 keypad(inputWnd, TRUE); |
0
|
314 } |
|
315 |
|
316 |
|
317 |
|
318 void scr_RoolWindow(void) |
|
319 { |
|
320 } |
|
321 |
3
|
322 window_entry_t *scr_SearchWindow(char *winId) |
0
|
323 { |
|
324 struct list_head *pos, *n; |
|
325 window_entry_t *search_entry = NULL; |
|
326 |
|
327 list_for_each_safe(pos, n, &window_list) { |
|
328 search_entry = window_entry(pos); |
|
329 if (search_entry->name) { |
3
|
330 if (!strcasecmp(search_entry->name, winId)) { |
0
|
331 return search_entry; |
|
332 } |
|
333 } |
|
334 } |
|
335 return NULL; |
|
336 } |
|
337 |
3
|
338 void scr_ShowWindow(char *winId) |
0
|
339 { |
|
340 int n, width, i; |
3
|
341 window_entry_t *tmp = scr_SearchWindow(winId); |
0
|
342 if (tmp != NULL) { |
|
343 top_panel(tmp->panel); |
3
|
344 currentWindow = tmp; |
0
|
345 width = scr_WindowHeight(tmp->win); |
|
346 for (n = 0; n < tmp->nlines; n++) { |
|
347 mvwprintw(tmp->win, n + 1, 1, ""); |
|
348 for (i = 0; i < width - 2; i++) |
|
349 waddch(tmp->win, ' '); |
|
350 mvwprintw(tmp->win, n + 1, 1, "%s", tmp->texto[n]); |
|
351 } |
9
|
352 //move(CHAT_WIN_HEIGHT - 1, maxX - 1); |
0
|
353 update_panels(); |
|
354 doupdate(); |
|
355 } |
|
356 } |
|
357 |
|
358 void scr_ShowBuddyWindow(void) |
|
359 { |
|
360 buddy_entry_t *tmp = bud_SelectedInfo(); |
|
361 if (tmp->jid != NULL) |
|
362 scr_ShowWindow(tmp->jid); |
|
363 } |
|
364 |
|
365 |
3
|
366 void scr_WriteInWindow(char *winId, char *texto, int TimeStamp) |
0
|
367 { |
|
368 time_t ahora; |
|
369 int n; |
|
370 int i; |
|
371 int width; |
|
372 window_entry_t *tmp; |
9
|
373 int dont_show = FALSE; |
0
|
374 |
3
|
375 |
|
376 tmp = scr_SearchWindow(winId); |
|
377 |
9
|
378 if (!currentWindow || (currentWindow != tmp)) |
|
379 dont_show = TRUE; |
|
380 scr_LogPrint("dont_show=%d", dont_show); |
3
|
381 |
0
|
382 if (tmp == NULL) { |
3
|
383 tmp = scr_CreatePanel(winId, 20, 0, CHAT_WIN_HEIGHT, maxX - 20, dont_show); |
1
|
384 tmp->texto = (char **) calloc((CHAT_WIN_HEIGHT+1) * 3, sizeof(char *)); |
|
385 for (n = 0; n < CHAT_WIN_HEIGHT * 3; n++) |
0
|
386 tmp->texto[n] = (char *) calloc(1, 1024); |
|
387 |
|
388 if (TimeStamp) { |
|
389 ahora = time(NULL); |
|
390 strftime(tmp->texto[tmp->nlines], 1024, "[%H:%M] ", |
|
391 localtime(&ahora)); |
|
392 strcat(tmp->texto[tmp->nlines], texto); |
|
393 } else { |
|
394 sprintf(tmp->texto[tmp->nlines], " %s", texto); |
|
395 } |
|
396 tmp->nlines++; |
|
397 } else { |
1
|
398 if (tmp->nlines < CHAT_WIN_HEIGHT - 2) { |
0
|
399 if (TimeStamp) { |
|
400 ahora = time(NULL); |
|
401 strftime(tmp->texto[tmp->nlines], 1024, |
|
402 "[%H:%M] ", localtime(&ahora)); |
|
403 strcat(tmp->texto[tmp->nlines], texto); |
|
404 } else { |
|
405 sprintf(tmp->texto[tmp->nlines], " %s", texto); |
|
406 } |
|
407 tmp->nlines++; |
|
408 } else { |
|
409 for (n = 0; n < tmp->nlines; n++) { |
|
410 memset(tmp->texto[n], 0, 1024); |
|
411 strncpy(tmp->texto[n], tmp->texto[n + 1], 1024); |
|
412 } |
|
413 if (TimeStamp) { |
|
414 ahora = time(NULL); |
|
415 strftime(tmp->texto[tmp->nlines - 1], 1024, |
|
416 "[%H:%M] ", localtime(&ahora)); |
|
417 strcat(tmp->texto[tmp->nlines - 1], texto); |
|
418 } else { |
|
419 sprintf(tmp->texto[tmp->nlines - 1], " %s", texto); |
|
420 } |
|
421 } |
|
422 } |
|
423 |
3
|
424 if (!dont_show) { |
|
425 top_panel(tmp->panel); |
|
426 width = scr_WindowHeight(tmp->win); |
|
427 for (n = 0; n < tmp->nlines; n++) { |
|
428 mvwprintw(tmp->win, n + 1, 1, ""); |
|
429 for (i = 0; i < width - 2; i++) |
|
430 waddch(tmp->win, ' '); |
|
431 mvwprintw(tmp->win, n + 1, 1, "%s", tmp->texto[n]); |
|
432 } |
|
433 |
|
434 update_panels(); |
|
435 doupdate(); |
0
|
436 } |
|
437 } |
|
438 |
|
439 void scr_InitCurses(void) |
|
440 { |
|
441 initscr(); |
|
442 noecho(); |
|
443 raw(); |
2
|
444 //cbreak(); |
0
|
445 start_color(); |
|
446 use_default_colors(); |
|
447 |
|
448 ParseColors(); |
|
449 |
|
450 getmaxyx(stdscr, maxY, maxX); |
1
|
451 inputLine[0] = 0; |
|
452 ptr_inputline = inputLine; |
0
|
453 |
|
454 return; |
|
455 } |
|
456 |
|
457 void scr_DrawMainWindow(void) |
|
458 { |
1
|
459 /* Draw main panels */ |
0
|
460 rosterWnd = newwin(maxY-1, 20, 0, 0); |
|
461 rosterPanel = new_panel(rosterWnd); |
|
462 scr_draw_box(rosterWnd, 0, 0, maxY-1, 20, COLOR_GENERAL, 0, 0); |
|
463 mvwprintw(rosterWnd, 0, (20 - strlen(i18n("Roster"))) / 2, |
|
464 i18n("Roster")); |
|
465 |
1
|
466 chatWnd = newwin(CHAT_WIN_HEIGHT, maxX - 20, 0, 20); |
0
|
467 chatPanel = new_panel(chatWnd); |
1
|
468 scr_draw_box(chatWnd, 0, 0, CHAT_WIN_HEIGHT, maxX - 20, COLOR_GENERAL, 0, 0); |
0
|
469 mvwprintw(chatWnd, 0, |
|
470 ((maxX - 20) - strlen(i18n("Status Window"))) / 2, |
|
471 i18n("Status Window")); |
4
|
472 //wbkgd(chatWnd, COLOR_PAIR(COLOR_GENERAL)); |
0
|
473 |
1
|
474 logWnd_border = newwin(LOG_WIN_HEIGHT, maxX - 20, CHAT_WIN_HEIGHT, 20); |
|
475 logPanel_border = new_panel(logWnd_border); |
|
476 scr_draw_box(logWnd_border, 0, 0, LOG_WIN_HEIGHT, maxX - 20, COLOR_GENERAL, 0, 0); |
|
477 // mvwprintw(logWnd_border, 0, |
|
478 // ((maxX - 20) - strlen(i18n("Log Window"))) / 2, |
|
479 // i18n("Log Window")); |
|
480 //logWnd = newwin(LOG_WIN_HEIGHT - 2, maxX-20 - 2, CHAT_WIN_HEIGHT+1, 20+1); |
|
481 logWnd = derwin(logWnd_border, LOG_WIN_HEIGHT-2, maxX-20-2, 1, 1); |
|
482 logPanel = new_panel(logWnd); |
|
483 wbkgd(logWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
484 //wattrset(logWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
485 wprintw(logWnd, "Here we are\n"); |
4
|
486 scr_LogPrint("Here we are :-)"); |
1
|
487 |
|
488 scrollok(logWnd,TRUE); |
|
489 idlok(logWnd,TRUE); // XXX Necessary? |
|
490 |
|
491 inputWnd = newwin(1, maxX, maxY-1, 0); |
|
492 inputPanel = new_panel(inputWnd); |
|
493 //wbkgd(inputWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
494 |
0
|
495 bud_DrawRoster(rosterWnd); |
|
496 update_panels(); |
|
497 doupdate(); |
|
498 return; |
|
499 } |
|
500 |
|
501 void scr_TerminateCurses(void) |
|
502 { |
|
503 clear(); |
|
504 refresh(); |
|
505 endwin(); |
|
506 return; |
|
507 } |
|
508 |
|
509 void scr_WriteIncomingMessage(char *jidfrom, char *text) |
|
510 { |
|
511 char **submsgs; |
|
512 int n, i; |
|
513 char *buffer = (char *) malloc(5 + strlen(text)); |
|
514 |
|
515 sprintf(buffer, "<<< %s", text); |
|
516 |
|
517 submsgs = |
|
518 ut_SplitMessage(buffer, &n, maxX - scr_WindowHeight(rosterWnd) - 20); |
|
519 |
|
520 for (i = 0; i < n; i++) { |
|
521 if (i == 0) |
|
522 scr_WriteInWindow(jidfrom, submsgs[i], TRUE); |
|
523 else |
|
524 scr_WriteInWindow(jidfrom, submsgs[i], FALSE); |
|
525 } |
|
526 |
|
527 for (i = 0; i < n; i++) |
|
528 free(submsgs[i]); |
|
529 |
|
530 free(submsgs); |
|
531 free(buffer); |
|
532 |
2
|
533 top_panel(inputPanel); |
|
534 //wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine); |
|
535 update_panels(); |
|
536 doupdate(); |
0
|
537 } |
|
538 |
|
539 void scr_WriteMessage(int sock) |
|
540 { |
|
541 char **submsgs; |
|
542 int n, i; |
|
543 char *buffer = (char *) calloc(1, 1024); |
|
544 char *buffer2 = (char *) calloc(1, 1024); |
|
545 buddy_entry_t *tmp = bud_SelectedInfo(); |
|
546 |
|
547 scr_ShowWindow(tmp->jid); |
|
548 |
|
549 ut_CenterMessage(i18n("write your message here"), 60, buffer2); |
|
550 |
|
551 scr_CreatePopup(tmp->jid, buffer2, 60, 1, buffer); |
|
552 |
|
553 if (strlen(buffer)) { |
|
554 sprintf(buffer2, ">>> %s", buffer); |
|
555 |
|
556 submsgs = |
|
557 ut_SplitMessage(buffer2, &n, |
|
558 maxX - scr_WindowHeight(rosterWnd) - 20); |
|
559 for (i = 0; i < n; i++) { |
|
560 if (i == 0) |
|
561 scr_WriteInWindow(tmp->jid, submsgs[i], TRUE); |
|
562 else |
|
563 scr_WriteInWindow(tmp->jid, submsgs[i], FALSE); |
|
564 } |
|
565 |
|
566 for (i = 0; i < n; i++) |
|
567 free(submsgs[i]); |
|
568 free(submsgs); |
|
569 |
1
|
570 move(CHAT_WIN_HEIGHT - 1, maxX - 1); |
0
|
571 refresh(); |
|
572 sprintf(buffer2, "%s@%s/%s", cfg_read("username"), |
|
573 cfg_read("server"), cfg_read("resource")); |
|
574 srv_sendtext(sock, tmp->jid, buffer, buffer2); |
|
575 } |
|
576 free(buffer); |
|
577 free(buffer2); |
|
578 } |
|
579 |
|
580 int scr_Getch(void) |
|
581 { |
|
582 int ch; |
2
|
583 // keypad(inputWnd, TRUE); |
|
584 ch = wgetch(inputWnd); |
0
|
585 return ch; |
|
586 } |
|
587 |
|
588 WINDOW *scr_GetRosterWindow(void) |
|
589 { |
|
590 return rosterWnd; |
|
591 } |
|
592 |
|
593 WINDOW *scr_GetStatusWindow(void) |
|
594 { |
|
595 return chatWnd; |
|
596 } |
1
|
597 |
2
|
598 WINDOW *scr_GetInputWindow(void) |
|
599 { |
|
600 return inputWnd; |
|
601 } |
|
602 |
4
|
603 void scr_LogPrint(const char *fmt, ...) |
|
604 { |
|
605 time_t timestamp; |
|
606 char *buffer; |
|
607 va_list ap; |
|
608 |
|
609 buffer = (char *) calloc(1, 4096); |
|
610 |
|
611 timestamp = time(NULL); |
|
612 strftime(buffer, 64, "[%H:%M:%S] ", localtime(×tamp)); |
|
613 wprintw(logWnd, "\n%s", buffer); |
|
614 |
|
615 va_start(ap, fmt); |
|
616 vsnprintf(buffer, 4096, fmt, ap); |
|
617 va_end(ap); |
|
618 |
|
619 wprintw(logWnd, "%s", buffer); |
|
620 free(buffer); |
|
621 } |
|
622 |
|
623 |
2
|
624 void send_message(int sock, char *msg) |
|
625 { |
|
626 char **submsgs; |
|
627 char *buffer = (char *) calloc(1, 24+strlen(msg)); |
|
628 char *buffer2 = (char *) calloc(1, 1024); |
|
629 int n, i; |
|
630 buddy_entry_t *tmp = bud_SelectedInfo(); |
|
631 |
|
632 scr_ShowWindow(tmp->jid); |
|
633 |
|
634 sprintf(buffer, ">>> %s", msg); |
|
635 |
|
636 submsgs = |
|
637 ut_SplitMessage(buffer, &n, |
|
638 maxX - scr_WindowHeight(rosterWnd) - 20); |
|
639 for (i = 0; i < n; i++) { |
|
640 if (i == 0) |
|
641 scr_WriteInWindow(tmp->jid, submsgs[i], TRUE); |
|
642 else |
|
643 scr_WriteInWindow(tmp->jid, submsgs[i], FALSE); |
|
644 } |
|
645 |
|
646 for (i = 0; i < n; i++) |
|
647 free(submsgs[i]); |
|
648 free(submsgs); |
|
649 |
|
650 //move(CHAT_WIN_HEIGHT - 1, maxX - 1); |
|
651 refresh(); |
|
652 sprintf(buffer2, "%s@%s/%s", cfg_read("username"), |
|
653 cfg_read("server"), cfg_read("resource")); |
3
|
654 srv_sendtext(sock, tmp->jid, msg, buffer2); |
2
|
655 free(buffer); |
|
656 free(buffer2); |
|
657 |
|
658 top_panel(inputPanel); |
|
659 } |
|
660 |
|
661 int process_line(char *line, int sock) |
|
662 { |
|
663 if (*line == 0) // XXX Simple checks should maybe be in process_key() |
|
664 return 0; |
|
665 if (*line != '/') { |
|
666 send_message(sock, line); |
|
667 return 0; |
|
668 } |
|
669 if (!strcasecmp(line, "/quit")) { |
|
670 return 255; |
|
671 } |
|
672 // Commands handling |
|
673 // TODO |
|
674 // say... |
4
|
675 |
|
676 scr_LogPrint("Unrecognised command, sorry."); |
2
|
677 return 0; |
|
678 } |
|
679 |
|
680 int process_key(int key, int sock) |
1
|
681 { |
|
682 if (isprint(key)) { |
|
683 char tmpLine[INPUTLINE_LENGTH]; |
|
684 strcpy(tmpLine, ptr_inputline); |
|
685 *ptr_inputline++ = key; |
|
686 strcpy(ptr_inputline, tmpLine); |
|
687 } else { |
|
688 switch(key) { |
|
689 case KEY_BACKSPACE: |
|
690 if (ptr_inputline != (char*)&inputLine) { |
|
691 *--ptr_inputline = 0; |
|
692 } |
|
693 break; |
2
|
694 case KEY_DC: |
|
695 if (*ptr_inputline) |
|
696 strcpy(ptr_inputline, ptr_inputline+1); |
|
697 break; |
1
|
698 case KEY_LEFT: |
|
699 if (ptr_inputline != (char*)&inputLine) { |
|
700 ptr_inputline--; |
|
701 } |
|
702 break; |
|
703 case KEY_RIGHT: |
|
704 if (*ptr_inputline) |
|
705 ptr_inputline++; |
|
706 break; |
|
707 case 9: // Tab |
4
|
708 scr_LogPrint("I'm unable to complete yet"); |
1
|
709 break; |
|
710 case '\n': // Enter |
|
711 // XXX Test: |
2
|
712 if (process_line(inputLine, sock)) |
1
|
713 return 255; |
|
714 ptr_inputline = inputLine; |
|
715 *ptr_inputline = 0; |
|
716 break; |
|
717 case KEY_UP: |
|
718 bud_RosterUp(); |
2
|
719 scr_ShowBuddyWindow(); |
9
|
720 top_panel(inputPanel); |
1
|
721 break; |
|
722 case KEY_DOWN: |
|
723 bud_RosterDown(); |
2
|
724 scr_ShowBuddyWindow(); |
9
|
725 top_panel(inputPanel); |
1
|
726 break; |
2
|
727 case KEY_PPAGE: |
4
|
728 scr_LogPrint("PageUp??"); |
2
|
729 break; |
|
730 case KEY_NPAGE: |
4
|
731 scr_LogPrint("PageDown??"); |
2
|
732 break; |
|
733 case KEY_HOME: |
|
734 case 1: |
|
735 ptr_inputline = inputLine; |
|
736 break; |
|
737 case KEY_END: |
|
738 case 5: |
|
739 for (; *ptr_inputline; ptr_inputline++) ; |
|
740 break; |
|
741 case 21: // Ctrl-u |
|
742 strcpy(inputLine, ptr_inputline); |
|
743 ptr_inputline = inputLine; |
|
744 break; |
|
745 case KEY_EOL: |
|
746 case 11: // Ctrl-k |
|
747 *ptr_inputline = 0; |
|
748 break; |
|
749 default: |
4
|
750 scr_LogPrint("Unkown key=%o", key); |
1
|
751 } |
4
|
752 //scr_LogPrint("[%02x]", key); |
1
|
753 } |
|
754 mvwprintw(inputWnd, 0,0, "%s", inputLine); |
|
755 wclrtoeol(inputWnd); |
|
756 if (*ptr_inputline) { |
|
757 wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine); |
|
758 } |
|
759 update_panels(); |
|
760 doupdate(); |
|
761 return 0; |
|
762 } |