2011-11-25 02:17:31 +04:00
|
|
|
/*
|
|
|
|
|
2012-05-04 21:29:02 +04:00
|
|
|
Copyright (c) 2010-2011 Samuel Lidén Borell <samuel@kodafritt.se>
|
2016-08-29 06:26:11 +03:00
|
|
|
Copyright (c) 2015 <the.cypher@gmail.com>
|
2020-07-14 02:27:27 +03:00
|
|
|
Copyright (c) 2019 <bakasura@protonmail.ch>
|
2011-11-25 02:17:31 +04: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:
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-12-16 06:57:27 +03:00
|
|
|
#include "config.h"
|
|
|
|
|
2011-11-25 02:37:43 +04:00
|
|
|
#include <glib.h>
|
2011-11-25 02:17:31 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-10-24 23:33:02 +04:00
|
|
|
#include "hexchat-plugin.h"
|
2011-11-25 02:17:31 +04:00
|
|
|
|
|
|
|
#include "fish.h"
|
2016-08-29 04:18:44 +03:00
|
|
|
#include "dh1080.h"
|
2011-11-25 02:17:31 +04:00
|
|
|
#include "keystore.h"
|
|
|
|
#include "irc.h"
|
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
static const char *fish_modes[] = {"", "ECB", "CBC", NULL};
|
|
|
|
|
2011-11-25 02:17:31 +04:00
|
|
|
static const char plugin_name[] = "FiSHLiM";
|
|
|
|
static const char plugin_desc[] = "Encryption plugin for the FiSH protocol. Less is More!";
|
2020-07-14 02:27:27 +03:00
|
|
|
static const char plugin_version[] = "1.0.0";
|
2011-11-25 02:17:31 +04:00
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
static const char usage_setkey[] = "Usage: SETKEY [<nick or #channel>] [<mode>:]<password>, sets the key for a channel or nick. Modes: ECB, CBC";
|
|
|
|
static const char usage_delkey[] = "Usage: DELKEY [<nick or #channel>], deletes the key for a channel or nick";
|
2016-08-29 04:18:44 +03:00
|
|
|
static const char usage_keyx[] = "Usage: KEYX [<nick>], performs DH1080 key-exchange with <nick>";
|
2016-08-29 06:26:11 +03:00
|
|
|
static const char usage_topic[] = "Usage: TOPIC+ <topic>, sets a new encrypted topic for the current channel";
|
|
|
|
static const char usage_notice[] = "Usage: NOTICE+ <nick or #channel> <notice>";
|
|
|
|
static const char usage_msg[] = "Usage: MSG+ <nick or #channel> <message>";
|
2016-08-29 04:18:44 +03:00
|
|
|
|
2011-11-25 02:17:31 +04:00
|
|
|
|
2012-10-30 11:42:48 +04:00
|
|
|
static hexchat_plugin *ph;
|
2016-08-29 04:18:44 +03:00
|
|
|
static GHashTable *pending_exchanges;
|
2011-11-25 02:17:31 +04:00
|
|
|
|
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
/**
|
|
|
|
* Compare two nicks using the current plugin
|
|
|
|
*/
|
|
|
|
int irc_nick_cmp(const char *a, const char *b) {
|
|
|
|
return hexchat_nickcmp (ph, a, b);
|
|
|
|
}
|
|
|
|
|
2011-11-25 02:37:43 +04:00
|
|
|
/**
|
|
|
|
* Returns the path to the key store file.
|
|
|
|
*/
|
2016-02-18 16:57:22 +03:00
|
|
|
gchar *get_config_filename(void) {
|
2014-12-12 13:29:43 +03:00
|
|
|
char *filename_fs, *filename_utf8;
|
|
|
|
|
|
|
|
filename_utf8 = g_build_filename(hexchat_get_info(ph, "configdir"), "addon_fishlim.conf", NULL);
|
|
|
|
filename_fs = g_filename_from_utf8 (filename_utf8, -1, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
g_free (filename_utf8);
|
|
|
|
return filename_fs;
|
2011-11-25 02:37:43 +04:00
|
|
|
}
|
|
|
|
|
2016-08-29 04:18:44 +03:00
|
|
|
static inline gboolean irc_is_query (const char *name) {
|
|
|
|
const char *chantypes = hexchat_list_str (ph, NULL, "chantypes");
|
|
|
|
|
|
|
|
return strchr (chantypes, name[0]) == NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static hexchat_context *find_context_on_network (const char *name) {
|
|
|
|
hexchat_list *channels;
|
|
|
|
hexchat_context *ret = NULL;
|
|
|
|
int id;
|
|
|
|
|
|
|
|
if (hexchat_get_prefs(ph, "id", NULL, &id) != 2)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
channels = hexchat_list_get(ph, "channels");
|
|
|
|
if (!channels)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (hexchat_list_next(ph, channels)) {
|
|
|
|
int chan_id = hexchat_list_int(ph, channels, "id");
|
|
|
|
const char *chan_name = hexchat_list_str(ph, channels, "channel");
|
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
if (chan_id == id && chan_name && irc_nick_cmp (chan_name, name) == 0) {
|
2016-08-29 04:18:44 +03:00
|
|
|
ret = (hexchat_context*)hexchat_list_str(ph, channels, "context");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
hexchat_list_free(ph, channels);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
/**
|
|
|
|
* Retrive the prefix character for own nick in current context
|
|
|
|
* @return @ or + or NULL
|
|
|
|
*/
|
|
|
|
char *get_my_own_prefix(void) {
|
|
|
|
char *result = NULL;
|
|
|
|
const char *own_nick;
|
|
|
|
hexchat_list *list;
|
|
|
|
|
|
|
|
/* Display message */
|
|
|
|
own_nick = hexchat_get_info(ph, "nick");
|
|
|
|
|
|
|
|
if (!own_nick)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Get prefix for own nick if any */
|
|
|
|
list = hexchat_list_get (ph, "users");
|
|
|
|
if (list) {
|
|
|
|
while (hexchat_list_next(ph, list)) {
|
|
|
|
if (irc_nick_cmp(own_nick, hexchat_list_str(ph, list, "nick")) == 0)
|
|
|
|
result = g_strdup(hexchat_list_str(ph, list, "prefix"));
|
|
|
|
}
|
|
|
|
hexchat_list_free(ph, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to decrypt the first occurrence of fish message
|
|
|
|
*
|
|
|
|
* @param message Message to decrypt
|
|
|
|
* @param key Key of message
|
|
|
|
* @return Array of char with decrypted message or NULL. The returned string
|
|
|
|
* should be freed with g_free() when no longer needed.
|
|
|
|
*/
|
|
|
|
char *decrypt_raw_message(const char *message, const char *key) {
|
|
|
|
const char *prefixes[] = {"+OK ", "mcps ", NULL};
|
|
|
|
char *start = NULL, *end = NULL;
|
|
|
|
char *left = NULL, *right = NULL;
|
|
|
|
char *encrypted = NULL, *decrypted = NULL;
|
|
|
|
int length = 0;
|
|
|
|
int index_prefix;
|
|
|
|
enum fish_mode mode;
|
|
|
|
GString *message_decrypted;
|
|
|
|
char *result = NULL;
|
|
|
|
|
|
|
|
if (message == NULL || key == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (index_prefix = 0; index_prefix < 2; index_prefix++) {
|
|
|
|
start = g_strstr_len(message, strlen(message), prefixes[index_prefix]);
|
|
|
|
if (start) {
|
|
|
|
/* Length ALWAYS will be less that original message
|
|
|
|
* add '[CBC] ' length */
|
|
|
|
message_decrypted = g_string_sized_new(strlen(message) + 6);
|
|
|
|
|
|
|
|
/* Left part of message */
|
|
|
|
left = g_strndup(message, start - message);
|
|
|
|
g_string_append(message_decrypted, left);
|
|
|
|
g_free(left);
|
|
|
|
|
|
|
|
/* Encrypted part */
|
|
|
|
start += strlen(prefixes[index_prefix]);
|
|
|
|
end = g_strstr_len(start, strlen(message), " ");
|
|
|
|
if (end) {
|
|
|
|
length = end - start;
|
|
|
|
right = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > 0) {
|
|
|
|
encrypted = g_strndup(start, length);
|
|
|
|
} else {
|
|
|
|
encrypted = g_strdup(start);
|
|
|
|
}
|
|
|
|
decrypted = fish_decrypt_from_nick(key, encrypted, &mode);
|
|
|
|
g_free(encrypted);
|
|
|
|
|
|
|
|
if (decrypted == NULL) {
|
|
|
|
g_string_free(message_decrypted, TRUE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add encrypted flag */
|
|
|
|
g_string_append(message_decrypted, "[");
|
|
|
|
g_string_append(message_decrypted, fish_modes[mode]);
|
|
|
|
g_string_append(message_decrypted, "] ");
|
|
|
|
/* Decrypted message */
|
|
|
|
g_string_append(message_decrypted, decrypted);
|
|
|
|
g_free(decrypted);
|
|
|
|
|
|
|
|
/* Right part of message */
|
|
|
|
if (right) {
|
|
|
|
g_string_append(message_decrypted, right);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = message_decrypted->str;
|
|
|
|
g_string_free(message_decrypted, FALSE);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2014-12-12 13:15:49 +03:00
|
|
|
}
|
|
|
|
|
2011-11-25 02:17:31 +04:00
|
|
|
/*static int handle_debug(char *word[], char *word_eol[], void *userdata) {
|
2012-10-30 11:42:48 +04:00
|
|
|
hexchat_printf(ph, "debug incoming: ");
|
2011-11-25 02:17:31 +04:00
|
|
|
for (size_t i = 1; word[i] != NULL && word[i][0] != '\0'; i++) {
|
2012-10-30 11:42:48 +04:00
|
|
|
hexchat_printf(ph, ">%s< ", word[i]);
|
2011-11-25 02:17:31 +04:00
|
|
|
}
|
2012-10-30 11:42:48 +04:00
|
|
|
hexchat_printf(ph, "\n");
|
2012-10-30 10:40:37 +04:00
|
|
|
return HEXCHAT_EAT_NONE;
|
2011-11-25 02:17:31 +04:00
|
|
|
}*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a message is to be sent.
|
|
|
|
*/
|
|
|
|
static int handle_outgoing(char *word[], char *word_eol[], void *userdata) {
|
2020-07-14 02:27:27 +03:00
|
|
|
char *prefix;
|
|
|
|
enum fish_mode mode;
|
|
|
|
char *message;
|
2014-12-18 02:49:59 +03:00
|
|
|
/* Encrypt the message if possible */
|
2012-10-30 11:42:48 +04:00
|
|
|
const char *channel = hexchat_get_info(ph, "channel");
|
2020-07-14 02:27:27 +03:00
|
|
|
char *encrypted = fish_encrypt_for_nick(channel, word_eol[1], &mode);
|
2012-10-30 10:40:37 +04:00
|
|
|
if (!encrypted) return HEXCHAT_EAT_NONE;
|
2011-11-25 02:17:31 +04:00
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
/* Get prefix for own nick if any */
|
|
|
|
prefix = get_my_own_prefix();
|
|
|
|
|
|
|
|
/* Add encrypted flag */
|
|
|
|
message = g_strconcat("[", fish_modes[mode], "] ", word_eol[1], NULL);
|
|
|
|
|
2014-12-18 02:49:59 +03:00
|
|
|
/* Display message */
|
2020-07-14 02:27:27 +03:00
|
|
|
hexchat_emit_print(ph, "Your Message", hexchat_get_info(ph, "nick"), message, prefix, NULL);
|
|
|
|
g_free(prefix);
|
|
|
|
g_free(message);
|
2011-11-25 02:17:31 +04:00
|
|
|
|
2014-12-18 02:49:59 +03:00
|
|
|
/* Send message */
|
2012-10-30 11:42:48 +04:00
|
|
|
hexchat_commandf(ph, "PRIVMSG %s :+OK %s", channel, encrypted);
|
2011-11-25 02:17:31 +04:00
|
|
|
|
2014-12-28 14:08:20 +03:00
|
|
|
g_free(encrypted);
|
2012-10-30 13:42:37 +04:00
|
|
|
return HEXCHAT_EAT_HEXCHAT;
|
2011-11-25 02:17:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a channel message or private message is received.
|
|
|
|
*/
|
2014-12-12 13:43:57 +03:00
|
|
|
static int handle_incoming(char *word[], char *word_eol[], hexchat_event_attrs *attrs, void *userdata) {
|
2011-11-25 02:17:31 +04:00
|
|
|
const char *prefix;
|
|
|
|
const char *command;
|
|
|
|
const char *recipient;
|
2020-07-14 02:27:27 +03:00
|
|
|
const char *raw_message = word_eol[1];
|
2011-11-25 13:24:33 +04:00
|
|
|
char *sender_nick;
|
2011-11-25 02:17:31 +04:00
|
|
|
char *decrypted;
|
2020-07-14 02:27:27 +03:00
|
|
|
size_t parameters_offset;
|
2014-12-12 12:04:39 +03:00
|
|
|
GString *message;
|
2011-11-25 02:17:31 +04:00
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
if (!irc_parse_message((const char **)word, &prefix, &command, ¶meters_offset))
|
2012-10-30 10:40:37 +04:00
|
|
|
return HEXCHAT_EAT_NONE;
|
2020-07-14 02:27:27 +03:00
|
|
|
|
2014-12-18 02:49:59 +03:00
|
|
|
/* Topic (command 332) has an extra parameter */
|
2020-07-14 02:27:27 +03:00
|
|
|
if (!strcmp(command, "332"))
|
|
|
|
parameters_offset++;
|
|
|
|
|
|
|
|
/* Extract sender nick and recipient nick/channel and try to decrypt */
|
|
|
|
recipient = word[parameters_offset];
|
|
|
|
decrypted = decrypt_raw_message(raw_message, recipient);
|
|
|
|
if (decrypted == NULL) {
|
|
|
|
sender_nick = irc_prefix_get_nick(prefix);
|
|
|
|
decrypted = decrypt_raw_message(raw_message, sender_nick);
|
|
|
|
g_free(sender_nick);
|
2011-11-25 02:17:31 +04:00
|
|
|
}
|
2020-07-14 02:27:27 +03:00
|
|
|
|
|
|
|
/* Nothing to decrypt */
|
|
|
|
if (decrypted == NULL)
|
|
|
|
return HEXCHAT_EAT_NONE;
|
|
|
|
|
|
|
|
/* Build decrypted message */
|
|
|
|
|
|
|
|
/* decrypted + 'RECV ' + '@time=YYYY-MM-DDTHH:MM:SS.fffffZ ' */
|
|
|
|
message = g_string_sized_new (strlen(decrypted) + 5 + 33);
|
|
|
|
g_string_append (message, "RECV ");
|
2014-12-12 13:43:57 +03:00
|
|
|
|
|
|
|
if (attrs->server_time_utc)
|
|
|
|
{
|
|
|
|
GTimeVal tv = { (glong)attrs->server_time_utc, 0 };
|
|
|
|
char *timestamp = g_time_val_to_iso8601 (&tv);
|
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
g_string_append (message, "@time=");
|
2014-12-12 13:43:57 +03:00
|
|
|
g_string_append (message, timestamp);
|
2020-07-14 02:27:27 +03:00
|
|
|
g_string_append (message, " ");
|
2014-12-12 13:43:57 +03:00
|
|
|
g_free (timestamp);
|
|
|
|
}
|
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
g_string_append (message, decrypted);
|
2014-12-28 14:08:20 +03:00
|
|
|
g_free(decrypted);
|
2016-08-29 04:18:44 +03:00
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
/* Fake server message
|
|
|
|
* RECV command will throw this function again, if message have multiple
|
|
|
|
* encrypted data, we will decrypt all */
|
2014-12-12 12:04:39 +03:00
|
|
|
hexchat_command(ph, message->str);
|
|
|
|
g_string_free (message, TRUE);
|
2016-08-29 04:18:44 +03:00
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
return HEXCHAT_EAT_HEXCHAT;
|
2011-11-25 02:17:31 +04:00
|
|
|
}
|
|
|
|
|
2016-08-29 04:18:44 +03:00
|
|
|
static int handle_keyx_notice(char *word[], char *word_eol[], void *userdata) {
|
|
|
|
const char *dh_message = word[4];
|
|
|
|
const char *dh_pubkey = word[5];
|
|
|
|
hexchat_context *query_ctx;
|
|
|
|
const char *prefix;
|
|
|
|
char *sender, *secret_key, *priv_key = NULL;
|
2020-07-14 02:27:27 +03:00
|
|
|
enum fish_mode mode = FISH_ECB_MODE;
|
2016-08-29 04:18:44 +03:00
|
|
|
|
|
|
|
if (!*dh_message || !*dh_pubkey || strlen(dh_pubkey) != 181)
|
|
|
|
return HEXCHAT_EAT_NONE;
|
|
|
|
|
|
|
|
if (!irc_parse_message((const char**)word, &prefix, NULL, NULL) || !prefix)
|
|
|
|
return HEXCHAT_EAT_NONE;
|
|
|
|
|
|
|
|
sender = irc_prefix_get_nick(prefix);
|
|
|
|
query_ctx = find_context_on_network(sender);
|
|
|
|
if (query_ctx)
|
2020-07-14 02:27:27 +03:00
|
|
|
g_assert(hexchat_set_context(ph, query_ctx) == 1);
|
2016-08-29 04:18:44 +03:00
|
|
|
|
|
|
|
dh_message++; /* : prefix */
|
|
|
|
if (*dh_message == '+' || *dh_message == '-')
|
|
|
|
dh_message++; /* identify-msg */
|
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
if (g_strcmp0 (word[6], "CBC") == 0)
|
|
|
|
mode = FISH_CBC_MODE;
|
2016-08-29 04:18:44 +03:00
|
|
|
|
|
|
|
if (!strcmp(dh_message, "DH1080_INIT")) {
|
|
|
|
char *pub_key;
|
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
hexchat_printf(ph, "Received public key from %s (%s), sending mine...", sender, fish_modes[mode]);
|
2016-08-29 04:18:44 +03:00
|
|
|
if (dh1080_generate_key(&priv_key, &pub_key)) {
|
2020-07-14 02:27:27 +03:00
|
|
|
hexchat_commandf(ph, "quote NOTICE %s :DH1080_FINISH %s%s", sender, pub_key, (mode == FISH_CBC_MODE) ? " CBC" : "");
|
2016-08-29 04:18:44 +03:00
|
|
|
g_free(pub_key);
|
|
|
|
} else {
|
|
|
|
hexchat_print(ph, "Failed to generate keys");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
} else if (!strcmp (dh_message, "DH1080_FINISH")) {
|
|
|
|
char *sender_lower = g_ascii_strdown(sender, -1);
|
|
|
|
/* FIXME: Properly respect irc casing */
|
|
|
|
priv_key = g_hash_table_lookup(pending_exchanges, sender_lower);
|
|
|
|
g_hash_table_steal(pending_exchanges, sender_lower);
|
|
|
|
g_free(sender_lower);
|
|
|
|
|
|
|
|
if (!priv_key) {
|
2016-11-28 23:00:10 +03:00
|
|
|
hexchat_printf(ph, "Received a key exchange response for unknown user: %s", sender);
|
2016-08-29 04:18:44 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Regular notice */
|
|
|
|
g_free(sender);
|
|
|
|
return HEXCHAT_EAT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dh1080_compute_key(priv_key, dh_pubkey, &secret_key)) {
|
2020-07-14 02:27:27 +03:00
|
|
|
keystore_store_key(sender, secret_key, mode);
|
|
|
|
hexchat_printf(ph, "Stored new key for %s (%s)", sender, fish_modes[mode]);
|
2016-08-29 04:18:44 +03:00
|
|
|
g_free(secret_key);
|
|
|
|
} else {
|
|
|
|
hexchat_print(ph, "Failed to create secret key!");
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
g_free(sender);
|
|
|
|
g_free(priv_key);
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
|
2011-11-25 02:17:31 +04:00
|
|
|
/**
|
|
|
|
* Command handler for /setkey
|
|
|
|
*/
|
|
|
|
static int handle_setkey(char *word[], char *word_eol[], void *userdata) {
|
|
|
|
const char *nick;
|
|
|
|
const char *key;
|
2020-07-14 02:27:27 +03:00
|
|
|
enum fish_mode mode;
|
2016-08-29 04:18:44 +03:00
|
|
|
|
2014-12-18 02:49:59 +03:00
|
|
|
/* Check syntax */
|
2011-11-25 02:17:31 +04:00
|
|
|
if (*word[2] == '\0') {
|
2012-10-30 11:42:48 +04:00
|
|
|
hexchat_printf(ph, "%s\n", usage_setkey);
|
2012-10-30 13:42:37 +04:00
|
|
|
return HEXCHAT_EAT_HEXCHAT;
|
2011-11-25 02:17:31 +04:00
|
|
|
}
|
2016-08-29 04:18:44 +03:00
|
|
|
|
2011-11-25 02:17:31 +04:00
|
|
|
if (*word[3] == '\0') {
|
2014-12-18 02:49:59 +03:00
|
|
|
/* /setkey password */
|
2012-10-30 11:42:48 +04:00
|
|
|
nick = hexchat_get_info(ph, "channel");
|
2011-11-25 02:17:31 +04:00
|
|
|
key = word_eol[2];
|
|
|
|
} else {
|
2014-12-18 02:49:59 +03:00
|
|
|
/* /setkey #channel password */
|
2011-11-25 02:17:31 +04:00
|
|
|
nick = word[2];
|
|
|
|
key = word_eol[3];
|
|
|
|
}
|
2016-08-29 04:18:44 +03:00
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
mode = FISH_ECB_MODE;
|
|
|
|
if (g_ascii_strncasecmp("cbc:", key, 4) == 0) {
|
|
|
|
key = key+4;
|
|
|
|
mode = FISH_CBC_MODE;
|
|
|
|
} else if (g_ascii_strncasecmp("ecb:", key, 4) == 0) {
|
|
|
|
key = key+4;
|
|
|
|
}
|
|
|
|
|
2014-12-18 02:49:59 +03:00
|
|
|
/* Set password */
|
2020-07-14 02:27:27 +03:00
|
|
|
if (keystore_store_key(nick, key, mode)) {
|
|
|
|
hexchat_printf(ph, "Stored key for %s (%s)\n", nick, fish_modes[mode]);
|
2011-11-25 02:17:31 +04:00
|
|
|
} else {
|
2014-07-18 15:16:43 +04:00
|
|
|
hexchat_printf(ph, "\00305Failed to store key in addon_fishlim.conf\n");
|
2011-11-25 02:17:31 +04:00
|
|
|
}
|
2016-08-29 04:18:44 +03:00
|
|
|
|
2012-10-30 13:42:37 +04:00
|
|
|
return HEXCHAT_EAT_HEXCHAT;
|
2011-11-25 02:17:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Command handler for /delkey
|
|
|
|
*/
|
|
|
|
static int handle_delkey(char *word[], char *word_eol[], void *userdata) {
|
2020-07-14 02:27:27 +03:00
|
|
|
char *nick = NULL;
|
|
|
|
int ctx_type = 0;
|
|
|
|
|
|
|
|
/* Delete key from input */
|
|
|
|
if (*word[2] != '\0') {
|
|
|
|
nick = g_strstrip(g_strdup(word_eol[2]));
|
|
|
|
} else { /* Delete key from current context */
|
|
|
|
nick = g_strdup(hexchat_get_info(ph, "channel"));
|
|
|
|
ctx_type = hexchat_list_int(ph, NULL, "type");
|
2016-08-29 04:18:44 +03:00
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
/* Only allow channel or dialog */
|
|
|
|
if (ctx_type < 2 || ctx_type > 3) {
|
|
|
|
hexchat_printf(ph, "%s\n", usage_delkey);
|
|
|
|
return HEXCHAT_EAT_HEXCHAT;
|
|
|
|
}
|
2011-11-25 02:17:31 +04:00
|
|
|
}
|
2016-08-29 04:18:44 +03:00
|
|
|
|
2014-12-18 02:49:59 +03:00
|
|
|
/* Delete the given nick from the key store */
|
2011-11-25 02:17:31 +04:00
|
|
|
if (keystore_delete_nick(nick)) {
|
2012-10-30 11:42:48 +04:00
|
|
|
hexchat_printf(ph, "Deleted key for %s\n", nick);
|
2011-11-25 02:17:31 +04:00
|
|
|
} else {
|
2014-07-18 15:16:43 +04:00
|
|
|
hexchat_printf(ph, "\00305Failed to delete key in addon_fishlim.conf!\n");
|
2011-11-25 02:17:31 +04:00
|
|
|
}
|
2020-07-14 02:27:27 +03:00
|
|
|
g_free(nick);
|
2016-08-29 04:18:44 +03:00
|
|
|
|
2012-10-30 13:42:37 +04:00
|
|
|
return HEXCHAT_EAT_HEXCHAT;
|
2011-11-25 02:17:31 +04:00
|
|
|
}
|
|
|
|
|
2016-08-29 04:18:44 +03:00
|
|
|
static int handle_keyx(char *word[], char *word_eol[], void *userdata) {
|
|
|
|
const char *target = word[2];
|
|
|
|
hexchat_context *query_ctx = NULL;
|
|
|
|
char *pub_key, *priv_key;
|
|
|
|
int ctx_type;
|
|
|
|
|
|
|
|
if (*target)
|
|
|
|
query_ctx = find_context_on_network(target);
|
|
|
|
else {
|
|
|
|
target = hexchat_get_info(ph, "channel");
|
|
|
|
query_ctx = hexchat_get_context (ph);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query_ctx) {
|
2020-07-14 02:27:27 +03:00
|
|
|
g_assert(hexchat_set_context(ph, query_ctx) == 1);
|
2016-08-29 04:18:44 +03:00
|
|
|
ctx_type = hexchat_list_int(ph, NULL, "type");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((query_ctx && ctx_type != 3) || (!query_ctx && !irc_is_query(target))) {
|
|
|
|
hexchat_print(ph, "You can only exchange keys with individuals");
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dh1080_generate_key(&priv_key, &pub_key)) {
|
|
|
|
g_hash_table_replace (pending_exchanges, g_ascii_strdown(target, -1), priv_key);
|
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
hexchat_commandf(ph, "quote NOTICE %s :DH1080_INIT %s CBC", target, pub_key);
|
|
|
|
hexchat_printf(ph, "Sent public key to %s (CBC), waiting for reply...", target);
|
2016-08-29 04:18:44 +03:00
|
|
|
|
|
|
|
g_free(pub_key);
|
|
|
|
} else {
|
|
|
|
hexchat_print(ph, "Failed to generate keys");
|
|
|
|
}
|
|
|
|
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
|
2016-08-29 06:26:11 +03:00
|
|
|
/**
|
|
|
|
* Command handler for /topic+
|
|
|
|
*/
|
|
|
|
static int handle_crypt_topic(char *word[], char *word_eol[], void *userdata) {
|
|
|
|
const char *target;
|
|
|
|
const char *topic = word_eol[2];
|
|
|
|
char *buf;
|
2020-07-14 02:27:27 +03:00
|
|
|
enum fish_mode mode;
|
2016-08-29 06:26:11 +03:00
|
|
|
|
|
|
|
if (!*topic) {
|
|
|
|
hexchat_print(ph, usage_topic);
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hexchat_list_int(ph, NULL, "type") != 2) {
|
|
|
|
hexchat_printf(ph, "Please change to the channel window where you want to set the topic!");
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
target = hexchat_get_info(ph, "channel");
|
2020-07-14 02:27:27 +03:00
|
|
|
buf = fish_encrypt_for_nick(target, topic, &mode);
|
2016-08-29 06:26:11 +03:00
|
|
|
if (buf == NULL) {
|
|
|
|
hexchat_printf(ph, "/topic+ error, no key found for %s", target);
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hexchat_commandf(ph, "TOPIC %s +OK %s", target, buf);
|
|
|
|
g_free(buf);
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Command handler for /notice+
|
|
|
|
*/
|
|
|
|
static int handle_crypt_notice(char *word[], char *word_eol[], void *userdata)
|
|
|
|
{
|
|
|
|
const char *target = word[2];
|
|
|
|
const char *notice = word_eol[3];
|
2020-07-14 02:27:27 +03:00
|
|
|
char *notice_flag;
|
2016-08-29 06:26:11 +03:00
|
|
|
char *buf;
|
2020-07-14 02:27:27 +03:00
|
|
|
enum fish_mode mode;
|
2016-08-29 06:26:11 +03:00
|
|
|
|
|
|
|
if (!*target || !*notice) {
|
|
|
|
hexchat_print(ph, usage_notice);
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
buf = fish_encrypt_for_nick(target, notice, &mode);
|
2016-08-29 06:26:11 +03:00
|
|
|
if (buf == NULL) {
|
|
|
|
hexchat_printf(ph, "/notice+ error, no key found for %s.", target);
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hexchat_commandf(ph, "quote NOTICE %s :+OK %s", target, buf);
|
2020-07-14 02:27:27 +03:00
|
|
|
notice_flag = g_strconcat("[", fish_modes[mode], "] ", notice, NULL);
|
|
|
|
hexchat_emit_print(ph, "Notice Send", target, notice_flag);
|
|
|
|
g_free(notice_flag);
|
2016-08-29 06:26:11 +03:00
|
|
|
g_free(buf);
|
|
|
|
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Command handler for /msg+
|
|
|
|
*/
|
2020-07-14 02:27:27 +03:00
|
|
|
static int handle_crypt_msg(char *word[], char *word_eol[], void *userdata) {
|
2016-08-29 06:26:11 +03:00
|
|
|
const char *target = word[2];
|
|
|
|
const char *message = word_eol[3];
|
2020-07-14 02:27:27 +03:00
|
|
|
char *message_flag;
|
|
|
|
char *prefix;
|
2016-08-29 06:26:11 +03:00
|
|
|
hexchat_context *query_ctx;
|
|
|
|
char *buf;
|
2020-07-14 02:27:27 +03:00
|
|
|
enum fish_mode mode;
|
2016-08-29 06:26:11 +03:00
|
|
|
|
|
|
|
if (!*target || !*message) {
|
|
|
|
hexchat_print(ph, usage_msg);
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
buf = fish_encrypt_for_nick(target, message, &mode);
|
2016-08-29 06:26:11 +03:00
|
|
|
if (buf == NULL) {
|
|
|
|
hexchat_printf(ph, "/msg+ error, no key found for %s", target);
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hexchat_commandf(ph, "PRIVMSG %s :+OK %s", target, buf);
|
|
|
|
|
|
|
|
query_ctx = find_context_on_network(target);
|
|
|
|
if (query_ctx) {
|
2020-07-14 02:27:27 +03:00
|
|
|
g_assert(hexchat_set_context(ph, query_ctx) == 1);
|
|
|
|
|
|
|
|
prefix = get_my_own_prefix();
|
2016-08-29 06:26:11 +03:00
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
/* Add encrypted flag */
|
|
|
|
message_flag = g_strconcat("[", fish_modes[mode], "] ", message, NULL);
|
2016-08-29 06:26:11 +03:00
|
|
|
hexchat_emit_print(ph, "Your Message", hexchat_get_info(ph, "nick"),
|
2020-07-14 02:27:27 +03:00
|
|
|
message_flag, prefix, NULL);
|
|
|
|
g_free(prefix);
|
|
|
|
g_free(message_flag);
|
|
|
|
} else {
|
2016-08-29 06:26:11 +03:00
|
|
|
hexchat_emit_print(ph, "Message Send", target, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(buf);
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
|
2016-08-29 06:47:45 +03:00
|
|
|
static int handle_crypt_me(char *word[], char *word_eol[], void *userdata) {
|
|
|
|
const char *channel = hexchat_get_info(ph, "channel");
|
|
|
|
char *buf;
|
2020-07-14 02:27:27 +03:00
|
|
|
enum fish_mode mode;
|
2016-08-29 06:47:45 +03:00
|
|
|
|
2020-07-14 02:27:27 +03:00
|
|
|
buf = fish_encrypt_for_nick(channel, word_eol[2], &mode);
|
2016-08-29 06:47:45 +03:00
|
|
|
if (!buf)
|
|
|
|
return HEXCHAT_EAT_NONE;
|
|
|
|
|
|
|
|
hexchat_commandf(ph, "PRIVMSG %s :\001ACTION +OK %s \001", channel, buf);
|
|
|
|
hexchat_emit_print(ph, "Your Action", hexchat_get_info(ph, "nick"),
|
|
|
|
word_eol[2], NULL);
|
|
|
|
|
|
|
|
g_free(buf);
|
|
|
|
return HEXCHAT_EAT_ALL;
|
|
|
|
}
|
|
|
|
|
2011-11-25 02:17:31 +04:00
|
|
|
/**
|
|
|
|
* Returns the plugin name version information.
|
|
|
|
*/
|
2012-10-30 11:42:48 +04:00
|
|
|
void hexchat_plugin_get_info(const char **name, const char **desc,
|
2011-11-25 13:24:33 +04:00
|
|
|
const char **version, void **reserved) {
|
2011-11-25 02:17:31 +04:00
|
|
|
*name = plugin_name;
|
|
|
|
*desc = plugin_desc;
|
|
|
|
*version = plugin_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plugin entry point.
|
|
|
|
*/
|
2012-10-30 11:42:48 +04:00
|
|
|
int hexchat_plugin_init(hexchat_plugin *plugin_handle,
|
2011-11-25 13:24:33 +04:00
|
|
|
const char **name,
|
|
|
|
const char **desc,
|
|
|
|
const char **version,
|
|
|
|
char *arg) {
|
2011-11-25 02:17:31 +04:00
|
|
|
ph = plugin_handle;
|
2016-08-29 04:18:44 +03:00
|
|
|
|
2012-10-30 14:47:12 +04:00
|
|
|
/* Send our info to HexChat */
|
2011-11-25 02:17:31 +04:00
|
|
|
*name = plugin_name;
|
|
|
|
*desc = plugin_desc;
|
|
|
|
*version = plugin_version;
|
2016-08-29 04:18:44 +03:00
|
|
|
|
2011-11-25 02:17:31 +04:00
|
|
|
/* Register commands */
|
2012-10-30 11:42:48 +04:00
|
|
|
hexchat_hook_command(ph, "SETKEY", HEXCHAT_PRI_NORM, handle_setkey, usage_setkey, NULL);
|
|
|
|
hexchat_hook_command(ph, "DELKEY", HEXCHAT_PRI_NORM, handle_delkey, usage_delkey, NULL);
|
2016-08-29 04:18:44 +03:00
|
|
|
hexchat_hook_command(ph, "KEYX", HEXCHAT_PRI_NORM, handle_keyx, usage_keyx, NULL);
|
2016-08-29 06:26:11 +03:00
|
|
|
hexchat_hook_command(ph, "TOPIC+", HEXCHAT_PRI_NORM, handle_crypt_topic, usage_topic, NULL);
|
|
|
|
hexchat_hook_command(ph, "NOTICE+", HEXCHAT_PRI_NORM, handle_crypt_notice, usage_notice, NULL);
|
|
|
|
hexchat_hook_command(ph, "MSG+", HEXCHAT_PRI_NORM, handle_crypt_msg, usage_msg, NULL);
|
2016-08-29 06:47:45 +03:00
|
|
|
hexchat_hook_command(ph, "ME", HEXCHAT_PRI_NORM, handle_crypt_me, NULL, NULL);
|
2016-08-29 04:18:44 +03:00
|
|
|
|
2011-11-25 02:17:31 +04:00
|
|
|
/* Add handlers */
|
2012-10-30 11:42:48 +04:00
|
|
|
hexchat_hook_command(ph, "", HEXCHAT_PRI_NORM, handle_outgoing, NULL, NULL);
|
2016-08-29 04:18:44 +03:00
|
|
|
hexchat_hook_server(ph, "NOTICE", HEXCHAT_PRI_HIGHEST, handle_keyx_notice, NULL);
|
2014-12-12 13:43:57 +03:00
|
|
|
hexchat_hook_server_attrs(ph, "NOTICE", HEXCHAT_PRI_NORM, handle_incoming, NULL);
|
|
|
|
hexchat_hook_server_attrs(ph, "PRIVMSG", HEXCHAT_PRI_NORM, handle_incoming, NULL);
|
2014-12-18 02:49:59 +03:00
|
|
|
/* hexchat_hook_server(ph, "RAW LINE", HEXCHAT_PRI_NORM, handle_debug, NULL); */
|
2014-12-12 13:43:57 +03:00
|
|
|
hexchat_hook_server_attrs(ph, "TOPIC", HEXCHAT_PRI_NORM, handle_incoming, NULL);
|
|
|
|
hexchat_hook_server_attrs(ph, "332", HEXCHAT_PRI_NORM, handle_incoming, NULL);
|
2016-08-29 04:18:44 +03:00
|
|
|
|
|
|
|
if (!dh1080_init())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pending_exchanges = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
|
|
|
|
2012-10-30 11:42:48 +04:00
|
|
|
hexchat_printf(ph, "%s plugin loaded\n", plugin_name);
|
2011-11-25 02:17:31 +04:00
|
|
|
/* Return success */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-30 11:42:48 +04:00
|
|
|
int hexchat_plugin_deinit(void) {
|
2016-08-29 04:18:44 +03:00
|
|
|
g_clear_pointer(&pending_exchanges, g_hash_table_destroy);
|
|
|
|
dh1080_deinit();
|
|
|
|
|
2012-10-30 11:42:48 +04:00
|
|
|
hexchat_printf(ph, "%s plugin unloaded\n", plugin_name);
|
2011-11-25 02:17:31 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2011-11-25 13:24:33 +04:00
|
|
|
|