You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
weechat-xmpp/xmpp-config.c

368 lines
12 KiB
C

7 years ago
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 years ago
#include <stdlib.h>
#include <string.h>
3 years ago
#include <strophe.h>
#include <weechat/weechat-plugin.h>
7 years ago
3 years ago
#include "xmpp.h"
#include "xmpp-config.h"
7 years ago
3 years ago
struct t_config_file *xmpp_config_file;
7 years ago
3 years ago
//struct t_config_section *xmpp_config_section_workspace_default;
//struct t_config_section *xmpp_config_section_workspace;
7 years ago
3 years ago
struct t_config_option *xmpp_config_serverdef_jid;
struct t_config_option *xmpp_config_serverdef_password;
struct t_config_option *xmpp_config_look_nick_completion_smart;
3 years ago
/*
struct t_config_option *xmpp_config_workspace_default[XMPP_WORKSPACE_NUM_OPTIONS];
7 years ago
3 years ago
int xmpp_config_workspace_check_value_cb(const void *pointer, void *data,
struct t_config_option *option,
7 years ago
const char *value)
{
(void) pointer;
(void) data;
(void) option;
(void) value;
3 years ago
return 1;
7 years ago
}
3 years ago
void xmpp_config_workspace_change_cb(const void *pointer, void *data,
7 years ago
struct t_config_option *option)
{
(void) pointer;
(void) data;
(void) option;
7 years ago
}
3 years ago
void xmpp_config_workspace_default_change_cb(const void *pointer, void *data,
7 years ago
struct t_config_option *option)
{
(void) pointer;
(void) data;
(void) option;
7 years ago
}
struct t_config_option *
3 years ago
xmpp_config_workspace_new_option (struct t_config_file *config_file,
7 years ago
struct t_config_section *section,
int index_option,
const char *option_name,
const char *default_value,
const char *value,
int null_value_allowed,
int (*callback_check_value)(const void *pointer,
void *data,
struct t_config_option *option,
const char *value),
const void *callback_check_value_pointer,
void *callback_check_value_data,
void (*callback_change)(const void *pointer,
void *data,
struct t_config_option *option),
const void *callback_change_pointer,
void *callback_change_data)
{
3 years ago
struct t_config_option *new_option;
7 years ago
new_option = NULL;
switch (index_option)
{
3 years ago
case XMPP_WORKSPACE_OPTION_TOKEN:
7 years ago
new_option = weechat_config_new_option (
config_file, section,
option_name, "string",
3 years ago
N_("xmpp api token"),
7 years ago
NULL, 0, 0,
default_value, value,
null_value_allowed,
callback_check_value,
callback_check_value_pointer,
callback_check_value_data,
callback_change,
callback_change_pointer,
callback_change_data,
NULL, NULL, NULL);
break;
3 years ago
case XMPP_WORKSPACE_NUM_OPTIONS:
7 years ago
break;
}
return new_option;
}
3 years ago
void xmpp_config_workspace_create_default_options(struct t_config_section *section)
7 years ago
{
int i;
3 years ago
for (i = 0; i < XMPP_WORKSPACE_NUM_OPTIONS; i++)
{
xmpp_config_workspace_default[i] = xmpp_config_workspace_new_option(
xmpp_config_file,
section,
i,
xmpp_workspace_options[i][0],
xmpp_workspace_options[i][1],
xmpp_workspace_options[i][1],
0,
&xmpp_config_workspace_check_value_cb,
xmpp_workspace_options[i][0],
NULL,
&xmpp_config_workspace_default_change_cb,
xmpp_workspace_options[i][0],
NULL);
}
7 years ago
}
3 years ago
int xmpp_config_workspace_read_cb (const void *pointer, void *data,
7 years ago
struct t_config_file *config_file,
struct t_config_section *section,
const char *option_name, const char *value)
{
3 years ago
struct t_xmpp_workspace *ptr_workspace;
7 years ago
int index_option, rc, i;
char *pos_option, *workspace_domain;
7 years ago
(void) pointer;
(void) data;
(void) config_file;
(void) section;
rc = WEECHAT_CONFIG_OPTION_SET_ERROR;
if (option_name)
{
pos_option = strrchr(option_name, '.');
if (pos_option)
{
workspace_domain = weechat_strndup(option_name,
pos_option - option_name);
7 years ago
pos_option++;
if (workspace_domain)
7 years ago
{
3 years ago
index_option = xmpp_workspace_search_option(pos_option);
7 years ago
if (index_option >= 0)
{
3 years ago
ptr_workspace = xmpp_workspace_search(workspace_domain);
7 years ago
if (!ptr_workspace)
3 years ago
ptr_workspace = xmpp_workspace_alloc(workspace_domain);
7 years ago
if (ptr_workspace)
{
if (ptr_workspace->reloading_from_config
&& !ptr_workspace->reloaded_from_config)
{
3 years ago
for (i = 0; i < XMPP_WORKSPACE_NUM_OPTIONS; i++)
7 years ago
{
weechat_config_option_set(
ptr_workspace->options[i], NULL, 1);
}
ptr_workspace->reloaded_from_config = 1;
}
rc = weechat_config_option_set(
ptr_workspace->options[index_option], value, 1);
}
else
{
weechat_printf(
NULL,
_("%s%s: error adding workspace \"%s\""),
3 years ago
weechat_prefix("error"), XMPP_PLUGIN_NAME,
workspace_domain);
7 years ago
}
}
free(workspace_domain);
7 years ago
}
}
}
if (rc == WEECHAT_CONFIG_OPTION_SET_ERROR)
{
weechat_printf(
NULL,
_("%s%s: error creating workspace option \"%s\""),
3 years ago
weechat_prefix("error"), XMPP_PLUGIN_NAME, option_name);
7 years ago
}
return rc;
}
3 years ago
int xmpp_config_workspace_write_cb (const void *pointer, void *data,
7 years ago
struct t_config_file *config_file,
const char *section_name)
{
3 years ago
struct t_xmpp_workspace *ptr_workspace;
7 years ago
int i;
(void) pointer;
(void) data;
if (!weechat_config_write_line(config_file, section_name, NULL))
return WEECHAT_CONFIG_WRITE_ERROR;
3 years ago
for (ptr_workspace = xmpp_workspaces; ptr_workspace;
7 years ago
ptr_workspace = ptr_workspace->next_workspace)
{
3 years ago
for (i = 0; i < XMPP_WORKSPACE_NUM_OPTIONS; i++)
7 years ago
{
if (!weechat_config_write_option(config_file,
ptr_workspace->options[i]))
return WEECHAT_CONFIG_WRITE_ERROR;
}
}
return WEECHAT_CONFIG_WRITE_OK;
}
3 years ago
*/
int xmpp_config_reload (const void *pointer, void *data,
struct t_config_file *config_file)
{
(void) pointer;
(void) data;
//weechat_config_section_free_options(xmpp_config_section_workspace_default);
//weechat_config_section_free_options(xmpp_config_section_workspace);
//xmpp_workspace_free_all();
return weechat_config_reload(config_file);
}
7 years ago
3 years ago
int xmpp_config_init()
7 years ago
{
3 years ago
struct t_config_section *ptr_section_serverdef;
struct t_config_section *ptr_section_server;
struct t_config_section *ptr_section_look;
7 years ago
3 years ago
xmpp_config_file = weechat_config_new(XMPP_CONFIG_NAME,
&xmpp_config_reload, NULL, NULL);
7 years ago
3 years ago
if(!xmpp_config_file)
7 years ago
return 0;
3 years ago
ptr_section_serverdef = weechat_config_new_section(
xmpp_config_file, "server_default",
0, 0,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL);
3 years ago
ptr_section_server = weechat_config_new_section(
xmpp_config_file, "server",
0, 0,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL);
3 years ago
ptr_section_look = weechat_config_new_section(
xmpp_config_file, "look",
7 years ago
0, 0,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL);
3 years ago
if (!ptr_section_serverdef
|| !ptr_section_server
|| !ptr_section_look)
7 years ago
{
3 years ago
weechat_config_free(xmpp_config_file);
xmpp_config_file = NULL;
7 years ago
return 0;
}
3 years ago
xmpp_config_serverdef_jid = weechat_config_new_option (
xmpp_config_file, ptr_section_serverdef,
"jid", "string",
N_("XMPP Server JID"),
NULL, 0, 0, "", "", 0,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
7 years ago
3 years ago
xmpp_config_serverdef_password = weechat_config_new_option (
xmpp_config_file, ptr_section_serverdef,
"password", "string",
N_("XMPP Server Password"),
NULL, 0, 0, "", "", 0,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
7 years ago
3 years ago
xmpp_config_look_nick_completion_smart = weechat_config_new_option (
xmpp_config_file, ptr_section_look,
"nick_completion_smart", "integer",
N_("smart completion for nicks (completes first with last speakers): "
"speakers = all speakers (including highlights), "
"speakers_highlights = only speakers with highlight"),
"off|speakers|speakers_highlights", 0, 0, "speakers", NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
7 years ago
3 years ago
//ptr_section = weechat_config_new_section(
// xmpp_config_file, "workspace_default",
// 0, 0,
// NULL, NULL, NULL,
// NULL, NULL, NULL,
// NULL, NULL, NULL,
// NULL, NULL, NULL,
// NULL, NULL, NULL);
//if (!ptr_section)
//{
// weechat_config_free(xmpp_config_file);
// xmpp_config_file = NULL;
// return 0;
//}
//xmpp_config_section_workspace_default = ptr_section;
//xmpp_config_workspace_create_default_options(ptr_section);
// ptr_section = weechat_config_new_section(
// xmpp_config_file, "workspace",
// 0, 0,
// &xmpp_config_workspace_read_cb, NULL, NULL,
// &xmpp_config_workspace_write_cb, NULL, NULL,
// NULL, NULL, NULL,
// NULL, NULL, NULL,
// NULL, NULL, NULL);
//if (!ptr_section)
//{
// weechat_config_free(xmpp_config_file);
// xmpp_config_file = NULL;
// return 0;
//}
//xmpp_config_section_workspace = ptr_section;
7 years ago
return 1;
}
3 years ago
int xmpp_config_read()
7 years ago
{
3 years ago
int rc;
3 years ago
rc = weechat_config_read(xmpp_config_file);
return rc;
7 years ago
}
3 years ago
int xmpp_config_write()
7 years ago
{
3 years ago
return weechat_config_write(xmpp_config_file);
7 years ago
}
3 years ago
void xmpp_config_free()
7 years ago
{
}