comparison mcabber/mcabber/screen.c @ 1668:41c26b7d2890

Install mcabber headers * Change mcabber headers naming scheme * Move 'src/' -> 'mcabber/' * Add missing include <mcabber/config.h>'s * Create and install clean config.h version in 'include/' * Move "dirty" config.h version to 'mcabber/' * Add $(top_srcdir) to compiler include path * Update modules HOWTO
author Myhailo Danylenko <isbear@ukrpost.net>
date Mon, 18 Jan 2010 15:36:19 +0200
parents mcabber/src/screen.c@1a4890514eb9
children 9a0ed33fb91b
comparison
equal deleted inserted replaced
1667:8af0e0ad20ad 1668:41c26b7d2890
1 /*
2 * screen.c -- UI stuff
3 *
4 * Copyright (C) 2005-2009 Mikael Berthe <mikael@lilotux.net>
5 * Parts of this file come from the Cabber project <cabber@ajmacias.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or (at
10 * your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <ctype.h>
28
29 #include <config.h>
30 #include <locale.h>
31 #include <assert.h>
32 #ifdef USE_SIGWINCH
33 # include <sys/ioctl.h>
34 # include <termios.h>
35 # include <unistd.h>
36 #endif
37
38 #ifdef HAVE_LOCALCHARSET_H
39 # include <localcharset.h>
40 #else
41 # include <langinfo.h>
42 #endif
43
44 #ifdef WITH_ENCHANT
45 # include <enchant.h>
46 #endif
47
48 #ifdef WITH_ASPELL
49 # include <aspell.h>
50 #endif
51
52 #include "screen.h"
53 #include "utf8.h"
54 #include "hbuf.h"
55 #include "commands.h"
56 #include "compl.h"
57 #include "roster.h"
58 #include "histolog.h"
59 #include "settings.h"
60 #include "utils.h"
61 #include "xmpp.h"
62 #include "main.h"
63
64 #define get_color(col) (COLOR_PAIR(col)|COLOR_ATTRIB[col])
65 #define compose_color(col) (COLOR_PAIR(col->color_pair)|col->color_attrib)
66
67 #define DEFAULT_LOG_WIN_HEIGHT (5+2)
68 #define DEFAULT_ROSTER_WIDTH 24
69 #define CHAT_WIN_HEIGHT (maxY-1-Log_Win_Height)
70
71 const char *LocaleCharSet = "C";
72
73 static unsigned short int Log_Win_Height;
74 static unsigned short int Roster_Width;
75
76 static inline void check_offset(int);
77 static void scr_cancel_current_completion(void);
78 static void scr_end_current_completion(void);
79 static void scr_insert_text(const char*);
80 static void scr_handle_tab(void);
81
82 #if defined(WITH_ENCHANT) || defined(WITH_ASPELL)
83 static void spellcheck(char *, char *);
84 #endif
85
86 static GHashTable *winbufhash;
87
88 typedef struct {
89 GList *hbuf;
90 GList *top; // If top is NULL, we'll display the last lines
91 char cleared; // For ex, user has issued a /clear command...
92 char lock;
93 } buffdata;
94
95 typedef struct {
96 WINDOW *win;
97 PANEL *panel;
98 buffdata *bd;
99 } winbuf;
100
101 struct dimensions {
102 int l;
103 int c;
104 };
105
106 static WINDOW *rosterWnd, *chatWnd, *activechatWnd, *inputWnd, *logWnd;
107 static WINDOW *mainstatusWnd, *chatstatusWnd;
108 static PANEL *rosterPanel, *chatPanel, *activechatPanel, *inputPanel;
109 static PANEL *mainstatusPanel, *chatstatusPanel;
110 static PANEL *logPanel;
111 static int maxY, maxX;
112 static int prev_chatwidth;
113 static winbuf *statusWindow;
114 static winbuf *currentWindow;
115 static GList *statushbuf;
116
117 static int roster_hidden;
118 static int chatmode;
119 static int multimode;
120 static char *multiline, *multimode_subj;
121 int update_roster;
122 int utf8_mode = 0;
123 static bool Curses;
124 static bool log_win_on_top;
125 static bool roster_win_on_right;
126 static time_t LastActivity;
127
128 static char inputLine[INPUTLINE_LENGTH+1];
129 #if defined(WITH_ENCHANT) || defined(WITH_ASPELL)
130 static char maskLine[INPUTLINE_LENGTH+1];
131 #endif
132 static char *ptr_inputline;
133 static short int inputline_offset;
134 static int completion_started;
135 static GList *cmdhisto;
136 static GList *cmdhisto_cur;
137 static guint cmdhisto_nblines;
138 static char cmdhisto_backup[INPUTLINE_LENGTH+1];
139
140 static int chatstate; /* (0=active, 1=composing, 2=paused) */
141 static bool lock_chatstate;
142 static time_t chatstate_timestamp;
143 static guint chatstate_timeout_id = 0;
144 int chatstates_disabled;
145
146 #define MAX_KEYSEQ_LENGTH 8
147
148 typedef struct {
149 char *seqstr;
150 guint mkeycode;
151 gint value;
152 } keyseq;
153
154 #ifdef HAVE_GLIB_REGEX
155 static GRegex *url_regex;
156 #endif
157
158 GSList *keyseqlist;
159 static void add_keyseq(char *seqstr, guint mkeycode, gint value);
160
161 void scr_WriteInWindow(const char *winId, const char *text, time_t timestamp,
162 unsigned int prefix_flags, int force_show,
163 unsigned mucnicklen, gpointer xep184);
164
165 void scr_WriteMessage(const char *bjid, const char *text,
166 time_t timestamp, guint prefix_flags,
167 unsigned mucnicklen, gpointer xep184);
168
169 inline void scr_UpdateBuddyWindow(void);
170 inline void scr_set_chatmode(int enable);
171
172 #define SPELLBADCHAR 5
173
174 #ifdef WITH_ENCHANT
175 EnchantBroker *spell_broker;
176 EnchantDict *spell_checker;
177 #endif
178
179 #ifdef WITH_ASPELL
180 AspellConfig *spell_config;
181 AspellSpeller *spell_checker;
182 #endif
183
184 typedef struct {
185 int color_pair;
186 int color_attrib;
187 } ccolor;
188
189 typedef struct {
190 char *status, *wildcard;
191 ccolor *color;
192 GPatternSpec *compiled;
193 } rostercolor;
194
195 static GSList *rostercolrules = NULL;
196
197 static GHashTable *muccolors = NULL, *nickcolors = NULL;
198
199 typedef struct {
200 bool manual; // Manually set?
201 ccolor *color;
202 } nickcolor;
203
204 static int nickcolcount = 0;
205 static ccolor ** nickcols = NULL;
206 static muccoltype glob_muccol = MC_OFF;
207
208 /* Functions */
209
210 static int FindColor(const char *name)
211 {
212 int result;
213
214 if (!strcmp(name, "default"))
215 return -1;
216 if (!strcmp(name, "black"))
217 return COLOR_BLACK;
218 if (!strcmp(name, "red"))
219 return COLOR_RED;
220 if (!strcmp(name, "green"))
221 return COLOR_GREEN;
222 if (!strcmp(name, "yellow"))
223 return COLOR_YELLOW;
224 if (!strcmp(name, "blue"))
225 return COLOR_BLUE;
226 if (!strcmp(name, "magenta"))
227 return COLOR_MAGENTA;
228 if (!strcmp(name, "cyan"))
229 return COLOR_CYAN;
230 if (!strcmp(name, "white"))
231 return COLOR_WHITE;
232
233 // Directly support 256-color values
234 result = atoi(name);
235 if (result > 0 && result < COLORS)
236 return result;
237
238 scr_LogPrint(LPRINT_LOGNORM, "ERROR: Wrong color: %s", name);
239 return -1;
240 }
241
242 static ccolor *get_user_color(const char *color)
243 {
244 bool isbright = FALSE;
245 int cl;
246 ccolor *ccol;
247 if (!strncmp(color, "bright", 6)) {
248 isbright = TRUE;
249 color += 6;
250 }
251 cl = FindColor(color);
252 if (cl < 0)
253 return NULL;
254 ccol = g_new0(ccolor, 1);
255 ccol->color_attrib = isbright ? A_BOLD : A_NORMAL;
256 ccol->color_pair = cl + COLOR_max; //user colors come after the internal ones
257 return ccol;
258 }
259
260 static void ensure_string_htable(GHashTable **table,
261 GDestroyNotify value_destroy_func)
262 {
263 if (*table)//Have it already
264 return;
265 *table = g_hash_table_new_full(g_str_hash, g_str_equal,
266 g_free, value_destroy_func);
267 }
268
269 // Sets the coloring mode for given MUC
270 // The MUC room does not need to be in the roster at that time
271 // muc - the JID of room
272 // type - the new type
273 void scr_MucColor(const char *muc, muccoltype type)
274 {
275 gchar *muclow = g_utf8_strdown(muc, -1);
276 if (type == MC_REMOVE) {//Remove it
277 if (strcmp(muc, "*")) {
278 if (muccolors && g_hash_table_lookup(muccolors, muclow))
279 g_hash_table_remove(muccolors, muclow);
280 } else {
281 scr_LogPrint(LPRINT_NORMAL, "Can not remove global coloring mode");
282 }
283 g_free(muclow);
284 } else {//Add or overwrite
285 if (strcmp(muc, "*")) {
286 muccoltype *value = g_new(muccoltype, 1);
287 *value = type;
288 ensure_string_htable(&muccolors, g_free);
289 g_hash_table_replace(muccolors, muclow, value);
290 } else {
291 glob_muccol = type;
292 g_free(muclow);
293 }
294 }
295 //Need to redraw?
296 if (chatmode &&
297 ((buddy_search_jid(muc) == current_buddy) || !strcmp(muc, "*")))
298 scr_UpdateBuddyWindow();
299 }
300
301 // Sets the color for nick in MUC
302 // If color is "-", the color is marked as automaticly assigned and is
303 // not used if the room is in the "preset" mode
304 void scr_MucNickColor(const char *nick, const char *color)
305 {
306 char *snick, *mnick;
307 bool need_update = FALSE;
308 snick = g_strdup_printf("<%s>", nick);
309 mnick = g_strdup_printf("*%s ", nick);
310 if (!strcmp(color, "-")) {//Remove the color
311 if (nickcolors) {
312 nickcolor *nc = g_hash_table_lookup(nickcolors, snick);
313 if (nc) {//Have this nick already
314 nc->manual = FALSE;
315 nc = g_hash_table_lookup(nickcolors, mnick);
316 assert(nc);//Must have both at the same time
317 nc->manual = FALSE;
318 }// Else -> no color saved, nothing to delete
319 }
320 g_free(snick);//They are not saved in the hash
321 g_free(mnick);
322 need_update = TRUE;
323 } else {
324 ccolor *cl = get_user_color(color);
325 if (!cl) {
326 scr_LogPrint(LPRINT_NORMAL, "No such color name");
327 g_free(snick);
328 g_free(mnick);
329 } else {
330 nickcolor *nc = g_new(nickcolor, 1);
331 ensure_string_htable(&nickcolors, NULL);
332 nc->manual = TRUE;
333 nc->color = cl;
334 //Free the struct, if any there already
335 g_free(g_hash_table_lookup(nickcolors, mnick));
336 //Save the new ones
337 g_hash_table_replace(nickcolors, mnick, nc);
338 g_hash_table_replace(nickcolors, snick, nc);
339 need_update = TRUE;
340 }
341 }
342 if (need_update && chatmode &&
343 (buddy_gettype(BUDDATA(current_buddy)) & ROSTER_TYPE_ROOM))
344 scr_UpdateBuddyWindow();
345 }
346
347 static void free_rostercolrule(rostercolor *col)
348 {
349 g_free(col->status);
350 g_free(col->wildcard);
351 g_free(col->color);
352 g_pattern_spec_free(col->compiled);
353 g_free(col);
354 }
355
356 // Removes all roster coloring rules
357 void scr_RosterClearColor(void)
358 {
359 GSList *head;
360 for (head = rostercolrules; head; head = g_slist_next(head)) {
361 free_rostercolrule(head->data);
362 }
363 g_slist_free(rostercolrules);
364 rostercolrules = NULL;
365 }
366
367 // Adds, modifies or removes roster coloring rule
368 // color set to "-" removes the rule,
369 // otherwise it is modified (if exists) or added
370 //
371 // Returns weather it was successfull (therefore the roster should be
372 // redrawed) or not. If it failed, for example because of invalid color
373 // name, it also prints the error.
374 bool scr_RosterColor(const char *status, const char *wildcard,
375 const char *color)
376 {
377 GSList *head;
378 GSList *found = NULL;
379 for (head = rostercolrules; head; head = g_slist_next(head)) {
380 rostercolor *rc = head->data;
381 if ((!strcmp(status, rc->status)) && (!strcmp(wildcard, rc->wildcard))) {
382 found = head;
383 break;
384 }
385 }
386 if (!strcmp(color,"-")) {//Delete the rule
387 if (found) {
388 free_rostercolrule(found->data);
389 rostercolrules = g_slist_delete_link(rostercolrules, found);
390 return TRUE;
391 } else {
392 scr_LogPrint(LPRINT_NORMAL, "No such color rule, nothing removed");
393 return FALSE;
394 }
395 } else {
396 ccolor *cl = get_user_color(color);
397 if (!cl) {
398 scr_LogPrint(LPRINT_NORMAL, "No such color name");
399 return FALSE;
400 }
401 if (found) {
402 rostercolor *rc = found->data;
403 g_free(rc->color);
404 rc->color = cl;
405 } else {
406 rostercolor *rc = g_new(rostercolor, 1);
407 rc->status = g_strdup(status);
408 rc->wildcard = g_strdup(wildcard);
409 rc->compiled = g_pattern_spec_new(wildcard);
410 rc->color = cl;
411 rostercolrules = g_slist_prepend(rostercolrules, rc);
412 }
413 return TRUE;
414 }
415 }
416
417 static void ParseColors(void)
418 {
419 const char *colors[] = {
420 "", "",
421 "general",
422 "msgout",
423 "msghl",
424 "status",
425 "roster",
426 "rostersel",
427 "rosterselmsg",
428 "rosternewmsg",
429 "info",
430 "msgin",
431 NULL
432 };
433
434 const char *color;
435 const char *background = settings_opt_get("color_background");
436 const char *backselected = settings_opt_get("color_bgrostersel");
437 const char *backstatus = settings_opt_get("color_bgstatus");
438 char *tmp;
439 int i;
440
441 // Initialize color attributes
442 memset(COLOR_ATTRIB, 0, sizeof(COLOR_ATTRIB));
443
444 // Default values
445 if (!background) background = "black";
446 if (!backselected) backselected = "cyan";
447 if (!backstatus) backstatus = "blue";
448
449 for (i=0; colors[i]; i++) {
450 tmp = g_strdup_printf("color_%s", colors[i]);
451 color = settings_opt_get(tmp);
452 g_free(tmp);
453
454 if (color) {
455 if (!strncmp(color, "bright", 6)) {
456 COLOR_ATTRIB[i+1] = A_BOLD;
457 color += 6;
458 }
459 }
460
461 switch (i + 1) {
462 case 1:
463 init_pair(1, COLOR_BLACK, COLOR_WHITE);
464 break;
465 case 2:
466 init_pair(2, COLOR_WHITE, COLOR_BLACK);
467 break;
468 case COLOR_GENERAL:
469 init_pair(i+1, ((color) ? FindColor(color) : COLOR_WHITE),
470 FindColor(background));
471 break;
472 case COLOR_MSGOUT:
473 init_pair(i+1, ((color) ? FindColor(color) : COLOR_CYAN),
474 FindColor(background));
475 break;
476 case COLOR_MSGHL:
477 init_pair(i+1, ((color) ? FindColor(color) : COLOR_YELLOW),
478 FindColor(background));
479 break;
480 case COLOR_STATUS:
481 init_pair(i+1, ((color) ? FindColor(color) : COLOR_WHITE),
482 FindColor(backstatus));
483 break;
484 case COLOR_ROSTER:
485 init_pair(i+1, ((color) ? FindColor(color) : COLOR_GREEN),
486 FindColor(background));
487 break;
488 case COLOR_ROSTERSEL:
489 init_pair(i+1, ((color) ? FindColor(color) : COLOR_BLUE),
490 FindColor(backselected));
491 break;
492 case COLOR_ROSTERSELNMSG:
493 init_pair(i+1, ((color) ? FindColor(color) : COLOR_RED),
494 FindColor(backselected));
495 break;
496 case COLOR_ROSTERNMSG:
497 init_pair(i+1, ((color) ? FindColor(color) : COLOR_RED),
498 FindColor(background));
499 break;
500 case COLOR_INFO:
501 init_pair(i+1, ((color) ? FindColor(color) : COLOR_WHITE),
502 FindColor(background));
503 break;
504 case COLOR_MSGIN:
505 init_pair(i+1, ((color) ? FindColor(color) : COLOR_WHITE),
506 FindColor(background));
507 break;
508 }
509 }
510 for (i = COLOR_max; i < (COLOR_max + COLORS); i++)
511 init_pair(i, i-COLOR_max, FindColor(background));
512
513 if (!nickcols) {
514 char *ncolors = g_strdup(settings_opt_get("nick_colors"));
515 if (ncolors) {
516 char *ncolor_start, *ncolor_end;
517 ncolor_start = ncolor_end = ncolors;
518
519 while (*ncolor_end)
520 ncolor_end++;
521
522 while (ncolors < ncolor_end && *ncolors) {
523 if ((*ncolors == ' ') || (*ncolors == '\t')) {
524 ncolors++;
525 } else {
526 char *end = ncolors;
527 ccolor *cl;
528 while (*end && (*end != ' ') && (*end != '\t'))
529 end++;
530 *end = '\0';
531 cl = get_user_color(ncolors);
532 if (!cl) {
533 scr_LogPrint(LPRINT_NORMAL, "Unknown color %s", ncolors);
534 } else {
535 nickcols = g_realloc(nickcols, (++nickcolcount) * sizeof *nickcols);
536 nickcols[nickcolcount-1] = cl;
537 }
538 ncolors = end+1;
539 }
540 }
541 g_free(ncolor_start);
542 }
543 if (!nickcols) {//Fallback to have something
544 nickcolcount = 1;
545 nickcols = g_new(ccolor*, 1);
546 *nickcols = g_new(ccolor, 1);
547 (*nickcols)->color_pair = COLOR_GENERAL;
548 (*nickcols)->color_attrib = A_NORMAL;
549 }
550 }
551 }
552
553 static void init_keycodes(void)
554 {
555 add_keyseq("O5A", MKEY_EQUIV, 521); // Ctrl-Up
556 add_keyseq("O5B", MKEY_EQUIV, 514); // Ctrl-Down
557 add_keyseq("O5C", MKEY_EQUIV, 518); // Ctrl-Right
558 add_keyseq("O5D", MKEY_EQUIV, 516); // Ctrl-Left
559 add_keyseq("O6A", MKEY_EQUIV, 520); // Shift-Up
560 add_keyseq("O6B", MKEY_EQUIV, 513); // Shift-Down
561 add_keyseq("O6C", MKEY_EQUIV, 402); // Shift-Right
562 add_keyseq("O6D", MKEY_EQUIV, 393); // Shift-Left
563 add_keyseq("O2A", MKEY_EQUIV, 520); // Shift-Up
564 add_keyseq("O2B", MKEY_EQUIV, 513); // Shift-Down
565 add_keyseq("O2C", MKEY_EQUIV, 402); // Shift-Right
566 add_keyseq("O2D", MKEY_EQUIV, 393); // Shift-Left
567 add_keyseq("[5^", MKEY_CTRL_PGUP, 0); // Ctrl-PageUp
568 add_keyseq("[6^", MKEY_CTRL_PGDOWN, 0); // Ctrl-PageDown
569 add_keyseq("[5@", MKEY_CTRL_SHIFT_PGUP, 0); // Ctrl-Shift-PageUp
570 add_keyseq("[6@", MKEY_CTRL_SHIFT_PGDOWN, 0); // Ctrl-Shift-PageDown
571 add_keyseq("[7@", MKEY_CTRL_SHIFT_HOME, 0); // Ctrl-Shift-Home
572 add_keyseq("[8@", MKEY_CTRL_SHIFT_END, 0); // Ctrl-Shift-End
573 add_keyseq("[8^", MKEY_CTRL_END, 0); // Ctrl-End
574 add_keyseq("[7^", MKEY_CTRL_HOME, 0); // Ctrl-Home
575 add_keyseq("[2^", MKEY_CTRL_INS, 0); // Ctrl-Insert
576 add_keyseq("[3^", MKEY_CTRL_DEL, 0); // Ctrl-Delete
577
578 // Xterm
579 add_keyseq("[1;5A", MKEY_EQUIV, 521); // Ctrl-Up
580 add_keyseq("[1;5B", MKEY_EQUIV, 514); // Ctrl-Down
581 add_keyseq("[1;5C", MKEY_EQUIV, 518); // Ctrl-Right
582 add_keyseq("[1;5D", MKEY_EQUIV, 516); // Ctrl-Left
583 add_keyseq("[1;6A", MKEY_EQUIV, 520); // Ctrl-Shift-Up
584 add_keyseq("[1;6B", MKEY_EQUIV, 513); // Ctrl-Shift-Down
585 add_keyseq("[1;6C", MKEY_EQUIV, 402); // Ctrl-Shift-Right
586 add_keyseq("[1;6D", MKEY_EQUIV, 393); // Ctrl-Shift-Left
587 add_keyseq("[1;6H", MKEY_CTRL_SHIFT_HOME, 0); // Ctrl-Shift-Home
588 add_keyseq("[1;6F", MKEY_CTRL_SHIFT_END, 0); // Ctrl-Shift-End
589 add_keyseq("[1;2A", MKEY_EQUIV, 521); // Shift-Up
590 add_keyseq("[1;2B", MKEY_EQUIV, 514); // Shift-Down
591 add_keyseq("[5;5~", MKEY_CTRL_PGUP, 0); // Ctrl-PageUp
592 add_keyseq("[6;5~", MKEY_CTRL_PGDOWN, 0); // Ctrl-PageDown
593 add_keyseq("[1;5F", MKEY_CTRL_END, 0); // Ctrl-End
594 add_keyseq("[1;5H", MKEY_CTRL_HOME, 0); // Ctrl-Home
595 add_keyseq("[2;5~", MKEY_CTRL_INS, 0); // Ctrl-Insert
596 add_keyseq("[3;5~", MKEY_CTRL_DEL, 0); // Ctrl-Delete
597
598 // PuTTY
599 add_keyseq("[A", MKEY_EQUIV, 521); // Ctrl-Up
600 add_keyseq("[B", MKEY_EQUIV, 514); // Ctrl-Down
601 add_keyseq("[C", MKEY_EQUIV, 518); // Ctrl-Right
602 add_keyseq("[D", MKEY_EQUIV, 516); // Ctrl-Left
603
604 // screen
605 add_keyseq("Oa", MKEY_EQUIV, 521); // Ctrl-Up
606 add_keyseq("Ob", MKEY_EQUIV, 514); // Ctrl-Down
607 add_keyseq("Oc", MKEY_EQUIV, 518); // Ctrl-Right
608 add_keyseq("Od", MKEY_EQUIV, 516); // Ctrl-Left
609 add_keyseq("[a", MKEY_EQUIV, 520); // Shift-Up
610 add_keyseq("[b", MKEY_EQUIV, 513); // Shift-Down
611 add_keyseq("[c", MKEY_EQUIV, 402); // Shift-Right
612 add_keyseq("[d", MKEY_EQUIV, 393); // Shift-Left
613 add_keyseq("[5$", MKEY_SHIFT_PGUP, 0); // Shift-PageUp
614 add_keyseq("[6$", MKEY_SHIFT_PGDOWN, 0); // Shift-PageDown
615
616 // VT100
617 add_keyseq("[H", MKEY_EQUIV, KEY_HOME); // Home
618 add_keyseq("[F", MKEY_EQUIV, KEY_END); // End
619
620 // Konsole Linux
621 add_keyseq("[1~", MKEY_EQUIV, KEY_HOME); // Home
622 add_keyseq("[4~", MKEY_EQUIV, KEY_END); // End
623 }
624
625 // scr_init_bindings()
626 // Create default key bindings
627 // Return 0 if error and 1 if none
628 void scr_init_bindings(void)
629 {
630 GString *sbuf = g_string_new("");
631
632 // Common backspace key codes: 8, 127
633 settings_set(SETTINGS_TYPE_BINDING, "8", "iline char_bdel"); // Ctrl-h
634 settings_set(SETTINGS_TYPE_BINDING, "127", "iline char_bdel");
635 g_string_printf(sbuf, "%d", KEY_BACKSPACE);
636 settings_set(SETTINGS_TYPE_BINDING, sbuf->str, "iline char_bdel");
637 g_string_printf(sbuf, "%d", KEY_DC);
638 settings_set(SETTINGS_TYPE_BINDING, sbuf->str, "iline char_fdel");
639 g_string_printf(sbuf, "%d", KEY_LEFT);
640 settings_set(SETTINGS_TYPE_BINDING, sbuf->str, "iline bchar");
641 g_string_printf(sbuf, "%d", KEY_RIGHT);
642 settings_set(SETTINGS_TYPE_BINDING, sbuf->str, "iline fchar");
643 settings_set(SETTINGS_TYPE_BINDING, "7", "iline compl_cancel"); // Ctrl-g
644 g_string_printf(sbuf, "%d", KEY_UP);
645 settings_set(SETTINGS_TYPE_BINDING, sbuf->str,
646 "iline hist_beginning_search_bwd");
647 g_string_printf(sbuf, "%d", KEY_DOWN);
648 settings_set(SETTINGS_TYPE_BINDING, sbuf->str,
649 "iline hist_beginning_search_fwd");
650 g_string_printf(sbuf, "%d", KEY_PPAGE);
651 settings_set(SETTINGS_TYPE_BINDING, sbuf->str, "roster up");
652 g_string_printf(sbuf, "%d", KEY_NPAGE);
653 settings_set(SETTINGS_TYPE_BINDING, sbuf->str, "roster down");
654 g_string_printf(sbuf, "%d", KEY_HOME);
655 settings_set(SETTINGS_TYPE_BINDING, sbuf->str, "iline iline_start");
656 settings_set(SETTINGS_TYPE_BINDING, "1", "iline iline_start"); // Ctrl-a
657 g_string_printf(sbuf, "%d", KEY_END);
658 settings_set(SETTINGS_TYPE_BINDING, sbuf->str, "iline iline_end");
659 settings_set(SETTINGS_TYPE_BINDING, "5", "iline iline_end"); // Ctrl-e
660 // Ctrl-o (accept-line-and-down-history):
661 settings_set(SETTINGS_TYPE_BINDING, "15", "iline iline_accept_down_hist");
662 settings_set(SETTINGS_TYPE_BINDING, "21", "iline iline_bdel"); // Ctrl-u
663 g_string_printf(sbuf, "%d", KEY_EOL);
664 settings_set(SETTINGS_TYPE_BINDING, sbuf->str, "iline iline_fdel");
665 settings_set(SETTINGS_TYPE_BINDING, "11", "iline iline_fdel"); // Ctrl-k
666 settings_set(SETTINGS_TYPE_BINDING, "16", "buffer up"); // Ctrl-p
667 settings_set(SETTINGS_TYPE_BINDING, "14", "buffer down"); // Ctrl-n
668 settings_set(SETTINGS_TYPE_BINDING, "20", "iline char_swap"); // Ctrl-t
669 settings_set(SETTINGS_TYPE_BINDING, "23", "iline word_bdel"); // Ctrl-w
670 settings_set(SETTINGS_TYPE_BINDING, "M98", "iline bword"); // Meta-b
671 settings_set(SETTINGS_TYPE_BINDING, "M102", "iline fword"); // Meta-f
672 settings_set(SETTINGS_TYPE_BINDING, "M100", "iline word_fdel"); // Meta-d
673 // Ctrl-Left (2 codes):
674 settings_set(SETTINGS_TYPE_BINDING, "515", "iline bword");
675 settings_set(SETTINGS_TYPE_BINDING, "516", "iline bword");
676 // Ctrl-Right (2 codes):
677 settings_set(SETTINGS_TYPE_BINDING, "517", "iline fword");
678 settings_set(SETTINGS_TYPE_BINDING, "518", "iline fword");
679 settings_set(SETTINGS_TYPE_BINDING, "12", "screen_refresh"); // Ctrl-l
680 settings_set(SETTINGS_TYPE_BINDING, "27", "chat_disable --show-roster");// Esc
681 settings_set(SETTINGS_TYPE_BINDING, "M27", "chat_disable"); // Esc-Esc
682 settings_set(SETTINGS_TYPE_BINDING, "4", "iline send_multiline"); // Ctrl-d
683 settings_set(SETTINGS_TYPE_BINDING, "M117", "iline word_upcase"); // Meta-u
684 settings_set(SETTINGS_TYPE_BINDING, "M108", "iline word_downcase"); // Meta-l
685 settings_set(SETTINGS_TYPE_BINDING, "M99", "iline word_capit"); // Meta-c
686
687 settings_set(SETTINGS_TYPE_BINDING, "265", "help"); // Bind F1 to help...
688
689 g_string_free(sbuf, TRUE);
690 }
691
692 // is_speckey(key)
693 // Return TRUE if key is a special code, i.e. no char should be displayed on
694 // the screen. It's not very nice, it's a workaround for the systems where
695 // isprint(KEY_PPAGE) returns TRUE...
696 static int is_speckey(int key)
697 {
698 switch (key) {
699 case 127:
700 case 393:
701 case 402:
702 case KEY_BACKSPACE:
703 case KEY_DC:
704 case KEY_LEFT:
705 case KEY_RIGHT:
706 case KEY_UP:
707 case KEY_DOWN:
708 case KEY_PPAGE:
709 case KEY_NPAGE:
710 case KEY_HOME:
711 case KEY_END:
712 case KEY_EOL:
713 return TRUE;
714 }
715
716 // Fn keys
717 if (key >= 265 && key < 265+12)
718 return TRUE;
719
720 // Special key combinations
721 if (key >= 513 && key <= 521)
722 return TRUE;
723
724 return FALSE;
725 }
726
727 void scr_InitLocaleCharSet(void)
728 {
729 setlocale(LC_ALL, "");
730 #ifdef HAVE_LOCALCHARSET_H
731 LocaleCharSet = locale_charset();
732 #else
733 LocaleCharSet = nl_langinfo(CODESET);
734 #endif
735 utf8_mode = (strcmp(LocaleCharSet, "UTF-8") == 0);
736 }
737
738 void scr_InitCurses(void)
739 {
740 /* Key sequences initialization */
741 init_keycodes();
742
743 initscr();
744 raw();
745 noecho();
746 nonl();
747 intrflush(stdscr, FALSE);
748 start_color();
749 use_default_colors();
750 #ifdef NCURSES_MOUSE_VERSION
751 if (settings_opt_get_int("use_mouse"))
752 mousemask(ALL_MOUSE_EVENTS, NULL);
753 #endif
754
755 if (settings_opt_get("escdelay")) {
756 #ifdef HAVE_ESCDELAY
757 ESCDELAY = (unsigned) settings_opt_get_int("escdelay");
758 #else
759 scr_LogPrint(LPRINT_LOGNORM, "ERROR: no ESCDELAY support.");
760 #endif
761 }
762
763 ParseColors();
764
765 getmaxyx(stdscr, maxY, maxX);
766 Log_Win_Height = DEFAULT_LOG_WIN_HEIGHT;
767 // Note scr_DrawMainWindow() should be called early after scr_InitCurses()
768 // to update Log_Win_Height and set max{X,Y}
769
770 inputLine[0] = 0;
771 ptr_inputline = inputLine;
772
773 if (settings_opt_get("url_regex")) {
774 #ifdef HAVE_GLIB_REGEX
775 url_regex = g_regex_new(settings_opt_get("url_regex"),
776 G_REGEX_OPTIMIZE, 0, NULL);
777 #else
778 scr_LogPrint(LPRINT_LOGNORM, "ERROR: Your glib version is too old, "
779 "cannot use url_regex.");
780 #endif // HAVE_GLIB_REGEX
781 }
782
783 Curses = TRUE;
784 return;
785 }
786
787 void scr_TerminateCurses(void)
788 {
789 if (!Curses) return;
790 clear();
791 refresh();
792 endwin();
793 #ifdef HAVE_GLIB_REGEX
794 if (url_regex)
795 g_regex_unref(url_regex);
796 #endif
797 Curses = FALSE;
798 return;
799 }
800
801 void scr_Beep(void)
802 {
803 beep();
804 }
805
806 // This and following belongs to dynamic setting of time prefix
807 static const char *timeprefixes[] = {
808 "%m-%d %H:%M ",
809 "%H:%M ",
810 " "
811 };
812
813 static const char *spectimeprefixes[] = {
814 "%m-%d %H:%M:%S ",
815 "%H:%M:%S ",
816 " "
817 };
818
819 static int timepreflengths[] = {
820 // (length of the corresponding timeprefix + 5)
821 17,
822 11,
823 6
824 };
825
826 static const char *gettprefix(void)
827 {
828 guint n = settings_opt_get_int("time_prefix");
829 return timeprefixes[(n < 3 ? n : 0)];
830 }
831
832 static const char *getspectprefix(void)
833 {
834 guint n = settings_opt_get_int("time_prefix");
835 return spectimeprefixes[(n < 3 ? n : 0)];
836 }
837
838 guint scr_getprefixwidth(void)
839 {
840 guint n = settings_opt_get_int("time_prefix");
841 return timepreflengths[(n < 3 ? n : 0)];
842 }
843
844 // scr_print_logwindow(string)
845 // Display the string in the log window.
846 // Note: The string must be in the user's locale!
847 void scr_print_logwindow(const char *string)
848 {
849 time_t timestamp;
850 char strtimestamp[64];
851
852 timestamp = time(NULL);
853 strftime(strtimestamp, 48, "[%H:%M:%S]", localtime(&timestamp));
854 if (Curses) {
855 wprintw(logWnd, "\n%s %s", strtimestamp, string);
856 update_panels();
857 } else {
858 printf("%s %s\n", strtimestamp, string);
859 }
860 }
861
862 // scr_LogPrint(...)
863 // Display a message in the log window and in the status buffer.
864 // Add the message to the tracelog file if the log flag is set.
865 // This function will convert from UTF-8 unless the LPRINT_NOTUTF8 flag is set.
866 void scr_LogPrint(unsigned int flag, const char *fmt, ...)
867 {
868 time_t timestamp;
869 char strtimestamp[64];
870 char *buffer, *btext;
871 char *convbuf1 = NULL, *convbuf2 = NULL;
872 va_list ap;
873
874 if (!(flag & ~LPRINT_NOTUTF8)) return; // Shouldn't happen
875
876 timestamp = time(NULL);
877 strftime(strtimestamp, 48, "[%H:%M:%S]", localtime(&timestamp));
878 va_start(ap, fmt);
879 btext = g_strdup_vprintf(fmt, ap);
880 va_end(ap);
881
882 if (flag & LPRINT_NORMAL) {
883 char *buffer_locale;
884 char *buf_specialwindow;
885
886 buffer = g_strdup_printf("%s %s", strtimestamp, btext);
887
888 // Convert buffer to current locale for wprintw()
889 if (!(flag & LPRINT_NOTUTF8))
890 buffer_locale = convbuf1 = from_utf8(buffer);
891 else
892 buffer_locale = buffer;
893
894 if (!buffer_locale) {
895 wprintw(logWnd,
896 "\n%s*Error: cannot convert string to locale.", strtimestamp);
897 update_panels();
898 g_free(buffer);
899 g_free(btext);
900 return;
901 }
902
903 // For the special status buffer, we need utf-8, but without the timestamp
904 if (flag & LPRINT_NOTUTF8)
905 buf_specialwindow = convbuf2 = to_utf8(btext);
906 else
907 buf_specialwindow = btext;
908
909 if (Curses) {
910 wprintw(logWnd, "\n%s", buffer_locale);
911 update_panels();
912 scr_WriteInWindow(NULL, buf_specialwindow, timestamp,
913 HBB_PREFIX_SPECIAL, FALSE, 0, NULL);
914 } else {
915 printf("%s\n", buffer_locale);
916 // ncurses are not initialized yet, so we call directly hbuf routine
917 hbuf_add_line(&statushbuf, buf_specialwindow, timestamp,
918 HBB_PREFIX_SPECIAL, 0, 0, 0, NULL);
919 }
920
921 g_free(convbuf1);
922 g_free(convbuf2);
923 g_free(buffer);
924 }
925
926 if (flag & (LPRINT_LOG|LPRINT_DEBUG)) {
927 strftime(strtimestamp, 23, "[%Y-%m-%d %H:%M:%S]", localtime(&timestamp));
928 buffer = g_strdup_printf("%s %s\n", strtimestamp, btext);
929 ut_WriteLog(flag, buffer);
930 g_free(buffer);
931 }
932 g_free(btext);
933 }
934
935 static winbuf *scr_SearchWindow(const char *winId, int special)
936 {
937 char *id;
938 winbuf *wbp;
939
940 if (special)
941 return statusWindow; // Only one special window atm.
942
943 if (!winId)
944 return NULL;
945
946 id = g_strdup(winId);
947 mc_strtolower(id);
948 wbp = g_hash_table_lookup(winbufhash, id);
949 g_free(id);
950 return wbp;
951 }
952
953 int scr_BuddyBufferExists(const char *bjid)
954 {
955 return (scr_SearchWindow(bjid, FALSE) != NULL);
956 }
957
958 // scr_new_buddy(title, dontshow)
959 // Note: title (aka winId/jid) can be NULL for special buffers
960 static winbuf *scr_new_buddy(const char *title, int dont_show)
961 {
962 winbuf *tmp;
963
964 tmp = g_new0(winbuf, 1);
965
966 tmp->win = activechatWnd;
967 tmp->panel = activechatPanel;
968
969 if (!dont_show) {
970 currentWindow = tmp;
971 } else {
972 if (currentWindow)
973 top_panel(currentWindow->panel);
974 else
975 top_panel(chatPanel);
976 }
977 update_panels();
978
979 // If title is NULL, this is a special buffer
980 if (title) {
981 char *id;
982 id = hlog_get_log_jid(title);
983 if (id) {
984 winbuf *wb = scr_SearchWindow(id, FALSE);
985 if (!wb)
986 wb = scr_new_buddy(id, TRUE);
987 tmp->bd=wb->bd;
988 g_free(id);
989 } else { // Load buddy history from file (if enabled)
990 tmp->bd = g_new0(buffdata, 1);
991 hlog_read_history(title, &tmp->bd->hbuf,
992 maxX - Roster_Width - scr_getprefixwidth());
993 }
994
995 id = g_strdup(title);
996 mc_strtolower(id);
997 g_hash_table_insert(winbufhash, id, tmp);
998 } else {
999 tmp->bd = g_new0(buffdata, 1);
1000 }
1001 return tmp;
1002 }
1003
1004 // scr_line_prefix(line, pref, preflen)
1005 // Use data from the hbb_line structure and write the prefix
1006 // to pref (not exceeding preflen, trailing null byte included).
1007 void scr_line_prefix(hbb_line *line, char *pref, guint preflen)
1008 {
1009 char date[64];
1010
1011 if (line->timestamp &&
1012 !(line->flags & (HBB_PREFIX_SPECIAL|HBB_PREFIX_CONT))) {
1013 strftime(date, 30, gettprefix(), localtime(&line->timestamp));
1014 } else
1015 strcpy(date, " ");
1016
1017 if (!(line->flags & HBB_PREFIX_CONT)) {
1018 if (line->flags & HBB_PREFIX_INFO) {
1019 char dir = '*';
1020 if (line->flags & HBB_PREFIX_IN)
1021 dir = '<';
1022 else if (line->flags & HBB_PREFIX_OUT)
1023 dir = '>';
1024 g_snprintf(pref, preflen, "%s*%c* ", date, dir);
1025 } else if (line->flags & HBB_PREFIX_ERR) {
1026 char dir = '#';
1027 if (line->flags & HBB_PREFIX_IN)
1028 dir = '<';
1029 else if (line->flags & HBB_PREFIX_OUT)
1030 dir = '>';
1031 g_snprintf(pref, preflen, "%s#%c# ", date, dir);
1032 } else if (line->flags & HBB_PREFIX_IN) {
1033 char cryptflag;
1034 if (line->flags & HBB_PREFIX_PGPCRYPT)
1035 cryptflag = '~';
1036 else if (line->flags & HBB_PREFIX_OTRCRYPT)
1037 cryptflag = 'O';
1038 else
1039 cryptflag = '=';
1040 g_snprintf(pref, preflen, "%s<%c= ", date, cryptflag);
1041 } else if (line->flags & HBB_PREFIX_OUT) {
1042 char cryptflag, receiptflag;
1043 if (line->flags & HBB_PREFIX_PGPCRYPT)
1044 cryptflag = '~';
1045 else if (line->flags & HBB_PREFIX_OTRCRYPT)
1046 cryptflag = 'O';
1047 else
1048 cryptflag = '-';
1049 if (line->flags & HBB_PREFIX_RECEIPT)
1050 receiptflag = 'r';
1051 else
1052 receiptflag = '-';
1053 g_snprintf(pref, preflen, "%s%c%c> ", date, receiptflag, cryptflag);
1054 } else if (line->flags & HBB_PREFIX_SPECIAL) {
1055 strftime(date, 30, getspectprefix(), localtime(&line->timestamp));
1056 g_snprintf(pref, preflen, "%s ", date);
1057 } else {
1058 g_snprintf(pref, preflen, "%s ", date);
1059 }
1060 } else {
1061 g_snprintf(pref, preflen, " ");
1062 }
1063 }
1064
1065 // scr_UpdateWindow()
1066 // (Re-)Display the given chat window.
1067 static void scr_UpdateWindow(winbuf *win_entry)
1068 {
1069 int n;
1070 guint prefixwidth;
1071 char pref[96];
1072 hbb_line **lines, *line;
1073 GList *hbuf_head;
1074 int color;
1075
1076 prefixwidth = scr_getprefixwidth();
1077 prefixwidth = MIN(prefixwidth, sizeof pref);
1078
1079 // Should the window be empty?
1080 if (win_entry->bd->cleared) {
1081 werase(win_entry->win);
1082 return;
1083 }
1084
1085 // win_entry->bd->top is the top message of the screen. If it set to NULL,
1086 // we are displaying the last messages.
1087
1088 // We will show the last CHAT_WIN_HEIGHT lines.
1089 // Let's find out where it begins.
1090 if (!win_entry->bd->top || (g_list_position(g_list_first(win_entry->bd->hbuf),
1091 win_entry->bd->top) == -1)) {
1092 // Move up CHAT_WIN_HEIGHT lines
1093 win_entry->bd->hbuf = g_list_last(win_entry->bd->hbuf);
1094 hbuf_head = win_entry->bd->hbuf;
1095 win_entry->bd->top = NULL; // (Just to make sure)
1096 n = 0;
1097 while (hbuf_head && (n < CHAT_WIN_HEIGHT-1) && g_list_previous(hbuf_head)) {
1098 hbuf_head = g_list_previous(hbuf_head);
1099 n++;
1100 }
1101 // If the buffer is locked, remember current "top" line for the next time.
1102 if (win_entry->bd->lock)
1103 win_entry->bd->top = hbuf_head;
1104 } else
1105 hbuf_head = win_entry->bd->top;
1106
1107 // Get the last CHAT_WIN_HEIGHT lines.
1108 lines = hbuf_get_lines(hbuf_head, CHAT_WIN_HEIGHT);
1109
1110 // Display these lines
1111 for (n = 0; n < CHAT_WIN_HEIGHT; n++) {
1112 wmove(win_entry->win, n, 0);
1113 line = *(lines+n);
1114 if (line) {
1115 if (line->flags & HBB_PREFIX_HLIGHT_OUT)
1116 color = COLOR_MSGOUT;
1117 else if (line->flags & HBB_PREFIX_HLIGHT)
1118 color = COLOR_MSGHL;
1119 else if (line->flags & HBB_PREFIX_INFO)
1120 color = COLOR_INFO;
1121 else if (line->flags & HBB_PREFIX_IN)
1122 color = COLOR_MSGIN;
1123 else
1124 color = COLOR_GENERAL;
1125
1126 if (color != COLOR_GENERAL)
1127 wattrset(win_entry->win, get_color(color));
1128
1129 // Generate the prefix area and display it
1130 scr_line_prefix(line, pref, prefixwidth);
1131 wprintw(win_entry->win, pref);
1132
1133 // Make sure we are at the right position
1134 wmove(win_entry->win, n, prefixwidth-1);
1135
1136 // The MUC nick - overwrite with proper color
1137 if (line->mucnicklen) {
1138 char *mucjid;
1139 char tmp;
1140 nickcolor *actual = NULL;
1141 muccoltype type, *typetmp;
1142
1143 // Store the char after the nick
1144 tmp = line->text[line->mucnicklen];
1145 type = glob_muccol;
1146 // Terminate the string after the nick
1147 line->text[line->mucnicklen] = '\0';
1148 mucjid = g_utf8_strdown(CURRENT_JID, -1);
1149 if (muccolors) {
1150 typetmp = g_hash_table_lookup(muccolors, mucjid);
1151 if (typetmp)
1152 type = *typetmp;
1153 }
1154 g_free(mucjid);
1155 // Need to generate a color for the specified nick?
1156 if ((type == MC_ALL) && (!nickcolors ||
1157 !g_hash_table_lookup(nickcolors, line->text))) {
1158 char *snick, *mnick;
1159 nickcolor *nc;
1160 const char *p = line->text;
1161 unsigned int nicksum = 0;
1162 snick = g_strdup(line->text);
1163 mnick = g_strdup(line->text);
1164 nc = g_new(nickcolor, 1);
1165 ensure_string_htable(&nickcolors, NULL);
1166 while (*p)
1167 nicksum += *p++;
1168 nc->color = nickcols[nicksum % nickcolcount];
1169 nc->manual = FALSE;
1170 *snick = '<';
1171 snick[strlen(snick)-1] = '>';
1172 *mnick = '*';
1173 mnick[strlen(mnick)-1] = ' ';
1174 // Insert them
1175 g_hash_table_insert(nickcolors, snick, nc);
1176 g_hash_table_insert(nickcolors, mnick, nc);
1177 }
1178 if (nickcolors)
1179 actual = g_hash_table_lookup(nickcolors, line->text);
1180 if (actual && ((type == MC_ALL) || (actual->manual))
1181 && (line->flags & HBB_PREFIX_IN) &&
1182 (!(line->flags & HBB_PREFIX_HLIGHT_OUT)))
1183 wattrset(win_entry->win, compose_color(actual->color));
1184 wprintw(win_entry->win, "%s", line->text);
1185 // Return the char
1186 line->text[line->mucnicklen] = tmp;
1187 // Return the color back
1188 wattrset(win_entry->win, get_color(color));
1189 }
1190
1191 // Display text line
1192 wprintw(win_entry->win, "%s", line->text+line->mucnicklen);
1193 wclrtoeol(win_entry->win);
1194
1195 // Return the color back
1196 if (color != COLOR_GENERAL)
1197 wattrset(win_entry->win, get_color(COLOR_GENERAL));
1198
1199 g_free(line->text);
1200 g_free(line);
1201 } else {
1202 wclrtobot(win_entry->win);
1203 break;
1204 }
1205 }
1206 g_free(lines);
1207 }
1208
1209 static winbuf *scr_CreateWindow(const char *winId, int special, int dont_show)
1210 {
1211 if (special) {
1212 if (!statusWindow) {
1213 statusWindow = scr_new_buddy(NULL, dont_show);
1214 statusWindow->bd->hbuf = statushbuf;
1215 }
1216 return statusWindow;
1217 } else {
1218 return scr_new_buddy(winId, dont_show);
1219 }
1220 }
1221
1222 // scr_ShowWindow()
1223 // Display the chat window with the given identifier.
1224 // "special" must be true if this is a special buffer window.
1225 static void scr_ShowWindow(const char *winId, int special)
1226 {
1227 winbuf *win_entry;
1228
1229 win_entry = scr_SearchWindow(winId, special);
1230
1231 if (!win_entry) {
1232 win_entry = scr_CreateWindow(winId, special, FALSE);
1233 }
1234
1235 top_panel(win_entry->panel);
1236 currentWindow = win_entry;
1237 chatmode = TRUE;
1238 if (!win_entry->bd->lock)
1239 roster_msg_setflag(winId, special, FALSE);
1240 if (!special)
1241 roster_setflags(winId, ROSTER_FLAG_LOCK, TRUE);
1242 update_roster = TRUE;
1243
1244 // Refresh the window
1245 scr_UpdateWindow(win_entry);
1246
1247 // Finished :)
1248 update_panels();
1249
1250 top_panel(inputPanel);
1251 }
1252
1253 // scr_ShowBuddyWindow()
1254 // Display the chat window buffer for the current buddy.
1255 void scr_ShowBuddyWindow(void)
1256 {
1257 const gchar *bjid;
1258
1259 if (!current_buddy) {
1260 bjid = NULL;
1261 } else {
1262 bjid = CURRENT_JID;
1263 if (buddy_gettype(BUDDATA(current_buddy)) & ROSTER_TYPE_SPECIAL) {
1264 scr_ShowWindow(buddy_getname(BUDDATA(current_buddy)), TRUE);
1265 return;
1266 }
1267 }
1268
1269 if (!bjid) {
1270 top_panel(chatPanel);
1271 top_panel(inputPanel);
1272 currentWindow = NULL;
1273 return;
1274 }
1275
1276 scr_ShowWindow(bjid, FALSE);
1277 }
1278
1279 // scr_UpdateBuddyWindow()
1280 // (Re)Display the current window.
1281 // If chatmode is enabled, call scr_ShowBuddyWindow(),
1282 // else display the chat window.
1283 inline void scr_UpdateBuddyWindow(void)
1284 {
1285 if (chatmode) {
1286 scr_ShowBuddyWindow();
1287 return;
1288 }
1289
1290 top_panel(chatPanel);
1291 top_panel(inputPanel);
1292 }
1293
1294 // scr_WriteInWindow()
1295 // Write some text in the winId window (this usually is a jid).
1296 // Use winId == NULL for the special status buffer.
1297 // Lines are splitted when they are too long to fit in the chat window.
1298 // If this window doesn't exist, it is created.
1299 void scr_WriteInWindow(const char *winId, const char *text, time_t timestamp,
1300 unsigned int prefix_flags, int force_show,
1301 unsigned mucnicklen, gpointer xep184)
1302 {
1303 winbuf *win_entry;
1304 char *text_locale;
1305 int dont_show = FALSE;
1306 int special;
1307 guint num_history_blocks;
1308 bool setmsgflg = FALSE;
1309 char *nicktmp, *nicklocaltmp;
1310
1311 // Look for the window entry.
1312 special = (winId == NULL);
1313 win_entry = scr_SearchWindow(winId, special);
1314
1315 // Do we have to really show the window?
1316 if (!chatmode)
1317 dont_show = TRUE;
1318 else if ((!force_show) && ((!currentWindow || (currentWindow != win_entry))))
1319 dont_show = TRUE;
1320
1321 // If the window entry doesn't exist yet, let's create it.
1322 if (!win_entry) {
1323 win_entry = scr_CreateWindow(winId, special, dont_show);
1324 }
1325
1326 // The message must be displayed -> update top pointer
1327 if (win_entry->bd->cleared)
1328 win_entry->bd->top = g_list_last(win_entry->bd->hbuf);
1329
1330 // Make sure we do not free the buffer while it's locked or when
1331 // top is set.
1332 if (win_entry->bd->lock || win_entry->bd->top)
1333 num_history_blocks = 0U;
1334 else
1335 num_history_blocks = get_max_history_blocks();
1336
1337 text_locale = from_utf8(text);
1338 //Convert the nick alone and compute its length
1339 if (mucnicklen) {
1340 nicktmp = g_strndup(text, mucnicklen);
1341 nicklocaltmp = from_utf8(nicktmp);
1342 mucnicklen = strlen(nicklocaltmp);
1343 g_free(nicklocaltmp);
1344 g_free(nicktmp);
1345 }
1346 hbuf_add_line(&win_entry->bd->hbuf, text_locale, timestamp, prefix_flags,
1347 maxX - Roster_Width - scr_getprefixwidth(), num_history_blocks,
1348 mucnicklen, xep184);
1349 g_free(text_locale);
1350
1351 if (win_entry->bd->cleared) {
1352 win_entry->bd->cleared = FALSE;
1353 if (g_list_next(win_entry->bd->top))
1354 win_entry->bd->top = g_list_next(win_entry->bd->top);
1355 }
1356
1357 // Make sure the last line appears in the window; update top if necessary
1358 if (!win_entry->bd->lock && win_entry->bd->top) {
1359 int dist;
1360 GList *first = g_list_first(win_entry->bd->hbuf);
1361 dist = g_list_position(first, g_list_last(win_entry->bd->hbuf)) -
1362 g_list_position(first, win_entry->bd->top);
1363 if (dist >= CHAT_WIN_HEIGHT)
1364 win_entry->bd->top = NULL;
1365 }
1366
1367 if (!dont_show) {
1368 if (win_entry->bd->lock)
1369 setmsgflg = TRUE;
1370 // Show and refresh the window
1371 top_panel(win_entry->panel);
1372 scr_UpdateWindow(win_entry);
1373 top_panel(inputPanel);
1374 update_panels();
1375 } else if (!(prefix_flags & HBB_PREFIX_NOFLAG)) {
1376 setmsgflg = TRUE;
1377 }
1378 if (setmsgflg && !special) {
1379 if (special && !winId)
1380 winId = SPECIAL_BUFFER_STATUS_ID;
1381 roster_msg_setflag(winId, special, TRUE);
1382 update_roster = TRUE;
1383 }
1384 }
1385
1386 // scr_UpdateMainStatus()
1387 // Redraw the main (bottom) status line.
1388 void scr_UpdateMainStatus(int forceupdate)
1389 {
1390 char *sm = from_utf8(xmpp_getstatusmsg());
1391 const char *info = settings_opt_get("info");
1392
1393 werase(mainstatusWnd);
1394 if (info) {
1395 char *info_locale = from_utf8(info);
1396 mvwprintw(mainstatusWnd, 0, 0, "%c[%c] %s: %s",
1397 (unread_msg(NULL) ? '#' : ' '),
1398 imstatus2char[xmpp_getstatus()],
1399 info_locale, (sm ? sm : ""));
1400 g_free(info_locale);
1401 } else
1402 mvwprintw(mainstatusWnd, 0, 0, "%c[%c] %s",
1403 (unread_msg(NULL) ? '#' : ' '),
1404 imstatus2char[xmpp_getstatus()], (sm ? sm : ""));
1405 if (forceupdate) {
1406 top_panel(inputPanel);
1407 update_panels();
1408 }
1409 g_free(sm);
1410 }
1411
1412 // scr_DrawMainWindow()
1413 // Set fullinit to TRUE to also create panels. Set it to FALSE for a resize.
1414 //
1415 // I think it could be improved a _lot_ but I'm really not an ncurses
1416 // expert... :-\ Mikael.
1417 //
1418 void scr_DrawMainWindow(unsigned int fullinit)
1419 {
1420 int requested_size;
1421 gchar *ver, *message;
1422 int chat_y_pos, chatstatus_y_pos, log_y_pos;
1423 int roster_x_pos, chat_x_pos;
1424
1425 Log_Win_Height = DEFAULT_LOG_WIN_HEIGHT;
1426 requested_size = settings_opt_get_int("log_win_height");
1427 if (requested_size > 0) {
1428 if (maxY > requested_size + 3)
1429 Log_Win_Height = requested_size + 2;
1430 else
1431 Log_Win_Height = ((maxY > 5) ? (maxY - 2) : 3);
1432 } else if (requested_size < 0) {
1433 Log_Win_Height = 3;
1434 }
1435
1436 if (maxY < Log_Win_Height+2) {
1437 if (maxY < 5) {
1438 Log_Win_Height = 3;
1439 maxY = Log_Win_Height+2;
1440 } else {
1441 Log_Win_Height = maxY - 2;
1442 }
1443 }
1444
1445 if (roster_hidden) {
1446 Roster_Width = 0;
1447 } else {
1448 requested_size = settings_opt_get_int("roster_width");
1449 if (requested_size > 1)
1450 Roster_Width = requested_size;
1451 else if (requested_size == 1)
1452 Roster_Width = 2;
1453 else
1454 Roster_Width = DEFAULT_ROSTER_WIDTH;
1455 }
1456
1457 log_win_on_top = (settings_opt_get_int("log_win_on_top") == 1);
1458 roster_win_on_right = (settings_opt_get_int("roster_win_on_right") == 1);
1459
1460 if (log_win_on_top) {
1461 chat_y_pos = Log_Win_Height-1;
1462 log_y_pos = 0;
1463 chatstatus_y_pos = Log_Win_Height-2;
1464 } else {
1465 chat_y_pos = 0;
1466 log_y_pos = CHAT_WIN_HEIGHT+1;
1467 chatstatus_y_pos = CHAT_WIN_HEIGHT;
1468 }
1469
1470 if (roster_win_on_right) {
1471 roster_x_pos = maxX - Roster_Width;
1472 chat_x_pos = 0;
1473 } else {
1474 roster_x_pos = 0;
1475 chat_x_pos = Roster_Width;
1476 }
1477
1478 if (fullinit) {
1479 if (!winbufhash)
1480 winbufhash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
1481 /* Create windows */
1482 rosterWnd = newwin(CHAT_WIN_HEIGHT, Roster_Width, chat_y_pos, roster_x_pos);
1483 chatWnd = newwin(CHAT_WIN_HEIGHT, maxX - Roster_Width, chat_y_pos,
1484 chat_x_pos);
1485 activechatWnd = newwin(CHAT_WIN_HEIGHT, maxX - Roster_Width, chat_y_pos,
1486 chat_x_pos);
1487 logWnd = newwin(Log_Win_Height-2, maxX, log_y_pos, 0);
1488 chatstatusWnd = newwin(1, maxX, chatstatus_y_pos, 0);
1489 mainstatusWnd = newwin(1, maxX, maxY-2, 0);
1490 inputWnd = newwin(1, maxX, maxY-1, 0);
1491 if (!rosterWnd || !chatWnd || !logWnd || !inputWnd) {
1492 scr_TerminateCurses();
1493 fprintf(stderr, "Cannot create windows!\n");
1494 exit(EXIT_FAILURE);
1495 }
1496 wbkgd(rosterWnd, get_color(COLOR_GENERAL));
1497 wbkgd(chatWnd, get_color(COLOR_GENERAL));
1498 wbkgd(activechatWnd, get_color(COLOR_GENERAL));
1499 wbkgd(logWnd, get_color(COLOR_GENERAL));
1500 wbkgd(chatstatusWnd, get_color(COLOR_STATUS));
1501 wbkgd(mainstatusWnd, get_color(COLOR_STATUS));
1502 } else {
1503 /* Resize/move windows */
1504 wresize(rosterWnd, CHAT_WIN_HEIGHT, Roster_Width);
1505 wresize(chatWnd, CHAT_WIN_HEIGHT, maxX - Roster_Width);
1506 wresize(logWnd, Log_Win_Height-2, maxX);
1507
1508 mvwin(chatWnd, chat_y_pos, chat_x_pos);
1509 mvwin(rosterWnd, chat_y_pos, roster_x_pos);
1510 mvwin(logWnd, log_y_pos, 0);
1511
1512 // Resize & move chat status window
1513 wresize(chatstatusWnd, 1, maxX);
1514 mvwin(chatstatusWnd, chatstatus_y_pos, 0);
1515 // Resize & move main status window
1516 wresize(mainstatusWnd, 1, maxX);
1517 mvwin(mainstatusWnd, maxY-2, 0);
1518 // Resize & move input line window
1519 wresize(inputWnd, 1, maxX);
1520 mvwin(inputWnd, maxY-1, 0);
1521
1522 werase(chatWnd);
1523 }
1524
1525 /* Draw/init windows */
1526
1527 ver = mcabber_version();
1528 message = g_strdup_printf("MCabber version %s.\n", ver);
1529 mvwprintw(chatWnd, 0, 0, message);
1530 mvwprintw(chatWnd, 1, 0, "http://mcabber.com/");
1531 g_free(ver);
1532 g_free(message);
1533
1534 // Auto-scrolling in log window
1535 scrollok(logWnd, TRUE);
1536
1537
1538 if (fullinit) {
1539 // Enable keypad (+ special keys)
1540 keypad(inputWnd, TRUE);
1541 #ifdef __MirBSD__
1542 wtimeout(inputWnd, 50 /* ms */);
1543 #else
1544 nodelay(inputWnd, TRUE);
1545 #endif
1546
1547 // Create panels
1548 rosterPanel = new_panel(rosterWnd);
1549 chatPanel = new_panel(chatWnd);
1550 activechatPanel = new_panel(activechatWnd);
1551 logPanel = new_panel(logWnd);
1552 chatstatusPanel = new_panel(chatstatusWnd);
1553 mainstatusPanel = new_panel(mainstatusWnd);
1554 inputPanel = new_panel(inputWnd);
1555
1556 // Build the buddylist at least once, to make sure the special buffer
1557 // is added
1558 buddylist_build();
1559
1560 // Init prev_chatwidth; this variable will be used to prevent us
1561 // from rewrapping buffers when the width doesn't change.
1562 prev_chatwidth = maxX - Roster_Width - scr_getprefixwidth();
1563 // Wrap existing status buffer lines
1564 hbuf_rebuild(&statushbuf, prev_chatwidth);
1565
1566 #ifndef UNICODE
1567 if (utf8_mode)
1568 scr_LogPrint(LPRINT_NORMAL,
1569 "WARNING: Compiled without full UTF-8 support!");
1570 #endif
1571 } else {
1572 // Update panels
1573 replace_panel(rosterPanel, rosterWnd);
1574 replace_panel(chatPanel, chatWnd);
1575 replace_panel(logPanel, logWnd);
1576 replace_panel(chatstatusPanel, chatstatusWnd);
1577 replace_panel(mainstatusPanel, mainstatusWnd);
1578 replace_panel(inputPanel, inputWnd);
1579 }
1580
1581 // We'll need to redraw the roster
1582 update_roster = TRUE;
1583 return;
1584 }
1585
1586 static void resize_win_buffer(gpointer key, gpointer value, gpointer data)
1587 {
1588 winbuf *wbp = value;
1589 struct dimensions *dim = data;
1590 int chat_x_pos, chat_y_pos;
1591 int new_chatwidth;
1592
1593 if (!(wbp && wbp->win))
1594 return;
1595
1596 if (log_win_on_top)
1597 chat_y_pos = Log_Win_Height-1;
1598 else
1599 chat_y_pos = 0;
1600
1601 if (roster_win_on_right)
1602 chat_x_pos = 0;
1603 else
1604 chat_x_pos = Roster_Width;
1605
1606 // Resize/move buddy window
1607 wresize(wbp->win, dim->l, dim->c);
1608 mvwin(wbp->win, chat_y_pos, chat_x_pos);
1609 werase(wbp->win);
1610 // If a panel exists, replace the old window with the new
1611 if (wbp->panel)
1612 replace_panel(wbp->panel, wbp->win);
1613 // Redo line wrapping
1614 wbp->bd->top = hbuf_previous_persistent(wbp->bd->top);
1615
1616 new_chatwidth = maxX - Roster_Width - scr_getprefixwidth();
1617 if (new_chatwidth != prev_chatwidth)
1618 hbuf_rebuild(&wbp->bd->hbuf, new_chatwidth);
1619 }
1620
1621 // scr_Resize()
1622 // Function called when the window is resized.
1623 // - Resize windows
1624 // - Rewrap lines in each buddy buffer
1625 void scr_Resize(void)
1626 {
1627 struct dimensions dim;
1628
1629 // First, update the global variables
1630 getmaxyx(stdscr, maxY, maxX);
1631 // scr_DrawMainWindow() will take care of maxY and Log_Win_Height
1632
1633 // Make sure the cursor stays inside the window
1634 check_offset(0);
1635
1636 // Resize windows and update panels
1637 scr_DrawMainWindow(FALSE);
1638
1639 // Resize all buddy windows
1640 dim.l = CHAT_WIN_HEIGHT;
1641 dim.c = maxX - Roster_Width;
1642 if (dim.c < 1)
1643 dim.c = 1;
1644
1645 // Resize all buffers
1646 g_hash_table_foreach(winbufhash, resize_win_buffer, &dim);
1647
1648 // Resize/move special status buffer
1649 if (statusWindow)
1650 resize_win_buffer(NULL, statusWindow, &dim);
1651
1652 // Update prev_chatwidth, now that all buffers have been resized
1653 prev_chatwidth = maxX - Roster_Width - scr_getprefixwidth();
1654
1655 // Refresh current buddy window
1656 if (chatmode)
1657 scr_ShowBuddyWindow();
1658 }
1659
1660 // scr_UpdateChatStatus(forceupdate)
1661 // Redraw the buddy status bar.
1662 // Set forceupdate to TRUE if update_panels() must be called.
1663 void scr_UpdateChatStatus(int forceupdate)
1664 {
1665 unsigned short btype, isgrp, ismuc, isspe;
1666 const char *btypetext = "Unknown";
1667 const char *fullname;
1668 const char *msg = NULL;
1669 char status;
1670 char *buf, *buf_locale;
1671
1672 // Usually we need to update the bottom status line too,
1673 // at least to refresh the pending message flag.
1674 scr_UpdateMainStatus(FALSE);
1675
1676 // Clear the line
1677 werase(chatstatusWnd);
1678
1679 if (!current_buddy) {
1680 if (forceupdate) {
1681 update_panels();
1682 }
1683 return;
1684 }
1685
1686 fullname = buddy_getname(BUDDATA(current_buddy));
1687 btype = buddy_gettype(BUDDATA(current_buddy));
1688
1689 isgrp = ismuc = isspe = 0;
1690 if (btype & ROSTER_TYPE_USER) {
1691 btypetext = "Buddy";
1692 } else if (btype & ROSTER_TYPE_GROUP) {
1693 btypetext = "Group";
1694 isgrp = 1;
1695 } else if (btype & ROSTER_TYPE_AGENT) {
1696 btypetext = "Agent";
1697 } else if (btype & ROSTER_TYPE_ROOM) {
1698 btypetext = "Room";
1699 ismuc = 1;
1700 } else if (btype & ROSTER_TYPE_SPECIAL) {
1701 btypetext = "Special buffer";
1702 isspe = 1;
1703 }
1704
1705 if (chatmode) {
1706 wprintw(chatstatusWnd, "~");
1707 } else {
1708 unsigned short bflags = buddy_getflags(BUDDATA(current_buddy));
1709 if (bflags & ROSTER_FLAG_MSG) {
1710 // There is an unread message from the current buddy
1711 wprintw(chatstatusWnd, "#");
1712 }
1713 }
1714
1715 if (chatmode && !isgrp) {
1716 winbuf *win_entry;
1717 win_entry = scr_SearchWindow(buddy_getjid(BUDDATA(current_buddy)), isspe);
1718 if (win_entry && win_entry->bd->lock)
1719 mvwprintw(chatstatusWnd, 0, 0, "*");
1720 }
1721
1722 if (isgrp || isspe) {
1723 buf_locale = from_utf8(fullname);
1724 mvwprintw(chatstatusWnd, 0, 5, "%s: %s", btypetext, buf_locale);
1725 g_free(buf_locale);
1726 if (forceupdate) {
1727 update_panels();
1728 }
1729 return;
1730 }
1731
1732 status = '?';
1733
1734 if (ismuc) {
1735 if (buddy_getinsideroom(BUDDATA(current_buddy)))
1736 status = 'C';
1737 else
1738 status = 'x';
1739 } else if (xmpp_getstatus() != offline) {
1740 enum imstatus budstate;
1741 budstate = buddy_getstatus(BUDDATA(current_buddy), NULL);
1742 if (budstate < imstatus_size)
1743 status = imstatus2char[budstate];
1744 }
1745
1746 // No status message for MUC rooms
1747 if (!ismuc) {
1748 GSList *resources, *p_res, *p_next_res;
1749 resources = buddy_getresources(BUDDATA(current_buddy));
1750
1751 for (p_res = resources ; p_res ; p_res = p_next_res) {
1752 p_next_res = g_slist_next(p_res);
1753 // Store the status message of the latest resource (highest priority)
1754 if (!p_next_res)
1755 msg = buddy_getstatusmsg(BUDDATA(current_buddy), p_res->data);
1756 g_free(p_res->data);
1757 }
1758 g_slist_free(resources);
1759 } else {
1760 msg = buddy_gettopic(BUDDATA(current_buddy));
1761 }
1762
1763 if (msg)
1764 buf = g_strdup_printf("[%c] %s: %s -- %s", status, btypetext, fullname, msg);
1765 else
1766 buf = g_strdup_printf("[%c] %s: %s", status, btypetext, fullname);
1767 replace_nl_with_dots(buf);
1768 buf_locale = from_utf8(buf);
1769 mvwprintw(chatstatusWnd, 0, 1, "%s", buf_locale);
1770 g_free(buf_locale);
1771 g_free(buf);
1772
1773 // Display chatstates of the contact, if available.
1774 if (btype & ROSTER_TYPE_USER) {
1775 char eventchar = 0;
1776 guint event;
1777
1778 // We do not specify the resource here, so one of the resources with the
1779 // highest priority will be used.
1780 event = buddy_resource_getevents(BUDDATA(current_buddy), NULL);
1781
1782 if (event == ROSTER_EVENT_ACTIVE)
1783 eventchar = 'A';
1784 else if (event == ROSTER_EVENT_COMPOSING)
1785 eventchar = 'C';
1786 else if (event == ROSTER_EVENT_PAUSED)
1787 eventchar = 'P';
1788 else if (event == ROSTER_EVENT_INACTIVE)
1789 eventchar = 'I';
1790 else if (event == ROSTER_EVENT_GONE)
1791 eventchar = 'G';
1792
1793 if (eventchar)
1794 mvwprintw(chatstatusWnd, 0, maxX-3, "[%c]", eventchar);
1795 }
1796
1797
1798 if (forceupdate) {
1799 update_panels();
1800 }
1801 }
1802
1803 void increment_if_buddy_not_filtered(gpointer rosterdata, void *param)
1804 {
1805 int *p = param;
1806 if (buddylist_is_status_filtered(buddy_getstatus(rosterdata, NULL)))
1807 *p=*p+1;
1808 }
1809
1810 // scr_DrawRoster()
1811 // Display the buddylist (not really the roster) on the screen
1812 void scr_DrawRoster(void)
1813 {
1814 static int offset = 0;
1815 char *name, *rline;
1816 int maxx, maxy;
1817 GList *buddy;
1818 int i, n;
1819 int rOffset;
1820 int cursor_backup;
1821 char status, pending;
1822 enum imstatus currentstatus = xmpp_getstatus();
1823 int x_pos;
1824
1825 // We can reset update_roster
1826 update_roster = FALSE;
1827
1828 getmaxyx(rosterWnd, maxy, maxx);
1829 maxx--; // Last char is for vertical border
1830
1831 cursor_backup = curs_set(0);
1832
1833 if (!buddylist)
1834 offset = 0;
1835 else
1836 scr_UpdateChatStatus(FALSE);
1837
1838 // Cleanup of roster window
1839 werase(rosterWnd);
1840
1841 if (Roster_Width) {
1842 int line_x_pos = roster_win_on_right ? 0 : Roster_Width-1;
1843 // Redraw the vertical line (not very good...)
1844 wattrset(rosterWnd, get_color(COLOR_GENERAL));
1845 for (i=0 ; i < CHAT_WIN_HEIGHT ; i++)
1846 mvwaddch(rosterWnd, i, line_x_pos, ACS_VLINE);
1847 }
1848
1849 // Leave now if buddylist is empty or the roster is hidden
1850 if (!buddylist || !Roster_Width) {
1851 update_panels();
1852 curs_set(cursor_backup);
1853 return;
1854 }
1855
1856 // Update offset if necessary
1857 // a) Try to show as many buddylist items as possible
1858 i = g_list_length(buddylist) - maxy;
1859 if (i < 0)
1860 i = 0;
1861 if (i < offset)
1862 offset = i;
1863 // b) Make sure the current_buddy is visible
1864 i = g_list_position(buddylist, current_buddy);
1865 if (i == -1) { // This is bad
1866 scr_LogPrint(LPRINT_NORMAL, "Doh! Can't find current selected buddy!!");
1867 curs_set(cursor_backup);
1868 return;
1869 } else if (i < offset) {
1870 offset = i;
1871 } else if (i+1 > offset + maxy) {
1872 offset = i + 1 - maxy;
1873 }
1874
1875 if (roster_win_on_right)
1876 x_pos = 1; // 1 char offset (vertical line)
1877 else
1878 x_pos = 0;
1879
1880 name = g_new0(char, 4*Roster_Width);
1881 rline = g_new0(char, 4*Roster_Width+1);
1882
1883 buddy = buddylist;
1884 rOffset = offset;
1885
1886 for (i=0; i<maxy && buddy; buddy = g_list_next(buddy)) {
1887 unsigned short bflags, btype, ismsg, isgrp, ismuc, ishid, isspe;
1888 gchar *rline_locale;
1889 GSList *resources, *p_res;
1890
1891 bflags = buddy_getflags(BUDDATA(buddy));
1892 btype = buddy_gettype(BUDDATA(buddy));
1893
1894 ismsg = bflags & ROSTER_FLAG_MSG;
1895 ishid = bflags & ROSTER_FLAG_HIDE;
1896 isgrp = btype & ROSTER_TYPE_GROUP;
1897 ismuc = btype & ROSTER_TYPE_ROOM;
1898 isspe = btype & ROSTER_TYPE_SPECIAL;
1899
1900 if (rOffset > 0) {
1901 rOffset--;
1902 continue;
1903 }
1904
1905 status = '?';
1906 pending = ' ';
1907
1908 resources = buddy_getresources(BUDDATA(buddy));
1909 for (p_res = resources ; p_res ; p_res = g_slist_next(p_res)) {
1910 guint events = buddy_resource_getevents(BUDDATA(buddy),
1911 p_res ? p_res->data : "");
1912 if ((events & ROSTER_EVENT_PAUSED) && pending != '+')
1913 pending = '.';
1914 if (events & ROSTER_EVENT_COMPOSING)
1915 pending = '+';
1916 g_free(p_res->data);
1917 }
1918 g_slist_free(resources);
1919
1920 // Display message notice if there is a message flag, but not
1921 // for unfolded groups.
1922 if (ismsg && (!isgrp || ishid)) {
1923 pending = '#';
1924 }
1925
1926 if (ismuc) {
1927 if (buddy_getinsideroom(BUDDATA(buddy)))
1928 status = 'C';
1929 else
1930 status = 'x';
1931 } else if (currentstatus != offline) {
1932 enum imstatus budstate;
1933 budstate = buddy_getstatus(BUDDATA(buddy), NULL);
1934 if (budstate < imstatus_size)
1935 status = imstatus2char[budstate];
1936 }
1937 if (buddy == current_buddy) {
1938 if (pending == '#')
1939 wattrset(rosterWnd, get_color(COLOR_ROSTERSELNMSG));
1940 else
1941 wattrset(rosterWnd, get_color(COLOR_ROSTERSEL));
1942 // The 3 following lines aim at coloring the whole line
1943 wmove(rosterWnd, i, x_pos);
1944 for (n = 0; n < maxx; n++)
1945 waddch(rosterWnd, ' ');
1946 } else {
1947 if (pending == '#')
1948 wattrset(rosterWnd, get_color(COLOR_ROSTERNMSG));
1949 else {
1950 int color = get_color(COLOR_ROSTER);
1951 if ((!isspe) && (!isgrp)) {//Look for color rules
1952 GSList *head;
1953 const char *jid = buddy_getjid(BUDDATA(buddy));
1954 for (head = rostercolrules; head; head = g_slist_next(head)) {
1955 rostercolor *rc = head->data;
1956 if (g_pattern_match_string(rc->compiled, jid) &&
1957 (!strcmp("*", rc->status) || strchr(rc->status, status))) {
1958 color = compose_color(rc->color);
1959 break;
1960 }
1961 }
1962 }
1963 wattrset(rosterWnd, color);
1964 }
1965 }
1966
1967 if (Roster_Width > 7)
1968 g_utf8_strncpy(name, buddy_getname(BUDDATA(buddy)), Roster_Width-7);
1969 else
1970 name[0] = 0;
1971
1972 if (isgrp) {
1973 if (ishid) {
1974 int group_count = 0;
1975 foreach_group_member(BUDDATA(buddy), increment_if_buddy_not_filtered,
1976 &group_count);
1977 snprintf(rline, 4*Roster_Width, " %c+++ %s (%i)", pending, name,
1978 group_count);
1979 /* Do not display the item count if there isn't enough space */
1980 if (g_utf8_strlen(rline, 4*Roster_Width) >= Roster_Width)
1981 snprintf(rline, 4*Roster_Width, " %c--- %s", pending, name);
1982 }
1983 else
1984 snprintf(rline, 4*Roster_Width, " %c--- %s", pending, name);
1985 } else if (isspe) {
1986 snprintf(rline, 4*Roster_Width, " %c%s", pending, name);
1987 } else {
1988 char sepleft = '[';
1989 char sepright = ']';
1990 if (btype & ROSTER_TYPE_USER) {
1991 guint subtype = buddy_getsubscription(BUDDATA(buddy));
1992 if (status == '_' && !(subtype & sub_to))
1993 status = '?';
1994 if (!(subtype & sub_from)) {
1995 sepleft = '{';
1996 sepright = '}';
1997 }
1998 }
1999
2000 snprintf(rline, 4*Roster_Width,
2001 " %c%c%c%c %s", pending, sepleft, status, sepright, name);
2002 }
2003
2004 rline_locale = from_utf8(rline);
2005 mvwprintw(rosterWnd, i, x_pos, "%s", rline_locale);
2006 g_free(rline_locale);
2007 i++;
2008 }
2009
2010 g_free(rline);
2011 g_free(name);
2012 top_panel(inputPanel);
2013 update_panels();
2014 curs_set(cursor_backup);
2015 }
2016
2017 // scr_RosterVisibility(status)
2018 // Set the roster visibility:
2019 // status=1 Show roster
2020 // status=0 Hide roster
2021 // status=-1 Toggle roster status
2022 void scr_RosterVisibility(int status)
2023 {
2024 int old_roster_status = roster_hidden;
2025
2026 if (status > 0)
2027 roster_hidden = FALSE;
2028 else if (status == 0)
2029 roster_hidden = TRUE;
2030 else
2031 roster_hidden = !roster_hidden;
2032
2033 if (roster_hidden != old_roster_status) {
2034 // Recalculate windows size and redraw
2035 scr_Resize();
2036 redrawwin(stdscr);
2037 }
2038 }
2039
2040 #ifdef HAVE_GLIB_REGEX
2041 static inline void scr_LogUrls(const gchar *string)
2042 {
2043 GMatchInfo *match_info;
2044
2045 g_regex_match_full(url_regex, string, -1, 0, 0, &match_info, NULL);
2046 while (g_match_info_matches(match_info)) {
2047 gchar *url = g_match_info_fetch(match_info, 0);
2048 scr_print_logwindow(url);
2049 g_free(url);
2050 g_match_info_next(match_info, NULL);
2051 }
2052 g_match_info_free(match_info);
2053 }
2054 #endif
2055
2056 void scr_WriteMessage(const char *bjid, const char *text,
2057 time_t timestamp, guint prefix_flags,
2058 unsigned mucnicklen, gpointer xep184)
2059 {
2060 char *xtext;
2061
2062 if (!timestamp) timestamp = time(NULL);
2063
2064 xtext = ut_expand_tabs(text); // Expand tabs and filter out some chars
2065
2066 scr_WriteInWindow(bjid, xtext, timestamp, prefix_flags, FALSE, mucnicklen,
2067 xep184);
2068
2069 if (xtext != (char*)text)
2070 g_free(xtext);
2071 }
2072
2073 // If prefix is NULL, HBB_PREFIX_IN is supposed.
2074 void scr_WriteIncomingMessage(const char *jidfrom, const char *text,
2075 time_t timestamp, guint prefix, unsigned mucnicklen)
2076 {
2077 if (!(prefix &
2078 ~HBB_PREFIX_NOFLAG & ~HBB_PREFIX_HLIGHT & ~HBB_PREFIX_HLIGHT_OUT &
2079 ~HBB_PREFIX_PGPCRYPT & ~HBB_PREFIX_OTRCRYPT))
2080 prefix |= HBB_PREFIX_IN;
2081
2082 #ifdef HAVE_GLIB_REGEX
2083 if (url_regex)
2084 scr_LogUrls(text);
2085 #endif
2086 scr_WriteMessage(jidfrom, text, timestamp, prefix, mucnicklen, NULL);
2087 }
2088
2089 void scr_WriteOutgoingMessage(const char *jidto, const char *text, guint prefix,
2090 gpointer xep184)
2091 {
2092 GSList *roster_elt;
2093 roster_elt = roster_find(jidto, jidsearch,
2094 ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM);
2095
2096 scr_WriteMessage(jidto, text,
2097 0, prefix|HBB_PREFIX_OUT|HBB_PREFIX_HLIGHT_OUT, 0, xep184);
2098
2099 // Show jidto's buffer unless the buddy is not in the buddylist
2100 if (roster_elt && g_list_position(buddylist, roster_elt->data) != -1)
2101 scr_ShowWindow(jidto, FALSE);
2102 }
2103
2104 void scr_RemoveReceiptFlag(const char *bjid, gpointer xep184)
2105 {
2106 winbuf *win_entry = scr_SearchWindow(bjid, FALSE);
2107 if (win_entry) {
2108 hbuf_remove_receipt(win_entry->bd->hbuf, xep184);
2109 if (chatmode && (buddy_search_jid(bjid) == current_buddy))
2110 scr_UpdateBuddyWindow();
2111 }
2112 }
2113
2114 static inline void set_autoaway(bool setaway)
2115 {
2116 static enum imstatus oldstatus;
2117 static char *oldmsg;
2118 Autoaway = setaway;
2119
2120 if (setaway) {
2121 const char *msg, *prevmsg;
2122 oldstatus = xmpp_getstatus();
2123 if (oldmsg) {
2124 g_free(oldmsg);
2125 oldmsg = NULL;
2126 }
2127 prevmsg = xmpp_getstatusmsg();
2128 msg = settings_opt_get("message_autoaway");
2129 if (!msg)
2130 msg = prevmsg;
2131 if (prevmsg)
2132 oldmsg = g_strdup(prevmsg);
2133 xmpp_setstatus(away, NULL, msg, FALSE);
2134 } else {
2135 // Back
2136 xmpp_setstatus(oldstatus, NULL, (oldmsg ? oldmsg : ""), FALSE);
2137 if (oldmsg) {
2138 g_free(oldmsg);
2139 oldmsg = NULL;
2140 }
2141 }
2142 }
2143
2144 // set_chatstate(state)
2145 // Set the current chat state (0=active, 1=composing, 2=paused)
2146 // If the chat state has changed, call xmpp_send_chatstate()
2147 static inline void set_chatstate(int state)
2148 {
2149 #if defined JEP0022 || defined JEP0085
2150 if (chatstates_disabled)
2151 return;
2152 if (!chatmode)
2153 state = 0;
2154 if (state != chatstate) {
2155 chatstate = state;
2156 if (current_buddy &&
2157 buddy_gettype(BUDDATA(current_buddy)) == ROSTER_TYPE_USER) {
2158 guint jep_state;
2159 if (chatstate == 1) {
2160 if (chatstate_timeout_id == 0)
2161 chatstate_timeout_id = g_timeout_add_seconds(1,
2162 scr_ChatStatesTimeout,
2163 NULL);
2164 jep_state = ROSTER_EVENT_COMPOSING;
2165 }
2166 else if (chatstate == 2)
2167 jep_state = ROSTER_EVENT_PAUSED;
2168 else
2169 jep_state = ROSTER_EVENT_ACTIVE;
2170 xmpp_send_chatstate(BUDDATA(current_buddy), jep_state);
2171 }
2172 if (!chatstate)
2173 chatstate_timestamp = 0;
2174 }
2175 #endif
2176 }
2177
2178 #if defined JEP0022 || defined JEP0085
2179 gboolean scr_ChatStatesTimeout(void)
2180 {
2181 time_t now;
2182 time(&now);
2183 // Check if we're currently composing...
2184 if (chatstate != 1 || !chatstate_timestamp) {
2185 chatstate_timeout_id = 0;
2186 return FALSE;
2187 }
2188
2189 // If the timeout is reached, let's change the state right now.
2190 if (now >= chatstate_timestamp + COMPOSING_TIMEOUT) {
2191 chatstate_timestamp = now;
2192 set_chatstate(2);
2193 chatstate_timeout_id = 0;
2194 return FALSE;
2195 }
2196 return TRUE;
2197 }
2198 #endif
2199
2200 // Check if we should enter/leave automatic away status
2201 void scr_CheckAutoAway(int activity)
2202 {
2203 enum imstatus cur_st;
2204 unsigned int autoaway_timeout = settings_opt_get_int("autoaway");
2205
2206 if (Autoaway && activity) set_autoaway(FALSE);
2207 if (!autoaway_timeout) return;
2208 if (!LastActivity || activity) time(&LastActivity);
2209
2210 cur_st = xmpp_getstatus();
2211 // Auto-away is disabled for the following states
2212 if ((cur_st != available) && (cur_st != freeforchat))
2213 return;
2214
2215 if (!activity) {
2216 time_t now;
2217 time(&now);
2218 if (!Autoaway && (now > LastActivity + (time_t)autoaway_timeout))
2219 set_autoaway(TRUE);
2220 }
2221 }
2222
2223 // set_current_buddy(newbuddy)
2224 // Set the current_buddy to newbuddy (if not NULL)
2225 // Lock the newbuddy, and unlock the previous current_buddy
2226 static void set_current_buddy(GList *newbuddy)
2227 {
2228 enum imstatus prev_st = imstatus_size;
2229 /* prev_st initialized to imstatus_size, which is used as "undef" value.
2230 * We are sure prev_st will get a different status value after the
2231 * buddy_getstatus() call.
2232 */
2233
2234 if (!current_buddy || !newbuddy) return;
2235 if (newbuddy == current_buddy) return;
2236
2237 // We're moving to another buddy. We're thus inactive wrt current_buddy.
2238 set_chatstate(0);
2239 // We don't want the chatstate to be changed again right now.
2240 lock_chatstate = TRUE;
2241
2242 prev_st = buddy_getstatus(BUDDATA(current_buddy), NULL);
2243 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
2244 if (chatmode)
2245 alternate_buddy = current_buddy;
2246 current_buddy = newbuddy;
2247 // Lock the buddy in the buddylist if we're in chat mode
2248 if (chatmode)
2249 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
2250 // We should rebuild the buddylist but not everytime
2251 // Here we check if we were locking a buddy who is actually offline,
2252 // and hide_offline_buddies is TRUE. In which case we need to rebuild.
2253 if (!(buddylist_get_filter() & 1<<prev_st))
2254 buddylist_build();
2255 update_roster = TRUE;
2256 }
2257
2258 // scr_RosterTop()
2259 // Go to the first buddy in the buddylist
2260 void scr_RosterTop(void)
2261 {
2262 set_current_buddy(buddylist);
2263 if (chatmode)
2264 scr_ShowBuddyWindow();
2265 }
2266
2267 // scr_RosterBottom()
2268 // Go to the last buddy in the buddylist
2269 void scr_RosterBottom(void)
2270 {
2271 set_current_buddy(g_list_last(buddylist));
2272 if (chatmode)
2273 scr_ShowBuddyWindow();
2274 }
2275
2276 // scr_RosterUpDown(updown, n)
2277 // Go to the nth next buddy in the buddylist
2278 // (up if updown == -1, down if updown == 1)
2279 void scr_RosterUpDown(int updown, unsigned int n)
2280 {
2281 unsigned int i;
2282
2283 if (updown < 0) {
2284 for (i = 0; i < n; i++)
2285 set_current_buddy(g_list_previous(current_buddy));
2286 } else {
2287 for (i = 0; i < n; i++)
2288 set_current_buddy(g_list_next(current_buddy));
2289 }
2290 if (chatmode)
2291 scr_ShowBuddyWindow();
2292 }
2293
2294 // scr_RosterPrevGroup()
2295 // Go to the previous group in the buddylist
2296 void scr_RosterPrevGroup(void)
2297 {
2298 GList *bud;
2299
2300 for (bud = current_buddy ; bud ; ) {
2301 bud = g_list_previous(bud);
2302 if (!bud)
2303 break;
2304 if (buddy_gettype(BUDDATA(bud)) & ROSTER_TYPE_GROUP) {
2305 set_current_buddy(bud);
2306 if (chatmode)
2307 scr_ShowBuddyWindow();
2308 break;
2309 }
2310 }
2311 }
2312
2313 // scr_RosterNextGroup()
2314 // Go to the next group in the buddylist
2315 void scr_RosterNextGroup(void)
2316 {
2317 GList *bud;
2318
2319 for (bud = current_buddy ; bud ; ) {
2320 bud = g_list_next(bud);
2321 if (!bud)
2322 break;
2323 if (buddy_gettype(BUDDATA(bud)) & ROSTER_TYPE_GROUP) {
2324 set_current_buddy(bud);
2325 if (chatmode)
2326 scr_ShowBuddyWindow();
2327 break;
2328 }
2329 }
2330 }
2331
2332 // scr_RosterSearch(str)
2333 // Look forward for a buddy with jid/name containing str.
2334 void scr_RosterSearch(char *str)
2335 {
2336 set_current_buddy(buddy_search(str));
2337 if (chatmode)
2338 scr_ShowBuddyWindow();
2339 }
2340
2341 // scr_RosterJumpJid(bjid)
2342 // Jump to buddy bjid.
2343 // NOTE: With this function, the buddy is added to the roster if doesn't exist.
2344 void scr_RosterJumpJid(char *barejid)
2345 {
2346 GSList *roster_elt;
2347 // Look for an existing buddy
2348 roster_elt = roster_find(barejid, jidsearch,
2349 ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM);
2350 // Create it if necessary
2351 if (!roster_elt)
2352 roster_elt = roster_add_user(barejid, NULL, NULL, ROSTER_TYPE_USER,
2353 sub_none, -1);
2354 // Set a lock to see it in the buddylist
2355 buddy_setflags(BUDDATA(roster_elt), ROSTER_FLAG_LOCK, TRUE);
2356 buddylist_build();
2357 // Jump to the buddy
2358 set_current_buddy(buddy_search_jid(barejid));
2359 if (chatmode)
2360 scr_ShowBuddyWindow();
2361 }
2362
2363 // scr_RosterUnreadMessage(next)
2364 // Go to a new message. If next is not null, try to go to the next new
2365 // message. If it is not possible or if next is NULL, go to the first new
2366 // message from unread_list.
2367 void scr_RosterUnreadMessage(int next)
2368 {
2369 gpointer unread_ptr;
2370 gpointer refbuddata;
2371 GList *nbuddy;
2372
2373 if (!current_buddy) return;
2374
2375 if (next) refbuddata = BUDDATA(current_buddy);
2376 else refbuddata = NULL;
2377
2378 unread_ptr = unread_msg(refbuddata);
2379 if (!unread_ptr) return;
2380
2381 if (!(buddy_gettype(unread_ptr) & ROSTER_TYPE_SPECIAL)) {
2382 gpointer ngroup;
2383 // If buddy is in a folded group, we need to expand it
2384 ngroup = buddy_getgroup(unread_ptr);
2385 if (buddy_getflags(ngroup) & ROSTER_FLAG_HIDE) {
2386 buddy_setflags(ngroup, ROSTER_FLAG_HIDE, FALSE);
2387 buddylist_build();
2388 }
2389 }
2390
2391 nbuddy = g_list_find(buddylist, unread_ptr);
2392 if (nbuddy) {
2393 set_current_buddy(nbuddy);
2394 if (chatmode) scr_ShowBuddyWindow();
2395 } else
2396 scr_LogPrint(LPRINT_LOGNORM, "Error: nbuddy == NULL"); // should not happen
2397 }
2398
2399 // scr_RosterJumpAlternate()
2400 // Try to jump to alternate (== previous) buddy
2401 void scr_RosterJumpAlternate(void)
2402 {
2403 if (!alternate_buddy || g_list_position(buddylist, alternate_buddy) == -1)
2404 return;
2405 set_current_buddy(alternate_buddy);
2406 if (chatmode)
2407 scr_ShowBuddyWindow();
2408 }
2409
2410 // scr_RosterDisplay(filter)
2411 // Set the roster filter mask. If filter is null/empty, the current
2412 // mask is displayed.
2413 void scr_RosterDisplay(const char *filter)
2414 {
2415 guchar status;
2416 enum imstatus budstate;
2417 char strfilter[imstatus_size+1];
2418 char *psfilter;
2419
2420 if (filter && *filter) {
2421 int show_all = (*filter == '*');
2422 status = 0;
2423 for (budstate = 0; budstate < imstatus_size-1; budstate++)
2424 if (strchr(filter, imstatus2char[budstate]) || show_all)
2425 status |= 1<<budstate;
2426 buddylist_set_filter(status);
2427 buddylist_build();
2428 update_roster = TRUE;
2429 return;
2430 }
2431
2432 // Display current filter
2433 psfilter = strfilter;
2434 status = buddylist_get_filter();
2435 for (budstate = 0; budstate < imstatus_size-1; budstate++)
2436 if (status & 1<<budstate)
2437 *psfilter++ = imstatus2char[budstate];
2438 *psfilter = '\0';
2439 scr_LogPrint(LPRINT_NORMAL, "Roster status filter: %s", strfilter);
2440 }
2441
2442 // scr_BufferScrollUpDown()
2443 // Scroll up/down the current buddy window,
2444 // - half a screen if nblines is 0,
2445 // - up if updown == -1, down if updown == 1
2446 void scr_BufferScrollUpDown(int updown, unsigned int nblines)
2447 {
2448 winbuf *win_entry;
2449 int n, nbl;
2450 GList *hbuf_top;
2451 guint isspe;
2452
2453 // Get win_entry
2454 if (!current_buddy) return;
2455
2456 isspe = buddy_gettype(BUDDATA(current_buddy)) & ROSTER_TYPE_SPECIAL;
2457 win_entry = scr_SearchWindow(CURRENT_JID, isspe);
2458 if (!win_entry) return;
2459
2460 if (!nblines) {
2461 // Scroll half a screen (or less)
2462 nbl = CHAT_WIN_HEIGHT/2;
2463 } else {
2464 nbl = nblines;
2465 }
2466 hbuf_top = win_entry->bd->top;
2467
2468 if (updown == -1) { // UP
2469 if (!hbuf_top) {
2470 hbuf_top = g_list_last(win_entry->bd->hbuf);
2471 if (!win_entry->bd->cleared) {
2472 if (!nblines) nbl = nbl*3 - 1;
2473 else nbl += CHAT_WIN_HEIGHT - 1;
2474 } else {
2475 win_entry->bd->cleared = FALSE;
2476 }
2477 }
2478 for (n=0 ; hbuf_top && n < nbl && g_list_previous(hbuf_top) ; n++)
2479 hbuf_top = g_list_previous(hbuf_top);
2480 win_entry->bd->top = hbuf_top;
2481 } else { // DOWN
2482 for (n=0 ; hbuf_top && n < nbl ; n++)
2483 hbuf_top = g_list_next(hbuf_top);
2484 win_entry->bd->top = hbuf_top;
2485 // Check if we are at the bottom
2486 for (n=0 ; hbuf_top && n < CHAT_WIN_HEIGHT-1 ; n++)
2487 hbuf_top = g_list_next(hbuf_top);
2488 if (!hbuf_top)
2489 win_entry->bd->top = NULL; // End reached
2490 }
2491
2492 // Refresh the window
2493 scr_UpdateWindow(win_entry);
2494
2495 // Finished :)
2496 update_panels();
2497 }
2498
2499 // scr_BufferClear()
2500 // Clear the current buddy window (used for the /clear command)
2501 void scr_BufferClear(void)
2502 {
2503 winbuf *win_entry;
2504 guint isspe;
2505
2506 // Get win_entry
2507 if (!current_buddy) return;
2508 isspe = buddy_gettype(BUDDATA(current_buddy)) & ROSTER_TYPE_SPECIAL;
2509 win_entry = scr_SearchWindow(CURRENT_JID, isspe);
2510 if (!win_entry) return;
2511
2512 win_entry->bd->cleared = TRUE;
2513 win_entry->bd->top = NULL;
2514
2515 // Refresh the window
2516 scr_UpdateWindow(win_entry);
2517
2518 // Finished :)
2519 update_panels();
2520 }
2521
2522 // buffer_purge()
2523 // key: winId/jid
2524 // value: winbuf structure
2525 // data: int, set to 1 if the buffer should be closed.
2526 // NOTE: does not work for special buffers.
2527 static void buffer_purge(gpointer key, gpointer value, gpointer data)
2528 {
2529 int *p_closebuf = data;
2530 winbuf *win_entry = value;
2531
2532 // Delete the current hbuf
2533 hbuf_free(&win_entry->bd->hbuf);
2534
2535 if (*p_closebuf) {
2536 g_hash_table_remove(winbufhash, key);
2537 } else {
2538 win_entry->bd->cleared = FALSE;
2539 win_entry->bd->top = NULL;
2540 }
2541 }
2542
2543 // scr_BufferPurge(closebuf, jid)
2544 // Purge/Drop the current buddy buffer or jid's buffer if jid != NULL.
2545 // If closebuf is 1, close the buffer.
2546 void scr_BufferPurge(int closebuf, const char *jid)
2547 {
2548 winbuf *win_entry;
2549 guint isspe;
2550 guint *p_closebuf;
2551 const char *cjid;
2552 guint hold_chatmode = FALSE;
2553
2554 if (jid) {
2555 cjid = jid;
2556 isspe = FALSE;
2557 // If closebuf is TRUE, it's probably better not to leave chat mode
2558 // if the change isn't related to the current buffer.
2559 if (closebuf && current_buddy) {
2560 if (buddy_gettype(BUDDATA(current_buddy)) & ROSTER_TYPE_SPECIAL ||
2561 strcasecmp(jid, CURRENT_JID))
2562 hold_chatmode = TRUE;
2563 }
2564 } else {
2565 // Get win_entry
2566 if (!current_buddy) return;
2567 cjid = CURRENT_JID;
2568 isspe = buddy_gettype(BUDDATA(current_buddy)) & ROSTER_TYPE_SPECIAL;
2569 }
2570 win_entry = scr_SearchWindow(cjid, isspe);
2571 if (!win_entry) return;
2572
2573 if (!isspe) {
2574 p_closebuf = g_new(guint, 1);
2575 *p_closebuf = closebuf;
2576 buffer_purge((gpointer)cjid, win_entry, p_closebuf);
2577 g_free(p_closebuf);
2578 if (closebuf && !hold_chatmode) {
2579 scr_set_chatmode(FALSE);
2580 currentWindow = NULL;
2581 }
2582 } else {
2583 // (Special buffer)
2584 // Reset the current hbuf
2585 hbuf_free(&win_entry->bd->hbuf);
2586 // Currently it can only be the status buffer
2587 statushbuf = NULL;
2588
2589 win_entry->bd->cleared = FALSE;
2590 win_entry->bd->top = NULL;
2591 }
2592
2593 // Refresh the window
2594 scr_UpdateBuddyWindow();
2595
2596 // Finished :)
2597 update_panels();
2598 }
2599
2600 void scr_BufferPurgeAll(int closebuf)
2601 {
2602 guint *p_closebuf;
2603 p_closebuf = g_new(guint, 1);
2604
2605 *p_closebuf = closebuf;
2606 g_hash_table_foreach(winbufhash, buffer_purge, p_closebuf);
2607 g_free(p_closebuf);
2608
2609 if (closebuf) {
2610 scr_set_chatmode(FALSE);
2611 currentWindow = NULL;
2612 }
2613
2614 // Refresh the window
2615 scr_UpdateBuddyWindow();
2616
2617 // Finished :)
2618 update_panels();
2619 }
2620
2621 // scr_BufferScrollLock(lock)
2622 // Lock/unlock the current buddy buffer
2623 // lock = 1 : lock
2624 // lock = 0 : unlock
2625 // lock = -1: toggle lock status
2626 void scr_BufferScrollLock(int lock)
2627 {
2628 winbuf *win_entry;
2629 guint isspe;
2630
2631 // Get win_entry
2632 if (!current_buddy) return;
2633 isspe = buddy_gettype(BUDDATA(current_buddy)) & ROSTER_TYPE_SPECIAL;
2634 win_entry = scr_SearchWindow(CURRENT_JID, isspe);
2635 if (!win_entry) return;
2636
2637 if (lock == -1)
2638 lock = !win_entry->bd->lock;
2639
2640 if (lock) {
2641 win_entry->bd->lock = TRUE;
2642 } else {
2643 win_entry->bd->lock = FALSE;
2644 //win_entry->bd->cleared = FALSE;
2645 if (isspe || (buddy_getflags(BUDDATA(current_buddy)) & ROSTER_FLAG_MSG))
2646 win_entry->bd->top = NULL;
2647 }
2648
2649 // If chatmode is disabled and we're at the bottom of the buffer,
2650 // we need to set the "top" line, so we need to call scr_ShowBuddyWindow()
2651 // at least once. (Maybe it will cause a double refresh...)
2652 if (!chatmode && !win_entry->bd->top) {
2653 chatmode = TRUE;
2654 scr_ShowBuddyWindow();
2655 chatmode = FALSE;
2656 }
2657
2658 // Refresh the window
2659 scr_UpdateBuddyWindow();
2660
2661 // Finished :)
2662 update_panels();
2663 }
2664
2665 // scr_BufferTopBottom()
2666 // Jump to the head/tail of the current buddy window
2667 // (top if topbottom == -1, bottom topbottom == 1)
2668 void scr_BufferTopBottom(int topbottom)
2669 {
2670 winbuf *win_entry;
2671 guint isspe;
2672
2673 // Get win_entry
2674 if (!current_buddy) return;
2675 isspe = buddy_gettype(BUDDATA(current_buddy)) & ROSTER_TYPE_SPECIAL;
2676 win_entry = scr_SearchWindow(CURRENT_JID, isspe);
2677 if (!win_entry) return;
2678
2679 win_entry->bd->cleared = FALSE;
2680 if (topbottom == 1)
2681 win_entry->bd->top = NULL;
2682 else
2683 win_entry->bd->top = g_list_first(win_entry->bd->hbuf);
2684
2685 // Refresh the window
2686 scr_UpdateWindow(win_entry);
2687
2688 // Finished :)
2689 update_panels();
2690 }
2691
2692 // scr_BufferSearch(direction, text)
2693 // Jump to the next line containing text
2694 // (backward search if direction == -1, forward if topbottom == 1)
2695 void scr_BufferSearch(int direction, const char *text)
2696 {
2697 winbuf *win_entry;
2698 GList *current_line, *search_res;
2699 guint isspe;
2700
2701 // Get win_entry
2702 if (!current_buddy) return;
2703 isspe = buddy_gettype(BUDDATA(current_buddy)) & ROSTER_TYPE_SPECIAL;
2704 win_entry = scr_SearchWindow(CURRENT_JID, isspe);
2705 if (!win_entry) return;
2706
2707 if (win_entry->bd->top)
2708 current_line = win_entry->bd->top;
2709 else
2710 current_line = g_list_last(win_entry->bd->hbuf);
2711
2712 search_res = hbuf_search(current_line, direction, text);
2713
2714 if (search_res) {
2715 win_entry->bd->cleared = FALSE;
2716 win_entry->bd->top = search_res;
2717
2718 // Refresh the window
2719 scr_UpdateWindow(win_entry);
2720
2721 // Finished :)
2722 update_panels();
2723 } else
2724 scr_LogPrint(LPRINT_NORMAL, "Search string not found");
2725 }
2726
2727 // scr_BufferPercent(n)
2728 // Jump to the specified position in the buffer, in %
2729 void scr_BufferPercent(int pc)
2730 {
2731 winbuf *win_entry;
2732 GList *search_res;
2733 guint isspe;
2734
2735 // Get win_entry
2736 if (!current_buddy) return;
2737 isspe = buddy_gettype(BUDDATA(current_buddy)) & ROSTER_TYPE_SPECIAL;
2738 win_entry = scr_SearchWindow(CURRENT_JID, isspe);
2739 if (!win_entry) return;
2740
2741 if (pc < 0 || pc > 100) {
2742 scr_LogPrint(LPRINT_NORMAL, "Bad % value");
2743 return;
2744 }
2745
2746 search_res = hbuf_jump_percent(win_entry->bd->hbuf, pc);
2747
2748 win_entry->bd->cleared = FALSE;
2749 win_entry->bd->top = search_res;
2750
2751 // Refresh the window
2752 scr_UpdateWindow(win_entry);
2753
2754 // Finished :)
2755 update_panels();
2756 }
2757
2758 // scr_BufferDate(t)
2759 // Jump to the first line after date t in the buffer
2760 // t is a date in seconds since `00:00:00 1970-01-01 UTC'
2761 void scr_BufferDate(time_t t)
2762 {
2763 winbuf *win_entry;
2764 GList *search_res;
2765 guint isspe;
2766
2767 // Get win_entry
2768 if (!current_buddy) return;
2769 isspe = buddy_gettype(BUDDATA(current_buddy)) & ROSTER_TYPE_SPECIAL;
2770 win_entry = scr_SearchWindow(CURRENT_JID, isspe);
2771 if (!win_entry) return;
2772
2773 search_res = hbuf_jump_date(win_entry->bd->hbuf, t);
2774
2775 win_entry->bd->cleared = FALSE;
2776 win_entry->bd->top = search_res;
2777
2778 // Refresh the window
2779 scr_UpdateWindow(win_entry);
2780
2781 // Finished :)
2782 update_panels();
2783 }
2784
2785 void scr_BufferDump(const char *file)
2786 {
2787 char *extfname;
2788
2789 if (!currentWindow) {
2790 scr_LogPrint(LPRINT_NORMAL, "No current buffer!");
2791 return;
2792 }
2793
2794 if (!file || !*file) {
2795 scr_LogPrint(LPRINT_NORMAL, "Missing parameter (file name)!");
2796 return;
2797 }
2798
2799 extfname = expand_filename(file);
2800 hbuf_dump_to_file(currentWindow->bd->hbuf, extfname);
2801 g_free(extfname);
2802 }
2803
2804 // buffer_list()
2805 // key: winId/jid
2806 // value: winbuf structure
2807 // data: none.
2808 static void buffer_list(gpointer key, gpointer value, gpointer data)
2809 {
2810 GList *head;
2811 winbuf *win_entry = value;
2812
2813 head = g_list_first(win_entry->bd->hbuf);
2814
2815 scr_LogPrint(LPRINT_NORMAL, " %s (%u/%u)", key,
2816 g_list_length(head), hbuf_get_blocks_number(head));
2817 }
2818
2819 void scr_BufferList(void)
2820 {
2821 scr_LogPrint(LPRINT_NORMAL, "Buffer list:");
2822 buffer_list("[status]", statusWindow, NULL);
2823 g_hash_table_foreach(winbufhash, buffer_list, NULL);
2824 scr_LogPrint(LPRINT_NORMAL, "End of buffer list.");
2825 scr_setmsgflag_if_needed(SPECIAL_BUFFER_STATUS_ID, TRUE);
2826 update_roster = TRUE;
2827 }
2828
2829 // scr_set_chatmode()
2830 // Public function to (un)set chatmode...
2831 inline void scr_set_chatmode(int enable)
2832 {
2833 chatmode = enable;
2834 scr_UpdateChatStatus(TRUE);
2835 }
2836
2837 // scr_get_chatmode()
2838 // Public function to get chatmode state.
2839 inline int scr_get_chatmode(void)
2840 {
2841 return chatmode;
2842 }
2843
2844 // scr_get_multimode()
2845 // Public function to get multimode status...
2846 inline int scr_get_multimode(void)
2847 {
2848 return multimode;
2849 }
2850
2851 // scr_setmsgflag_if_needed(jid)
2852 // Set the message flag unless we're already in the jid buffer window
2853 void scr_setmsgflag_if_needed(const char *bjid, int special)
2854 {
2855 const char *current_id;
2856 bool iscurrentlocked = FALSE;
2857
2858 if (!bjid)
2859 return;
2860
2861 if (current_buddy) {
2862 if (special)
2863 current_id = buddy_getname(BUDDATA(current_buddy));
2864 else
2865 current_id = buddy_getjid(BUDDATA(current_buddy));
2866 if (current_id) {
2867 winbuf *win_entry = scr_SearchWindow(current_id, special);
2868 if (!win_entry) return;
2869 iscurrentlocked = win_entry->bd->lock;
2870 }
2871 } else {
2872 current_id = NULL;
2873 }
2874 if (!chatmode || !current_id || strcmp(bjid, current_id) || iscurrentlocked)
2875 roster_msg_setflag(bjid, special, TRUE);
2876 }
2877
2878 // scr_set_multimode()
2879 // Public function to (un)set multimode...
2880 // Convention:
2881 // 0 = disabled / 1 = multimode / 2 = multimode verbatim (commands disabled)
2882 void scr_set_multimode(int enable, char *subject)
2883 {
2884 g_free(multiline);
2885 multiline = NULL;
2886
2887 g_free(multimode_subj);
2888 if (enable && subject)
2889 multimode_subj = g_strdup(subject);
2890 else
2891 multimode_subj = NULL;
2892
2893 multimode = enable;
2894 }
2895
2896 // scr_get_multiline()
2897 // Public function to get the current multi-line.
2898 const char *scr_get_multiline(void)
2899 {
2900 if (multimode && multiline)
2901 return multiline;
2902 return NULL;
2903 }
2904
2905 // scr_get_multimode_subj()
2906 // Public function to get the multi-line subject, if any.
2907 const char *scr_get_multimode_subj(void)
2908 {
2909 if (multimode)
2910 return multimode_subj;
2911 return NULL;
2912 }
2913
2914 // scr_append_multiline(line)
2915 // Public function to append a line to the current multi-line message.
2916 // Skip empty leading lines.
2917 void scr_append_multiline(const char *line)
2918 {
2919 static int num;
2920
2921 if (!multimode) {
2922 scr_LogPrint(LPRINT_NORMAL, "Error: Not in multi-line message mode!");
2923 return;
2924 }
2925 if (multiline) {
2926 int len = strlen(multiline)+strlen(line)+2;
2927 if (len >= HBB_BLOCKSIZE - 1) {
2928 // We don't handle single messages with size > HBB_BLOCKSIZE
2929 // (see hbuf)
2930 scr_LogPrint(LPRINT_NORMAL, "Your multi-line message is too big, "
2931 "this line has not been added.");
2932 scr_LogPrint(LPRINT_NORMAL, "Please send this part now...");
2933 return;
2934 }
2935 if (num >= MULTILINE_MAX_LINE_NUMBER) {
2936 // We don't allow too many lines; however the maximum is arbitrary
2937 // (It should be < 1000 yet)
2938 scr_LogPrint(LPRINT_NORMAL, "Your message has too many lines, "
2939 "this one has not been added.");
2940 scr_LogPrint(LPRINT_NORMAL, "Please send this part now...");
2941 return;
2942 }
2943 multiline = g_renew(char, multiline, len);
2944 strcat(multiline, "\n");
2945 strcat(multiline, line);
2946 num++;
2947 } else {
2948 // First message line (we skip leading empty lines)
2949 num = 0;
2950 if (line[0]) {
2951 multiline = g_strdup(line);
2952 num++;
2953 } else
2954 return;
2955 }
2956 scr_LogPrint(LPRINT_NORMAL|LPRINT_NOTUTF8,
2957 "Multi-line mode: line #%d added [%.25s...", num, line);
2958 }
2959
2960 // scr_cmdhisto_addline()
2961 // Add a line to the inputLine history
2962 static inline void scr_cmdhisto_addline(char *line)
2963 {
2964 int max_histo_lines;
2965
2966 if (!line || !*line)
2967 return;
2968
2969 max_histo_lines = settings_opt_get_int("cmdhistory_lines");
2970
2971 if (max_histo_lines < 0)
2972 max_histo_lines = 1;
2973
2974 if (max_histo_lines)
2975 while (cmdhisto_nblines >= (guint)max_histo_lines) {
2976 if (cmdhisto_cur && cmdhisto_cur == cmdhisto)
2977 break;
2978 g_free(cmdhisto->data);
2979 cmdhisto = g_list_delete_link(cmdhisto, cmdhisto);
2980 cmdhisto_nblines--;
2981 }
2982
2983 cmdhisto = g_list_append(cmdhisto, g_strdup(line));
2984 cmdhisto_nblines++;
2985 }
2986
2987 // scr_cmdhisto_prev()
2988 // Look for previous line beginning w/ the given mask in the inputLine history
2989 // Returns NULL if none found
2990 static const char *scr_cmdhisto_prev(char *mask, guint len)
2991 {
2992 GList *hl;
2993 if (!cmdhisto_cur) {
2994 hl = g_list_last(cmdhisto);
2995 if (hl) { // backup current line
2996 strncpy(cmdhisto_backup, mask, INPUTLINE_LENGTH);
2997 }
2998 } else {
2999 hl = g_list_previous(cmdhisto_cur);
3000 }
3001 while (hl) {
3002 if (!strncmp((char*)hl->data, mask, len)) {
3003 // Found a match
3004 cmdhisto_cur = hl;
3005 return (const char*)hl->data;
3006 }
3007 hl = g_list_previous(hl);
3008 }
3009 return NULL;
3010 }
3011
3012 // scr_cmdhisto_next()
3013 // Look for next line beginning w/ the given mask in the inputLine history
3014 // Returns NULL if none found
3015 static const char *scr_cmdhisto_next(char *mask, guint len)
3016 {
3017 GList *hl;
3018 if (!cmdhisto_cur) return NULL;
3019 hl = cmdhisto_cur;
3020 while ((hl = g_list_next(hl)) != NULL)
3021 if (!strncmp((char*)hl->data, mask, len)) {
3022 // Found a match
3023 cmdhisto_cur = hl;
3024 return (const char*)hl->data;
3025 }
3026 // If the "backuped" line matches, we'll use it
3027 if (strncmp(cmdhisto_backup, mask, len)) return NULL; // No match
3028 cmdhisto_cur = NULL;
3029 return cmdhisto_backup;
3030 }
3031
3032 // readline_transpose_chars()
3033 // Drag the character before point forward over the character at
3034 // point, moving point forward as well. If point is at the end of
3035 // the line, then this transposes the two characters before point.
3036 void readline_transpose_chars(void)
3037 {
3038 char *c1, *c2;
3039 unsigned a, b;
3040
3041 if (ptr_inputline == inputLine) return;
3042
3043 if (!*ptr_inputline) { // We're at EOL
3044 // If line is only 1 char long, nothing to do...
3045 if (ptr_inputline == prev_char(ptr_inputline, inputLine)) return;
3046 // Transpose the two previous characters
3047 c2 = prev_char(ptr_inputline, inputLine);
3048 c1 = prev_char(c2, inputLine);
3049 a = get_char(c1);
3050 b = get_char(c2);
3051 put_char(put_char(c1, b), a);
3052 } else {
3053 // Swap the two characters before the cursor and move right.
3054 c2 = ptr_inputline;
3055 c1 = prev_char(c2, inputLine);
3056 a = get_char(c1);
3057 b = get_char(c2);
3058 put_char(put_char(c1, b), a);
3059 check_offset(1);
3060 }
3061 }
3062
3063 void readline_forward_kill_word(void)
3064 {
3065 char *c, *old = ptr_inputline;
3066 int spaceallowed = 1;
3067
3068 if (! *ptr_inputline) return;
3069
3070 for (c = ptr_inputline ; *c ; c = next_char(c)) {
3071 if (!iswalnum(get_char(c))) {
3072 if (iswblank(get_char(c))) {
3073 if (!spaceallowed) break;
3074 } else spaceallowed = 0;
3075 } else spaceallowed = 0;
3076 }
3077
3078 // Modify the line
3079 for (;;) {
3080 *old = *c++;
3081 if (!*old++) break;
3082 }
3083 }
3084
3085 // readline_backward_kill_word()
3086 // Kill the word before the cursor, in input line
3087 void readline_backward_kill_word(void)
3088 {
3089 char *c, *old = ptr_inputline;
3090 int spaceallowed = 1;
3091
3092 if (ptr_inputline == inputLine) return;
3093
3094 c = prev_char(ptr_inputline, inputLine);
3095 for ( ; c > inputLine ; c = prev_char(c, inputLine)) {
3096 if (!iswalnum(get_char(c))) {
3097 if (iswblank(get_char(c))) {
3098 if (!spaceallowed) break;
3099 } else spaceallowed = 0;
3100 } else spaceallowed = 0;
3101 }
3102
3103 if (c == inputLine && *c == COMMAND_CHAR && old != c+1) {
3104 c = next_char(c);
3105 } else if (c != inputLine || iswblank(get_char(c))) {
3106 if ((c < prev_char(ptr_inputline, inputLine)) && (!iswalnum(get_char(c))))
3107 c = next_char(c);
3108 }
3109
3110 // Modify the line
3111 ptr_inputline = c;
3112 for (;;) {
3113 *c = *old++;
3114 if (!*c++) break;
3115 }
3116 check_offset(-1);
3117 }
3118
3119 // readline_backward_word()
3120 // Move back to the start of the current or previous word
3121 void readline_backward_word(void)
3122 {
3123 int i = 0;
3124
3125 if (ptr_inputline == inputLine) return;
3126
3127 if (iswalnum(get_char(ptr_inputline)) &&
3128 !iswalnum(get_char(prev_char(ptr_inputline, inputLine))))
3129 i--;
3130
3131 for ( ;
3132 ptr_inputline > inputLine;
3133 ptr_inputline = prev_char(ptr_inputline, inputLine)) {
3134 if (!iswalnum(get_char(ptr_inputline))) {
3135 if (i) {
3136 ptr_inputline = next_char(ptr_inputline);
3137 break;
3138 }
3139 } else i++;
3140 }
3141
3142 check_offset(-1);
3143 }
3144
3145 // readline_forward_word()
3146 // Move forward to the end of the next word
3147 void readline_forward_word(void)
3148 {
3149 int stopsymbol_allowed = 1;
3150
3151 while (*ptr_inputline) {
3152 if (!iswalnum(get_char(ptr_inputline))) {
3153 if (!stopsymbol_allowed) break;
3154 } else stopsymbol_allowed = 0;
3155 ptr_inputline = next_char(ptr_inputline);
3156 }
3157
3158 check_offset(1);
3159 }
3160
3161 void readline_updowncase_word(int upcase)
3162 {
3163 int stopsymbol_allowed = 1;
3164
3165 while (*ptr_inputline) {
3166 if (!iswalnum(get_char(ptr_inputline))) {
3167 if (!stopsymbol_allowed) break;
3168 } else {
3169 stopsymbol_allowed = 0;
3170 if (upcase)
3171 *ptr_inputline = towupper(get_char(ptr_inputline));
3172 else
3173 *ptr_inputline = towlower(get_char(ptr_inputline));
3174 }
3175 ptr_inputline = next_char(ptr_inputline);
3176 }
3177
3178 check_offset(1);
3179 }
3180
3181 void readline_capitalize_word(void)
3182 {
3183 int stopsymbol_allowed = 1;
3184 int upcased = 0;
3185
3186 while (*ptr_inputline) {
3187 if (!iswalnum(get_char(ptr_inputline))) {
3188 if (!stopsymbol_allowed) break;
3189 } else {
3190 stopsymbol_allowed = 0;
3191 if (!upcased) {
3192 *ptr_inputline = towupper(get_char(ptr_inputline));
3193 upcased = 1;
3194 } else *ptr_inputline = towlower(get_char(ptr_inputline));
3195 }
3196 ptr_inputline = next_char(ptr_inputline);
3197 }
3198
3199 check_offset(1);
3200 }
3201
3202 void readline_backward_char(void)
3203 {
3204 if (ptr_inputline == (char*)&inputLine) return;
3205
3206 ptr_inputline = prev_char(ptr_inputline, inputLine);
3207 check_offset(-1);
3208 }
3209
3210 void readline_forward_char(void)
3211 {
3212 if (!*ptr_inputline) return;
3213
3214 ptr_inputline = next_char(ptr_inputline);
3215 check_offset(1);
3216 }
3217
3218 // readline_accept_line(down_history)
3219 // Validate current command line.
3220 // If down_history is true, load the next history line.
3221 int readline_accept_line(int down_history)
3222 {
3223 scr_CheckAutoAway(TRUE);
3224 if (process_line(inputLine))
3225 return 255;
3226 // Add line to history
3227 scr_cmdhisto_addline(inputLine);
3228 // Reset the line
3229 ptr_inputline = inputLine;
3230 *ptr_inputline = 0;
3231 inputline_offset = 0;
3232
3233 if (down_history) {
3234 // Use next history line instead of a blank line
3235 const char *l = scr_cmdhisto_next("", 0);
3236 if (l) strcpy(inputLine, l);
3237 // Reset backup history line
3238 cmdhisto_backup[0] = 0;
3239 } else {
3240 // Reset history line pointer
3241 cmdhisto_cur = NULL;
3242 }
3243 return 0;
3244 }
3245
3246 void readline_cancel_completion(void)
3247 {
3248 scr_cancel_current_completion();
3249 scr_end_current_completion();
3250 check_offset(-1);
3251 }
3252
3253 void readline_do_completion(void)
3254 {
3255 int i, n;
3256
3257 if (multimode != 2) {
3258 // Not in verbatim multi-line mode
3259 scr_handle_tab();
3260 } else {
3261 // Verbatim multi-line mode: expand tab
3262 char tabstr[9];
3263 n = 8 - (ptr_inputline - inputLine) % 8;
3264 for (i = 0; i < n; i++)
3265 tabstr[i] = ' ';
3266 tabstr[i] = '\0';
3267 scr_insert_text(tabstr);
3268 }
3269 check_offset(0);
3270 }
3271
3272 void readline_refresh_screen(void)
3273 {
3274 scr_CheckAutoAway(TRUE);
3275 ParseColors();
3276 scr_Resize();
3277 redrawwin(stdscr);
3278 }
3279
3280 void readline_disable_chat_mode(guint show_roster)
3281 {
3282 scr_CheckAutoAway(TRUE);
3283 currentWindow = NULL;
3284 chatmode = FALSE;
3285 if (current_buddy)
3286 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
3287 if (show_roster)
3288 scr_RosterVisibility(1);
3289 scr_UpdateChatStatus(FALSE);
3290 top_panel(chatPanel);
3291 top_panel(inputPanel);
3292 update_panels();
3293 }
3294
3295 void readline_hist_beginning_search_bwd(void)
3296 {
3297 const char *l = scr_cmdhisto_prev(inputLine, ptr_inputline-inputLine);
3298 if (l) strcpy(inputLine, l);
3299 }
3300
3301 void readline_hist_beginning_search_fwd(void)
3302 {
3303 const char *l = scr_cmdhisto_next(inputLine, ptr_inputline-inputLine);
3304 if (l) strcpy(inputLine, l);
3305 }
3306
3307 void readline_hist_prev(void)
3308 {
3309 const char *l = scr_cmdhisto_prev(inputLine, 0);
3310 if (l) {
3311 strcpy(inputLine, l);
3312 // Set the pointer at the EOL.
3313 // We have to move it to BOL first, because we could be too far already.
3314 readline_iline_start();
3315 readline_iline_end();
3316 }
3317 }
3318
3319 void readline_hist_next(void)
3320 {
3321 const char *l = scr_cmdhisto_next(inputLine, 0);
3322 if (l) {
3323 strcpy(inputLine, l);
3324 // Set the pointer at the EOL.
3325 // We have to move it to BOL first, because we could be too far already.
3326 readline_iline_start();
3327 readline_iline_end();
3328 }
3329 }
3330
3331 void readline_backward_kill_char(void)
3332 {
3333 char *src, *c;
3334
3335 if (ptr_inputline == (char*)&inputLine)
3336 return;
3337
3338 src = ptr_inputline;
3339 c = prev_char(ptr_inputline, inputLine);
3340 ptr_inputline = c;
3341 for ( ; *src ; )
3342 *c++ = *src++;
3343 *c = 0;
3344 check_offset(-1);
3345 }
3346
3347 void readline_forward_kill_char(void)
3348 {
3349 if (!*ptr_inputline)
3350 return;
3351
3352 strcpy(ptr_inputline, next_char(ptr_inputline));
3353 }
3354
3355 void readline_iline_start(void)
3356 {
3357 ptr_inputline = inputLine;
3358 inputline_offset = 0;
3359 }
3360
3361 void readline_iline_end(void)
3362 {
3363 for (; *ptr_inputline; ptr_inputline++) ;
3364 check_offset(1);
3365 }
3366
3367 void readline_backward_kill_iline(void)
3368 {
3369 strcpy(inputLine, ptr_inputline);
3370 ptr_inputline = inputLine;
3371 inputline_offset = 0;
3372 }
3373
3374 void readline_forward_kill_iline(void)
3375 {
3376 *ptr_inputline = 0;
3377 }
3378
3379 void readline_send_multiline(void)
3380 {
3381 // Validate current multi-line
3382 if (multimode)
3383 process_command(mkcmdstr("msay send"), TRUE);
3384 }
3385
3386 // which_row()
3387 // Tells which row our cursor is in, in the command line.
3388 // -2 -> normal text
3389 // -1 -> room: nickname completion
3390 // 0 -> command
3391 // 1 -> parameter 1 (etc.)
3392 // If > 0, then *p_row is set to the beginning of the row
3393 static int which_row(const char **p_row)
3394 {
3395 int row = -1;
3396 char *p;
3397 int quote = FALSE;
3398
3399 // Not a command?
3400 if ((ptr_inputline == inputLine) || (inputLine[0] != COMMAND_CHAR)) {
3401 if (!current_buddy) return -2;
3402 if (buddy_gettype(BUDDATA(current_buddy)) == ROSTER_TYPE_ROOM) {
3403 *p_row = inputLine;
3404 return -1;
3405 }
3406 return -2;
3407 }
3408
3409 // This is a command
3410 row = 0;
3411 for (p = inputLine ; p < ptr_inputline ; p = next_char(p)) {
3412 if (quote) {
3413 if (*p == '"' && *(p-1) != '\\')
3414 quote = FALSE;
3415 continue;
3416 }
3417 if (*p == '"' && *(p-1) != '\\') {
3418 quote = TRUE;
3419 } else if (*p == ' ') {
3420 if (*(p-1) != ' ')
3421 row++;
3422 *p_row = p+1;
3423 }
3424 }
3425 return row;
3426 }
3427
3428 // scr_insert_text()
3429 // Insert the given text at the current cursor position.
3430 // The cursor is moved. We don't check if the cursor still is in the screen
3431 // after, the caller should do that.
3432 static void scr_insert_text(const char *text)
3433 {
3434 char tmpLine[INPUTLINE_LENGTH+1];
3435 int len = strlen(text);
3436 // Check the line isn't too long
3437 if (strlen(inputLine) + len >= INPUTLINE_LENGTH) {
3438 scr_LogPrint(LPRINT_LOGNORM, "Cannot insert text, line too long.");
3439 return;
3440 }
3441
3442 strcpy(tmpLine, ptr_inputline);
3443 strcpy(ptr_inputline, text);
3444 ptr_inputline += len;
3445 strcpy(ptr_inputline, tmpLine);
3446 }
3447
3448 static void scr_cancel_current_completion(void);
3449
3450 // scr_handle_tab()
3451 // Function called when tab is pressed.
3452 // Initiate or continue a completion...
3453 static void scr_handle_tab(void)
3454 {
3455 int nrow;
3456 const char *row;
3457 const char *cchar;
3458 guint compl_categ;
3459
3460 row = inputLine; // (Kills a GCC warning)
3461 nrow = which_row(&row);
3462
3463 // a) No completion if no leading slash ('cause not a command),
3464 // unless this is a room (then, it is a nickname completion)
3465 // b) We can't have more than 2 parameters (we use 2 flags)
3466 if ((nrow == -2) || (nrow == 3 && !completion_started) || nrow > 3)
3467 return;
3468
3469 if (nrow == 0) { // Command completion
3470 row = next_char(inputLine);
3471 compl_categ = COMPL_CMD;
3472 } else if (nrow == -1) { // Nickname completion
3473 compl_categ = COMPL_RESOURCE;
3474 } else { // Other completion, depending on the command
3475 int alias = FALSE;
3476 cmd *com;
3477 char *xpline = expandalias(inputLine);
3478 com = cmd_get(xpline);
3479 if (xpline != inputLine) {
3480 // This is an alias, so we can't complete rows > 0
3481 alias = TRUE;
3482 g_free(xpline);
3483 }
3484 if ((!com && (!alias || !completion_started)) || !row) {
3485 scr_LogPrint(LPRINT_NORMAL, "I cannot complete that...");
3486 return;
3487 }
3488 if (!alias)
3489 compl_categ = com->completion_flags[nrow-1];
3490 else
3491 compl_categ = 0;
3492 }
3493
3494 if (!completion_started) {
3495 guint dynlist;
3496 GSList *list = compl_get_category_list(compl_categ, &dynlist);
3497 if (list) {
3498 guint n;
3499 char *prefix = g_strndup(row, ptr_inputline-row);
3500 // Init completion
3501 n = new_completion(prefix, list);
3502 g_free(prefix);
3503 if (n == 0 && nrow == -1) {
3504 // This is a MUC room and we can't complete from the beginning of the
3505 // line. Let's try a bit harder and complete the current word.
3506 row = prev_char(ptr_inputline, inputLine);
3507 while (row >= inputLine) {
3508 if (iswspace(get_char(row)) || get_char(row) == '(') {
3509 row = next_char((char*)row);
3510 break;
3511 }
3512 if (row == inputLine)
3513 break;
3514 row = prev_char((char*)row, inputLine);
3515 }
3516 // There's no need to try again if row == inputLine
3517 if (row > inputLine) {
3518 prefix = g_strndup(row, ptr_inputline-row);
3519 new_completion(prefix, list);
3520 g_free(prefix);
3521 }
3522 }
3523 // Free the list if it's a dynamic one
3524 if (dynlist) {
3525 GSList *slp;
3526 for (slp = list; slp; slp = g_slist_next(slp))
3527 g_free(slp->data);
3528 g_slist_free(list);
3529 }
3530 // Now complete
3531 cchar = complete();
3532 if (cchar)
3533 scr_insert_text(cchar);
3534 completion_started = TRUE;
3535 }
3536 } else { // Completion already initialized
3537 scr_cancel_current_completion();
3538 // Now complete again
3539 cchar = complete();
3540 if (cchar)
3541 scr_insert_text(cchar);
3542 }
3543 }
3544
3545 static void scr_cancel_current_completion(void)
3546 {
3547 char *c;
3548 char *src = ptr_inputline;
3549 guint back = cancel_completion();
3550 guint i;
3551 // Remove $back chars
3552 for (i = 0; i < back; i++)
3553 ptr_inputline = prev_char(ptr_inputline, inputLine);
3554 c = ptr_inputline;
3555 for ( ; *src ; )
3556 *c++ = *src++;
3557 *c = 0;
3558 }
3559
3560 static void scr_end_current_completion(void)
3561 {
3562 done_completion();
3563 completion_started = FALSE;
3564 }
3565
3566 // check_offset(int direction)
3567 // Check inputline_offset value, and make sure the cursor is inside the
3568 // screen.
3569 static inline void check_offset(int direction)
3570 {
3571 int i;
3572 char *c = &inputLine[inputline_offset];
3573 // Left side
3574 if (inputline_offset && direction <= 0) {
3575 while (ptr_inputline <= c) {
3576 for (i = 0; i < 5; i++)
3577 c = prev_char(c, inputLine);
3578 if (c == inputLine)
3579 break;
3580 }
3581 }
3582 // Right side
3583 if (direction >= 0) {
3584 int delta = get_char_width(c);
3585 while (ptr_inputline > c) {
3586 c = next_char(c);
3587 delta += get_char_width(c);
3588 }
3589 c = &inputLine[inputline_offset];
3590 while (delta >= maxX) {
3591 for (i = 0; i < 5; i++) {
3592 delta -= get_char_width(c);
3593 c = next_char(c);
3594 }
3595 }
3596 }
3597 inputline_offset = c - inputLine;
3598 }
3599
3600 #if defined(WITH_ENCHANT) || defined(WITH_ASPELL)
3601 // prints inputLine with underlined words when misspelled
3602 static inline void print_checked_line(void)
3603 {
3604 char *wprint_char_fmt = "%c";
3605 int point;
3606 int nrchar = maxX;
3607 char *ptrCur = inputLine + inputline_offset;
3608
3609 #ifdef UNICODE
3610 // We need this to display a single UTF-8 char... Any better solution?
3611 if (utf8_mode)
3612 wprint_char_fmt = "%lc";
3613 #endif
3614
3615 wmove(inputWnd, 0, 0); // problem with backspace
3616
3617 while (*ptrCur && nrchar-- > 0) {
3618 point = ptrCur - inputLine;
3619 if (maskLine[point])
3620 wattrset(inputWnd, A_UNDERLINE);
3621 wprintw(inputWnd, wprint_char_fmt, get_char(ptrCur));
3622 wattrset(inputWnd, A_NORMAL);
3623 ptrCur = next_char(ptrCur);
3624 }
3625 }
3626 #endif
3627
3628 static inline void refresh_inputline(void)
3629 {
3630 #if defined(WITH_ENCHANT) || defined(WITH_ASPELL)
3631 if (settings_opt_get_int("spell_enable")) {
3632 memset(maskLine, 0, INPUTLINE_LENGTH+1);
3633 spellcheck(inputLine, maskLine);
3634 }
3635 print_checked_line();
3636 wclrtoeol(inputWnd);
3637 if (*ptr_inputline) {
3638 // hack to set cursor pos. Characters can have different width,
3639 // so I know of no better way.
3640 char c = *ptr_inputline;
3641 *ptr_inputline = 0;
3642 print_checked_line();
3643 *ptr_inputline = c;
3644 }
3645 #else
3646 mvwprintw(inputWnd, 0, 0, "%s", inputLine + inputline_offset);
3647 wclrtoeol(inputWnd);
3648 if (*ptr_inputline) {
3649 // hack to set cursor pos. Characters can have different width,
3650 // so I know of no better way.
3651 char c = *ptr_inputline;
3652 *ptr_inputline = 0;
3653 mvwprintw(inputWnd, 0, 0, "%s", inputLine + inputline_offset);
3654 *ptr_inputline = c;
3655 }
3656 #endif
3657 }
3658
3659 void scr_handle_CtrlC(void)
3660 {
3661 if (!Curses) return;
3662 // Leave multi-line mode
3663 process_command(mkcmdstr("msay abort"), TRUE);
3664 // Same as Ctrl-g, now
3665 scr_cancel_current_completion();
3666 scr_end_current_completion();
3667 check_offset(-1);
3668 refresh_inputline();
3669 }
3670
3671 static void add_keyseq(char *seqstr, guint mkeycode, gint value)
3672 {
3673 keyseq *ks;
3674
3675 // Let's make sure the length is correct
3676 if (strlen(seqstr) > MAX_KEYSEQ_LENGTH) {
3677 scr_LogPrint(LPRINT_LOGNORM, "add_keyseq(): key sequence is too long!");
3678 return;
3679 }
3680
3681 ks = g_new0(keyseq, 1);
3682 ks->seqstr = g_strdup(seqstr);
3683 ks->mkeycode = mkeycode;
3684 ks->value = value;
3685 keyseqlist = g_slist_append(keyseqlist, ks);
3686 }
3687
3688 // match_keyseq(iseq, &ret)
3689 // Check if "iseq" is a known key escape sequence.
3690 // Return value:
3691 // -1 if "seq" matches no known sequence
3692 // 0 if "seq" could match 1 or more known sequences
3693 // >0 if "seq" matches a key sequence; the mkey code is returned
3694 // and *ret is set to the matching keyseq structure.
3695 static inline gint match_keyseq(int *iseq, keyseq **ret)
3696 {
3697 GSList *ksl;
3698 keyseq *ksp;
3699 char *p, c;
3700 int *i;
3701 int needmore = FALSE;
3702
3703 for (ksl = keyseqlist; ksl; ksl = g_slist_next(ksl)) {
3704 ksp = ksl->data;
3705 p = ksp->seqstr;
3706 i = iseq;
3707 while (1) {
3708 c = (unsigned char)*i;
3709 if (!*p && !c) { // Match
3710 (*ret) = ksp;
3711 return ksp->mkeycode;
3712 }
3713 if (!c) {
3714 // iseq is too short
3715 needmore = TRUE;
3716 break;
3717 } else if (!*p || c != *p) {
3718 // This isn't a match
3719 break;
3720 }
3721 p++; i++;
3722 }
3723 }
3724
3725 if (needmore)
3726 return 0;
3727 return -1;
3728 }
3729
3730 static inline int match_utf8_keyseq(int *iseq)
3731 {
3732 int *strp = iseq;
3733 unsigned c = *strp++;
3734 unsigned mask = 0x80;
3735 int len = -1;
3736 while (c & mask) {
3737 mask >>= 1;
3738 len++;
3739 }
3740 if (len <= 0 || len > 4)
3741 return -1;
3742 c &= mask - 1;
3743 while ((*strp & 0xc0) == 0x80) {
3744 if (len-- <= 0) // can't happen
3745 return -1;
3746 c = (c << 6) | (*strp++ & 0x3f);
3747 }
3748 if (len)
3749 return 0;
3750 return c;
3751 }
3752
3753 void scr_Getch(keycode *kcode)
3754 {
3755 keyseq *mks = NULL;
3756 int ks[MAX_KEYSEQ_LENGTH+1];
3757 int i;
3758
3759 memset(kcode, 0, sizeof(keycode));
3760 memset(ks, 0, sizeof(ks));
3761
3762 kcode->value = wgetch(inputWnd);
3763 if (utf8_mode) {
3764 bool ismeta = (kcode->value == 27);
3765 #ifdef NCURSES_MOUSE_VERSION
3766 bool ismouse = (kcode->value == KEY_MOUSE);
3767
3768 if (ismouse) {
3769 MEVENT mouse;
3770 getmouse(&mouse);
3771 kcode->value = mouse.bstate;
3772 kcode->mcode = MKEY_MOUSE;
3773 return;
3774 } else if (ismeta)
3775 #else
3776 if (ismeta)
3777 #endif
3778 ks[0] = wgetch(inputWnd);
3779 else
3780 ks[0] = kcode->value;
3781
3782 for (i = 0; i < MAX_KEYSEQ_LENGTH - 1; i++) {
3783 int match = match_utf8_keyseq(ks);
3784 if (match == -1)
3785 break;
3786 if (match > 0) {
3787 kcode->value = match;
3788 kcode->utf8 = 1;
3789 if (ismeta)
3790 kcode->mcode = MKEY_META;
3791 return;
3792 }
3793 ks[i + 1] = wgetch(inputWnd);
3794 if (ks[i + 1] == ERR)
3795 break;
3796 }
3797 while (i > 0)
3798 ungetch(ks[i--]);
3799 if (ismeta)
3800 ungetch(ks[0]);
3801 memset(ks, 0, sizeof(ks));
3802 }
3803 if (kcode->value != 27)
3804 return;
3805
3806 // Check for escape key sequence
3807 for (i=0; i < MAX_KEYSEQ_LENGTH; i++) {
3808 int match;
3809 ks[i] = wgetch(inputWnd);
3810 if (ks[i] == ERR) break;
3811 match = match_keyseq(ks, &mks);
3812 if (match == -1) {
3813 // No such key sequence. Let's increment i as it is a valid key.
3814 i++;
3815 break;
3816 }
3817 if (match > 0) {
3818 // We have a matching sequence
3819 kcode->mcode = mks->mkeycode;
3820 kcode->value = mks->value;
3821 return;
3822 }
3823 }
3824
3825 // No match. Let's return a meta-key.
3826 if (i > 0) {
3827 kcode->mcode = MKEY_META;
3828 kcode->value = ks[0];
3829 }
3830 if (i > 1) {
3831 // We need to push some keys back to the keyboard buffer
3832 while (i-- > 1)
3833 ungetch(ks[i]);
3834 }
3835 return;
3836 }
3837
3838 void scr_DoUpdate(void)
3839 {
3840 doupdate();
3841 }
3842
3843 static int bindcommand(keycode kcode)
3844 {
3845 gchar asciikey[16], asciicode[16];
3846 const gchar *boundcmd;
3847
3848 if (kcode.utf8)
3849 g_snprintf(asciicode, 15, "U%d", kcode.value);
3850 else
3851 g_snprintf(asciicode, 15, "%d", kcode.value);
3852
3853 if (!kcode.mcode || kcode.mcode == MKEY_EQUIV)
3854 g_snprintf(asciikey, 15, "%s", asciicode);
3855 else if (kcode.mcode == MKEY_META)
3856 g_snprintf(asciikey, 15, "M%s", asciicode);
3857 else if (kcode.mcode == MKEY_MOUSE)
3858 g_snprintf(asciikey, 15, "p%s", asciicode);
3859 else
3860 g_snprintf(asciikey, 15, "MK%d", kcode.mcode);
3861
3862 boundcmd = settings_get(SETTINGS_TYPE_BINDING, asciikey);
3863
3864 if (boundcmd) {
3865 gchar *cmdline = from_utf8(boundcmd);
3866 scr_CheckAutoAway(TRUE);
3867 if (process_command(cmdline, TRUE))
3868 return 255; // Quit
3869 g_free(cmdline);
3870 return 0;
3871 }
3872
3873 scr_LogPrint(LPRINT_NORMAL, "Unknown key=%s", asciikey);
3874 #ifndef UNICODE
3875 if (utf8_mode)
3876 scr_LogPrint(LPRINT_NORMAL,
3877 "WARNING: Compiled without full UTF-8 support!");
3878 #endif
3879 return -1;
3880 }
3881
3882 // process_key(key)
3883 // Handle the pressed key, in the command line (bottom).
3884 void process_key(keycode kcode)
3885 {
3886 int key = kcode.value;
3887 int display_char = FALSE;
3888
3889 lock_chatstate = FALSE;
3890
3891 switch (kcode.mcode) {
3892 case 0:
3893 break;
3894 case MKEY_EQUIV:
3895 key = kcode.value;
3896 break;
3897 case MKEY_META:
3898 default:
3899 if (bindcommand(kcode) == 255) {
3900 mcabber_set_terminate_ui();
3901 return;
3902 }
3903 key = ERR; // Do not process any further
3904 }
3905
3906 if (kcode.utf8) {
3907 if (key != ERR && !kcode.mcode)
3908 display_char = TRUE;
3909 goto display;
3910 }
3911
3912 switch (key) {
3913 case 0:
3914 case ERR:
3915 break;
3916 case 9: // Tab
3917 readline_do_completion();
3918 break;
3919 case 13: // Enter
3920 if (readline_accept_line(FALSE) == 255) {
3921 mcabber_set_terminate_ui();
3922 return;
3923 }
3924 break;
3925 case 3: // Ctrl-C
3926 scr_handle_CtrlC();
3927 break;
3928 case KEY_RESIZE:
3929 #ifdef USE_SIGWINCH
3930 {
3931 struct winsize size;
3932 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &size) != -1)
3933 resizeterm(size.ws_row, size.ws_col);
3934 }
3935 #endif
3936 scr_Resize();
3937 break;
3938 default:
3939 display_char = TRUE;
3940 } // switch
3941
3942 display:
3943 if (display_char) {
3944 guint printable;
3945
3946 if (kcode.utf8) {
3947 printable = iswprint(key);
3948 } else {
3949 #ifdef __CYGWIN__
3950 printable = (isprint(key) || (key >= 161 && key <= 255))
3951 && !is_speckey(key);
3952 #else
3953 printable = isprint(key) && !is_speckey(key);
3954 #endif
3955 }
3956 if (printable) {
3957 char tmpLine[INPUTLINE_LENGTH+1];
3958
3959 // Check the line isn't too long
3960 if (strlen(inputLine) + 4 > INPUTLINE_LENGTH)
3961 return;
3962
3963 // Insert char
3964 strcpy(tmpLine, ptr_inputline);
3965 ptr_inputline = put_char(ptr_inputline, key);
3966 strcpy(ptr_inputline, tmpLine);
3967 check_offset(1);
3968 } else {
3969 // Look for a key binding.
3970 if (!kcode.utf8 && (bindcommand(kcode) == 255)) {
3971 mcabber_set_terminate_ui();
3972 return;
3973 }
3974 }
3975 }
3976
3977 if (completion_started && key != 9 && key != KEY_RESIZE)
3978 scr_end_current_completion();
3979 refresh_inputline();
3980
3981 if (!lock_chatstate) {
3982 // Set chat state to composing (1) if the user is currently composing,
3983 // i.e. not an empty line and not a command line.
3984 if (inputLine[0] == 0 || inputLine[0] == COMMAND_CHAR)
3985 set_chatstate(0);
3986 else
3987 set_chatstate(1);
3988 if (chatstate)
3989 time(&chatstate_timestamp);
3990 }
3991 return;
3992 }
3993
3994 #if defined(WITH_ENCHANT) || defined(WITH_ASPELL)
3995 // initialization
3996 void spellcheck_init(void)
3997 {
3998 int spell_enable = settings_opt_get_int("spell_enable");
3999 const char *spell_lang = settings_opt_get("spell_lang");
4000 #ifdef WITH_ASPELL
4001 const char *spell_encoding = settings_opt_get("spell_encoding");
4002 AspellCanHaveError *possible_err;
4003 #endif
4004
4005 if (!spell_enable)
4006 return;
4007
4008 #ifdef WITH_ENCHANT
4009 if (spell_checker) {
4010 enchant_broker_free_dict(spell_broker, spell_checker);
4011 enchant_broker_free(spell_broker);
4012 spell_checker = NULL;
4013 spell_broker = NULL;
4014 }
4015
4016 spell_broker = enchant_broker_init();
4017 spell_checker = enchant_broker_request_dict(spell_broker, spell_lang);
4018 #endif
4019 #ifdef WITH_ASPELL
4020 if (spell_checker) {
4021 delete_aspell_speller(spell_checker);
4022 delete_aspell_config(spell_config);
4023 spell_checker = NULL;
4024 spell_config = NULL;
4025 }
4026
4027 spell_config = new_aspell_config();
4028 aspell_config_replace(spell_config, "encoding", spell_encoding);
4029 aspell_config_replace(spell_config, "lang", spell_lang);
4030 possible_err = new_aspell_speller(spell_config);
4031
4032 if (aspell_error_number(possible_err) != 0) {
4033 spell_checker = NULL;
4034 delete_aspell_config(spell_config);
4035 spell_config = NULL;
4036 } else {
4037 spell_checker = to_aspell_speller(possible_err);
4038 }
4039 #endif
4040 }
4041
4042 // Deinitialization of spellchecker
4043 void spellcheck_deinit(void)
4044 {
4045 if (spell_checker) {
4046 #ifdef WITH_ENCHANT
4047 enchant_broker_free_dict(spell_broker, spell_checker);
4048 #endif
4049 #ifdef WITH_ASPELL
4050 delete_aspell_speller(spell_checker);
4051 #endif
4052 spell_checker = NULL;
4053 }
4054
4055 #ifdef WITH_ENCHANT
4056 if (spell_broker) {
4057 enchant_broker_free(spell_broker);
4058 spell_broker = NULL;
4059 }
4060 #endif
4061 #ifdef WITH_ASPELL
4062 if (spell_config) {
4063 delete_aspell_config(spell_config);
4064 spell_config = NULL;
4065 }
4066 #endif
4067 }
4068
4069 #define spell_isalpha(c) (utf8_mode ? iswalpha(get_char(c)) : isalpha(*c))
4070
4071 // Spell checking function
4072 static void spellcheck(char *line, char *checked)
4073 {
4074 const char *start, *line_start;
4075
4076 if (inputLine[0] == 0 || inputLine[0] == COMMAND_CHAR)
4077 return;
4078
4079 line_start = line;
4080
4081 while (*line) {
4082
4083 if (!spell_isalpha(line)) {
4084 line = next_char(line);
4085 continue;
4086 }
4087
4088 if (!strncmp(line, "http://", 7)) {
4089 line += 7; // : and / characters are 1 byte long in utf8, right?
4090
4091 while (!strchr(" \t\r\n", *line))
4092 line = next_char(line); // i think line++ would be fine here?
4093
4094 continue;
4095 }
4096
4097 if (!strncmp(line, "ftp://", 6)) {
4098 line += 6;
4099
4100 while (!strchr(" \t\r\n", *line))
4101 line = next_char(line);
4102
4103 continue;
4104 }
4105
4106 start = line;
4107
4108 while (spell_isalpha(line))
4109 line = next_char(line);
4110
4111 if (spell_checker &&
4112 #ifdef WITH_ENCHANT
4113 enchant_dict_check(spell_checker, start, line - start) != 0
4114 #endif
4115 #ifdef WITH_ASPELL
4116 aspell_speller_check(spell_checker, start, line - start) == 0
4117 #endif
4118 )
4119 memset(&checked[start - line_start], SPELLBADCHAR, line - start);
4120 }
4121 }
4122 #endif
4123
4124 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */