1 /*
2 * session.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 <string.h>
40 #include <stdlib.h>
41
42 #include "profanity.h"
43 #include "log.h"
44 #include "common.h"
45 #include "config/preferences.h"
46 #include "plugins/plugins.h"
47 #include "event/server_events.h"
48 #include "event/client_events.h"
49 #include "xmpp/bookmark.h"
50 #include "xmpp/blocking.h"
51 #include "xmpp/connection.h"
52 #include "xmpp/capabilities.h"
53 #include "xmpp/session.h"
54 #include "xmpp/iq.h"
55 #include "xmpp/message.h"
56 #include "xmpp/presence.h"
57 #include "xmpp/roster.h"
58 #include "xmpp/stanza.h"
59 #include "xmpp/xmpp.h"
60 #include "xmpp/muc.h"
61 #include "xmpp/chat_session.h"
62 #include "xmpp/jid.h"
63
64 #ifdef HAVE_OMEMO
65 #include "omemo/omemo.h"
66 #include "xmpp/omemo.h"
67 #endif
68
69 // for auto reconnect
70 static struct
71 {
72 char* name;
73 char* passwd;
74 } saved_account;
75
76 static struct
77 {
78 char* name;
79 char* jid;
80 char* passwd;
81 char* altdomain;
82 int port;
83 char* tls_policy;
84 char* auth_policy;
85 } saved_details;
86
87 typedef enum {
88 ACTIVITY_ST_ACTIVE,
89 ACTIVITY_ST_IDLE,
90 ACTIVITY_ST_AWAY,
91 ACTIVITY_ST_XA,
92 } activity_state_t;
93
94 static GTimer* reconnect_timer;
95 static activity_state_t activity_state;
96 static resource_presence_t saved_presence;
97 static char* saved_status;
98
99 static void _session_reconnect(void);
100
101 static void _session_free_saved_account(void);
102 static void _session_free_saved_details(void);
103
104 void
session_init(void)105 session_init(void)
106 {
107 log_info("Initialising XMPP");
108 connection_init();
109 presence_sub_requests_init();
110 caps_init();
111 }
112
113 jabber_conn_status_t
session_connect_with_account(const ProfAccount * const account)114 session_connect_with_account(const ProfAccount* const account)
115 {
116 assert(account != NULL);
117
118 log_info("Connecting using account: %s", account->name);
119
120 _session_free_saved_account();
121 _session_free_saved_details();
122
123 // save account name and password for reconnect
124 saved_account.name = strdup(account->name);
125 saved_account.passwd = strdup(account->password);
126
127 char* jid = NULL;
128 if (account->resource) {
129 Jid* jidp = jid_create_from_bare_and_resource(account->jid, account->resource);
130 jid = strdup(jidp->fulljid);
131 jid_destroy(jidp);
132 } else {
133 jid = strdup(account->jid);
134 }
135
136 jabber_conn_status_t result = connection_connect(
137 jid,
138 account->password,
139 account->server,
140 account->port,
141 account->tls_policy,
142 account->auth_policy);
143 free(jid);
144
145 return result;
146 }
147
148 jabber_conn_status_t
session_connect_with_details(const char * const jid,const char * const passwd,const char * const altdomain,const int port,const char * const tls_policy,const char * const auth_policy)149 session_connect_with_details(const char* const jid, const char* const passwd, const char* const altdomain,
150 const int port, const char* const tls_policy, const char* const auth_policy)
151 {
152 assert(jid != NULL);
153 assert(passwd != NULL);
154
155 _session_free_saved_account();
156 _session_free_saved_details();
157
158 // save details for reconnect, remember name for account creating on success
159 saved_details.name = strdup(jid);
160 saved_details.passwd = strdup(passwd);
161 if (altdomain) {
162 saved_details.altdomain = strdup(altdomain);
163 } else {
164 saved_details.altdomain = NULL;
165 }
166 if (port != 0) {
167 saved_details.port = port;
168 } else {
169 saved_details.port = 0;
170 }
171 if (tls_policy) {
172 saved_details.tls_policy = strdup(tls_policy);
173 } else {
174 saved_details.tls_policy = NULL;
175 }
176 if (auth_policy) {
177 saved_details.auth_policy = strdup(auth_policy);
178 } else {
179 saved_details.auth_policy = NULL;
180 }
181
182 // use 'profanity' when no resourcepart in provided jid
183 Jid* jidp = jid_create(jid);
184 if (jidp->resourcepart == NULL) {
185 jid_destroy(jidp);
186 char* resource = jid_random_resource();
187 jidp = jid_create_from_bare_and_resource(jid, resource);
188 free(resource);
189 saved_details.jid = strdup(jidp->fulljid);
190 } else {
191 saved_details.jid = strdup(jid);
192 }
193 jid_destroy(jidp);
194
195 // connect with fulljid
196 log_info("Connecting without account, JID: %s", saved_details.jid);
197
198 return connection_connect(
199 saved_details.jid,
200 passwd,
201 saved_details.altdomain,
202 saved_details.port,
203 saved_details.tls_policy,
204 saved_details.auth_policy);
205 }
206
207 void
session_autoping_fail(void)208 session_autoping_fail(void)
209 {
210 session_lost_connection();
211 }
212
213 void
session_disconnect(void)214 session_disconnect(void)
215 {
216 // if connected, send end stream and wait for response
217 if (connection_get_status() == JABBER_CONNECTED) {
218 log_info("Closing connection");
219
220 char* account_name = session_get_account_name();
221 const char* fulljid = connection_get_fulljid();
222 plugins_on_disconnect(account_name, fulljid);
223
224 accounts_set_last_activity(session_get_account_name());
225
226 iq_rooms_cache_clear();
227 iq_handlers_clear();
228
229 connection_disconnect();
230 message_handlers_clear();
231
232 connection_clear_data();
233 chat_sessions_clear();
234 presence_clear_sub_requests();
235 }
236
237 connection_set_disconnected();
238 }
239
240 void
session_shutdown(void)241 session_shutdown(void)
242 {
243 _session_free_saved_account();
244 _session_free_saved_details();
245
246 chat_sessions_clear();
247 presence_clear_sub_requests();
248
249 connection_shutdown();
250 if (saved_status) {
251 free(saved_status);
252 }
253 }
254
255 void
session_process_events(void)256 session_process_events(void)
257 {
258 int reconnect_sec;
259
260 jabber_conn_status_t conn_status = connection_get_status();
261 switch (conn_status) {
262 case JABBER_CONNECTED:
263 case JABBER_CONNECTING:
264 case JABBER_DISCONNECTING:
265 connection_check_events();
266 break;
267 case JABBER_DISCONNECTED:
268 reconnect_sec = prefs_get_reconnect();
269 if ((reconnect_sec != 0) && reconnect_timer) {
270 int elapsed_sec = g_timer_elapsed(reconnect_timer, NULL);
271 if (elapsed_sec > reconnect_sec) {
272 _session_reconnect();
273 }
274 }
275 break;
276 default:
277 break;
278 }
279 }
280
281 char*
session_get_account_name(void)282 session_get_account_name(void)
283 {
284 return saved_account.name;
285 }
286
287 void
session_login_success(gboolean secured)288 session_login_success(gboolean secured)
289 {
290 chat_sessions_init();
291
292 message_handlers_init();
293 presence_handlers_init();
294 iq_handlers_init();
295
296 // logged in with account
297 if (saved_account.name) {
298 log_debug("Connection handler: logged in with account name: %s", saved_account.name);
299 sv_ev_login_account_success(saved_account.name, secured);
300
301 // logged in without account, use details to create new account
302 } else {
303 log_debug("Connection handler: logged in with jid: %s", saved_details.name);
304 accounts_add(saved_details.name, saved_details.altdomain, saved_details.port, saved_details.tls_policy, saved_details.auth_policy);
305 accounts_set_jid(saved_details.name, saved_details.jid);
306
307 saved_account.name = strdup(saved_details.name);
308 saved_account.passwd = strdup(saved_details.passwd);
309
310 _session_free_saved_details();
311 sv_ev_login_account_success(saved_account.name, secured);
312 }
313
314 roster_request();
315 bookmark_request();
316 blocking_request();
317
318 // items discovery
319 connection_request_features();
320 char* domain = connection_get_domain();
321 iq_disco_items_request_onconnect(domain);
322
323 if (prefs_get_boolean(PREF_CARBONS)) {
324 iq_enable_carbons();
325 }
326
327 if ((prefs_get_reconnect() != 0) && reconnect_timer) {
328 g_timer_destroy(reconnect_timer);
329 reconnect_timer = NULL;
330 }
331 }
332
333 void
session_login_failed(void)334 session_login_failed(void)
335 {
336 if (reconnect_timer == NULL) {
337 log_debug("Connection handler: No reconnect timer");
338 sv_ev_failed_login();
339 _session_free_saved_account();
340 _session_free_saved_details();
341 } else {
342 log_debug("Connection handler: Restarting reconnect timer");
343 if (prefs_get_reconnect() != 0) {
344 g_timer_start(reconnect_timer);
345 }
346 }
347
348 connection_clear_data();
349 chat_sessions_clear();
350 presence_clear_sub_requests();
351 }
352
353 void
session_lost_connection(void)354 session_lost_connection(void)
355 {
356 /* this callback also clears all cached data */
357 sv_ev_lost_connection();
358 if (prefs_get_reconnect() != 0) {
359 assert(reconnect_timer == NULL);
360 reconnect_timer = g_timer_new();
361 } else {
362 _session_free_saved_account();
363 _session_free_saved_details();
364 }
365 }
366
367 void
session_init_activity(void)368 session_init_activity(void)
369 {
370 activity_state = ACTIVITY_ST_ACTIVE;
371 saved_status = NULL;
372 }
373
374 void
session_check_autoaway(void)375 session_check_autoaway(void)
376 {
377 jabber_conn_status_t conn_status = connection_get_status();
378 if (conn_status != JABBER_CONNECTED) {
379 return;
380 }
381
382 char* mode = prefs_get_string(PREF_AUTOAWAY_MODE);
383 gboolean check = prefs_get_boolean(PREF_AUTOAWAY_CHECK);
384 gint away_time = prefs_get_autoaway_time();
385 gint xa_time = prefs_get_autoxa_time();
386 int away_time_ms = away_time * 60000;
387 int xa_time_ms = xa_time * 60000;
388
389 char* account = session_get_account_name();
390 resource_presence_t curr_presence = accounts_get_last_presence(account);
391 char* curr_status = accounts_get_last_status(account);
392
393 unsigned long idle_ms = ui_get_idle_time();
394
395 switch (activity_state) {
396 case ACTIVITY_ST_ACTIVE:
397 if (idle_ms >= away_time_ms) {
398 if (g_strcmp0(mode, "away") == 0) {
399 if ((curr_presence == RESOURCE_ONLINE) || (curr_presence == RESOURCE_CHAT) || (curr_presence == RESOURCE_DND)) {
400 activity_state = ACTIVITY_ST_AWAY;
401
402 // save current presence
403 saved_presence = curr_presence;
404 if (saved_status) {
405 free(saved_status);
406 }
407 if (curr_status) {
408 saved_status = strdup(curr_status);
409 } else {
410 saved_status = NULL;
411 }
412
413 // send away presence with last activity
414 char* message = prefs_get_string(PREF_AUTOAWAY_MESSAGE);
415 connection_set_presence_msg(message);
416 if (prefs_get_boolean(PREF_LASTACTIVITY)) {
417 cl_ev_presence_send(RESOURCE_AWAY, idle_ms / 1000);
418 } else {
419 cl_ev_presence_send(RESOURCE_AWAY, 0);
420 }
421
422 int pri = accounts_get_priority_for_presence_type(account, RESOURCE_AWAY);
423 if (message) {
424 cons_show("Idle for %d minutes, status set to away (priority %d), \"%s\".", away_time, pri, message);
425 } else {
426 cons_show("Idle for %d minutes, status set to away (priority %d).", away_time, pri);
427 }
428 g_free(message);
429
430 title_bar_set_presence(CONTACT_AWAY);
431 }
432 } else if (g_strcmp0(mode, "idle") == 0) {
433 activity_state = ACTIVITY_ST_IDLE;
434
435 // send current presence with last activity
436 connection_set_presence_msg(curr_status);
437 cl_ev_presence_send(curr_presence, idle_ms / 1000);
438 }
439 }
440 break;
441 case ACTIVITY_ST_IDLE:
442 if (check && (idle_ms < away_time_ms)) {
443 activity_state = ACTIVITY_ST_ACTIVE;
444
445 cons_show("No longer idle.");
446
447 // send current presence without last activity
448 connection_set_presence_msg(curr_status);
449 cl_ev_presence_send(curr_presence, 0);
450 }
451 break;
452 case ACTIVITY_ST_AWAY:
453 if (xa_time_ms > 0 && (idle_ms >= xa_time_ms)) {
454 activity_state = ACTIVITY_ST_XA;
455
456 // send extended away presence with last activity
457 char* message = prefs_get_string(PREF_AUTOXA_MESSAGE);
458 connection_set_presence_msg(message);
459 if (prefs_get_boolean(PREF_LASTACTIVITY)) {
460 cl_ev_presence_send(RESOURCE_XA, idle_ms / 1000);
461 } else {
462 cl_ev_presence_send(RESOURCE_XA, 0);
463 }
464
465 int pri = accounts_get_priority_for_presence_type(account, RESOURCE_XA);
466 if (message) {
467 cons_show("Idle for %d minutes, status set to xa (priority %d), \"%s\".", xa_time, pri, message);
468 } else {
469 cons_show("Idle for %d minutes, status set to xa (priority %d).", xa_time, pri);
470 }
471 g_free(message);
472
473 title_bar_set_presence(CONTACT_XA);
474 } else if (check && (idle_ms < away_time_ms)) {
475 activity_state = ACTIVITY_ST_ACTIVE;
476
477 cons_show("No longer idle.");
478
479 // send saved presence without last activity
480 connection_set_presence_msg(saved_status);
481 cl_ev_presence_send(saved_presence, 0);
482 contact_presence_t contact_pres = contact_presence_from_resource_presence(saved_presence);
483 title_bar_set_presence(contact_pres);
484 }
485 break;
486 case ACTIVITY_ST_XA:
487 if (check && (idle_ms < away_time_ms)) {
488 activity_state = ACTIVITY_ST_ACTIVE;
489
490 cons_show("No longer idle.");
491
492 // send saved presence without last activity
493 connection_set_presence_msg(saved_status);
494 cl_ev_presence_send(saved_presence, 0);
495 contact_presence_t contact_pres = contact_presence_from_resource_presence(saved_presence);
496 title_bar_set_presence(contact_pres);
497 }
498 break;
499 }
500
501 free(curr_status);
502 g_free(mode);
503 }
504
505 static void
_session_reconnect(void)506 _session_reconnect(void)
507 {
508 // reconnect with account.
509 ProfAccount* account = accounts_get_account(saved_account.name);
510 if (account == NULL) {
511 log_error("Unable to reconnect, account no longer exists: %s", saved_account.name);
512 return;
513 }
514
515 char* jid = NULL;
516 if (account->resource) {
517 jid = create_fulljid(account->jid, account->resource);
518 } else {
519 jid = strdup(account->jid);
520 }
521
522 log_debug("Attempting reconnect with account %s", account->name);
523 connection_connect(jid, saved_account.passwd, account->server, account->port, account->tls_policy, account->auth_policy);
524 free(jid);
525 account_free(account);
526 g_timer_start(reconnect_timer);
527 }
528
529 static void
_session_free_saved_account(void)530 _session_free_saved_account(void)
531 {
532 FREE_SET_NULL(saved_account.name);
533 FREE_SET_NULL(saved_account.passwd);
534 }
535
536 static void
_session_free_saved_details(void)537 _session_free_saved_details(void)
538 {
539 FREE_SET_NULL(saved_details.name);
540 FREE_SET_NULL(saved_details.jid);
541 FREE_SET_NULL(saved_details.passwd);
542 FREE_SET_NULL(saved_details.altdomain);
543 FREE_SET_NULL(saved_details.tls_policy);
544 FREE_SET_NULL(saved_details.auth_policy);
545 }
546