Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rmnews.c « src - gitlab.com/Remmina/Remmina.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f44af8704232070c8f2ee8c62c9e584f15f531e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
 * Remmina - The GTK+ Remote Desktop Client
 * Copyright (C) 2016-2022 Antenore Gatta, Giovanni Panozzo
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of portions of this program with the
 *  OpenSSL library under certain conditions as described in each
 *  individual source file, and distribute linked combinations
 *  including the two.
 *  You must obey the GNU General Public License in all respects
 *  for all of the code used other than OpenSSL. *  If you modify
 *  file(s) with this exception, you may extend this exception to your
 *  version of the file(s), but you are not obligated to do so. *  If you
 *  do not wish to do so, delete this exception statement from your
 *  version. *  If you delete this exception statement from all source
 *  files in the program, then also delete it here.
 *
 */

#include "config.h"
#include "remmina/remmina_trace_calls.h"

#include <fcntl.h>
#include <gio/gdesktopappinfo.h>
#include <gio/gio.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <libsoup/soup.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>

#include "remmina.h"
#include "remmina_main.h"
#include "remmina_log.h"
#include "remmina_pref.h"
#include "remmina_public.h"
#include "remmina_sodium.h"
#include "remmina_utils.h"
#include "remmina_scheduler.h"
#include "remmina_sysinfo.h"
#include "rmnews.h"

#define ARR_SIZE(arr) (sizeof((arr)) / sizeof((arr[0])))
/* Neas file buffer */
#define READ_BUFFER_LEN 1024
/* Timers */
#define RMNEWS_CHECK_1ST_MS 3000
#define RMNEWS_CHECK_INTERVAL_MS 12000
/* How many seconds before to get news */
#define RMNEWS_INTERVAL_SEC 604800
/* TODO: move in config.h */
#define REMMINA_URL "https://remmina.org/"
#define RMNEWS_OUTPUT "/var/tmp/latest_news.md"

static RemminaNewsDialog *rmnews_news_dialog;
#define GET_OBJ(object_name) gtk_builder_get_object(rmnews_news_dialog->builder, object_name)

static SoupSession *session;

#if SOUP_MAJOR_VERSION < 3
#define soup_message_get_status(message) message->status_code
#define soup_message_get_response_headers(message) message->response_headers
#endif

static const gchar *output_file_path = NULL;

static
const gchar *supported_mime_types[] = {
	"x-scheme-handler/rdp",
	"x-scheme-handler/spice",
	"x-scheme-handler/vnc",
	"x-scheme-handler/remmina",
	"application/x-remmina",
	NULL
};

gint eweekdays[7] = {
	86400,
	172800,
	259200,
	345600,
	432000,
	518400,
	604800
};


#if SOUP_CHECK_VERSION (2, 99, 2)
static void rmnews_on_stream_splice (GObject *source, GAsyncResult *result, gpointer user_data)
{
        GError *error = NULL;
        g_output_stream_splice_finish (G_OUTPUT_STREAM (source),
                                       result,
                                       &error);
        if (error) {
                g_printerr ("Failed to download: %s\n", error->message);
                g_error_free (error);
                return;
        }

}
#endif

void rmnews_news_switch_state_set_cb()
{
	TRACE_CALL(__func__);
	if (rmnews_news_dialog->rmnews_news_switch && \
	    gtk_switch_get_active(rmnews_news_dialog->rmnews_news_switch)) {
		remmina_pref.periodic_news_permitted = TRUE;
		remmina_pref_save();
	} else {
		remmina_pref.periodic_news_permitted = FALSE;
		remmina_pref_save();
	}
}

void rmnews_defaultcl_on_click()
{
	TRACE_CALL(__func__);
	g_autoptr(GError) error = NULL;
	GDesktopAppInfo *desktop_info;
	GAppInfo *info = NULL;
	g_autofree gchar *id = g_strconcat(REMMINA_APP_ID, ".desktop", NULL);
	int i;

	desktop_info = g_desktop_app_info_new(id);
	if (!desktop_info)
		return;

	info = G_APP_INFO(desktop_info);

	for (i = 0; supported_mime_types[i]; i++) {
		if (!g_app_info_set_as_default_for_type(info, supported_mime_types[i], &error))
			REMMINA_DEBUG("Failed to set '%s' as the default application for secondary content type '%s': %s",
				  g_app_info_get_name(info), supported_mime_types[i], error->message);
		else
			REMMINA_DEBUG("Set '%s' as the default application for '%s'",
				      g_app_info_get_name(info),
				      supported_mime_types[i]);
	}
}

static gchar *rmnews_get_file_contents(gchar *path)
{
	gsize size;
	gchar *content;

	if (g_file_get_contents(path, &content, &size, NULL)) {
		if (!g_utf8_validate(content, size, NULL)) {
			REMMINA_DEBUG("%s content is not UTF-8", path);
			g_free(content);
			content = NULL;
		}
	}
	//return g_markup_escape_text(content, strlen(content));
	return content;
}

static void rmnews_close_clicked(GtkButton *btn, gpointer user_data)
{
	TRACE_CALL(__func__);
	if (rmnews_news_dialog->dialog)
		gtk_widget_destroy(GTK_WIDGET(rmnews_news_dialog->dialog));
	rmnews_news_dialog->dialog = NULL;
	g_free(rmnews_news_dialog);
	rmnews_news_dialog = NULL;
}

static gboolean rmnews_dialog_deleted(GtkButton *btn, gpointer user_data)
{
	TRACE_CALL(__func__);
	gtk_widget_destroy(GTK_WIDGET(rmnews_news_dialog->dialog));
	rmnews_news_dialog->dialog = NULL;
	g_free(rmnews_news_dialog);
	rmnews_news_dialog = NULL;

	return FALSE;
}

void rmnews_show_news(GtkWindow *parent)
{
	TRACE_CALL(__func__);

	if (disablenews) return;

	rmnews_news_dialog = g_new0(RemminaNewsDialog, 1);
	rmnews_news_dialog->retval = 1;

	rmnews_news_dialog->builder = remmina_public_gtk_builder_new_from_resource("/org/remmina/Remmina/src/../data/ui/remmina_news.glade");
	rmnews_news_dialog->dialog = GTK_DIALOG(gtk_builder_get_object(rmnews_news_dialog->builder, "RemminaNewsDialog"));

	rmnews_news_dialog->rmnews_text_view = GTK_TEXT_VIEW(GET_OBJ("rmnews_text_view"));
	rmnews_news_dialog->rmnews_label = GTK_LABEL(GET_OBJ("rmnews_label"));
	rmnews_news_dialog->rmnews_defaultcl_label = GTK_LABEL(GET_OBJ("rmnews_defaultcl_label"));
	rmnews_news_dialog->rmnews_defaultcl_button = GTK_BUTTON(GET_OBJ("rmnews_defaultcl_switch"));
	rmnews_news_dialog->rmnews_news_switch = GTK_SWITCH(GET_OBJ("rmnews_news_switch"));
	if (remmina_pref.periodic_news_permitted == 1)
		gtk_switch_set_active(rmnews_news_dialog->rmnews_news_switch, TRUE);
	else
		gtk_switch_set_active(rmnews_news_dialog->rmnews_news_switch, FALSE);
	gtk_widget_set_sensitive(GTK_WIDGET(rmnews_news_dialog->rmnews_news_switch), RMNEWS_ENABLE_NEWS);

	rmnews_news_dialog->rmnews_button_close = GTK_BUTTON(GET_OBJ("rmnews_button_close"));
	gtk_widget_set_can_default(GTK_WIDGET(rmnews_news_dialog->rmnews_button_close), TRUE);
	gtk_widget_grab_default(GTK_WIDGET(rmnews_news_dialog->rmnews_button_close));

	if (remmina_pref.periodic_news_permitted == 1) {
		gchar *contents = rmnews_get_file_contents(g_strdup(output_file_path));
		if (contents) {
			gtk_label_set_markup(rmnews_news_dialog->rmnews_label, contents);
			g_free(contents);
		}
	}

	g_signal_connect(rmnews_news_dialog->rmnews_button_close, "clicked",
			 G_CALLBACK(rmnews_close_clicked), (gpointer)rmnews_news_dialog);
	g_signal_connect(rmnews_news_dialog->dialog, "close",
			 G_CALLBACK(rmnews_close_clicked), NULL);
	g_signal_connect(rmnews_news_dialog->dialog, "delete-event",
			 G_CALLBACK(rmnews_dialog_deleted), NULL);

	/* Connect signals */
	gtk_builder_connect_signals(rmnews_news_dialog->builder, NULL);

	/* Show the non-modal news dialog */
	gtk_widget_show_all(GTK_WIDGET(rmnews_news_dialog->dialog));
	gtk_window_present(GTK_WINDOW(rmnews_news_dialog->dialog));
	if (parent)
		gtk_window_set_transient_for(GTK_WINDOW(rmnews_news_dialog->dialog), parent);
	gtk_window_set_modal(GTK_WINDOW(rmnews_news_dialog->dialog), TRUE);
}

#if SOUP_CHECK_VERSION (2, 99, 2)
static void rmnews_get_url_cb (GObject *source, GAsyncResult *result, gpointer user_data)
{
	TRACE_CALL(__func__);
	const char *name;
	const char *header;
	GFile *output_file;
	gchar *filesha = NULL;
	gchar *filesha_after = NULL;

	GError *error = NULL;
        GInputStream *in = soup_session_send_finish (SOUP_SESSION (source), result, &error);

	if (error) {
		REMMINA_DEBUG ("Failed to send request: %s", error->message);
		g_error_free (error);
		return;
	}

	GDateTime *gdt = g_date_time_new_now_utc();
	gint64 unixts = g_date_time_to_unix(gdt);
	g_date_time_unref(gdt);

        if (output_file_path) {
		REMMINA_DEBUG("Calculating the SHA1 of the local file");
		filesha = remmina_sha1_file(output_file_path);
		REMMINA_DEBUG("SHA1 is %s", filesha);
		if (filesha == NULL || filesha[0] == 0) filesha = "0\0";
		REMMINA_DEBUG("Opening %s output file for writing", output_file_path);
                GFile *output_file = g_file_new_for_commandline_arg (output_file_path);
		GOutputStream *out = G_OUTPUT_STREAM (g_file_replace (output_file, NULL, NULL,
							G_FILE_CREATE_REPLACE_DESTINATION, NULL, &error));
                if (error) {
			REMMINA_DEBUG("Failed to create \"%s\": %s", output_file_path, error->message);
			remmina_pref.periodic_rmnews_last_get = unixts;
			REMMINA_DEBUG ("periodic_rmnews_last_get set to %ld", remmina_pref.periodic_rmnews_last_get);
			REMMINA_DEBUG ("Saving preferences");
			remmina_pref_save();
			g_free(filesha); filesha = NULL;
                        g_error_free (error);
                        g_object_unref (in);
                        g_object_unref (output_file);
			g_object_unref (out);
                        return;
                }

		/* Start downloading to the file */
		// g_output_stream_splice_async (G_OUTPUT_STREAM (out), in,
		// 		G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
		// 		G_PRIORITY_DEFAULT,
		// 		NULL,
		// 		rmnews_on_stream_splice,
		// NULL);
		g_output_stream_splice (G_OUTPUT_STREAM (out), in,
			  G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
			  NULL,
			  &error);

		if (error) {
			REMMINA_DEBUG ("Failed to download: %s", error->message);
			remmina_pref.periodic_rmnews_last_get = unixts;
			REMMINA_DEBUG ("periodic_rmnews_last_get set to %ld", remmina_pref.periodic_rmnews_last_get);
			REMMINA_DEBUG ("Saving preferences");
			remmina_pref_save();
			g_free(filesha); filesha = NULL;
			g_error_free (error);
                        g_object_unref (in);
                        g_object_unref (output_file);
			g_object_unref (out);
			return;
		}


		filesha_after = remmina_sha1_file(output_file_path);

		REMMINA_DEBUG("SHA1 after download is %s", filesha_after);
		if (g_strcmp0(filesha, filesha_after) != 0) {
			REMMINA_DEBUG("SHA1 differs, we show the news and reset the counter");
			remmina_pref.periodic_rmnews_last_get = 0;
			REMMINA_DEBUG ("periodic_rmnews_last_get set to %ld", remmina_pref.periodic_rmnews_last_get);
			REMMINA_DEBUG ("Saving preferences");
			GtkWindow *parent = remmina_main_get_window();
			if (!kioskmode && kioskmode == FALSE)
			rmnews_show_news(parent);
		} else {
			remmina_pref.periodic_rmnews_last_get = unixts;
		}
		/* Increase counter with number of successful GETs */
		remmina_pref.periodic_rmnews_get_count = remmina_pref.periodic_rmnews_get_count + 1;
		remmina_pref_save();
		g_free(filesha); filesha = NULL;
		g_object_unref (out);
	} else {
		REMMINA_DEBUG("Cannot open output file for writing, because output_file_path is NULL");
		remmina_pref.periodic_rmnews_last_get = unixts;
		REMMINA_DEBUG ("periodic_rmnews_last_get set to %ld", remmina_pref.periodic_rmnews_last_get);
		REMMINA_DEBUG ("Saving preferences");
		remmina_pref_save();
		return;
        }

        g_object_unref (in);


}
#else
static void rmnews_get_url_cb(SoupSession *session, SoupMessage *msg, gpointer data)
{
	TRACE_CALL(__func__);
	const char *name;
	const char *header;
	g_autoptr(SoupBuffer) sb;
	FILE *output_file = NULL;
	gchar *filesha = NULL;
	gchar *filesha_after = NULL;
	GDateTime *gdt;
	gint64 unixts;
	gint status;

	status = soup_message_get_status(msg);
	REMMINA_DEBUG("Status code %d", status);

	name = soup_message_get_uri(msg)->path;

	gdt = g_date_time_new_now_utc();
	unixts = g_date_time_to_unix(gdt);
	g_date_time_unref(gdt);

	if (SOUP_STATUS_IS_REDIRECTION(status)) {
		header = soup_message_headers_get_one(soup_message_get_response_headers(msg),
						      "Location");
		REMMINA_DEBUG("Redirection detected");
		if (header) {
			SoupURI *uri;
			char *uri_string;

			REMMINA_DEBUG("  -> %s\n", header);

			uri = soup_uri_new_with_base(soup_message_get_uri(msg), header);
			uri_string = soup_uri_to_string(uri, FALSE);
			rmnews_get_url(uri_string);
			g_free(uri_string);
			soup_uri_free(uri);
		}
		remmina_pref.periodic_rmnews_last_get = unixts;
		REMMINA_DEBUG ("periodic_rmnews_last_get set to %ld", remmina_pref.periodic_rmnews_last_get);
		REMMINA_DEBUG ("Saving preferences");
		remmina_pref_save();
		return;
	}

	if (!SOUP_STATUS_IS_SUCCESSFUL(status)) {
		REMMINA_DEBUG ("Could not access %s: %s", name , soup_status_get_phrase(status));
		REMMINA_DEBUG ("periodic_rmnews_last_get set to %ld", remmina_pref.periodic_rmnews_last_get);
		REMMINA_DEBUG ("Saving preferences");
		remmina_pref_save();
		return;
	} else {
		REMMINA_DEBUG("Status 200");
		if (output_file_path) {
			REMMINA_DEBUG("Calculating the SHA1 of the local file");
			filesha = remmina_sha1_file(output_file_path);
			REMMINA_DEBUG("SHA1 is %s", filesha);
			if (filesha == NULL || filesha[0] == 0) filesha = "0\0";
			REMMINA_DEBUG("Opening %s output file for writing", output_file_path);
			output_file = fopen(output_file_path, "w");
			if (!output_file) {
				REMMINA_DEBUG("Error trying to create file %s.", output_file_path);
				remmina_pref.periodic_rmnews_last_get = unixts;
				REMMINA_DEBUG ("periodic_rmnews_last_get set to %ld", remmina_pref.periodic_rmnews_last_get);
				REMMINA_DEBUG ("Saving preferences");
				remmina_pref_save();
				g_free(filesha); filesha = NULL;
				return;
			}
		} else {
			REMMINA_DEBUG("Cannot open output file for writing, because output_file_path is NULL");
			remmina_pref.periodic_rmnews_last_get = unixts;
			REMMINA_DEBUG ("periodic_rmnews_last_get set to %ld", remmina_pref.periodic_rmnews_last_get);
			REMMINA_DEBUG ("Saving preferences");
			remmina_pref_save();
			return;
		}
		sb = soup_message_body_flatten(msg->response_body);
		if (output_file) {
			fwrite(sb->data, 1, sb->length, output_file);

			if (output_file_path) {
				fclose(output_file);
				filesha_after = remmina_sha1_file(output_file_path);
			}
			REMMINA_DEBUG("SHA1 after download is %s", filesha_after);
			if (g_strcmp0(filesha, filesha_after) != 0) {
				REMMINA_DEBUG("SHA1 differs, we show the news and reset the counter");
				remmina_pref.periodic_rmnews_last_get = 0;
				REMMINA_DEBUG ("periodic_rmnews_last_get set to %ld", remmina_pref.periodic_rmnews_last_get);
				REMMINA_DEBUG ("Saving preferences");
				GtkWindow *parent = remmina_main_get_window();
				if (!kioskmode && kioskmode == FALSE)
					rmnews_show_news(parent);
			} else {
				remmina_pref.periodic_rmnews_last_get = unixts;
			}
			/* Increase counter with number of successful GETs */
			remmina_pref.periodic_rmnews_get_count = remmina_pref.periodic_rmnews_get_count + 1;
			remmina_pref_save();
			g_free(filesha); filesha = NULL;
		}
	}

	g_object_unref(msg);
}
#endif

/**
 * Try to get a unique system+user ID to identify this remmina user
 * and avoid some duplicated task, especially on news management
 * @return a string
 * @warning The returned string must be freed with g_free.
 */
gchar *rmnews_get_uid()
{
	TRACE_CALL(__func__);
	GChecksum *chs;
	const gchar *uname, *hname;
	const gchar *uid_suffix;
	gchar *uid_prefix;
	gchar *uid;

	/* This code is very similar to remmina_stats_get_uid() */

	if (remmina_pref.periodic_rmnews_uuid_prefix == NULL || remmina_pref.periodic_rmnews_uuid_prefix[0] == 0) {
		/* Generate a new UUID_PREFIX for news on this installation */
		uid_prefix = remmina_gen_random_uuid();
		if (remmina_pref.periodic_rmnews_uuid_prefix)
			g_free(remmina_pref.periodic_rmnews_uuid_prefix);
		remmina_pref.periodic_rmnews_uuid_prefix = uid_prefix;
		remmina_pref_save();
	}

	uname = g_get_user_name();
	hname = g_get_host_name();
	chs = g_checksum_new(G_CHECKSUM_SHA256);
	g_checksum_update(chs, (const guchar *)uname, strlen(uname));
	g_checksum_update(chs, (const guchar *)hname, strlen(hname));
	uid_suffix = g_checksum_get_string(chs);

	uid = g_strdup_printf("02-%s-%.10s", remmina_pref.periodic_rmnews_uuid_prefix, uid_suffix);
	g_checksum_free(chs);

	return uid;
}

void rmnews_get_url(const char *url)
{
	TRACE_CALL(__func__);

	SoupMessage *msg;

	msg = soup_message_new("GET", url);
	soup_message_set_flags(msg, SOUP_MESSAGE_NO_REDIRECT);

	REMMINA_DEBUG("Fetching %s", url);

#if SOUP_CHECK_VERSION (2, 99, 2)
	// Use soup_session_send_async or soup_session_send_and_read_async
	soup_session_send_async ( session, msg, G_PRIORITY_DEFAULT,
			NULL,			// cancellable
			rmnews_get_url_cb,	// callback
			NULL);			// user_data
#else
	g_object_ref(msg);
	soup_session_queue_message(session, msg, rmnews_get_url_cb, NULL);
#endif
}

void rmnews_get_news()
{
	TRACE_CALL(__func__);

	SoupLogger *logger = NULL;
	int fd;
	gchar *uid;
	gchar mage[20], gcount[20];
	struct stat sb;

	gchar *cachedir = g_build_path("/", g_get_user_cache_dir(), REMMINA_APP_ID, NULL);
	gint d = g_mkdir_with_parents(cachedir, 0750);
	if (d < 0)
		output_file_path = RMNEWS_OUTPUT;
	else
		output_file_path = g_build_path("/", cachedir, "latest_news.md", NULL);

	REMMINA_DEBUG("Output file set to %s", output_file_path);

	if (remmina_pref.periodic_rmnews_last_get == 0 &&
	    remmina_pref.periodic_rmnews_get_count == 0) {
		g_file_set_contents(output_file_path, "", 0, NULL);
		/* Just a symolic date */
		remmina_pref.periodic_rmnews_last_get = 191469343000;
		REMMINA_DEBUG ("periodic_rmnews_last_get set to %ld", remmina_pref.periodic_rmnews_last_get);
		REMMINA_DEBUG ("Preferences NOT saved");
	}

	fd = g_open(output_file_path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
	REMMINA_DEBUG("Returned %d while creating %s", fd, output_file_path);
	/* If we cannot create the remmina_news file, we avoid connections */
	if (fd < 0) {
		REMMINA_DEBUG("Cannot store the remmina news file");
		return;
	}
	g_close(fd, NULL);

	REMMINA_DEBUG("Output file %s created successfully", output_file_path);

	if (output_file_path) {
	} else {
		REMMINA_DEBUG("Output file set to %s", output_file_path);
	}

	REMMINA_DEBUG("Gathering news");
	/* Build the session with all of the features we need */
	session = soup_session_new_with_options ("user-agent", "get ",
                                                 "accept-language-auto", TRUE,
                                                 "timeout", 15,
                                                 NULL);

#if SOUP_CHECK_VERSION (2, 99, 2)
	soup_session_add_feature_by_type (session, SOUP_TYPE_COOKIE_JAR);
	logger = soup_logger_new(SOUP_LOGGER_LOG_NONE);
#else
	session = g_object_new(SOUP_TYPE_SESSION,
			       SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_DECODER,
			       SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_COOKIE_JAR,
			       SOUP_SESSION_USER_AGENT, "get ",
			       SOUP_SESSION_ACCEPT_LANGUAGE_AUTO, TRUE,
			       NULL);
	logger = soup_logger_new(SOUP_LOGGER_LOG_NONE, -1);
#endif
	/* TODO: Catch log level and set SOUP_LOGGER_LOG_MINIMAL or more */
	soup_session_add_feature(session, SOUP_SESSION_FEATURE(logger));
	g_object_unref(logger);

	gchar *lang = remmina_utils_get_lang();
	REMMINA_DEBUG("Language %s", lang);

	uid = rmnews_get_uid();

	if (stat("/etc/machine-id", &sb) == 0)
		sprintf(mage, "%ld", (long)(time(NULL) - sb.st_mtim.tv_sec));
	else
		strcpy(mage, "0");

	sprintf(gcount, "%ld", remmina_pref.periodic_rmnews_get_count);

	rmnews_get_url(g_strconcat(REMMINA_URL,
				   "news/remmina_news.php?lang=",
				   lang,
				   "&ver="
				   VERSION,
				   "&uid=",
				   uid,
				   "&sa=0",
				   "&mage=",
				   mage,
				   "&gcount=",
				   gcount,
				   NULL));

	g_free(uid);
	g_object_unref(session);
}

static gboolean rmnews_periodic_check(gpointer user_data)
{
	TRACE_CALL(__func__);
	GDateTime *gdt;
	gint64 unixts;
	glong next = 0;

	gdt = g_date_time_new_now_utc();
	unixts = g_date_time_to_unix(gdt);
	g_date_time_unref(gdt);

	/* if remmina_pref is not writable ... */
	if (remmina_pref_is_rw() == FALSE && remmina_pref.periodic_rmnews_last_get == 0) {
		/* We randomly set periodic_rmnews_last_get to a day between today
		 * and 7 days ago */
		gint randidx = randombytes_uniform(8);
		REMMINA_DEBUG("Setting a random periodic_rmnews_last_get to %d - %d", unixts, eweekdays[randidx]);
		remmina_pref.periodic_rmnews_last_get = unixts - eweekdays[randidx];
	}
	//REMMINA_DEBUG("periodic_rmnews_last_get is %ld", remmina_pref.periodic_rmnews_last_get);

	if (remmina_pref.periodic_news_permitted == 0 && remmina_pref.periodic_rmnews_get_count < 1) {
		remmina_pref.periodic_rmnews_last_get =
			(remmina_pref.periodic_rmnews_last_get > 1514764800 ? remmina_pref.periodic_rmnews_last_get : 0);
		remmina_pref_save();
	}
	next = remmina_pref.periodic_rmnews_last_get + RMNEWS_INTERVAL_SEC;
	if (unixts > next || (unixts < remmina_pref.periodic_rmnews_last_get && unixts > 1514764800)) {
		//REMMINA_DEBUG("remmina_pref.periodic_news_permitted is %d", remmina_pref.periodic_news_permitted);
		if (remmina_pref.periodic_news_permitted == 1) {
			rmnews_get_news();
		} else if (remmina_pref.periodic_rmnews_get_count == 0) {
			rmnews_show_news(remmina_main_get_window());
			remmina_pref.periodic_rmnews_get_count = 1;
			remmina_pref_save();
		}
	}
	return G_SOURCE_CONTINUE;
}

void rmnews_schedule()
{
	TRACE_CALL(__func__);
	remmina_scheduler_setup(rmnews_periodic_check,
				NULL,
				RMNEWS_CHECK_1ST_MS,
				RMNEWS_CHECK_INTERVAL_MS);
}