hexchat/plugins/checksum/checksum.c

266 lines
6.8 KiB
C
Raw Normal View History

2012-07-14 00:27:12 +04:00
/* HexChat
* Copyright (c) 2010-2012 Berke Viktor.
2011-02-28 20:59:32 +03:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2011-02-28 20:59:32 +03:00
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2011-08-11 10:12:35 +04:00
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
2011-02-28 20:59:32 +03:00
*/
2014-12-16 06:57:27 +03:00
#include "config.h"
#include <stdlib.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
2012-10-24 23:33:02 +04:00
#include "hexchat-plugin.h"
#define BUFSIZE 32768
#define DEFAULT_LIMIT 256 /* default size is 256 MiB */
#define SHA256_DIGEST_LENGTH 32
#define SHA256_BUFFER_LENGTH 65
2011-02-28 20:59:32 +03:00
2012-10-30 11:42:48 +04:00
static hexchat_plugin *ph; /* plugin handle */
2012-10-26 15:38:13 +04:00
static char name[] = "Checksum";
static char desc[] = "Calculate checksum for DCC file transfers";
static char version[] = "3.1";
2011-02-28 20:59:32 +03:00
static void
set_limit (char *size)
2011-02-28 20:59:32 +03:00
{
int limit = atoi (size);
if (limit > 0 && limit < INT_MAX)
2011-02-28 20:59:32 +03:00
{
if (hexchat_pluginpref_set_int (ph, "limit", limit))
hexchat_printf (ph, "Checksum: File size limit has successfully been set to: %d MiB\n", limit);
else
hexchat_printf (ph, "Checksum: File access error while saving!\n");
2011-02-28 20:59:32 +03:00
}
else
2011-02-28 20:59:32 +03:00
{
hexchat_printf (ph, "Checksum: Invalid input!\n");
2011-02-28 20:59:32 +03:00
}
}
static int
get_limit ()
2011-02-28 20:59:32 +03:00
{
int size = hexchat_pluginpref_get_int (ph, "limit");
2011-02-28 20:59:32 +03:00
if (size <= 0 || size >= INT_MAX)
return DEFAULT_LIMIT;
else
return size;
}
2011-02-28 20:59:32 +03:00
static gboolean
check_limit (GFile *file)
{
GFileInfo *file_info;
goffset file_size;
2011-02-28 20:59:32 +03:00
file_info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE,
NULL, NULL);
2011-02-28 20:59:32 +03:00
if (!file_info)
return FALSE;
file_size = g_file_info_get_size (file_info);
g_object_unref (file_info);
2011-02-28 20:59:32 +03:00
if (file_size > get_limit () * 1048576ll)
return FALSE;
2011-02-28 20:59:32 +03:00
return TRUE;
2011-02-28 20:59:32 +03:00
}
static gboolean
sha256_from_stream (GFileInputStream *file_stream, char out_buf[])
2011-02-28 20:59:32 +03:00
{
GChecksum *checksum;
gssize bytes_read;
guint8 digest[SHA256_DIGEST_LENGTH];
gsize digest_len = sizeof(digest);
guchar buffer[BUFSIZE];
gsize i;
2011-02-28 20:59:32 +03:00
checksum = g_checksum_new (G_CHECKSUM_SHA256);
while ((bytes_read = g_input_stream_read (G_INPUT_STREAM (file_stream), buffer, sizeof (buffer), NULL, NULL)))
2011-02-28 20:59:32 +03:00
{
if (bytes_read == -1)
2011-02-28 20:59:32 +03:00
{
g_checksum_free (checksum);
return FALSE;
2011-02-28 20:59:32 +03:00
}
g_checksum_update (checksum, buffer, bytes_read);
2011-02-28 20:59:32 +03:00
}
g_checksum_get_digest (checksum, digest, &digest_len);
g_checksum_free (checksum);
for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
2011-02-28 20:59:32 +03:00
{
/* out_buf will be exactly SHA256_BUFFER_LENGTH including null */
g_sprintf (out_buf + (i * 2), "%02x", digest[i]);
2011-02-28 20:59:32 +03:00
}
return TRUE;
2011-02-28 20:59:32 +03:00
}
static gboolean
sha256_from_file (char *filename, char out_buf[])
2011-02-28 20:59:32 +03:00
{
GFileInputStream *file_stream;
char *filename_fs;
GFile *file;
2011-02-28 20:59:32 +03:00
filename_fs = g_filename_from_utf8 (filename, -1, NULL, NULL, NULL);
if (!filename_fs)
2011-02-28 20:59:32 +03:00
{
hexchat_printf (ph, "Checksum: Invalid filename (%s)\n", filename);
return FALSE;
}
file = g_file_new_for_path (filename_fs);
g_free (filename_fs);
if (!file)
2011-02-28 20:59:32 +03:00
{
hexchat_printf (ph, "Checksum: Failed to open %s\n", filename);
return FALSE;
2011-02-28 20:59:32 +03:00
}
if (!check_limit (file))
{
hexchat_printf (ph, "Checksum: %s is larger than size limit. You can increase it with /CHECKSUM SET.\n", filename);
g_object_unref (file);
return FALSE;
}
file_stream = g_file_read (file, NULL, NULL);
if (!file_stream)
{
hexchat_printf (ph, "Checksum: Failed to read file %s\n", filename);
g_object_unref (file);
return FALSE;
}
2011-02-28 20:59:32 +03:00
if (!sha256_from_stream (file_stream, out_buf))
2011-02-28 20:59:32 +03:00
{
hexchat_printf (ph, "Checksum: Failed to generate checksum for %s\n", filename);
g_object_unref (file_stream);
g_object_unref (file);
return FALSE;
}
g_object_unref (file_stream);
g_object_unref (file);
return TRUE;
}
static int
dccrecv_cb (char *word[], void *userdata)
{
const char *dcc_completed_dir;
char *filename, checksum[SHA256_BUFFER_LENGTH];
/* Print in the privmsg tab of the sender */
hexchat_set_context (ph, hexchat_find_context (ph, NULL, word[3]));
if (hexchat_get_prefs (ph, "dcc_completed_dir", &dcc_completed_dir, NULL) == 1 && dcc_completed_dir[0] != '\0')
filename = g_build_filename (dcc_completed_dir, word[1], NULL);
else
filename = g_strdup (word[2]);
if (sha256_from_file (filename, checksum))
2011-02-28 20:59:32 +03:00
{
hexchat_printf (ph, "SHA-256 checksum for %s (local): %s\n", word[1], checksum);
2011-02-28 20:59:32 +03:00
}
g_free (filename);
2012-10-30 10:40:37 +04:00
return HEXCHAT_EAT_NONE;
2011-02-28 20:59:32 +03:00
}
static int
dccoffer_cb (char *word[], void *userdata)
{
char checksum[SHA256_BUFFER_LENGTH];
2011-02-28 20:59:32 +03:00
/* Print in the privmsg tab of the receiver */
hexchat_set_context (ph, hexchat_find_context (ph, NULL, word[3]));
if (sha256_from_file (word[3], checksum))
2011-02-28 20:59:32 +03:00
{
hexchat_commandf (ph, "quote PRIVMSG %s :SHA-256 checksum for %s (remote): %s", word[2], word[1], checksum);
2011-02-28 20:59:32 +03:00
}
2012-10-30 10:40:37 +04:00
return HEXCHAT_EAT_NONE;
2011-02-28 20:59:32 +03:00
}
2012-10-26 15:38:13 +04:00
static int
checksum (char *word[], char *word_eol[], void *userdata)
2011-02-28 20:59:32 +03:00
{
if (!g_ascii_strcasecmp ("GET", word[2]))
2011-02-28 20:59:32 +03:00
{
hexchat_printf (ph, "File size limit for checksums: %d MiB", get_limit ());
}
else if (!g_ascii_strcasecmp ("SET", word[2]))
2011-02-28 20:59:32 +03:00
{
set_limit (word[3]);
}
else
2011-02-28 20:59:32 +03:00
{
2012-11-26 15:49:28 +04:00
hexchat_printf (ph, "Usage: /CHECKSUM GET|SET\n");
2012-10-30 11:42:48 +04:00
hexchat_printf (ph, " GET - print the maximum file size (in MiB) to be hashed\n");
hexchat_printf (ph, " SET <filesize> - set the maximum file size (in MiB) to be hashed\n");
2011-02-28 20:59:32 +03:00
}
2012-10-26 15:38:13 +04:00
return HEXCHAT_EAT_ALL;
2011-02-28 20:59:32 +03:00
}
int
2012-10-30 11:42:48 +04:00
hexchat_plugin_init (hexchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg)
2011-02-28 20:59:32 +03:00
{
ph = plugin_handle;
*plugin_name = name;
*plugin_desc = desc;
*plugin_version = version;
2011-02-28 20:59:32 +03:00
/* this is required for the very first run */
2012-10-30 11:42:48 +04:00
if (hexchat_pluginpref_get_int (ph, "limit") == -1)
{
2012-10-30 11:42:48 +04:00
hexchat_pluginpref_set_int (ph, "limit", DEFAULT_LIMIT);
}
2011-02-28 20:59:32 +03:00
hexchat_hook_command (ph, "CHECKSUM", HEXCHAT_PRI_NORM, checksum, "Usage: /CHECKSUM GET|SET", NULL);
2012-10-30 11:42:48 +04:00
hexchat_hook_print (ph, "DCC RECV Complete", HEXCHAT_PRI_NORM, dccrecv_cb, NULL);
hexchat_hook_print (ph, "DCC Offer", HEXCHAT_PRI_NORM, dccoffer_cb, NULL);
2011-02-28 20:59:32 +03:00
2012-10-30 11:42:48 +04:00
hexchat_printf (ph, "%s plugin loaded\n", name);
2011-02-28 20:59:32 +03:00
return 1;
}
int
2012-10-30 11:42:48 +04:00
hexchat_plugin_deinit (void)
2011-02-28 20:59:32 +03:00
{
2012-10-30 11:42:48 +04:00
hexchat_printf (ph, "%s plugin unloaded\n", name);
2011-02-28 20:59:32 +03:00
return 1;
}