1
0
mirror of https://github.com/moparisthebest/hexchat synced 2024-11-04 16:35:06 -05:00
hexchat/plugins/checksum/checksum.c

278 lines
6.8 KiB
C
Raw Normal View History

2012-07-13 16:27:12 -04:00
/* HexChat
* Copyright (c) 2010-2012 Berke Viktor.
2011-02-28 12:59:32 -05: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 12:59:32 -05:00
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2011-08-11 02: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 12:59:32 -05:00
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
2011-02-28 12:59:32 -05:00
#include <malloc.h>
#include <errno.h>
#include <sys/types.h>
#include <openssl/sha.h>
#include <glib.h>
2011-02-28 12:59:32 -05:00
#ifdef WIN32
2011-02-28 12:59:32 -05:00
#ifndef snprintf
#define snprintf _snprintf
#endif
#ifndef stat64
#define stat64 _stat64
#endif
#else
/* for INT_MAX */
#include <limits.h>
#define __USE_LARGEFILE64
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#endif
#include <sys/stat.h>
2012-10-24 15:33:02 -04:00
#include "hexchat-plugin.h"
#define BUFSIZE 32768
#define DEFAULT_LIMIT 256 /* default size is 256 MiB */
2011-02-28 12:59:32 -05:00
2012-10-30 03:42:48 -04:00
static hexchat_plugin *ph; /* plugin handle */
2012-10-26 07: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 12:59:32 -05:00
/* Use of OpenSSL SHA256 interface: http://adamlamers.com/?p=5 */
2011-02-28 12:59:32 -05:00
static void
sha256_hash_string (unsigned char hash[SHA256_DIGEST_LENGTH], char outputBuffer[65])
{
int i;
for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
sprintf (outputBuffer + (i * 2), "%02x", hash[i]);
}
outputBuffer[64] = 0;
}
static void
sha256 (char *string, char outputBuffer[65])
{
int i;
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init (&sha256);
SHA256_Update (&sha256, string, strlen (string));
SHA256_Final (hash, &sha256);
for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
sprintf (outputBuffer + (i * 2), "%02x", hash[i]);
}
outputBuffer[64] = 0;
}
static int
sha256_file (char *path, char outputBuffer[65])
{
int bytesRead;
unsigned char *buffer;
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
FILE *file = fopen (path, "rb");
if (!file)
{
return -534;
}
SHA256_Init (&sha256);
buffer = malloc (BUFSIZE);
bytesRead = 0;
if (!buffer)
{
return ENOMEM;
}
while ((bytesRead = fread (buffer, 1, BUFSIZE, file)))
{
SHA256_Update (&sha256, buffer, bytesRead);
}
SHA256_Final (hash, &sha256);
sha256_hash_string (hash, outputBuffer);
fclose (file);
free (buffer);
return 0;
}
static void
set_limit (char* size)
2011-02-28 12:59:32 -05:00
{
int buffer = atoi (size);
2011-02-28 12:59:32 -05:00
if (buffer > 0 && buffer < INT_MAX)
2011-02-28 12:59:32 -05:00
{
2012-10-30 03:42:48 -04:00
if (hexchat_pluginpref_set_int (ph, "limit", buffer))
2011-02-28 12:59:32 -05:00
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "File size limit has successfully been set to: %d MiB\n", buffer);
}
else
2011-02-28 12:59:32 -05:00
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "File access error while saving!\n");
2011-02-28 12:59:32 -05:00
}
}
else
2011-02-28 12:59:32 -05:00
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "Invalid input!\n");
2011-02-28 12:59:32 -05:00
}
}
static int
get_limit ()
2011-02-28 12:59:32 -05:00
{
2012-10-30 03:42:48 -04:00
int size = hexchat_pluginpref_get_int (ph, "limit");
2011-02-28 12:59:32 -05:00
if (size <= -1 || size >= INT_MAX)
2011-02-28 12:59:32 -05:00
{
return DEFAULT_LIMIT;
}
else
2011-02-28 12:59:32 -05:00
{
return size;
2011-02-28 12:59:32 -05:00
}
}
static void
print_limit ()
2011-02-28 12:59:32 -05:00
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "File size limit for checksums: %d MiB", get_limit ());
2011-02-28 12:59:32 -05:00
}
static int
dccrecv_cb (char *word[], void *userdata)
{
int result;
struct stat64 buffer; /* buffer for storing file info */
char sum[65]; /* buffer for checksum */
result = stat64 (word[2], &buffer);
if (result == 0) /* stat returns 0 on success */
{
if (buffer.st_size <= (unsigned long long) get_limit () * 1048576)
2011-02-28 12:59:32 -05:00
{
sha256_file (word[2], sum); /* word[2] is the full filename */
/* try to print the checksum in the privmsg tab of the sender */
2012-10-30 03:42:48 -04:00
hexchat_set_context (ph, hexchat_find_context (ph, NULL, word[3]));
hexchat_printf (ph, "SHA-256 checksum for %s (local): %s\n", word[1], sum);
}
else
2011-02-28 12:59:32 -05:00
{
2012-10-30 03:42:48 -04:00
hexchat_set_context (ph, hexchat_find_context (ph, NULL, word[3]));
hexchat_printf (ph, "SHA-256 checksum for %s (local): (size limit reached, no checksum calculated, you can increase it with /CHECKSUM INC)\n", word[1]);
2011-02-28 12:59:32 -05:00
}
}
else
2011-02-28 12:59:32 -05:00
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "File access error!\n");
2011-02-28 12:59:32 -05:00
}
2012-10-30 02:40:37 -04:00
return HEXCHAT_EAT_NONE;
2011-02-28 12:59:32 -05:00
}
static int
dccoffer_cb (char *word[], void *userdata)
{
int result;
struct stat64 buffer; /* buffer for storing file info */
char sum[65]; /* buffer for checksum */
result = stat64 (word[3], &buffer);
if (result == 0) /* stat returns 0 on success */
{
if (buffer.st_size <= (unsigned long long) get_limit () * 1048576)
2011-02-28 12:59:32 -05:00
{
sha256_file (word[3], sum); /* word[3] is the full filename */
2012-10-30 03:42:48 -04:00
hexchat_commandf (ph, "quote PRIVMSG %s :SHA-256 checksum for %s (remote): %s", word[2], word[1], sum);
}
else
2011-02-28 12:59:32 -05:00
{
2012-10-30 03:42:48 -04:00
hexchat_set_context (ph, hexchat_find_context (ph, NULL, word[3]));
hexchat_printf (ph, "quote PRIVMSG %s :SHA-256 checksum for %s (remote): (size limit reached, no checksum calculated)", word[2], word[1]);
2011-02-28 12:59:32 -05:00
}
}
else
2011-02-28 12:59:32 -05:00
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "File access error!\n");
2011-02-28 12:59:32 -05:00
}
2012-10-30 02:40:37 -04:00
return HEXCHAT_EAT_NONE;
2011-02-28 12:59:32 -05:00
}
2012-10-26 07:38:13 -04:00
static int
checksum (char *word[], char *word_eol[], void *userdata)
2011-02-28 12:59:32 -05:00
{
if (!g_ascii_strcasecmp ("GET", word[2]))
2011-02-28 12:59:32 -05:00
{
print_limit ();
}
else if (!g_ascii_strcasecmp ("SET", word[2]))
2011-02-28 12:59:32 -05:00
{
set_limit (word[3]);
}
else
2011-02-28 12:59:32 -05:00
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "Usage: /CHECKSUM GET|INC|DEC\n");
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 12:59:32 -05:00
}
2012-10-26 07:38:13 -04:00
2012-10-30 02:40:37 -04:00
return HEXCHAT_EAT_NONE;
2011-02-28 12:59:32 -05:00
}
int
2012-10-30 03: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 12:59:32 -05:00
{
ph = plugin_handle;
*plugin_name = name;
*plugin_desc = desc;
*plugin_version = version;
2011-02-28 12:59:32 -05:00
/* this is required for the very first run */
2012-10-30 03:42:48 -04:00
if (hexchat_pluginpref_get_int (ph, "limit") == -1)
{
2012-10-30 03:42:48 -04:00
hexchat_pluginpref_set_int (ph, "limit", DEFAULT_LIMIT);
}
2011-02-28 12:59:32 -05:00
2012-10-30 03:42:48 -04:00
hexchat_hook_command (ph, "CHECKSUM", HEXCHAT_PRI_NORM, checksum, "Usage: /CHECKSUM GET|SET", 0);
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 12:59:32 -05:00
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s plugin loaded\n", name);
2011-02-28 12:59:32 -05:00
return 1;
}
int
2012-10-30 03:42:48 -04:00
hexchat_plugin_deinit (void)
2011-02-28 12:59:32 -05:00
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s plugin unloaded\n", name);
2011-02-28 12:59:32 -05:00
return 1;
}