diff options
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/checksum/checksum.c | 269 | ||||
-rw-r--r-- | plugins/fishlim/fish.c | 3 | ||||
-rw-r--r-- | plugins/fishlim/tests/tests.c | 2 | ||||
-rw-r--r-- | plugins/python/python.py | 14 | ||||
-rw-r--r-- | plugins/python/python2.vcxproj | 65 | ||||
-rw-r--r-- | plugins/python/python2.vcxproj.filters | 23 | ||||
-rw-r--r-- | plugins/upd/upd.c | 2 |
7 files changed, 121 insertions, 257 deletions
diff --git a/plugins/checksum/checksum.c b/plugins/checksum/checksum.c index 6aa64b64..4db14c93 100644 --- a/plugins/checksum/checksum.c +++ b/plugins/checksum/checksum.c @@ -22,216 +22,162 @@ #include "config.h" -#include <stdlib.h> -#include <glib.h> -#include <glib/gstdio.h> #include <gio/gio.h> #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 - static hexchat_plugin *ph; /* plugin handle */ static char name[] = "Checksum"; static char desc[] = "Calculate checksum for DCC file transfers"; -static char version[] = "3.1"; - -static void -set_limit (char *size) -{ - int limit = atoi (size); - - if (limit > 0 && limit < INT_MAX) - { - 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"); - } - else - { - hexchat_printf (ph, "Checksum: Invalid input!\n"); - } -} - -static int -get_limit (void) -{ - int size = hexchat_pluginpref_get_int (ph, "limit"); - - if (size <= 0 || size >= INT_MAX) - return DEFAULT_LIMIT; - else - return size; -} - -static gboolean -check_limit (GFile *file) -{ - GFileInfo *file_info; - goffset file_size; - - file_info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, - NULL, NULL); - - if (!file_info) - return FALSE; +static char version[] = "4.0"; - file_size = g_file_info_get_size (file_info); - g_object_unref (file_info); - if (file_size > get_limit () * 1048576ll) - return FALSE; +typedef struct { + gboolean send_message; + char *servername; + char *channel; +} ChecksumCallbackInfo; - return TRUE; -} -static gboolean -sha256_from_stream (GFileInputStream *file_stream, char out_buf[]) +static void +print_sha256_result (ChecksumCallbackInfo *info, const char *checksum, const char *filename, GError *error) { - GChecksum *checksum; - gssize bytes_read; - guint8 digest[SHA256_DIGEST_LENGTH]; - gsize digest_len = sizeof(digest); - guchar buffer[BUFSIZE]; - gsize i; - - checksum = g_checksum_new (G_CHECKSUM_SHA256); - - while ((bytes_read = g_input_stream_read (G_INPUT_STREAM (file_stream), buffer, sizeof (buffer), NULL, NULL))) - { - if (bytes_read == -1) - { - g_checksum_free (checksum); - return FALSE; + // So then we get the next best available channel, since we always want to print at least somewhere, it's fine + hexchat_context *ctx = hexchat_find_context(ph, info->servername, info->channel); + if (!ctx) { + // before we print a private message to the wrong channel, we exit early + if (info->send_message) { + return; } - g_checksum_update (checksum, buffer, bytes_read); + // if the context isn't found the first time, we search in the server + ctx = hexchat_find_context(ph, info->servername, NULL); + if (!ctx) { + // The second time we exit early, since printing in another server isn't desireable + return; + } } - g_checksum_get_digest (checksum, digest, &digest_len); - g_checksum_free (checksum); + hexchat_set_context(ph, ctx); - for (i = 0; i < SHA256_DIGEST_LENGTH; i++) - { - /* out_buf will be exactly SHA256_BUFFER_LENGTH including null */ - g_sprintf (out_buf + (i * 2), "%02x", digest[i]); + if (error) { + hexchat_printf (ph, "Failed to create checksum for %s: %s\n", filename, error->message); + } else if (info->send_message) { + hexchat_commandf (ph, "quote PRIVMSG %s :SHA-256 checksum for %s (remote): %s", hexchat_get_info (ph, "channel"), filename, checksum); + } else { + hexchat_printf (ph, "SHA-256 checksum for %s (local): %s\n", filename, checksum); } +} - return TRUE; +static void +file_sha256_complete (GFile *file, GAsyncResult *result, gpointer user_data) +{ + ChecksumCallbackInfo * callback_info = user_data; + GError *error = NULL; + char *sha256 = NULL; + const char *filename = g_task_get_task_data (G_TASK (result)); + + sha256 = g_task_propagate_pointer (G_TASK (result), &error); + print_sha256_result (callback_info, sha256, filename, error); + + g_free(callback_info->servername); + g_free(callback_info->channel); + g_free(callback_info); + g_free (sha256); + g_clear_error (&error); } -static gboolean -sha256_from_file (char *filename, char out_buf[]) +static void +thread_sha256_file (GTask *task, GFile *file, gpointer task_data, GCancellable *cancellable) { - GFileInputStream *file_stream; - char *filename_fs; - GFile *file; - - filename_fs = g_filename_from_utf8 (filename, -1, NULL, NULL, NULL); - if (!filename_fs) - { - 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) - { - hexchat_printf (ph, "Checksum: Failed to open %s\n", filename); - return FALSE; + GChecksum *checksum; + GFileInputStream *istream; + guint8 buffer[32768]; + GError *error = NULL; + gssize ret; + + istream = g_file_read (file, cancellable, &error); + if (error) { + g_task_return_error (task, error); + return; } - 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; - } + checksum = g_checksum_new (G_CHECKSUM_SHA256); - 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; - } + while ((ret = g_input_stream_read (G_INPUT_STREAM (istream), buffer, sizeof(buffer), cancellable, &error)) > 0) + g_checksum_update (checksum, buffer, ret); - if (!sha256_from_stream (file_stream, out_buf)) - { - hexchat_printf (ph, "Checksum: Failed to generate checksum for %s\n", filename); - g_object_unref (file_stream); - g_object_unref (file); - return FALSE; + if (error) { + g_checksum_free (checksum); + g_task_return_error (task, error); + return; } - g_object_unref (file_stream); - g_object_unref (file); - return TRUE; + g_task_return_pointer (task, g_strdup (g_checksum_get_string (checksum)), g_free); + g_checksum_free (checksum); } static int dccrecv_cb (char *word[], void *userdata) { + GTask *task; + char *filename_fs; + GFile *file; 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])); + char *filename; 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)) - { - hexchat_printf (ph, "SHA-256 checksum for %s (local): %s\n", word[1], checksum); + filename_fs = g_filename_from_utf8 (filename, -1, NULL, NULL, NULL); + if (!filename_fs) { + hexchat_printf (ph, "Checksum: Invalid filename (%s)\n", filename); + g_free (filename); + return HEXCHAT_EAT_NONE; } - g_free (filename); - return HEXCHAT_EAT_NONE; -} + ChecksumCallbackInfo *callback_data = g_new (ChecksumCallbackInfo, 1); + callback_data->servername = g_strdup(hexchat_get_info(ph, "server")); + callback_data->channel = g_strdup(hexchat_get_info(ph, "channel")); + callback_data->send_message = FALSE; -static int -dccoffer_cb (char *word[], void *userdata) -{ - char checksum[SHA256_BUFFER_LENGTH]; - /* Print in the privmsg tab of the receiver */ - hexchat_set_context (ph, hexchat_find_context (ph, NULL, word[3])); + file = g_file_new_for_path (filename_fs); + task = g_task_new (file, NULL, (GAsyncReadyCallback) file_sha256_complete, (gpointer)callback_data); + g_task_set_task_data (task, filename, g_free); + g_task_run_in_thread (task, (GTaskThreadFunc) thread_sha256_file); - if (sha256_from_file (word[3], checksum)) - { - hexchat_commandf (ph, "quote PRIVMSG %s :SHA-256 checksum for %s (remote): %s", word[2], word[1], checksum); - } + g_free (filename_fs); + g_object_unref (file); + g_object_unref (task); return HEXCHAT_EAT_NONE; } static int -checksum (char *word[], char *word_eol[], void *userdata) +dccoffer_cb (char *word[], void *userdata) { - if (!g_ascii_strcasecmp ("GET", word[2])) - { - hexchat_printf (ph, "File size limit for checksums: %d MiB", get_limit ()); - } - else if (!g_ascii_strcasecmp ("SET", word[2])) - { - set_limit (word[3]); - } - else - { - hexchat_printf (ph, "Usage: /CHECKSUM GET|SET\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"); - } + GFile *file; + GTask *task; + char *filename; + + ChecksumCallbackInfo *callback_data = g_new (ChecksumCallbackInfo, 1); + callback_data->servername = g_strdup(hexchat_get_info(ph, "server")); + callback_data->channel = g_strdup(hexchat_get_info(ph, "channel")); + callback_data->send_message = TRUE; + + filename = g_strdup (word[3]); + file = g_file_new_for_path (filename); + task = g_task_new (file, NULL, (GAsyncReadyCallback) file_sha256_complete, (gpointer)callback_data); + g_task_set_task_data (task, filename, g_free); + g_task_run_in_thread (task, (GTaskThreadFunc) thread_sha256_file); + + g_object_unref (file); + g_object_unref (task); - return HEXCHAT_EAT_ALL; + return HEXCHAT_EAT_NONE; } int @@ -243,13 +189,6 @@ hexchat_plugin_init (hexchat_plugin *plugin_handle, char **plugin_name, char **p *plugin_desc = desc; *plugin_version = version; - /* this is required for the very first run */ - if (hexchat_pluginpref_get_int (ph, "limit") == -1) - { - hexchat_pluginpref_set_int (ph, "limit", DEFAULT_LIMIT); - } - - hexchat_hook_command (ph, "CHECKSUM", HEXCHAT_PRI_NORM, checksum, "Usage: /CHECKSUM GET|SET", NULL); 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); diff --git a/plugins/fishlim/fish.c b/plugins/fishlim/fish.c index 7fe7e287..0b24ed48 100644 --- a/plugins/fishlim/fish.c +++ b/plugins/fishlim/fish.c @@ -145,7 +145,8 @@ void fish_deinit(void) */ char *fish_base64_encode(const char *message, size_t message_len) { BF_LONG left = 0, right = 0; - int i, j; + int i; + size_t j; char *encoded = NULL; char *end = NULL; char *msg = NULL; diff --git a/plugins/fishlim/tests/tests.c b/plugins/fishlim/tests/tests.c index 12b10d1d..553816d3 100644 --- a/plugins/fishlim/tests/tests.c +++ b/plugins/fishlim/tests/tests.c @@ -36,7 +36,7 @@ static void random_string(char *out, size_t len) { GRand *rand = NULL; - int i = 0; + size_t i = 0; rand = g_rand_new(); for (i = 0; i < len; ++i) { diff --git a/plugins/python/python.py b/plugins/python/python.py index 7a794784..9eca7d6e 100644 --- a/plugins/python/python.py +++ b/plugins/python/python.py @@ -64,6 +64,10 @@ class Stdout: else: self.buffer += string + def flush(self): + lib.hexchat_print(lib.ph, bytes(self.buffer)) + self.buffer = bytearray() + def isatty(self): return False @@ -287,7 +291,15 @@ def _on_timer_hook(userdata): if hook.callback(hook.userdata) == True: return 1 - hook.is_unload = True # Don't unhook + try: + # Avoid calling hexchat_unhook twice if unnecessary + hook.is_unload = True + except ReferenceError: + # hook is a weak reference, it might have been destroyed by the callback + # in which case it has already been removed from hook.plugin.hooks and + # we wouldn't be able to test it with h == hook anyway. + return 0 + for h in hook.plugin.hooks: if h == hook: hook.plugin.hooks.remove(h) diff --git a/plugins/python/python2.vcxproj b/plugins/python/python2.vcxproj deleted file mode 100644 index 42895ce4..00000000 --- a/plugins/python/python2.vcxproj +++ /dev/null @@ -1,65 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> - <PropertyGroup Label="Configuration"> - <PlatformToolset>v142</PlatformToolset> - <ConfigurationType>DynamicLibrary</ConfigurationType> - </PropertyGroup> - <ItemGroup Label="ProjectConfigurations"> - <ProjectConfiguration Include="Release|Win32"> - <Configuration>Release</Configuration> - <Platform>Win32</Platform> - </ProjectConfiguration> - <ProjectConfiguration Include="Release|x64"> - <Configuration>Release</Configuration> - <Platform>x64</Platform> - </ProjectConfiguration> - </ItemGroup> - <PropertyGroup Label="Globals"> - <ProjectGuid>{19C52A0A-A790-409E-A28A-9745FF990F5C}</ProjectGuid> - <Keyword>Win32Proj</Keyword> - <RootNamespace>python2</RootNamespace> - </PropertyGroup> - <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> - <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> - <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> - <Import Project="..\..\win32\hexchat.props" /> - <PropertyGroup> - <TargetName>$(Python2Output)</TargetName> - <OutDir>$(HexChatRel)plugins\</OutDir> - </PropertyGroup> - <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> - <ClCompile> - <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;PYTHON_EXPORTS;$(OwnFlags);%(PreprocessorDefinitions)</PreprocessorDefinitions> - <AdditionalIncludeDirectories>$(Glib);$(Python2Path)\include;..\..\src\common;$(HexChatLib);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> - </ClCompile> - <Link> - <ModuleDefinitionFile>python.def</ModuleDefinitionFile> - <AdditionalDependencies>"$(Python2Lib).lib";$(DepLibs);%(AdditionalDependencies)</AdditionalDependencies> - <AdditionalLibraryDirectories>$(DepsRoot)\lib;$(Python2Path)\libs;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> - </Link> - <PreBuildEvent> - <Command>"$(Python3Path)\python.exe" generate_plugin.py ..\..\src\common\hexchat-plugin.h python.py "$(IntDir)python.c"</Command> - </PreBuildEvent> - </ItemDefinitionGroup> - <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> - <ClCompile> - <PreprocessorDefinitions>WIN32;_WIN64;_AMD64_;NDEBUG;_WINDOWS;_USRDLL;PYTHON_EXPORTS;$(OwnFlags);%(PreprocessorDefinitions)</PreprocessorDefinitions> - <AdditionalIncludeDirectories>$(Glib);$(Python2Path)\include;..\..\src\common;$(HexChatLib);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> - </ClCompile> - <Link> - <ModuleDefinitionFile>python.def</ModuleDefinitionFile> - <AdditionalDependencies>"$(Python2Lib).lib";$(DepLibs);%(AdditionalDependencies)</AdditionalDependencies> - <AdditionalLibraryDirectories>$(DepsRoot)\lib;$(Python2Path)\libs;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> - </Link> - <PreBuildEvent> - <Command>"$(Python3Path)\python.exe" generate_plugin.py ..\..\src\common\hexchat-plugin.h python.py "$(IntDir)python.c"</Command> - </PreBuildEvent> - </ItemDefinitionGroup> - <ItemGroup> - <None Include="python.def" /> - </ItemGroup> - <ItemGroup> - <ClCompile Include="$(IntDir)python.c" /> - </ItemGroup> - <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> -</Project> diff --git a/plugins/python/python2.vcxproj.filters b/plugins/python/python2.vcxproj.filters deleted file mode 100644 index d56e53b6..00000000 --- a/plugins/python/python2.vcxproj.filters +++ /dev/null @@ -1,23 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> - <ItemGroup> - <Filter Include="Source Files"> - <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> - <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> - </Filter> - <Filter Include="Resource Files"> - <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> - <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> - </Filter> - </ItemGroup> - <ItemGroup> - <None Include="python.def"> - <Filter>Resource Files</Filter> - </None> - </ItemGroup> - <ItemGroup> - <ClCompile Include="python.c"> - <Filter>Source Files</Filter> - </ClCompile> - </ItemGroup> -</Project> \ No newline at end of file diff --git a/plugins/upd/upd.c b/plugins/upd/upd.c index c9011c04..2a938596 100644 --- a/plugins/upd/upd.c +++ b/plugins/upd/upd.c @@ -24,7 +24,7 @@ #include "hexchat-plugin.h" -#define APPCAST_URL "https://dl.hexchat.net/appcast.xml" +#define APPCAST_URL "https://hexchat.github.io/appcast.xml" static hexchat_plugin *ph; /* plugin handle */ static char name[] = "Update Checker"; |