Implement STS profile serialisation and deserialisation.

This commit is contained in:
Sadie Powell 2021-05-29 18:47:15 +01:00
parent 2dbc6adbc2
commit 1d65bfdf08
6 changed files with 158 additions and 0 deletions

View File

@ -36,6 +36,7 @@
<ClInclude Include="server.h" />
<ClInclude Include="servlist.h" />
<ClInclude Include="ssl.h" />
<ClInclude Include="sts.h" />
<ClInclude Include="sysinfo\sysinfo.h" />
<ClInclude Include="text.h" />
<ClInclude Include="$(HexChatLib)textenums.h" />
@ -69,6 +70,7 @@
<ClCompile Include="server.c" />
<ClCompile Include="servlist.c" />
<ClCompile Include="ssl.c" />
<ClCompile Include="sts.c" />
<ClCompile Include="sysinfo\win32\backend.c" />
<ClCompile Include="text.c" />
<ClCompile Include="tree.c" />

View File

@ -74,6 +74,9 @@
<ClInclude Include="ssl.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="sts.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="text.h">
<Filter>Header Files</Filter>
</ClInclude>
@ -172,6 +175,9 @@
<ClCompile Include="ssl.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="sts.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="text.c">
<Filter>Source Files</Filter>
</ClCompile>

View File

@ -48,6 +48,7 @@
#include "notify.h"
#include "server.h"
#include "servlist.h"
#include "sts.h"
#include "outbound.h"
#include "text.h"
#include "url.h"
@ -844,6 +845,7 @@ xchat_init (void)
sound_load ();
notify_load ();
ignore_load ();
sts_load ();
g_snprintf (buf, sizeof (buf),
"NAME %s~%s~\n" "CMD query %%s\n\n"\
@ -996,6 +998,7 @@ hexchat_exit (void)
sound_save ();
notify_save ();
ignore_save ();
sts_save ();
free_sessions ();
chanopt_save_all (TRUE);
servlist_cleanup ();

View File

@ -17,6 +17,7 @@ common_sources = [
'proto-irc.c',
'server.c',
'servlist.c',
'sts.c',
'text.c',
'tree.c',
'url.c',

94
src/common/sts.c Normal file
View File

@ -0,0 +1,94 @@
/* HexChat
* Copyright (C) 1998-2010 Peter Zelezny.
* Copyright (C) 2009-2013 Berke Viktor.
*
* 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 St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <fcntl.h>
#include <time.h>
#include "cfgfiles.h"
#include "sts.h"
#include "util.h"
#define STS_CONFIG_FILE "sts.conf"
GSList *profiles = NULL;
void
sts_load (void)
{
char buf[256];
int fh;
struct sts_profile *profile;
fh = hexchat_open_file (STS_CONFIG_FILE, O_RDONLY, 0, 0);
if (fh < 0)
return; /* Filesystem not readable. */
while (waitline (fh, buf, sizeof buf, FALSE) != -1)
{
profile = sts_new ();
if (sscanf (buf, "%s %u %ld", profile->host, &profile->port, &profile->expiry) != 3)
{
/* Malformed profile; drop it. */
g_free (profile);
continue;
}
profiles = g_slist_append (profiles, profile);
}
}
struct sts_profile *
sts_new (void)
{
struct sts_profile *profile;
profile = malloc (sizeof (struct sts_profile));
profile->host[0] = 0;
profile->port = 0;
profile->expiry = 0;
return profile;
}
void
sts_save (void)
{
char buf[512];
int fh;
const GSList *next;
struct sts_profile *nextprofile;
time_t now;
fh = hexchat_open_file (STS_CONFIG_FILE, O_TRUNC | O_WRONLY | O_CREAT, 0600, XOF_DOMODE);
if (fh < 0)
return; /* Filesystem not writable. */
now = time (NULL);
for (next = profiles; next; next = profiles->next)
{
nextprofile = (struct sts_profile *)next->data;
if (now >= nextprofile->expiry)
continue; /* Profile has expired. */
g_snprintf (buf, sizeof buf, "%s %u %ld\n", nextprofile->host, nextprofile->port,
(unsigned long)nextprofile->expiry);
write (fh, buf, strlen (buf));
}
close (fh);
}

52
src/common/sts.h Normal file
View File

@ -0,0 +1,52 @@
/* HexChat
* Copyright (C) 1998-2010 Peter Zelezny.
* Copyright (C) 2009-2013 Berke Viktor.
*
* 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 St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef HEXCHAT_STS_H
#define HEXCHAT_STS_H
#include <time.h>
#include <glib.h>
/* Encapsulates a STS profile.
*
* See https://ircv3.net/specs/extensions/sts
*/
struct sts_profile
{
/* The hostname the profile applies to. */
char host[256];
/* The port all secure connections should happen on. */
unsigned int port;
/* The time at which this STS profile expires. */
time_t expiry;
};
/* Loads STS profiles from sts.conf */
void sts_load (void);
/* Creates a new empty STS profile. */
struct sts_profile * sts_new (void);
/* Saves STS profiles to sts.conf */
void sts_save (void);
#endif