reformatting, cleanup

This commit is contained in:
Berke Viktor 2012-01-10 06:15:03 +01:00
parent 1cbe3789da
commit 2456d0d3fa
1 changed files with 162 additions and 177 deletions

View File

@ -1,8 +1,30 @@
/* XChat-WDK
* Copyright (c) 2010 <ygrek@autistici.org>
* Copyright (c) 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.
*/
/* /*
* SASL authentication plugin for XChat * SASL authentication plugin for XChat
* Extremely primitive: only PLAIN, no error checking * Extremely primitive: only PLAIN, no error checking
* *
* Copyright (c) 2010, <ygrek@autistici.org>
* http://ygrek.org.ua/p/cap_sasl.html * http://ygrek.org.ua/p/cap_sasl.html
* *
* Docs: * Docs:
@ -10,7 +32,6 @@
* http://tools.ietf.org/html/rfc4422 * http://tools.ietf.org/html/rfc4422
*/ */
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
@ -18,235 +39,199 @@
#include "xchat-plugin.h" #include "xchat-plugin.h"
#define PNAME "XSASL"
#define PDESC "SASL authentication plugin";
#define PVERSION "1.0"
static xchat_plugin *ph; /* plugin handle */ static xchat_plugin *ph; /* plugin handle */
static const char name[] = "XSASL";
struct sasl_info; static const char desc[] = "SASL authentication plugin for XChat";
static const char version[] = "1.0";
struct sasl_info struct sasl_info
{ {
char const* login; char const* login;
char const* password; char const* password;
char const* network; char const* network;
struct sasl_info* next;
}; };
typedef struct sasl_info sasl_info; typedef struct sasl_info sasl_info;
sasl_info* all_info = NULL; static void
add_info (char const* login, char const* password, char const* network)
static void add_info(char const* login, char const* password, char const* network)
{ {
char buffer[512]; char buffer[512];
sasl_info* prev = all_info;
sasl_info* info = (sasl_info*)malloc(sizeof(sasl_info));
info->login = strdup(login);
info->password = strdup(password);
info->network = strdup(network);
info->next = prev;
all_info = info;
sprintf (buffer, "%s:%s", login, password); sprintf (buffer, "%s:%s", login, password);
xchat_set_pluginpref_str (ph, network, buffer); xchat_set_pluginpref_str (ph, network, buffer);
} }
static sasl_info* find_info(char const* network) static sasl_info*
find_info (char const* network)
{ {
//sasl_info* cur;
sasl_info* cur = (sasl_info*)malloc(sizeof(sasl_info));
char buffer[512]; char buffer[512];
char* pos;
char* token; char* token;
sasl_info* cur = (sasl_info*) malloc (sizeof (sasl_info));
// DEBUG
if (xchat_get_pluginpref_str (ph, network, buffer)) if (xchat_get_pluginpref_str (ph, network, buffer))
{ {
cur->network = strdup (network);
//pos = strchr (buffer, ':');
//cur->login = g_strndup (buffer, pos-buffer);
token = strtok (buffer, ":"); token = strtok (buffer, ":");
cur->login = g_strdup (token); cur->login = g_strdup (token);
token = strtok (NULL, ":"); token = strtok (NULL, ":");
cur->password = g_strdup (token); cur->password = g_strdup (token);
//xchat_printf (ph, "network: %s\n", cur->network); cur->network = g_strdup (network);
//xchat_printf (ph, "login: %s\n", cur->login);
//xchat_printf (ph, "password: %s\n", cur->password);
cur->next = NULL;
return cur; return cur;
} }
#if 0
cur = all_info;
while (cur)
{
if (0 == strcmp(cur->network, network)) return cur;
cur = cur->next;
}
#endif
return NULL; return NULL;
} }
static sasl_info* get_info(void) static sasl_info*
get_info (void)
{ {
const char* name = xchat_get_info(ph, "network"); const char* name;
name = xchat_get_info (ph, "network");
if (name) if (name)
return find_info(name); {
return find_info (name);
}
else else
{
return NULL; return NULL;
}
} }
static int authend_cb(char *word[], char *word_eol[], void *userdata) static int
authend_cb (char *word[], char *word_eol[], void *userdata)
{ {
if (get_info()) if (get_info ())
{ {
xchat_printf(ph, "XSASL result: %s", word_eol[1]); xchat_printf (ph, "XSASL result: %s\n", word_eol[1]);
xchat_commandf(ph, "QUOTE CAP END"); xchat_commandf (ph, "QUOTE CAP END");
} }
return XCHAT_EAT_ALL; return XCHAT_EAT_ALL;
} }
/* /*
static int disconnect_cb(char *word[], void *userdata) static int
disconnect_cb (char *word[], void *userdata)
{ {
xchat_printf(ph, "disconnected"); xchat_printf (ph, "disconnected\n");
return XCHAT_EAT_NONE; return XCHAT_EAT_NONE;
} }
*/ */
static int server_cb(char *word[], char *word_eol[], void *userdata) static int
server_cb (char *word[], char *word_eol[], void *userdata)
{ {
if (0 == strcmp("AUTHENTICATE",word[1]) && 0 == strcmp("+",word[2]))
{
size_t len; size_t len;
char* buf; char* buf;
char* enc; char* enc;
sasl_info* p = get_info(); sasl_info* p;
if (!p) return XCHAT_EAT_NONE;
xchat_printf(ph,"XSASL authenticating as %s",p->login); if (strcmp ("AUTHENTICATE", word[1]) == 0 && strcmp ("+", word[2]) == 0)
{
p = get_info ();
len = strlen(p->login)*2 + 2 + strlen(p->password); if (!p)
buf = (char*)malloc(len + 1); {
strcpy(buf,p->login); return XCHAT_EAT_NONE;
strcpy(buf+strlen(p->login)+1,p->login); }
strcpy(buf+strlen(p->login)*2+2,p->password);
enc = g_base64_encode((unsigned char*)buf,len);
/*xchat_printf(ph,"AUTHENTICATE %s",enc);*/ xchat_printf (ph, "XSASL authenticating as %s\n", p->login);
xchat_commandf(ph,"QUOTE AUTHENTICATE %s",enc);
free(enc); len = strlen (p->login) * 2 + 2 + strlen (p->password);
free(buf); buf = (char*) malloc (len + 1);
strcpy (buf, p->login);
strcpy (buf + strlen (p->login) + 1, p->login);
strcpy (buf + strlen (p->login) * 2 + 2, p->password);
enc = g_base64_encode ((unsigned char*) buf, len);
/* xchat_printf (ph, "AUTHENTICATE %s\}", enc); */
xchat_commandf (ph, "QUOTE AUTHENTICATE %s", enc);
free (enc);
free (buf);
return XCHAT_EAT_ALL; return XCHAT_EAT_ALL;
} }
return XCHAT_EAT_NONE; return XCHAT_EAT_NONE;
} }
static int cap_cb(char *word[], char *word_eol[], void *userdata) static int
cap_cb (char *word[], char *word_eol[], void *userdata)
{ {
if (get_info()) if (get_info ())
{ {
/* FIXME test sasl cap */ /* FIXME test sasl cap */
xchat_printf(ph, "XSASL info: %s", word_eol[1]); xchat_printf (ph, "XSASL info: %s\n", word_eol[1]);
xchat_commandf(ph,"QUOTE AUTHENTICATE PLAIN"); xchat_commandf (ph, "QUOTE AUTHENTICATE PLAIN");
} }
return XCHAT_EAT_ALL; return XCHAT_EAT_ALL;
} }
static int sasl_cmd_cb(char *word[], char *word_eol[], void *userdata) static int
sasl_cmd_cb (char *word[], char *word_eol[], void *userdata)
{ {
const char* login = word[2]; const char* login = word[2];
const char* password = word[3]; const char* password = word[3];
const char* network = word_eol[4]; const char* network = word_eol[4];
if (!login || !*login)
{
sasl_info *cur = all_info;
if (NULL == cur)
{
xchat_printf(ph,"Nothing, see /HELP XSASL");
return XCHAT_EAT_ALL;
}
while (cur)
{
xchat_printf(ph,"%s:%s at %s",cur->login,cur->password,cur->network);
cur = cur->next;
}
return XCHAT_EAT_ALL;
}
if (!login || !password || !network || !*login || !*password || !*network) if (!login || !password || !network || !*login || !*password || !*network)
{ {
xchat_printf(ph,"Wrong usage, try /HELP XSASL"); xchat_printf (ph, "Usage: SASL <login> <password> <network>, enable SASL authentication for given network\n");
return XCHAT_EAT_ALL; return XCHAT_EAT_ALL;
} }
add_info(login,password,network); add_info (login, password, network);
xchat_printf (ph, "Enabled SASL authentication for the \"%s\" network\n", network);
xchat_printf(ph,"Enabled SASL authentication for %s",network);
return XCHAT_EAT_ALL; return XCHAT_EAT_ALL;
} }
static int connect_cb(char *word[], void *userdata) static int
connect_cb (char *word[], void *userdata)
{ {
if (get_info()) if (get_info ())
{ {
xchat_printf(ph, "XSASL enabled"); xchat_printf (ph, "XSASL enabled\n");
xchat_commandf(ph, "QUOTE CAP REQ :sasl"); xchat_commandf (ph, "QUOTE CAP REQ :sasl");
} }
return XCHAT_EAT_NONE; return XCHAT_EAT_NONE;
} }
int xchat_plugin_init(xchat_plugin *plugin_handle, int
char **plugin_name, xchat_plugin_init (xchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg)
char **plugin_desc,
char **plugin_version,
char *arg)
{ {
/* we need to save this for use with any xchat_* functions */ /* we need to save this for use with any xchat_* functions */
ph = plugin_handle; ph = plugin_handle;
/* tell xchat our info */ /* tell xchat our info */
*plugin_name = PNAME; *plugin_name = name;
*plugin_desc = PDESC; *plugin_desc = desc;
*plugin_version = PVERSION; *plugin_version = version;
xchat_hook_command(ph, "xsasl", XCHAT_PRI_NORM, sasl_cmd_cb, xchat_hook_command (ph, "XSASL", XCHAT_PRI_NORM, sasl_cmd_cb, "Usage: SASL <login> <password> <network>, enable SASL authentication for given network", 0);
"Usage: SASL <login> <password> <network>, enable SASL authentication for given network", 0); xchat_hook_print (ph, "Connected", XCHAT_PRI_NORM, connect_cb, NULL);
/* xchat_hook_print (ph, "Disconnected", XCHAT_PRI_NORM, disconnect_cb, NULL); */
xchat_hook_server (ph, "CAP", XCHAT_PRI_NORM, cap_cb, NULL);
xchat_hook_server (ph, "RAW LINE", XCHAT_PRI_NORM, server_cb, NULL);
xchat_hook_server (ph, "903", XCHAT_PRI_NORM, authend_cb, NULL);
xchat_hook_server (ph, "904", XCHAT_PRI_NORM, authend_cb, NULL);
xchat_hook_server (ph, "905", XCHAT_PRI_NORM, authend_cb, NULL);
xchat_hook_server (ph, "906", XCHAT_PRI_NORM, authend_cb, NULL);
xchat_hook_server (ph, "907", XCHAT_PRI_NORM, authend_cb, NULL);
xchat_hook_print(ph, "Connected", XCHAT_PRI_NORM, connect_cb, NULL); xchat_printf (ph, "%s plugin loaded\n", name);
/*
xchat_hook_print(ph, "Disconnected", XCHAT_PRI_NORM, disconnect_cb, NULL);
*/
xchat_hook_server(ph, "CAP", XCHAT_PRI_NORM, cap_cb, NULL);
xchat_hook_server(ph, "RAW LINE", XCHAT_PRI_NORM, server_cb, NULL);
xchat_hook_server(ph, "903", XCHAT_PRI_NORM, authend_cb, NULL);
xchat_hook_server(ph, "904", XCHAT_PRI_NORM, authend_cb, NULL);
xchat_hook_server(ph, "905", XCHAT_PRI_NORM, authend_cb, NULL);
xchat_hook_server(ph, "906", XCHAT_PRI_NORM, authend_cb, NULL);
xchat_hook_server(ph, "907", XCHAT_PRI_NORM, authend_cb, NULL);
/* xchat_print(ph,"Loading cap_sasl.conf");
xchat_commandf(ph, "load -e %s/cap_sasl.conf",xchat_get_info(ph, "xchatdir")); */
xchat_printf(ph, PNAME " plugin loaded\n");
return 1; return 1;
} }
int xchat_plugin_deinit (void) int
xchat_plugin_deinit (void)
{ {
xchat_printf(ph, PNAME " plugin unloaded\n"); xchat_printf (ph, "%s plugin unloaded\n", name);
return 1; return 1;
} }