1 /*
2 * core.c
3 * vim: expandtab:ts=4:sts=4:sw=4
4 *
5 * Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
6 *
7 * This file is part of Profanity.
8 *
9 * Profanity is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Profanity is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Profanity. If not, see <https://www.gnu.org/licenses/>.
21 *
22 * In addition, as a special exception, the copyright holders give permission to
23 * link the code of portions of this program with the OpenSSL library under
24 * certain conditions as described in each individual source file, and
25 * distribute linked combinations including the two.
26 *
27 * You must obey the GNU General Public License in all respects for all of the
28 * code used other than OpenSSL. If you modify file(s) with this exception, you
29 * may extend this exception to your version of the file(s), but you are not
30 * obligated to do so. If you do not wish to do so, delete this exception
31 * statement from your version. If you delete this exception statement from all
32 * source files in the program, then also delete it here.
33 *
34 */
35
36 #include "config.h"
37
38 #ifdef HAVE_GIT_VERSION
39 #include "gitversion.h"
40 #endif
41
42 #include <stdlib.h>
43 #include <string.h>
44 #include <assert.h>
45 #include <sys/ioctl.h>
46 #include <unistd.h>
47
48 #include <glib.h>
49
50 #ifdef HAVE_LIBXSS
51 #include <X11/extensions/scrnsaver.h>
52 #endif
53
54 #ifdef HAVE_NCURSESW_NCURSES_H
55 #include <ncursesw/ncurses.h>
56 #elif HAVE_NCURSES_H
57 #include <ncurses.h>
58 #elif HAVE_CURSES_H
59 #include <curses.h>
60 #endif
61
62 #include "log.h"
63 #include "common.h"
64 #include "command/cmd_defs.h"
65 #include "command/cmd_ac.h"
66 #include "config/preferences.h"
67 #include "config/theme.h"
68 #include "ui/ui.h"
69 #include "ui/titlebar.h"
70 #include "ui/statusbar.h"
71 #include "ui/inputwin.h"
72 #include "ui/window.h"
73 #include "ui/window_list.h"
74 #include "xmpp/xmpp.h"
75 #include "xmpp/muc.h"
76 #include "xmpp/chat_session.h"
77 #include "xmpp/contact.h"
78 #include "xmpp/roster_list.h"
79 #include "xmpp/jid.h"
80
81 #ifdef HAVE_LIBOTR
82 #include "otr/otr.h"
83 #endif
84
85 static int inp_size;
86 static gboolean perform_resize = FALSE;
87 static GTimer* ui_idle_time;
88
89 #ifdef HAVE_LIBXSS
90 static Display* display;
91 #endif
92
93 static void _ui_draw_term_title(void);
94
95 void
ui_init(void)96 ui_init(void)
97 {
98 log_info("Initialising UI");
99 initscr();
100 nonl();
101 cbreak();
102 noecho();
103 keypad(stdscr, TRUE);
104 ui_load_colours();
105 refresh();
106 create_title_bar();
107 status_bar_init();
108 status_bar_active(1, WIN_CONSOLE, "console");
109 create_input_window();
110 wins_init();
111 notifier_initialise();
112 cons_about();
113 #ifdef HAVE_LIBXSS
114 display = XOpenDisplay(0);
115 #endif
116 ui_idle_time = g_timer_new();
117 inp_size = 0;
118 ProfWin* window = wins_get_current();
119 win_update_virtual(window);
120 }
121
122 void
ui_sigwinch_handler(int sig)123 ui_sigwinch_handler(int sig)
124 {
125 perform_resize = TRUE;
126 }
127
128 void
ui_update(void)129 ui_update(void)
130 {
131 ProfWin* current = wins_get_current();
132 if (current->layout->paged == 0) {
133 win_move_to_end(current);
134 }
135
136 win_update_virtual(current);
137
138 if (prefs_get_boolean(PREF_WINTITLE_SHOW)) {
139 _ui_draw_term_title();
140 }
141 title_bar_update_virtual();
142 status_bar_draw();
143 inp_put_back();
144 doupdate();
145
146 if (perform_resize) {
147 signal(SIGWINCH, SIG_IGN);
148 ui_resize();
149 perform_resize = FALSE;
150 signal(SIGWINCH, ui_sigwinch_handler);
151 }
152 }
153
154 unsigned long
ui_get_idle_time(void)155 ui_get_idle_time(void)
156 {
157 // if compiled with libxss, get the x sessions idle time
158 #ifdef HAVE_LIBXSS
159 XScreenSaverInfo* info = XScreenSaverAllocInfo();
160 if (info && display) {
161 XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
162 unsigned long result = info->idle;
163 XFree(info);
164 return result;
165 }
166 if (info) {
167 XFree(info);
168 }
169 // if no libxss or xss idle time failed, use profanity idle time
170 #endif
171 gdouble seconds_elapsed = g_timer_elapsed(ui_idle_time, NULL);
172 unsigned long ms_elapsed = seconds_elapsed * 1000.0;
173 return ms_elapsed;
174 }
175
176 void
ui_reset_idle_time(void)177 ui_reset_idle_time(void)
178 {
179 g_timer_start(ui_idle_time);
180 }
181
182 void
ui_close(void)183 ui_close(void)
184 {
185 notifier_uninit();
186 cons_clear_alerts();
187 wins_destroy();
188 inp_close();
189 status_bar_close();
190 endwin();
191 }
192
193 void
ui_resize(void)194 ui_resize(void)
195 {
196 struct winsize w;
197 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
198 erase();
199 resizeterm(w.ws_row, w.ws_col);
200 refresh();
201
202 log_debug("Resizing UI");
203 title_bar_resize();
204 wins_resize_all();
205 status_bar_resize();
206 inp_win_resize();
207 ProfWin* window = wins_get_current();
208 win_update_virtual(window);
209 }
210
211 void
ui_redraw(void)212 ui_redraw(void)
213 {
214 title_bar_resize();
215 wins_resize_all();
216 status_bar_resize();
217 inp_win_resize();
218 }
219
220 void
ui_load_colours(void)221 ui_load_colours(void)
222 {
223 if (has_colors()) {
224 use_default_colors();
225 start_color();
226 theme_init_colours();
227 }
228 }
229
230 void
ui_contact_online(char * barejid,Resource * resource,GDateTime * last_activity)231 ui_contact_online(char* barejid, Resource* resource, GDateTime* last_activity)
232 {
233 char* show_console = prefs_get_string(PREF_STATUSES_CONSOLE);
234 char* show_chat_win = prefs_get_string(PREF_STATUSES_CHAT);
235 PContact contact = roster_get_contact(barejid);
236
237 // show nothing
238 if (g_strcmp0(p_contact_subscription(contact), "none") == 0) {
239 free(show_console);
240 free(show_chat_win);
241 return;
242 }
243
244 // show in console if "all"
245 if (g_strcmp0(show_console, "all") == 0) {
246 cons_show_contact_online(contact, resource, last_activity);
247
248 // show in console of "online" and presence online
249 } else if (g_strcmp0(show_console, "online") == 0 && resource->presence == RESOURCE_ONLINE) {
250 cons_show_contact_online(contact, resource, last_activity);
251 }
252
253 // show in chat win if "all"
254 if (g_strcmp0(show_chat_win, "all") == 0) {
255 ProfChatWin* chatwin = wins_get_chat(barejid);
256 if (chatwin) {
257 chatwin_contact_online(chatwin, resource, last_activity);
258 }
259
260 // show in char win if "online" and presence online
261 } else if (g_strcmp0(show_chat_win, "online") == 0 && resource->presence == RESOURCE_ONLINE) {
262 ProfChatWin* chatwin = wins_get_chat(barejid);
263 if (chatwin) {
264 chatwin_contact_online(chatwin, resource, last_activity);
265 }
266 }
267
268 free(show_console);
269 free(show_chat_win);
270 }
271
272 void
ui_contact_typing(const char * const barejid,const char * const resource)273 ui_contact_typing(const char* const barejid, const char* const resource)
274 {
275 ProfChatWin* chatwin = wins_get_chat(barejid);
276 ProfWin* window = (ProfWin*)chatwin;
277 ChatSession* session = chat_session_get(barejid);
278
279 // no chat window for user
280 if (chatwin == NULL) {
281 if (prefs_get_boolean(PREF_INTYPE_CONSOLE)) {
282 cons_show_typing(barejid);
283 }
284
285 // have chat window but not currently in it
286 } else if (!wins_is_current(window)) {
287 if (prefs_get_boolean(PREF_INTYPE_CONSOLE)) {
288 cons_show_typing(barejid);
289 }
290
291 // in chat window with user, no session or session with resource
292 } else if (!session || (session && g_strcmp0(session->resource, resource) == 0)) {
293 if (prefs_get_boolean(PREF_INTYPE)) {
294 title_bar_set_typing(TRUE);
295
296 int num = wins_get_num(window);
297 status_bar_active(num, WIN_CHAT, chatwin->barejid);
298 }
299 }
300
301 if (prefs_get_boolean(PREF_NOTIFY_TYPING)) {
302 gboolean is_current = FALSE;
303 if (window) {
304 is_current = wins_is_current(window);
305 }
306 if (!is_current || (is_current && prefs_get_boolean(PREF_NOTIFY_TYPING_CURRENT))) {
307 PContact contact = roster_get_contact(barejid);
308 char const* display_usr = NULL;
309 if (contact) {
310 if (p_contact_name(contact)) {
311 display_usr = p_contact_name(contact);
312 } else {
313 display_usr = barejid;
314 }
315 } else {
316 display_usr = barejid;
317 }
318 notify_typing(display_usr);
319 }
320 }
321 }
322
323 void
ui_roster_add(const char * const barejid,const char * const name)324 ui_roster_add(const char* const barejid, const char* const name)
325 {
326 if (name) {
327 cons_show("Roster item added: %s (%s)", barejid, name);
328 } else {
329 cons_show("Roster item added: %s", barejid);
330 }
331 rosterwin_roster();
332 }
333
334 void
ui_roster_remove(const char * const barejid)335 ui_roster_remove(const char* const barejid)
336 {
337 cons_show("Roster item removed: %s", barejid);
338 rosterwin_roster();
339 }
340
341 void
ui_contact_already_in_group(const char * const contact,const char * const group)342 ui_contact_already_in_group(const char* const contact, const char* const group)
343 {
344 cons_show("%s already in group %s", contact, group);
345 rosterwin_roster();
346 }
347
348 void
ui_contact_not_in_group(const char * const contact,const char * const group)349 ui_contact_not_in_group(const char* const contact, const char* const group)
350 {
351 cons_show("%s is not currently in group %s", contact, group);
352 rosterwin_roster();
353 }
354
355 void
ui_group_added(const char * const contact,const char * const group)356 ui_group_added(const char* const contact, const char* const group)
357 {
358 cons_show("%s added to group %s", contact, group);
359 rosterwin_roster();
360 }
361
362 void
ui_group_removed(const char * const contact,const char * const group)363 ui_group_removed(const char* const contact, const char* const group)
364 {
365 cons_show("%s removed from group %s", contact, group);
366 rosterwin_roster();
367 }
368
369 void
ui_handle_login_account_success(ProfAccount * account,gboolean secured)370 ui_handle_login_account_success(ProfAccount* account, gboolean secured)
371 {
372 if (account->theme) {
373 if (theme_load(account->theme, false)) {
374 ui_load_colours();
375 if (prefs_get_boolean(PREF_ROSTER)) {
376 ui_show_roster();
377 } else {
378 ui_hide_roster();
379 }
380 if (prefs_get_boolean(PREF_OCCUPANTS)) {
381 ui_show_all_room_rosters();
382 } else {
383 ui_hide_all_room_rosters();
384 }
385 ui_resize();
386 } else {
387 cons_show("Couldn't find account theme: %s", account->theme);
388 }
389 }
390
391 resource_presence_t resource_presence = accounts_get_login_presence(account->name);
392 contact_presence_t contact_presence = contact_presence_from_resource_presence(resource_presence);
393 cons_show_login_success(account, secured);
394 title_bar_set_presence(contact_presence);
395 title_bar_set_connected(TRUE);
396 title_bar_set_tls(secured);
397
398 status_bar_set_fulljid(connection_get_fulljid());
399 }
400
401 void
ui_update_presence(const resource_presence_t resource_presence,const char * const message,const char * const show)402 ui_update_presence(const resource_presence_t resource_presence,
403 const char* const message, const char* const show)
404 {
405 contact_presence_t contact_presence = contact_presence_from_resource_presence(resource_presence);
406 title_bar_set_presence(contact_presence);
407 gint priority = accounts_get_priority_for_presence_type(session_get_account_name(), resource_presence);
408 if (message) {
409 cons_show("Status set to %s (priority %d), \"%s\".", show, priority, message);
410 } else {
411 cons_show("Status set to %s (priority %d).", show, priority);
412 }
413 }
414
415 void
ui_handle_recipient_error(const char * const recipient,const char * const err_msg)416 ui_handle_recipient_error(const char* const recipient, const char* const err_msg)
417 {
418 // always show in console
419 cons_show_error("Error from %s: %s", recipient, err_msg);
420
421 ProfChatWin* chatwin = wins_get_chat(recipient);
422 if (chatwin) {
423 win_println((ProfWin*)chatwin, THEME_ERROR, "!", "Error from %s: %s", recipient, err_msg);
424 return;
425 }
426
427 ProfMucWin* mucwin = wins_get_muc(recipient);
428 if (mucwin) {
429 win_println((ProfWin*)mucwin, THEME_ERROR, "!", "Error from %s: %s", recipient, err_msg);
430 return;
431 }
432
433 ProfPrivateWin* privatewin = wins_get_private(recipient);
434 if (privatewin) {
435 win_println((ProfWin*)privatewin, THEME_ERROR, "!", "Error from %s: %s", recipient, err_msg);
436 return;
437 }
438 }
439
440 void
ui_handle_otr_error(const char * const barejid,const char * const message)441 ui_handle_otr_error(const char* const barejid, const char* const message)
442 {
443 ProfChatWin* chatwin = wins_get_chat(barejid);
444 if (chatwin) {
445 win_println((ProfWin*)chatwin, THEME_ERROR, "!", "%s", message);
446 } else {
447 cons_show_error("%s - %s", barejid, message);
448 }
449 }
450
451 void
ui_handle_error(const char * const err_msg)452 ui_handle_error(const char* const err_msg)
453 {
454 GString* msg = g_string_new("");
455 g_string_printf(msg, "Error %s", err_msg);
456
457 cons_show_error(msg->str);
458
459 g_string_free(msg, TRUE);
460 }
461
462 void
ui_invalid_command_usage(const char * const cmd,void (* setting_func)(void))463 ui_invalid_command_usage(const char* const cmd, void (*setting_func)(void))
464 {
465 GString* msg = g_string_new("");
466 g_string_printf(msg, "Invalid usage, see '/help %s' for details.", &cmd[1]);
467
468 if (setting_func) {
469 cons_show("");
470 (*setting_func)();
471 } else {
472 cons_show("");
473 cons_show(msg->str);
474 ProfWin* current = wins_get_current();
475 if (current->type == WIN_CHAT) {
476 win_println(current, THEME_DEFAULT, "-", "%s", msg->str);
477 }
478 }
479
480 g_string_free(msg, TRUE);
481 }
482
483 void
ui_disconnected(void)484 ui_disconnected(void)
485 {
486 wins_lost_connection();
487 title_bar_set_connected(FALSE);
488 title_bar_set_tls(FALSE);
489 title_bar_set_presence(CONTACT_OFFLINE);
490 status_bar_clear_fulljid();
491 ui_hide_roster();
492 }
493
494 void
ui_close_connected_win(int index)495 ui_close_connected_win(int index)
496 {
497 ProfWin* window = wins_get_by_num(index);
498 if (window) {
499 if (window->type == WIN_MUC) {
500 ProfMucWin* mucwin = (ProfMucWin*)window;
501 assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);
502 presence_leave_chat_room(mucwin->roomjid);
503 muc_leave(mucwin->roomjid);
504 ui_leave_room(mucwin->roomjid);
505 } else if (window->type == WIN_CHAT) {
506 ProfChatWin* chatwin = (ProfChatWin*)window;
507 assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);
508 #ifdef HAVE_LIBOTR
509 if (chatwin->is_otr) {
510 otr_end_session(chatwin->barejid);
511 }
512 #endif
513 chat_state_gone(chatwin->barejid, chatwin->state);
514 chat_session_remove(chatwin->barejid);
515 }
516 }
517 }
518
519 int
ui_close_all_wins(void)520 ui_close_all_wins(void)
521 {
522 int count = 0;
523 jabber_conn_status_t conn_status = connection_get_status();
524
525 GList* win_nums = wins_get_nums();
526 GList* curr = win_nums;
527
528 while (curr) {
529 int num = GPOINTER_TO_INT(curr->data);
530 if ((num != 1) && (!ui_win_has_unsaved_form(num))) {
531 if (conn_status == JABBER_CONNECTED) {
532 ui_close_connected_win(num);
533 }
534 ui_close_win(num);
535 count++;
536 }
537 curr = g_list_next(curr);
538 }
539
540 g_list_free(curr);
541 g_list_free(win_nums);
542
543 return count;
544 }
545
546 int
ui_close_read_wins(void)547 ui_close_read_wins(void)
548 {
549 int count = 0;
550 jabber_conn_status_t conn_status = connection_get_status();
551
552 GList* win_nums = wins_get_nums();
553 GList* curr = win_nums;
554
555 while (curr) {
556 int num = GPOINTER_TO_INT(curr->data);
557 if ((num != 1) && (ui_win_unread(num) == 0) && (!ui_win_has_unsaved_form(num))) {
558 if (conn_status == JABBER_CONNECTED) {
559 ui_close_connected_win(num);
560 }
561 ui_close_win(num);
562 count++;
563 }
564 curr = g_list_next(curr);
565 }
566
567 g_list_free(curr);
568 g_list_free(win_nums);
569
570 return count;
571 }
572
573 void
ui_redraw_all_room_rosters(void)574 ui_redraw_all_room_rosters(void)
575 {
576 GList* win_nums = wins_get_nums();
577 GList* curr = win_nums;
578
579 while (curr) {
580 int num = GPOINTER_TO_INT(curr->data);
581 ProfWin* window = wins_get_by_num(num);
582 if (window->type == WIN_MUC && win_has_active_subwin(window)) {
583 ProfMucWin* mucwin = (ProfMucWin*)window;
584 assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);
585 occupantswin_occupants(mucwin->roomjid);
586 }
587 curr = g_list_next(curr);
588 }
589
590 g_list_free(curr);
591 g_list_free(win_nums);
592 }
593
594 void
ui_hide_all_room_rosters(void)595 ui_hide_all_room_rosters(void)
596 {
597 GList* win_nums = wins_get_nums();
598 GList* curr = win_nums;
599
600 while (curr) {
601 int num = GPOINTER_TO_INT(curr->data);
602 ProfWin* window = wins_get_by_num(num);
603 if (window->type == WIN_MUC && win_has_active_subwin(window)) {
604 ProfMucWin* mucwin = (ProfMucWin*)window;
605 assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);
606 mucwin_hide_occupants(mucwin);
607 }
608 curr = g_list_next(curr);
609 }
610
611 g_list_free(curr);
612 g_list_free(win_nums);
613 }
614
615 void
ui_show_all_room_rosters(void)616 ui_show_all_room_rosters(void)
617 {
618 GList* win_nums = wins_get_nums();
619 GList* curr = win_nums;
620
621 while (curr) {
622 int num = GPOINTER_TO_INT(curr->data);
623 ProfWin* window = wins_get_by_num(num);
624 if (window->type == WIN_MUC && !win_has_active_subwin(window)) {
625 ProfMucWin* mucwin = (ProfMucWin*)window;
626 assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);
627 mucwin_show_occupants(mucwin);
628 }
629 curr = g_list_next(curr);
630 }
631
632 g_list_free(curr);
633 g_list_free(win_nums);
634 }
635
636 gboolean
ui_win_has_unsaved_form(int num)637 ui_win_has_unsaved_form(int num)
638 {
639 ProfWin* window = wins_get_by_num(num);
640
641 if (window->type == WIN_CONFIG) {
642 ProfConfWin* confwin = (ProfConfWin*)window;
643 assert(confwin->memcheck == PROFCONFWIN_MEMCHECK);
644 return confwin->form->modified;
645 } else {
646 return FALSE;
647 }
648 }
649
650 void
ui_focus_win(ProfWin * window)651 ui_focus_win(ProfWin* window)
652 {
653 assert(window != NULL);
654
655 if (wins_is_current(window)) {
656 return;
657 }
658
659 ProfWin* old_current = wins_get_current();
660
661 if (old_current->type == WIN_CONFIG) {
662 ProfConfWin* confwin = (ProfConfWin*)old_current;
663 cmd_ac_remove_form_fields(confwin->form);
664 }
665 if (window->type == WIN_CONFIG) {
666 ProfConfWin* confwin = (ProfConfWin*)window;
667 cmd_ac_add_form_fields(confwin->form);
668 }
669
670 // check for trackbar last position separator
671 switch (old_current->type) {
672 case WIN_CHAT:
673 {
674 ProfChatWin* chatwin = (ProfChatWin*)old_current;
675 win_remove_entry_message(old_current, chatwin->barejid);
676 break;
677 }
678 case WIN_MUC:
679 {
680 ProfMucWin* mucwin = (ProfMucWin*)old_current;
681 win_remove_entry_message(old_current, mucwin->roomjid);
682 break;
683 }
684 case WIN_PRIVATE:
685 {
686 ProfPrivateWin* privwin = (ProfPrivateWin*)old_current;
687 win_remove_entry_message(old_current, privwin->fulljid);
688 break;
689 }
690 default:
691 break;
692 }
693
694 int i = wins_get_num(window);
695 wins_set_current_by_num(i);
696
697 if (i == 1) {
698 title_bar_console();
699 rosterwin_roster();
700 } else {
701 title_bar_switch();
702 }
703 status_bar_current(i);
704
705 char* identifier = win_get_tab_identifier(window);
706 status_bar_active(i, window->type, identifier);
707 free(identifier);
708 }
709
710 void
ui_close_win(int index)711 ui_close_win(int index)
712 {
713 ProfWin* window = wins_get_by_num(index);
714 if (window && window->type == WIN_CONFIG) {
715 ProfConfWin* confwin = (ProfConfWin*)window;
716 if (confwin->form) {
717 cmd_ac_remove_form_fields(confwin->form);
718 }
719 }
720
721 wins_close_by_num(index);
722 title_bar_console();
723 status_bar_current(1);
724 status_bar_active(1, WIN_CONSOLE, "console");
725 }
726
727 void
ui_prune_wins(void)728 ui_prune_wins(void)
729 {
730 jabber_conn_status_t conn_status = connection_get_status();
731 gboolean pruned = FALSE;
732
733 GSList* wins = wins_get_prune_wins();
734 if (wins) {
735 pruned = TRUE;
736 }
737
738 GSList* curr = wins;
739 while (curr) {
740 ProfWin* window = curr->data;
741 if (window->type == WIN_CHAT) {
742 if (conn_status == JABBER_CONNECTED) {
743 ProfChatWin* chatwin = (ProfChatWin*)window;
744 chat_session_remove(chatwin->barejid);
745 }
746 }
747
748 int num = wins_get_num(window);
749 ui_close_win(num);
750
751 curr = g_slist_next(curr);
752 }
753
754 if (wins) {
755 g_slist_free(wins);
756 }
757
758 wins_tidy();
759 if (pruned) {
760 cons_show("Windows pruned.");
761 } else {
762 cons_show("No prune needed.");
763 }
764 }
765
766 void
ui_print_system_msg_from_recipient(const char * const barejid,const char * message)767 ui_print_system_msg_from_recipient(const char* const barejid, const char* message)
768 {
769 if (barejid == NULL || message == NULL)
770 return;
771
772 ProfChatWin* chatwin = wins_get_chat(barejid);
773 ProfWin* window = (ProfWin*)chatwin;
774 if (window == NULL) {
775 window = wins_new_chat(barejid);
776 if (window) {
777 chatwin = (ProfChatWin*)window;
778 int num = wins_get_num(window);
779 status_bar_active(num, WIN_CHAT, chatwin->barejid);
780 } else {
781 window = wins_get_console();
782 status_bar_active(1, WIN_CONSOLE, "console");
783 }
784 }
785
786 win_println(window, THEME_DEFAULT, "-", "*%s %s", barejid, message);
787 }
788
789 void
ui_room_join(const char * const roomjid,gboolean focus)790 ui_room_join(const char* const roomjid, gboolean focus)
791 {
792 ProfMucWin* mucwin = wins_get_muc(roomjid);
793 if (mucwin == NULL) {
794 mucwin = mucwin_new(roomjid);
795 }
796 ProfWin* window = (ProfWin*)mucwin;
797
798 char* nick = muc_nick(roomjid);
799 win_print(window, THEME_ROOMINFO, "!", "-> You have joined the room as %s", nick);
800 if (prefs_get_boolean(PREF_MUC_PRIVILEGES)) {
801 char* role = muc_role_str(roomjid);
802 char* affiliation = muc_affiliation_str(roomjid);
803 if (role) {
804 win_append(window, THEME_ROOMINFO, ", role: %s", role);
805 }
806 if (affiliation) {
807 win_append(window, THEME_ROOMINFO, ", affiliation: %s", affiliation);
808 }
809 }
810 win_appendln(window, THEME_ROOMINFO, "");
811
812 if (focus) {
813 ui_focus_win(window);
814 } else {
815 int num = wins_get_num(window);
816 status_bar_active(num, WIN_MUC, mucwin->roomjid);
817 ProfWin* console = wins_get_console();
818 win_println(console, THEME_TYPING, "!", "-> Autojoined %s as %s (%d).", roomjid, nick, num);
819 }
820
821 GList* privwins = wins_get_private_chats(roomjid);
822 GList* curr = privwins;
823 while (curr) {
824 ProfPrivateWin* privwin = curr->data;
825 privwin_room_joined(privwin);
826 curr = g_list_next(curr);
827 }
828 g_list_free(privwins);
829 }
830
831 void
ui_switch_to_room(const char * const roomjid)832 ui_switch_to_room(const char* const roomjid)
833 {
834 ProfWin* window = (ProfWin*)wins_get_muc(roomjid);
835 ui_focus_win(window);
836 }
837
838 void
ui_room_destroy(const char * const roomjid)839 ui_room_destroy(const char* const roomjid)
840 {
841 ProfWin* window = (ProfWin*)wins_get_muc(roomjid);
842 GList* privwins = wins_get_private_chats(roomjid);
843 if (window == NULL) {
844 log_error("Received room destroy result, but no window open for %s.", roomjid);
845 } else {
846 int num = wins_get_num(window);
847 ui_close_win(num);
848 cons_show("Room destroyed: %s", roomjid);
849 }
850
851 GList* curr = privwins;
852 while (curr) {
853 ProfPrivateWin* privwin = curr->data;
854 privwin_room_destroyed(privwin);
855 curr = g_list_next(curr);
856 }
857 g_list_free(privwins);
858 }
859
860 void
ui_leave_room(const char * const roomjid)861 ui_leave_room(const char* const roomjid)
862 {
863 ProfWin* window = (ProfWin*)wins_get_muc(roomjid);
864 GList* privwins = wins_get_private_chats(roomjid);
865 if (window) {
866 int num = wins_get_num(window);
867 ui_close_win(num);
868 }
869
870 GList* curr = privwins;
871 while (curr) {
872 ProfPrivateWin* privwin = curr->data;
873 privwin_room_left(privwin);
874 curr = g_list_next(curr);
875 }
876 g_list_free(privwins);
877 }
878
879 void
ui_room_destroyed(const char * const roomjid,const char * const reason,const char * const new_jid,const char * const password)880 ui_room_destroyed(const char* const roomjid, const char* const reason, const char* const new_jid,
881 const char* const password)
882 {
883 ProfWin* window = (ProfWin*)wins_get_muc(roomjid);
884 GList* privwins = wins_get_private_chats(roomjid);
885 if (window == NULL) {
886 log_error("Received room destroy, but no window open for %s.", roomjid);
887 } else {
888 int num = wins_get_num(window);
889 ui_close_win(num);
890 ProfWin* console = wins_get_console();
891
892 if (reason) {
893 win_println(console, THEME_TYPING, "!", "<- Room destroyed: %s, reason: %s", roomjid, reason);
894 } else {
895 win_println(console, THEME_TYPING, "!", "<- Room destroyed: %s", roomjid);
896 }
897
898 if (new_jid) {
899 if (password) {
900 win_println(console, THEME_TYPING, "!", "Replacement room: %s, password: %s", new_jid, password);
901 } else {
902 win_println(console, THEME_TYPING, "!", "Replacement room: %s", new_jid);
903 }
904 }
905 }
906
907 GList* curr = privwins;
908 while (curr) {
909 ProfPrivateWin* privwin = curr->data;
910 privwin_room_destroyed(privwin);
911 curr = g_list_next(curr);
912 }
913 g_list_free(privwins);
914 }
915
916 void
ui_room_kicked(const char * const roomjid,const char * const actor,const char * const reason)917 ui_room_kicked(const char* const roomjid, const char* const actor, const char* const reason)
918 {
919 ProfWin* window = (ProfWin*)wins_get_muc(roomjid);
920 GList* privwins = wins_get_private_chats(roomjid);
921 if (window == NULL) {
922 log_error("Received kick, but no window open for %s.", roomjid);
923 } else {
924 int num = wins_get_num(window);
925 ui_close_win(num);
926
927 GString* message = g_string_new("Kicked from ");
928 g_string_append(message, roomjid);
929 if (actor) {
930 g_string_append(message, " by ");
931 g_string_append(message, actor);
932 }
933 if (reason) {
934 g_string_append(message, ", reason: ");
935 g_string_append(message, reason);
936 }
937
938 ProfWin* console = wins_get_console();
939 win_println(console, THEME_TYPING, "!", "<- %s", message->str);
940 g_string_free(message, TRUE);
941 }
942
943 GList* curr = privwins;
944 while (curr) {
945 ProfPrivateWin* privwin = curr->data;
946 privwin_room_kicked(privwin, actor, reason);
947 curr = g_list_next(curr);
948 }
949 g_list_free(privwins);
950 }
951
952 void
ui_room_banned(const char * const roomjid,const char * const actor,const char * const reason)953 ui_room_banned(const char* const roomjid, const char* const actor, const char* const reason)
954 {
955 ProfWin* window = (ProfWin*)wins_get_muc(roomjid);
956 GList* privwins = wins_get_private_chats(roomjid);
957 if (window == NULL) {
958 log_error("Received ban, but no window open for %s.", roomjid);
959 } else {
960 int num = wins_get_num(window);
961 ui_close_win(num);
962
963 GString* message = g_string_new("Banned from ");
964 g_string_append(message, roomjid);
965 if (actor) {
966 g_string_append(message, " by ");
967 g_string_append(message, actor);
968 }
969 if (reason) {
970 g_string_append(message, ", reason: ");
971 g_string_append(message, reason);
972 }
973
974 ProfWin* console = wins_get_console();
975 win_println(console, THEME_TYPING, "!", "<- %s", message->str);
976 g_string_free(message, TRUE);
977 }
978
979 GList* curr = privwins;
980 while (curr) {
981 ProfPrivateWin* privwin = curr->data;
982 privwin_room_banned(privwin, actor, reason);
983 curr = g_list_next(curr);
984 }
985 g_list_free(privwins);
986 }
987
988 int
ui_win_unread(int index)989 ui_win_unread(int index)
990 {
991 ProfWin* window = wins_get_by_num(index);
992 if (window) {
993 return win_unread(window);
994 } else {
995 return 0;
996 }
997 }
998
999 gboolean
ui_win_has_attention(int index)1000 ui_win_has_attention(int index)
1001 {
1002 gboolean ret = FALSE;
1003
1004 ProfWin* window = wins_get_by_num(index);
1005 if (window) {
1006 ret = win_has_attention(window);
1007 }
1008
1009 return ret;
1010 }
1011
1012
1013 char*
ui_ask_password(gboolean confirm)1014 ui_ask_password(gboolean confirm)
1015 {
1016 if (!confirm) {
1017 status_bar_set_prompt("Enter password:");
1018 } else {
1019 status_bar_set_prompt("Confirm password:");
1020 }
1021 return inp_get_password();
1022 }
1023
1024 char*
ui_get_line(void)1025 ui_get_line(void)
1026 {
1027 status_bar_draw();
1028 return inp_get_line();
1029 }
1030
1031 char*
ui_ask_pgp_passphrase(const char * hint,int prev_fail)1032 ui_ask_pgp_passphrase(const char* hint, int prev_fail)
1033 {
1034 ProfWin* current = wins_get_current();
1035
1036 win_println(current, THEME_DEFAULT, "-", "");
1037
1038 if (prev_fail) {
1039 win_println(current, THEME_DEFAULT, "!", "Incorrect passphrase");
1040 }
1041
1042 if (hint) {
1043 win_println(current, THEME_DEFAULT, "!", "Enter PGP key passphrase for %s", hint);
1044 } else {
1045 win_println(current, THEME_DEFAULT, "!", "Enter PGP key passphrase");
1046 }
1047
1048 ui_update();
1049
1050 status_bar_set_prompt("Enter password:");
1051 return inp_get_password();
1052 }
1053
1054 void
ui_contact_offline(char * barejid,char * resource,char * status)1055 ui_contact_offline(char* barejid, char* resource, char* status)
1056 {
1057 char* show_console = prefs_get_string(PREF_STATUSES_CONSOLE);
1058 char* show_chat_win = prefs_get_string(PREF_STATUSES_CHAT);
1059 Jid* jid = jid_create_from_bare_and_resource(barejid, resource);
1060 PContact contact = roster_get_contact(barejid);
1061 if (p_contact_subscription(contact)) {
1062 if (strcmp(p_contact_subscription(contact), "none") != 0) {
1063
1064 // show in console if "all"
1065 if (g_strcmp0(show_console, "all") == 0) {
1066 cons_show_contact_offline(contact, resource, status);
1067
1068 // show in console of "online"
1069 } else if (g_strcmp0(show_console, "online") == 0) {
1070 cons_show_contact_offline(contact, resource, status);
1071 }
1072
1073 // show in chat win if "all"
1074 if (g_strcmp0(show_chat_win, "all") == 0) {
1075 ProfChatWin* chatwin = wins_get_chat(barejid);
1076 if (chatwin) {
1077 chatwin_contact_offline(chatwin, resource, status);
1078 }
1079
1080 // show in chat win if "online" and presence online
1081 } else if (g_strcmp0(show_chat_win, "online") == 0) {
1082 ProfChatWin* chatwin = wins_get_chat(barejid);
1083 if (chatwin) {
1084 chatwin_contact_offline(chatwin, resource, status);
1085 }
1086 }
1087 }
1088 }
1089
1090 ProfChatWin* chatwin = wins_get_chat(barejid);
1091 if (chatwin && chatwin->resource_override && (g_strcmp0(resource, chatwin->resource_override) == 0)) {
1092 FREE_SET_NULL(chatwin->resource_override);
1093 }
1094
1095 g_free(show_console);
1096 g_free(show_chat_win);
1097 jid_destroy(jid);
1098 }
1099
1100 void
ui_clear_win_title(void)1101 ui_clear_win_title(void)
1102 {
1103 fputs("\e]0;\a", stdout);
1104 fflush(stdout);
1105 }
1106
1107 void
ui_goodbye_title(void)1108 ui_goodbye_title(void)
1109 {
1110 fputs("\e]0;Thanks for using Profanity\a", stdout);
1111 fflush(stdout);
1112 }
1113
1114 static void
_ui_draw_term_title(void)1115 _ui_draw_term_title(void)
1116 {
1117 jabber_conn_status_t status = connection_get_status();
1118
1119 if (status == JABBER_CONNECTED) {
1120 const char* const jid = connection_get_fulljid();
1121 gint unread = wins_get_total_unread();
1122
1123 if (unread != 0) {
1124 fprintf(stdout, "\e]0;Profanity (%d) - %s\a", unread, jid);
1125 } else {
1126 fprintf(stdout, "\e]0;Profanity - %s\a", jid);
1127 }
1128 } else {
1129 fputs("\e]0;Profanity\a", stdout);
1130 }
1131 fflush(stdout);
1132 }
1133
1134 void
ui_handle_room_configuration_form_error(const char * const roomjid,const char * const message)1135 ui_handle_room_configuration_form_error(const char* const roomjid, const char* const message)
1136 {
1137 ProfWin* window = NULL;
1138 GString* message_str = g_string_new("");
1139
1140 if (roomjid) {
1141 window = (ProfWin*)wins_get_muc(roomjid);
1142 g_string_printf(message_str, "Could not get room configuration for %s", roomjid);
1143 } else {
1144 window = wins_get_console();
1145 g_string_printf(message_str, "Could not get room configuration");
1146 }
1147
1148 if (message) {
1149 g_string_append(message_str, ": ");
1150 g_string_append(message_str, message);
1151 }
1152
1153 win_println(window, THEME_ERROR, "-", "%s", message_str->str);
1154
1155 g_string_free(message_str, TRUE);
1156 }
1157
1158 void
ui_handle_room_config_submit_result(const char * const roomjid)1159 ui_handle_room_config_submit_result(const char* const roomjid)
1160 {
1161 if (roomjid) {
1162 ProfWin* form_window = NULL;
1163 ProfWin* muc_window = (ProfWin*)wins_get_muc(roomjid);
1164
1165 GString* form_recipient = g_string_new(roomjid);
1166 g_string_append(form_recipient, " config");
1167 form_window = (ProfWin*)wins_get_conf(form_recipient->str);
1168 g_string_free(form_recipient, TRUE);
1169
1170 if (form_window) {
1171 int num = wins_get_num(form_window);
1172 wins_close_by_num(num);
1173 }
1174
1175 if (muc_window) {
1176 ui_focus_win((ProfWin*)muc_window);
1177 win_println(muc_window, THEME_ROOMINFO, "!", "Room configuration successful");
1178 } else {
1179 ProfWin* console = wins_get_console();
1180 ui_focus_win(console);
1181 cons_show("Room configuration successful: %s", roomjid);
1182 }
1183 } else {
1184 cons_show("Room configuration successful");
1185 }
1186 }
1187
1188 void
ui_handle_room_config_submit_result_error(const char * const roomjid,const char * const message)1189 ui_handle_room_config_submit_result_error(const char* const roomjid, const char* const message)
1190 {
1191 ProfWin* console = wins_get_console();
1192 if (roomjid) {
1193 ProfWin* muc_window = NULL;
1194 ProfWin* form_window = NULL;
1195 muc_window = (ProfWin*)wins_get_muc(roomjid);
1196
1197 GString* form_recipient = g_string_new(roomjid);
1198 g_string_append(form_recipient, " config");
1199 form_window = (ProfWin*)wins_get_conf(form_recipient->str);
1200 g_string_free(form_recipient, TRUE);
1201
1202 if (form_window) {
1203 if (message) {
1204 win_println(form_window, THEME_ERROR, "!", "Configuration error: %s", message);
1205 } else {
1206 win_println(form_window, THEME_ERROR, "!", "Configuration error");
1207 }
1208 } else if (muc_window) {
1209 if (message) {
1210 win_println(muc_window, THEME_ERROR, "!", "Configuration error: %s", message);
1211 } else {
1212 win_println(muc_window, THEME_ERROR, "!", "Configuration error");
1213 }
1214 } else {
1215 if (message) {
1216 win_println(console, THEME_ERROR, "!", "Configuration error for %s: %s", roomjid, message);
1217 } else {
1218 win_println(console, THEME_ERROR, "!", "Configuration error for %s", roomjid);
1219 }
1220 }
1221 } else {
1222 win_println(console, THEME_ERROR, "!", "Configuration error");
1223 }
1224 }
1225
1226 void
ui_show_lines(ProfWin * window,gchar ** lines)1227 ui_show_lines(ProfWin* window, gchar** lines)
1228 {
1229 if (lines) {
1230 for (int i = 0; lines[i] != NULL; i++) {
1231 win_println(window, THEME_DEFAULT, "-", "%s", lines[i]);
1232 }
1233 }
1234 }
1235
1236 void
ui_show_roster(void)1237 ui_show_roster(void)
1238 {
1239 ProfWin* window = wins_get_console();
1240 if (window && !win_has_active_subwin(window)) {
1241 wins_show_subwin(window);
1242 rosterwin_roster();
1243 }
1244 }
1245
1246 void
ui_hide_roster(void)1247 ui_hide_roster(void)
1248 {
1249 ProfWin* window = wins_get_console();
1250 if (window && win_has_active_subwin(window)) {
1251 wins_hide_subwin(window);
1252 }
1253 }
1254
1255 void
ui_handle_software_version_error(const char * const roomjid,const char * const message)1256 ui_handle_software_version_error(const char* const roomjid, const char* const message)
1257 {
1258 GString* message_str = g_string_new("");
1259
1260 ProfWin* window = wins_get_console();
1261 g_string_printf(message_str, "Could not get software version");
1262
1263 if (message) {
1264 g_string_append(message_str, ": ");
1265 g_string_append(message_str, message);
1266 }
1267
1268 win_println(window, THEME_ERROR, "-", "%s", message_str->str);
1269
1270 g_string_free(message_str, TRUE);
1271 }
1272
1273 void
ui_show_software_version(const char * const jid,const char * const presence,const char * const name,const char * const version,const char * const os)1274 ui_show_software_version(const char* const jid, const char* const presence,
1275 const char* const name, const char* const version, const char* const os)
1276 {
1277 Jid* jidp = jid_create(jid);
1278 ProfWin* window = NULL;
1279 ProfWin* chatwin = (ProfWin*)wins_get_chat(jidp->barejid);
1280 ProfWin* mucwin = (ProfWin*)wins_get_muc(jidp->barejid);
1281 ProfWin* privwin = (ProfWin*)wins_get_private(jidp->fulljid);
1282 ProfWin* console = wins_get_console();
1283 jid_destroy(jidp);
1284
1285 if (chatwin) {
1286 if (wins_is_current(chatwin)) {
1287 window = chatwin;
1288 } else {
1289 window = console;
1290 }
1291 } else if (privwin) {
1292 if (wins_is_current(privwin)) {
1293 window = privwin;
1294 } else {
1295 window = console;
1296 }
1297 } else if (mucwin) {
1298 if (wins_is_current(mucwin)) {
1299 window = mucwin;
1300 } else {
1301 window = console;
1302 }
1303 } else {
1304 window = console;
1305 }
1306
1307 if (name || version || os) {
1308 win_println(window, THEME_DEFAULT, "-", "");
1309 theme_item_t presence_colour = theme_main_presence_attrs(presence);
1310 win_print(window, presence_colour, "-", "%s", jid);
1311 win_appendln(window, THEME_DEFAULT, ":");
1312 }
1313 if (name) {
1314 win_println(window, THEME_DEFAULT, "-", "Name : %s", name);
1315 }
1316 if (version) {
1317 win_println(window, THEME_DEFAULT, "-", "Version : %s", version);
1318 }
1319 if (os) {
1320 win_println(window, THEME_DEFAULT, "-", "OS : %s", os);
1321 }
1322 }
1323