c.sexchat/plugins/fishlim/plugin_hexchat.c

293 lines
8.9 KiB
C
Raw Normal View History

2011-11-24 23:17:31 +01:00
/*
2012-05-04 19:29:02 +02:00
Copyright (c) 2010-2011 Samuel Lidén Borell <samuel@kodafritt.se>
2011-11-24 23:17:31 +01: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.
*/
2011-11-24 23:37:43 +01:00
#include <glib.h>
2011-11-24 23:17:31 +01:00
#include <stdlib.h>
#include <string.h>
// #pragma GCC visibility push(default)
2012-10-24 21:33:02 +02:00
#include "hexchat-plugin.h"
2012-10-30 11:47:12 +01:00
#define HEXCHAT_MAX_WORDS 32
2011-11-24 23:17:31 +01:00
// #pragma GCC visibility pop
//#define EXPORT __attribute((visibility("default")))
//#define EXPORT
#include "fish.h"
#include "keystore.h"
#include "irc.h"
static const char plugin_name[] = "FiSHLiM";
static const char plugin_desc[] = "Encryption plugin for the FiSH protocol. Less is More!";
2012-05-13 14:45:32 +02:00
static const char plugin_version[] = "0.0.16";
2011-11-24 23:17:31 +01:00
static const char usage_setkey[] = "Usage: SETKEY [<nick or #channel>] <password>, sets the key for a channel or nick";
static const char usage_delkey[] = "Usage: DELKEY <nick or #channel>, deletes the key for a channel or nick";
2012-10-30 08:42:48 +01:00
static hexchat_plugin *ph;
2011-11-24 23:17:31 +01:00
2011-11-24 23:37:43 +01:00
/**
* Returns the path to the key store file.
*/
gchar *get_config_filename() {
return g_build_filename(hexchat_get_info(ph, "hexchatdirfs"), "addon_fishlim.conf", NULL);
2011-11-24 23:37:43 +01:00
}
2011-11-24 23:17:31 +01:00
/**
* Appends data to a string. Returns true if there was sufficient memory.
* Frees *s and returns false if an error occurs.
*/
static bool append(char **s, size_t *length, const char *data) {
size_t datalen = strlen(data);
char *extended = realloc(*s, *length + datalen + 1);
if (!extended) {
free(*s);
return false;
}
memcpy(extended + *length, data, datalen + 1);
*s = extended;
*length += datalen;
return true;
}
/*static int handle_debug(char *word[], char *word_eol[], void *userdata) {
2012-10-30 08:42:48 +01:00
hexchat_printf(ph, "debug incoming: ");
2011-11-24 23:17:31 +01:00
for (size_t i = 1; word[i] != NULL && word[i][0] != '\0'; i++) {
2012-10-30 08:42:48 +01:00
hexchat_printf(ph, ">%s< ", word[i]);
2011-11-24 23:17:31 +01:00
}
2012-10-30 08:42:48 +01:00
hexchat_printf(ph, "\n");
2012-10-30 07:40:37 +01:00
return HEXCHAT_EAT_NONE;
2011-11-24 23:17:31 +01:00
}*/
/**
* Called when a message is to be sent.
*/
static int handle_outgoing(char *word[], char *word_eol[], void *userdata) {
const char *own_nick;
// Encrypt the message if possible
2012-10-30 08:42:48 +01:00
const char *channel = hexchat_get_info(ph, "channel");
2011-11-24 23:17:31 +01:00
char *encrypted = fish_encrypt_for_nick(channel, word_eol[1]);
2012-10-30 07:40:37 +01:00
if (!encrypted) return HEXCHAT_EAT_NONE;
2011-11-24 23:17:31 +01:00
// Display message
2012-10-30 08:42:48 +01:00
own_nick = hexchat_get_info(ph, "nick");
hexchat_emit_print(ph, "Your Message", own_nick, word_eol[1], NULL);
2011-11-24 23:17:31 +01:00
// Send message
2012-10-30 08:42:48 +01:00
hexchat_commandf(ph, "PRIVMSG %s :+OK %s", channel, encrypted);
2011-11-24 23:17:31 +01:00
free(encrypted);
2012-10-30 10:42:37 +01:00
return HEXCHAT_EAT_HEXCHAT;
2011-11-24 23:17:31 +01:00
}
/**
* Called when a channel message or private message is received.
*/
static int handle_incoming(char *word[], char *word_eol[], void *userdata) {
const char *prefix;
const char *command;
const char *recipient;
const char *encrypted;
const char *peice;
2011-11-25 10:24:33 +01:00
char *sender_nick;
2011-11-24 23:17:31 +01:00
char *decrypted;
char *message;
size_t w;
size_t ew;
size_t uw;
size_t length;
if (!irc_parse_message((const char **)word, &prefix, &command, &w))
2012-10-30 07:40:37 +01:00
return HEXCHAT_EAT_NONE;
2011-11-24 23:17:31 +01:00
// Topic (command 332) has an extra parameter
if (!strcmp(command, "332")) w++;
// Look for encrypted data
2012-10-30 11:47:12 +01:00
for (ew = w+1; ew < HEXCHAT_MAX_WORDS-1; ew++) {
2011-11-24 23:17:31 +01:00
const char *s = (ew == w+1 ? word[ew]+1 : word[ew]);
2012-03-16 00:17:03 +01:00
if (strcmp(s, "+OK") == 0 || strcmp(s, "mcps") == 0) goto has_encrypted_data;
2011-11-24 23:17:31 +01:00
}
2012-10-30 07:40:37 +01:00
return HEXCHAT_EAT_NONE;
2011-11-24 23:17:31 +01:00
has_encrypted_data: ;
// Extract sender nick and recipient nick/channel
sender_nick = irc_prefix_get_nick(prefix);
recipient = word[w];
// Try to decrypt with these (the keys are searched for in the key store)
encrypted = word[ew+1];
decrypted = fish_decrypt_from_nick(recipient, encrypted);
if (!decrypted) decrypted = fish_decrypt_from_nick(sender_nick, encrypted);
// Check for error
if (!decrypted) goto decrypt_error;
// Build unecrypted message
message = NULL;
length = 0;
if (!append(&message, &length, "RECV")) goto decrypt_error;
2012-10-30 11:47:12 +01:00
for (uw = 1; uw < HEXCHAT_MAX_WORDS; uw++) {
2011-11-24 23:17:31 +01:00
if (word[uw][0] != '\0' && !append(&message, &length, " ")) goto decrypt_error;
if (uw == ew) {
// Add the encrypted data
peice = decrypted;
uw++; // Skip "OK+"
2012-05-13 14:45:32 +02:00
if (ew == w+1) {
// Prefix with colon, which gets stripped out otherwise
if (!append(&message, &length, ":")) goto decrypt_error;
}
2011-11-24 23:17:31 +01:00
} else {
// Add unencrypted data (for example, a prefix from a bouncer or bot)
2012-05-13 14:45:32 +02:00
peice = word[uw];
2011-11-24 23:17:31 +01:00
}
if (!append(&message, &length, peice)) goto decrypt_error;
}
free(decrypted);
// Simulate unencrypted message
2012-10-30 08:42:48 +01:00
//hexchat_printf(ph, "simulating: %s\n", message);
hexchat_command(ph, message);
2011-11-24 23:17:31 +01:00
free(message);
free(sender_nick);
2012-10-30 10:42:37 +01:00
return HEXCHAT_EAT_HEXCHAT;
2011-11-24 23:17:31 +01:00
decrypt_error:
free(decrypted);
free(sender_nick);
2012-10-30 07:40:37 +01:00
return HEXCHAT_EAT_NONE;
2011-11-24 23:17:31 +01:00
}
/**
* Command handler for /setkey
*/
static int handle_setkey(char *word[], char *word_eol[], void *userdata) {
const char *nick;
const char *key;
// Check syntax
if (*word[2] == '\0') {
2012-10-30 08:42:48 +01:00
hexchat_printf(ph, "%s\n", usage_setkey);
2012-10-30 10:42:37 +01:00
return HEXCHAT_EAT_HEXCHAT;
2011-11-24 23:17:31 +01:00
}
if (*word[3] == '\0') {
// /setkey password
2012-10-30 08:42:48 +01:00
nick = hexchat_get_info(ph, "channel");
2011-11-24 23:17:31 +01:00
key = word_eol[2];
} else {
// /setkey #channel password
nick = word[2];
key = word_eol[3];
}
// Set password
if (keystore_store_key(nick, key)) {
2012-10-30 08:42:48 +01:00
hexchat_printf(ph, "Stored key for %s\n", nick);
2011-11-24 23:17:31 +01:00
} else {
2012-10-30 08:42:48 +01:00
hexchat_printf(ph, "\00305Failed to store key in blow.ini\n", nick, key);
2011-11-24 23:17:31 +01:00
}
2012-10-30 10:42:37 +01:00
return HEXCHAT_EAT_HEXCHAT;
2011-11-24 23:17:31 +01:00
}
/**
* Command handler for /delkey
*/
static int handle_delkey(char *word[], char *word_eol[], void *userdata) {
const char *nick;
// Check syntax
if (*word[2] == '\0' || *word[3] != '\0') {
2012-10-30 08:42:48 +01:00
hexchat_printf(ph, "%s\n", usage_delkey);
2012-10-30 10:42:37 +01:00
return HEXCHAT_EAT_HEXCHAT;
2011-11-24 23:17:31 +01:00
}
nick = word_eol[2];
// Delete the given nick from the key store
if (keystore_delete_nick(nick)) {
2012-10-30 08:42:48 +01:00
hexchat_printf(ph, "Deleted key for %s\n", nick);
2011-11-24 23:17:31 +01:00
} else {
2012-10-30 08:42:48 +01:00
hexchat_printf(ph, "\00305Failed to delete key in blow.ini!\n", nick);
2011-11-24 23:17:31 +01:00
}
2012-10-30 10:42:37 +01:00
return HEXCHAT_EAT_HEXCHAT;
2011-11-24 23:17:31 +01:00
}
/**
* Returns the plugin name version information.
*/
2012-10-30 08:42:48 +01:00
void hexchat_plugin_get_info(const char **name, const char **desc,
2011-11-25 10:24:33 +01:00
const char **version, void **reserved) {
2011-11-24 23:17:31 +01:00
*name = plugin_name;
*desc = plugin_desc;
*version = plugin_version;
}
/**
* Plugin entry point.
*/
2012-10-30 08:42:48 +01:00
int hexchat_plugin_init(hexchat_plugin *plugin_handle,
2011-11-25 10:24:33 +01:00
const char **name,
const char **desc,
const char **version,
char *arg) {
2011-11-24 23:17:31 +01:00
ph = plugin_handle;
2012-10-30 11:47:12 +01:00
/* Send our info to HexChat */
2011-11-24 23:17:31 +01:00
*name = plugin_name;
*desc = plugin_desc;
*version = plugin_version;
/* Register commands */
2012-10-30 08:42:48 +01: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);
2011-11-24 23:17:31 +01:00
/* Add handlers */
2012-10-30 08:42:48 +01:00
hexchat_hook_command(ph, "", HEXCHAT_PRI_NORM, handle_outgoing, NULL, NULL);
hexchat_hook_server(ph, "NOTICE", HEXCHAT_PRI_NORM, handle_incoming, NULL);
hexchat_hook_server(ph, "PRIVMSG", HEXCHAT_PRI_NORM, handle_incoming, NULL);
//hexchat_hook_server(ph, "RAW LINE", HEXCHAT_PRI_NORM, handle_debug, NULL);
hexchat_hook_server(ph, "TOPIC", HEXCHAT_PRI_NORM, handle_incoming, NULL);
hexchat_hook_server(ph, "332", HEXCHAT_PRI_NORM, handle_incoming, NULL);
2011-11-24 23:17:31 +01:00
2012-10-30 08:42:48 +01:00
hexchat_printf(ph, "%s plugin loaded\n", plugin_name);
2011-11-24 23:17:31 +01:00
/* Return success */
return 1;
}
2012-10-30 08:42:48 +01:00
int hexchat_plugin_deinit(void) {
hexchat_printf(ph, "%s plugin unloaded\n", plugin_name);
2011-11-24 23:17:31 +01:00
return 1;
}
2011-11-25 10:24:33 +01:00