summary refs log tree commit diff stats
path: root/src/version/version.c
blob: f5ea95847edd27ede0ca5dd8b178b5a39b167297 (plain) (blame)
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
/* HexChat
 * Copyright (c) 2011-2012 Berke Viktor.
 *
 * 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:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * 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.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../config-win32.h"

char *
comma ()
{
# if 0	/* for WDK version numbers */
	int major, minor;
	char *version_string; /* nnnn,n,n,n format */

	version_string = (char*) malloc (11);

	if (sscanf (PACKAGE_VERSION, "%d-%d", &major, &minor) > 1)
	{
		sprintf (version_string, "%d,%d,0,0", major, minor);
	}
	else
	{
		sprintf (version_string, "%d,0,0,0", major);
	}
#endif
	int major, minor, build;
	char *version_string; /* n,n,n,n format */

	version_string = (char*) malloc (8);

	sscanf (PACKAGE_VERSION, "%d.%d.%d", &major, &minor, &build);
	sprintf (version_string, "%d,%d,%d,0", major, minor, build);

	return version_string;
}

char *
point ()
{
	int major1, major2, major3, major4, minor;
	char *version_string; /* nn.nn.nn.nn format */

	version_string = (char*) malloc (12);

	if (sscanf (PACKAGE_VERSION, "%c%c%c%c-%d", &major1, &major2, &major3, &major4, &minor) > 4)
	{
		sprintf (version_string, "%c%c.%c%c.%d.0", major1, major2, major3, major4, minor);
	}
	else
	{
		sprintf (version_string, "%c%c.%c%c.0.0", major1, major2, major3, major4);
	}

	return version_string;
}

int
main (int argc, char *argv[])
{
	if (argc > 1)
	{
		if (!strcmp (argv[1], "-r"))			/* hexchat.rc/FILEVERSION, PRODUCTVERSION */
		{
			printf ("#define COMMA_VERSION %s\n", comma ());
		}
		else if (!strcmp (argv[1], "-a"))	/* hexchat.iss/AppVerName */
		{
#ifdef _WIN64
			printf ("AppVerName=HexChat %s (x64)\n", PACKAGE_VERSION);
#else
			printf ("AppVerName=HexChat %s (x86)\n", PACKAGE_VERSION);
#endif
		}
		else if (!strcmp (argv[1], "-v"))	/* hexchat.iss/AppVersion */
		{
			/* printf ("AppVersion=%s\n", point ()); this was required only for nnnn[-n] version numbers */
			printf ("AppVersion=%s\n", PACKAGE_VERSION);
		}
		else if (!strcmp (argv[1], "-i"))	/* hexchat.iss/VersionInfoVersion */
		{
			/* printf ("VersionInfoVersion=%s\n", point ()); this was required only for nnnn[-n] version numbers */
			printf ("VersionInfoVersion=%s\n", PACKAGE_VERSION);
		}
		else if (!strcmp (argv[1], "-o"))	/* hexchat.iss/OutputBaseFilename */
		{
#ifdef _WIN64
			printf ("OutputBaseFilename=HexChat %s x64\n", PACKAGE_VERSION);
#else
			printf ("OutputBaseFilename=HexChat %s x86\n", PACKAGE_VERSION);
#endif
		}
		else if (!strcmp (argv[1], "-v"))	/* version.txt */
		{
			printf ("%s", PACKAGE_VERSION);
		}
		else
		{
			printf ("usage:\n\t-a\thexchat.iss/AppVerName\n\t-i\thexchat.iss/VersionInfoVersion\n\t-o\thexchat.iss/OutputBaseFilename\n\t-r\thexchat.rc/FILEVERSION, PRODUCTVERSION\n\t-v\thexchat.iss/AppVersion\n");
		}
	} else
	{
		printf ("usage:\n\t-a\thexchat.iss/AppVerName\n\t-i\thexchat.iss/VersionInfoVersion\n\t-o\thexchat.iss/OutputBaseFilename\n\t-r\thexchat.rc/FILEVERSION, PRODUCTVERSION\n\t-v\thexchat.iss/AppVersion\n");
	}

#if 0 /* ugly hack */
	switch ((int) argv[1][0])
	{
		case 'r':	/* hexchat.rc/FILEVERSION, PRODUCTVERSION*/
			printf ("#define COMMA_VERSION \"%s\"\n", comma ());
			break;
		case 'a':	/* hexchat.iss/AppVerName */
			printf ("AppVerName=HexChat %s\n", PACKAGE_VERSION);
			break;
		case 'v':	/* hexchat.iss/AppVersion */
			printf ("AppVersion=%s\n", point ());
			break;
		case 'i':	/* hexchat.iss/VersionInfoVersion */
			printf ("VersionInfoVersion=%s\n", point ());
			break;
		case 'o':	/* hexchat.iss/OutputBaseFilename */
			printf ("OutputBaseFilename=HexChat %s\n", PACKAGE_VERSION);
			break;
		case 'u':	/* version.txt */
			printf ("%s", PACKAGE_VERSION);
			break;
		default:
			printf ("use a, i, o, r or v.\n");
			break;
	}
#endif

	return 0;
}
ss="w"> outputBuffer); fclose (file); free (buffer); return 0; } static void init () { /* check whether the config file exists, if it doesn't, try to create it */ FILE * file_in; FILE * file_out; char buffer[FILE_BUF_SIZE]; config_fail = 0; snprintf (buffer, sizeof (buffer), "%s/checksum.conf", xchat_get_info (ph, "xchatdirfs")); if ((file_in = fopen (buffer, "r")) == NULL) { if ((file_out = fopen (buffer, "w")) == NULL) { config_fail = 1; } else { fprintf (file_out, "%llu\n", (unsigned long long) DEFAULT_MAX_HASH_SIZE); fclose (file_out); } } else { fclose (file_in); } /* nasty easter egg: if FILE_BUF_SIZE is set to 1024 and you build for x86, you can do fclose () at the end of init (), which is plain wrong as it will only work if fopen () != 0. */ } static unsigned long long get_max_hash_size () { FILE * file_in; char buffer[FILE_BUF_SIZE]; unsigned long long max_hash_size; if (config_fail) { return (unsigned long long) DEFAULT_MAX_HASH_SIZE; } else { snprintf (buffer, sizeof (buffer), "%s/checksum.conf", xchat_get_info (ph, "xchatdirfs")); file_in = fopen (buffer, "r"); fscanf (file_in, "%llu", &max_hash_size); fclose (file_in); return max_hash_size; } } static void print_size () { unsigned long long size; char suffix[3]; size = get_max_hash_size (); if (size >= 1073741824) { size /= 1073741824; snprintf (suffix, sizeof (suffix), "GB"); } else if (size >= 1048576) { size /= 1048576; snprintf (suffix, sizeof (suffix), "MB"); } else if (size >= 1024) { size /= 1024; snprintf (suffix, sizeof (suffix), "kB"); } else { snprintf (suffix, sizeof (suffix), "B"); } xchat_printf (ph, "File size limit for checksums: %llu %s\n", size, suffix); } static void increase_max_hash_size () { unsigned long long size; FILE * file_out; char buffer[FILE_BUF_SIZE]; if (config_fail) { xchat_printf (ph, "Config file is unavailable, falling back to the default value\n"); print_size (); } else { size = get_max_hash_size (); if (size <= ULLONG_MAX/2) { size *= 2; } snprintf (buffer, sizeof (buffer), "%s/checksum.conf", xchat_get_info (ph, "xchatdirfs")); file_out = fopen (buffer, "w"); fprintf (file_out, "%llu\n", size); fclose (file_out); print_size (); } } static void decrease_max_hash_size () { unsigned long long size; FILE * file_out; char buffer[FILE_BUF_SIZE]; if (config_fail) { xchat_printf (ph, "Config file is unavailable, falling back to the default value\n"); print_size (); } else { size = get_max_hash_size (); if (size >= 2) { size /= 2; } snprintf (buffer, sizeof (buffer), "%s/checksum.conf", xchat_get_info (ph, "xchatdirfs")); file_out = fopen (buffer, "w"); fprintf (file_out, "%llu\n", size); fclose (file_out); print_size (); } } 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 <= get_max_hash_size ()) { sha256_file (word[2], sum); /* word[2] is the full filename */ /* try to print the checksum in the privmsg tab of the sender */ xchat_set_context (ph, xchat_find_context (ph, NULL, word[3])); xchat_printf (ph, "SHA-256 checksum for %s (local): %s\n", word[1], sum); } else { xchat_set_context (ph, xchat_find_context (ph, NULL, word[3])); xchat_printf (ph, "SHA-256 checksum for %s (local): (size limit reached, no checksum calculated, you can increase it with /CHECKSUM INC)\n", word[1]); } } else { xchat_printf (ph, "File access error\n"); } return XCHAT_EAT_NONE; } 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 <= get_max_hash_size ()) { sha256_file (word[3], sum); /* word[3] is the full filename */ xchat_commandf (ph, "quote PRIVMSG %s :SHA-256 checksum for %s (remote): %s", word[2], word[1], sum); } else { xchat_set_context (ph, xchat_find_context (ph, NULL, word[3])); xchat_printf (ph, "quote PRIVMSG %s :SHA-256 checksum for %s (remote): (size limit reached, no checksum calculated)", word[2], word[1]); } } else { xchat_printf (ph, "File access error\n"); } return XCHAT_EAT_NONE; } static void checksum (char *word[], void *userdata) { if (!stricmp ("GET", word[2])) { print_size (); } else if (!stricmp ("INC", word[2])) { increase_max_hash_size (); } else if (!stricmp ("DEC", word[2])) { decrease_max_hash_size (); } else { xchat_printf (ph, "Usage: /CHECKSUM GET|INC|DEC\n"); xchat_printf (ph, " GET - print the maximum file size to be hashed\n"); xchat_printf (ph, " INC - double the maximum file size to be hashed\n"); xchat_printf (ph, " DEC - halve the maximum file size to be hashed\n"); } } int xchat_plugin_init (xchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg) { ph = plugin_handle; *plugin_name = "Checksum"; *plugin_desc = "Calculate checksum for DCC file transfers"; *plugin_version = "2.0"; init (); xchat_hook_command (ph, "CHECKSUM", XCHAT_PRI_NORM, checksum, "Usage: /CHECKSUM GET|INC|DEC", 0); xchat_hook_print (ph, "DCC RECV Complete", XCHAT_PRI_NORM, dccrecv_cb, NULL); xchat_hook_print (ph, "DCC Offer", XCHAT_PRI_NORM, dccoffer_cb, NULL); xchat_print (ph, "Checksum plugin loaded\n"); return 1; } int xchat_plugin_deinit (void) { xchat_print (ph, "Checksum plugin unloaded\n"); return 1; }