weechat-xmpp/command.c

416 lines
11 KiB
C
Raw Normal View History

2018-05-09 04:39:16 +00:00
// 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/.
2021-06-27 22:51:05 +00:00
#include <strophe.h>
2018-04-29 21:26:14 +00:00
#include <stdlib.h>
#include <string.h>
2021-06-27 22:51:05 +00:00
#include <weechat/weechat-plugin.h>
2021-06-28 01:17:02 +00:00
#include "plugin.h"
2021-06-30 06:26:06 +00:00
//#include "oauth.h"
//#include "teaminfo.h"
#include "account.h"
#include "channel.h"
#include "buffer.h"
#include "message.h"
2021-06-28 01:17:02 +00:00
#include "command.h"
2021-06-27 22:51:05 +00:00
2021-06-30 06:26:06 +00:00
void command__display_account(struct t_account *account)
2018-04-29 21:26:14 +00:00
{
int num_channels, num_pv;
2021-06-30 06:26:06 +00:00
if (account->is_connected)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
num_channels = 0;//xmpp_account_get_channel_count(account);
num_pv = 0;//xmpp_account_get_pv_count(account);
2018-04-29 21:26:14 +00:00
weechat_printf(
NULL,
2021-06-30 06:26:06 +00:00
" %s %s%s%s %s(%s%s%s) [%s%s%s]%s, %d %s, %d pv",
(account->is_connected) ? "*" : " ",
2018-05-01 10:23:37 +00:00
weechat_color("chat_server"),
2021-06-30 06:26:06 +00:00
account->name,
2018-05-01 10:23:37 +00:00
weechat_color("reset"),
weechat_color("chat_delimiters"),
weechat_color("chat_server"),
2021-06-30 06:26:06 +00:00
(account->jid) ? account->jid : "???",
2018-04-29 21:26:14 +00:00
weechat_color("chat_delimiters"),
weechat_color("reset"),
2021-06-30 06:26:06 +00:00
(account->is_connected) ? _("connected") : _("not connected"),
2018-04-29 21:26:14 +00:00
weechat_color("chat_delimiters"),
weechat_color("reset"),
num_channels,
NG_("channel", "channels", num_channels),
num_pv);
}
else
{
weechat_printf(
NULL,
2021-06-30 06:26:06 +00:00
" %s%s%s %s(%s%s%s)%s",
2018-05-01 10:23:37 +00:00
weechat_color("chat_server"),
2021-06-30 06:26:06 +00:00
account->name,
2018-05-01 10:23:37 +00:00
weechat_color("reset"),
weechat_color("chat_delimiters"),
weechat_color("chat_server"),
2021-06-30 06:26:06 +00:00
(account->jid) ? account->jid : "???",
2018-05-01 10:23:37 +00:00
weechat_color("chat_delimiters"),
2018-04-29 21:26:14 +00:00
weechat_color("reset"));
}
}
2021-06-30 06:26:06 +00:00
void command__account_list(int argc, char **argv)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
int i, one_account_found;
struct t_account *ptr_account2;
char *account_name = NULL;
2018-04-29 21:26:14 +00:00
for (i = 2; i < argc; i++)
{
2021-06-30 06:26:06 +00:00
if (!account_name)
account_name = argv[i];
2018-04-29 21:26:14 +00:00
}
2021-06-30 06:26:06 +00:00
if (!account_name)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
if (accounts)
2018-04-29 21:26:14 +00:00
{
weechat_printf(NULL, "");
2021-06-30 06:26:06 +00:00
weechat_printf(NULL, _("All accounts:"));
for (ptr_account2 = accounts; ptr_account2;
ptr_account2 = ptr_account2->next_account)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
command__display_account(ptr_account2);
2018-04-29 21:26:14 +00:00
}
}
else
2021-06-30 06:26:06 +00:00
weechat_printf(NULL, _("No account"));
2018-04-29 21:26:14 +00:00
}
else
{
2021-06-30 06:26:06 +00:00
one_account_found = 0;
for (ptr_account2 = accounts; ptr_account2;
ptr_account2 = ptr_account2->next_account)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
if (weechat_strcasestr(ptr_account2->name, account_name))
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
if (!one_account_found)
2018-04-29 21:26:14 +00:00
{
weechat_printf(NULL, "");
weechat_printf(NULL,
_("Servers with \"%s\":"),
2021-06-30 06:26:06 +00:00
account_name);
2018-04-29 21:26:14 +00:00
}
2021-06-30 06:26:06 +00:00
one_account_found = 1;
command__display_account(ptr_account2);
2018-04-29 21:26:14 +00:00
}
}
2021-06-30 06:26:06 +00:00
if (!one_account_found)
2018-04-29 21:26:14 +00:00
weechat_printf(NULL,
2021-06-30 06:26:06 +00:00
_("No account found with \"%s\""),
account_name);
2018-04-29 21:26:14 +00:00
}
}
2021-06-30 06:26:06 +00:00
void command__add_account(const char *name, const char *jid, const char *password)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
struct t_account *account;
2018-05-01 10:23:37 +00:00
2021-06-30 06:26:06 +00:00
account = account__casesearch(name);
if (account)
2018-05-01 10:23:37 +00:00
{
weechat_printf(
NULL,
2021-06-30 06:26:06 +00:00
_("%s%s: account \"%s\" already exists, can't add it!"),
weechat_prefix("error"), WEECHAT_XMPP_PLUGIN_NAME,
name);
2018-05-01 10:23:37 +00:00
return;
}
2021-06-30 06:26:06 +00:00
account = account__alloc(name);
if (!account)
2018-05-01 10:23:37 +00:00
{
weechat_printf(
NULL,
2021-06-30 06:26:06 +00:00
_("%s%s: unable to add account"),
weechat_prefix("error"), WEECHAT_XMPP_PLUGIN_NAME);
2018-05-01 10:23:37 +00:00
return;
}
2021-06-30 06:26:06 +00:00
account->name = strdup(name);
if (jid)
account->jid = strdup(jid);
if (password)
account->password = strdup(password);
weechat_config_option_set(account->options[ACCOUNT_OPTION_JID],
account->jid, 1);
weechat_config_option_set(account->options[ACCOUNT_OPTION_PASSWORD],
account->password, 1);
weechat_config_option_set(account->options[ACCOUNT_OPTION_NICKNAME],
account->jid ? xmpp_jid_node(account->context,
account->jid)
: NULL, 1);
2018-05-01 10:23:37 +00:00
weechat_printf (
NULL,
2021-06-30 06:26:06 +00:00
_("%s: account %s%s%s %s(%s%s%s)%s added"),
WEECHAT_XMPP_PLUGIN_NAME,
weechat_color("chat_server"),
2021-06-30 06:26:06 +00:00
account->name,
weechat_color("reset"),
weechat_color("chat_delimiters"),
weechat_color("chat_server"),
2021-06-30 06:26:06 +00:00
account->jid ? account->jid : "???",
weechat_color("chat_delimiters"),
weechat_color("reset"));
2018-04-29 21:26:14 +00:00
}
2021-06-30 06:26:06 +00:00
void command__account_add(int argc, char **argv)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
char *name, *jid = NULL, *password = NULL;
2018-04-29 21:26:14 +00:00
2021-06-30 06:26:06 +00:00
switch (argc)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
case 5:
password = argv[4];
case 4:
jid = argv[3];
case 3:
name = argv[2];
command__add_account(name, jid, password);
break;
default:
weechat_printf(NULL, _("account add: wrong number of arguments"));
break;
2018-04-29 21:26:14 +00:00
}
}
2021-06-30 06:26:06 +00:00
int command__connect_account(struct t_account *account)
2018-05-01 10:23:37 +00:00
{
2021-06-30 06:26:06 +00:00
if (!account)
2018-05-01 10:23:37 +00:00
return 0;
2021-06-30 06:26:06 +00:00
if (account->is_connected)
2018-05-01 10:23:37 +00:00
{
weechat_printf(
NULL,
2021-06-30 06:26:06 +00:00
_("%s%s: already connected to account \"%s\"!"),
weechat_prefix("error"), WEECHAT_XMPP_PLUGIN_NAME,
account->name);
2018-05-01 10:23:37 +00:00
}
2021-06-30 06:26:06 +00:00
account__connect(account);
2018-05-01 10:23:37 +00:00
return 1;
}
2021-06-30 06:26:06 +00:00
int command__account_connect(int argc, char **argv)
2018-05-01 10:23:37 +00:00
{
int i, nb_connect, connect_ok;
2021-06-30 06:26:06 +00:00
struct t_account *ptr_account;
2018-05-01 10:23:37 +00:00
(void) argc;
(void) argv;
connect_ok = 1;
nb_connect = 0;
for (i = 2; i < argc; i++)
{
2018-05-01 10:23:37 +00:00
nb_connect++;
2021-06-30 06:26:06 +00:00
ptr_account = account__search(argv[i]);
if (ptr_account)
{
2021-06-30 06:26:06 +00:00
if (!command__connect_account(ptr_account))
2018-05-01 10:23:37 +00:00
{
connect_ok = 0;
}
}
2018-05-01 10:23:37 +00:00
else
{
weechat_printf(
NULL,
2021-06-30 06:26:06 +00:00
_("%s%s: account not found \"%s\" "
"(add one first with: /account add)"),
weechat_prefix("error"), WEECHAT_XMPP_PLUGIN_NAME,
2018-05-01 10:23:37 +00:00
argv[i]);
}
}
2018-05-01 10:23:37 +00:00
return (connect_ok) ? WEECHAT_RC_OK : WEECHAT_RC_ERROR;
}
2021-06-30 06:26:06 +00:00
void command__account_delete(int argc, char **argv)
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
struct t_account *account;
char *account_name;
2018-05-01 10:23:37 +00:00
if (argc < 3)
{
weechat_printf(
NULL,
_("%sToo few arguments for command\"%s %s\" "
"(help on command: /help %s)"),
weechat_prefix("error"),
argv[0], argv[1], argv[0] + 1);
return;
}
2021-06-30 06:26:06 +00:00
account = account__search(argv[2]);
if (!account)
2018-05-01 10:23:37 +00:00
{
weechat_printf(
NULL,
2021-06-30 06:26:06 +00:00
_("%s%s: account \"%s\" not found for \"%s\" command"),
weechat_prefix("error"), WEECHAT_XMPP_PLUGIN_NAME,
2021-06-27 22:51:05 +00:00
argv[2], "xmpp delete");
2018-05-01 10:23:37 +00:00
return;
}
2021-06-30 06:26:06 +00:00
if (account->is_connected)
2018-05-01 10:23:37 +00:00
{
weechat_printf(
NULL,
2021-06-30 06:26:06 +00:00
_("%s%s: you cannot delete account \"%s\" because you"
2021-06-27 22:51:05 +00:00
"are connected. Try \"/xmpp disconnect %s\" first."),
2021-06-30 06:26:06 +00:00
weechat_prefix("error"), WEECHAT_XMPP_PLUGIN_NAME,
2018-05-01 10:23:37 +00:00
argv[2], argv[2]);
return;
}
2021-06-30 06:26:06 +00:00
account_name = strdup(account->name);
account__free(account);
2018-05-01 10:23:37 +00:00
weechat_printf (
NULL,
2021-06-30 06:26:06 +00:00
_("%s: account %s%s%s has been deleted"),
WEECHAT_XMPP_PLUGIN_NAME,
2018-05-01 10:23:37 +00:00
weechat_color("chat_server"),
2021-06-30 06:26:06 +00:00
(account_name) ? account_name : "???",
2018-05-01 10:23:37 +00:00
weechat_color("reset"));
2021-06-30 06:26:06 +00:00
if (account_name)
free(account_name);
2018-04-29 21:26:14 +00:00
}
2021-06-30 06:26:06 +00:00
int command__account(const void *pointer, void *data,
struct t_gui_buffer *buffer, int argc,
char **argv, char **argv_eol)
2018-04-29 21:26:14 +00:00
{
2021-06-27 22:51:05 +00:00
2018-04-29 21:26:14 +00:00
(void) pointer;
(void) data;
(void) buffer;
2021-06-30 06:26:06 +00:00
if (argc <= 1 || weechat_strcasecmp(argv[1], "list") == 0)
{
command__account_list(argc, argv);
return WEECHAT_RC_OK;
}
if (argc > 1)
{
if (weechat_strcasecmp(argv[1], "add") == 0)
{
command__account_add(argc, argv);
return WEECHAT_RC_OK;
}
if (weechat_strcasecmp(argv[1], "connect") == 0)
{
command__account_connect(argc, argv);
return WEECHAT_RC_OK;
}
if (weechat_strcasecmp(argv[1], "delete") == 0)
{
command__account_delete(argc, argv);
return WEECHAT_RC_OK;
}
WEECHAT_COMMAND_ERROR;
}
2018-04-29 21:26:14 +00:00
return WEECHAT_RC_OK;
}
2021-06-30 06:26:06 +00:00
int command__me(const void *pointer, void *data,
struct t_gui_buffer *buffer, int argc,
char **argv, char **argv_eol)
{
2021-06-30 06:26:06 +00:00
struct t_account *ptr_account = NULL;
struct t_xmpp_channel *ptr_channel = NULL;
2021-06-27 22:51:05 +00:00
//struct t_xmpp_request *request;
char *text;
2021-06-27 22:51:05 +00:00
(void) pointer;
(void) data;
(void) buffer;
(void) argv;
2021-06-30 06:26:06 +00:00
buffer__get_account_and_channel(buffer, &ptr_account, &ptr_channel);
if (!ptr_account)
return WEECHAT_RC_ERROR;
if (!ptr_channel)
{
weechat_printf (
ptr_account->buffer,
_("%s%s: \"%s\" command can not be executed on a account buffer"),
weechat_prefix("error"), WEECHAT_XMPP_PLUGIN_NAME, "me");
return WEECHAT_RC_OK;
}
if (!ptr_account->is_connected)
{
weechat_printf(buffer,
_("%s%s: you are not connected to server"),
weechat_prefix("error"), WEECHAT_XMPP_PLUGIN_NAME);
return WEECHAT_RC_OK;
}
if (argc > 1)
{
text = argv_eol[1];
//request = xmpp_request_chat_memessage(ptr_account,
// weechat_config_string(
// ptr_account->options[XMPP_ACCOUNT_OPTION_TOKEN]),
// ptr_channel->id, text);
//if (request)
// xmpp_account_register_request(ptr_account, request);
}
return WEECHAT_RC_OK;
}
2021-06-30 06:26:06 +00:00
void command__init()
2018-04-29 21:26:14 +00:00
{
2021-06-30 06:26:06 +00:00
struct t_hook *hook;
hook = weechat_hook_command(
"account",
N_("handle xmpp accounts"),
2018-04-29 21:26:14 +00:00
N_("list"
2021-06-30 06:26:06 +00:00
" || add <account>"
" || connect <account>"
" || delete <account>"),
N_(" list: list accounts\n"
" add: add a xmpp account\n"
"connect: connect to a xmpp account\n"
" delete: delete a xmpp account\n"),
2018-04-29 21:26:14 +00:00
"list"
2021-06-30 06:26:06 +00:00
" || add %(xmpp_account)"
" || connect %(xmpp_account)"
" || delete %(xmpp_account)",
&command__account, NULL, NULL);
if (!hook)
weechat_printf(NULL, "Failed to setup command /account");
hook = weechat_hook_command(
"me",
2021-06-27 22:51:05 +00:00
N_("send a xmpp action to the current channel"),
N_("<message>"),
N_("message: message to send"),
2021-06-30 06:26:06 +00:00
NULL, &command__me, NULL, NULL);
if (!hook)
weechat_printf(NULL, "Failed to setup command /me");
2018-04-29 21:26:14 +00:00
}