1 /*
2  * privwin.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 #include <assert.h>
39 #include <glib.h>
40 #include <stdlib.h>
41 
42 #include "config/preferences.h"
43 #include "ui/win_types.h"
44 #include "ui/window.h"
45 #include "ui/titlebar.h"
46 #include "ui/window_list.h"
47 
48 void
privwin_incoming_msg(ProfPrivateWin * privatewin,ProfMessage * message)49 privwin_incoming_msg(ProfPrivateWin* privatewin, ProfMessage* message)
50 {
51     assert(privatewin != NULL);
52 
53     ProfWin* window = (ProfWin*)privatewin;
54     int num = wins_get_num(window);
55 
56     Jid* jidp = jid_create(privatewin->fulljid);
57     if (jidp == NULL) {
58         return;
59     }
60 
61     gboolean is_current = wins_is_current(window);
62     gboolean notify = prefs_do_chat_notify(is_current);
63 
64     // currently viewing chat window with sender
65     if (wins_is_current(window)) {
66         win_print_incoming(window, jidp->resourcepart, message);
67         title_bar_set_typing(FALSE);
68         status_bar_active(num, WIN_PRIVATE, privatewin->fulljid);
69 
70         // not currently viewing chat window with sender
71     } else {
72         status_bar_new(num, WIN_PRIVATE, privatewin->fulljid);
73         cons_show_incoming_private_message(jidp->resourcepart, jidp->barejid, num, privatewin->unread, window);
74         win_insert_last_read_position_marker((ProfWin*)privatewin, privatewin->fulljid);
75         win_print_incoming(window, jidp->resourcepart, message);
76 
77         privatewin->unread++;
78 
79         if (prefs_get_boolean(PREF_FLASH)) {
80             flash();
81         }
82     }
83 
84     wins_add_urls_ac(window, message);
85 
86     if (prefs_get_boolean(PREF_BEEP)) {
87         beep();
88     }
89 
90     if (notify) {
91         notify_message(jidp->resourcepart, num, message->plain);
92     }
93 
94     jid_destroy(jidp);
95 }
96 
97 void
privwin_outgoing_msg(ProfPrivateWin * privwin,const char * const message)98 privwin_outgoing_msg(ProfPrivateWin* privwin, const char* const message)
99 {
100     assert(privwin != NULL);
101 
102     win_print_outgoing((ProfWin*)privwin, "-", NULL, NULL, message);
103 }
104 
105 void
privwin_message_occupant_offline(ProfPrivateWin * privwin)106 privwin_message_occupant_offline(ProfPrivateWin* privwin)
107 {
108     assert(privwin != NULL);
109 
110     win_println((ProfWin*)privwin, THEME_ERROR, "-", "Unable to send message, occupant no longer present in room.");
111 }
112 
113 void
privwin_message_left_room(ProfPrivateWin * privwin)114 privwin_message_left_room(ProfPrivateWin* privwin)
115 {
116     assert(privwin != NULL);
117 
118     win_println((ProfWin*)privwin, THEME_ERROR, "-", "Unable to send message, you are no longer present in room.");
119 }
120 
121 void
privwin_occupant_offline(ProfPrivateWin * privwin)122 privwin_occupant_offline(ProfPrivateWin* privwin)
123 {
124     assert(privwin != NULL);
125 
126     privwin->occupant_offline = TRUE;
127     Jid* jidp = jid_create(privwin->fulljid);
128     win_println((ProfWin*)privwin, THEME_OFFLINE, "-", "<- %s has left the room.", jidp->resourcepart);
129     jid_destroy(jidp);
130 }
131 
132 void
privwin_occupant_kicked(ProfPrivateWin * privwin,const char * const actor,const char * const reason)133 privwin_occupant_kicked(ProfPrivateWin* privwin, const char* const actor, const char* const reason)
134 {
135     assert(privwin != NULL);
136 
137     privwin->occupant_offline = TRUE;
138     Jid* jidp = jid_create(privwin->fulljid);
139     GString* message = g_string_new(jidp->resourcepart);
140     jid_destroy(jidp);
141     g_string_append(message, " has been kicked from the room");
142     if (actor) {
143         g_string_append(message, " by ");
144         g_string_append(message, actor);
145     }
146     if (reason) {
147         g_string_append(message, ", reason: ");
148         g_string_append(message, reason);
149     }
150 
151     win_println((ProfWin*)privwin, THEME_OFFLINE, "!", "<- %s", message->str);
152     g_string_free(message, TRUE);
153 }
154 
155 void
privwin_occupant_banned(ProfPrivateWin * privwin,const char * const actor,const char * const reason)156 privwin_occupant_banned(ProfPrivateWin* privwin, const char* const actor, const char* const reason)
157 {
158     assert(privwin != NULL);
159 
160     privwin->occupant_offline = TRUE;
161     Jid* jidp = jid_create(privwin->fulljid);
162     GString* message = g_string_new(jidp->resourcepart);
163     jid_destroy(jidp);
164     g_string_append(message, " has been banned from the room");
165     if (actor) {
166         g_string_append(message, " by ");
167         g_string_append(message, actor);
168     }
169     if (reason) {
170         g_string_append(message, ", reason: ");
171         g_string_append(message, reason);
172     }
173 
174     win_println((ProfWin*)privwin, THEME_OFFLINE, "!", "<- %s", message->str);
175     g_string_free(message, TRUE);
176 }
177 
178 void
privwin_occupant_online(ProfPrivateWin * privwin)179 privwin_occupant_online(ProfPrivateWin* privwin)
180 {
181     assert(privwin != NULL);
182 
183     privwin->occupant_offline = FALSE;
184     Jid* jidp = jid_create(privwin->fulljid);
185     win_println((ProfWin*)privwin, THEME_ONLINE, "-", "-- %s has joined the room.", jidp->resourcepart);
186     jid_destroy(jidp);
187 }
188 
189 void
privwin_room_destroyed(ProfPrivateWin * privwin)190 privwin_room_destroyed(ProfPrivateWin* privwin)
191 {
192     assert(privwin != NULL);
193 
194     privwin->room_left = TRUE;
195     Jid* jidp = jid_create(privwin->fulljid);
196     win_println((ProfWin*)privwin, THEME_OFFLINE, "!", "-- %s has been destroyed.", jidp->barejid);
197     jid_destroy(jidp);
198 }
199 
200 void
privwin_room_joined(ProfPrivateWin * privwin)201 privwin_room_joined(ProfPrivateWin* privwin)
202 {
203     assert(privwin != NULL);
204 
205     privwin->room_left = FALSE;
206     Jid* jidp = jid_create(privwin->fulljid);
207     win_println((ProfWin*)privwin, THEME_OFFLINE, "!", "-- You have joined %s.", jidp->barejid);
208     jid_destroy(jidp);
209 }
210 
211 void
privwin_room_left(ProfPrivateWin * privwin)212 privwin_room_left(ProfPrivateWin* privwin)
213 {
214     assert(privwin != NULL);
215 
216     privwin->room_left = TRUE;
217     Jid* jidp = jid_create(privwin->fulljid);
218     win_println((ProfWin*)privwin, THEME_OFFLINE, "!", "-- You have left %s.", jidp->barejid);
219     jid_destroy(jidp);
220 }
221 
222 void
privwin_room_kicked(ProfPrivateWin * privwin,const char * const actor,const char * const reason)223 privwin_room_kicked(ProfPrivateWin* privwin, const char* const actor, const char* const reason)
224 {
225     assert(privwin != NULL);
226 
227     privwin->room_left = TRUE;
228     GString* message = g_string_new("Kicked from ");
229     Jid* jidp = jid_create(privwin->fulljid);
230     g_string_append(message, jidp->barejid);
231     jid_destroy(jidp);
232     if (actor) {
233         g_string_append(message, " by ");
234         g_string_append(message, actor);
235     }
236     if (reason) {
237         g_string_append(message, ", reason: ");
238         g_string_append(message, reason);
239     }
240 
241     win_println((ProfWin*)privwin, THEME_OFFLINE, "!", "<- %s", message->str);
242     g_string_free(message, TRUE);
243 }
244 
245 void
privwin_room_banned(ProfPrivateWin * privwin,const char * const actor,const char * const reason)246 privwin_room_banned(ProfPrivateWin* privwin, const char* const actor, const char* const reason)
247 {
248     assert(privwin != NULL);
249 
250     privwin->room_left = TRUE;
251     GString* message = g_string_new("Banned from ");
252     Jid* jidp = jid_create(privwin->fulljid);
253     g_string_append(message, jidp->barejid);
254     jid_destroy(jidp);
255     if (actor) {
256         g_string_append(message, " by ");
257         g_string_append(message, actor);
258     }
259     if (reason) {
260         g_string_append(message, ", reason: ");
261         g_string_append(message, reason);
262     }
263 
264     win_println((ProfWin*)privwin, THEME_OFFLINE, "!", "<- %s", message->str);
265     g_string_free(message, TRUE);
266 }
267 
268 char*
privwin_get_string(ProfPrivateWin * privwin)269 privwin_get_string(ProfPrivateWin* privwin)
270 {
271     assert(privwin != NULL);
272 
273     GString* res = g_string_new("Private ");
274     g_string_append(res, privwin->fulljid);
275 
276     if (privwin->unread > 0) {
277         g_string_append_printf(res, ", %d unread", privwin->unread);
278     }
279 
280     char* resstr = res->str;
281     g_string_free(res, FALSE);
282 
283     return resstr;
284 }
285