tox-weechat/src/twc-completion.c

155 lines
4.9 KiB
C
Raw Normal View History

2014-09-28 01:29:34 +00:00
/*
2016-05-10 10:04:24 +00:00
* Copyright (c) 2016 Håvard Pettersson <mail@haavard.me>
2014-09-28 01:29:34 +00:00
*
* This file is part of Tox-WeeChat.
*
* Tox-WeeChat is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tox-WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tox-WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdio.h>
2014-09-28 01:29:34 +00:00
#include <weechat/weechat-plugin.h>
#include "twc.h"
#include "twc-list.h"
2014-09-28 03:55:18 +00:00
#include "twc-profile.h"
2014-10-04 22:24:52 +00:00
#include "twc-utils.h"
2014-09-28 01:29:34 +00:00
#include "twc-completion.h"
enum
{
TWC_ALL_PROFILES,
TWC_LOADED_PROFILES,
TWC_UNLOADED_PROFILES,
};
2014-10-04 22:24:52 +00:00
enum
{
TWC_COMPLETE_FRIEND_NAME = 2 << 0,
TWC_COMPLETE_FRIEND_ID = 1 << 1,
};
/**
2014-10-04 22:28:47 +00:00
* Complete a friends name and/or Tox ID.
2014-10-04 22:24:52 +00:00
*/
int
twc_completion_friend(const void *pointer, void *data,
2014-10-04 22:24:52 +00:00
const char *completion_item,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
int flags = (int)(intptr_t)pointer;
2014-10-04 22:24:52 +00:00
struct t_twc_profile *profile = twc_profile_search_buffer(buffer);
if (!profile)
return WEECHAT_RC_OK;
uint32_t friend_count = tox_self_get_friend_list_size(profile->tox);
uint32_t *friend_numbers = malloc(sizeof(uint32_t) * friend_count);
tox_self_get_friend_list(profile->tox, friend_numbers);
2014-10-04 22:24:52 +00:00
for (uint32_t i = 0; i < friend_count; ++i)
{
if (flags & TWC_COMPLETE_FRIEND_ID)
{
uint8_t tox_id[TOX_PUBLIC_KEY_SIZE];
char hex_id[TOX_PUBLIC_KEY_SIZE * 2 + 1];
2014-10-04 22:24:52 +00:00
2015-08-26 18:02:36 +00:00
TOX_ERR_FRIEND_GET_PUBLIC_KEY err;
tox_friend_get_public_key(profile->tox, friend_numbers[i], tox_id, &err);
if (err == TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK)
{
twc_bin2hex(tox_id, TOX_PUBLIC_KEY_SIZE, hex_id);
2014-10-04 22:24:52 +00:00
2015-08-26 18:02:36 +00:00
weechat_hook_completion_list_add(completion, hex_id, 0,
WEECHAT_LIST_POS_SORT);
}
2014-10-04 22:24:52 +00:00
}
if (flags & TWC_COMPLETE_FRIEND_NAME)
{
char *name = twc_get_name_nt(profile->tox, friend_numbers[i]);
// add quotes if needed
if (strchr(name, ' '))
{
size_t length = strlen(name) + 3;
char *quoted_name = malloc(length);
snprintf(quoted_name, length, "\"%s\"", name);
free(name);
name = quoted_name;
}
2014-11-22 21:31:32 +00:00
weechat_hook_completion_list_add(completion, name, 0,
2014-10-04 22:24:52 +00:00
WEECHAT_LIST_POS_SORT);
free(name);
2014-10-04 22:24:52 +00:00
}
}
return WEECHAT_RC_OK;
}
2014-09-28 01:29:34 +00:00
/**
* Complete a profile name, possibly filtering by loaded/unloaded profiles.
*/
int
twc_completion_profile(const void *pointer, void *data,
2014-09-28 01:29:34 +00:00
const char *completion_item,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
int flag = (int)(intptr_t)pointer;
2014-09-28 01:29:34 +00:00
size_t index;
struct t_twc_list_item *item;
twc_list_foreach(twc_profiles, index, item)
{
if (flag == TWC_ALL_PROFILES
|| (flag == TWC_LOADED_PROFILES && item->profile->tox != NULL)
|| (flag == TWC_UNLOADED_PROFILES && item->profile->tox == NULL))
{
weechat_hook_completion_list_add(completion,
item->profile->name,
2014-11-22 21:31:32 +00:00
0, WEECHAT_LIST_POS_SORT);
2014-09-28 01:29:34 +00:00
}
}
return WEECHAT_RC_OK;
}
void
twc_completion_init()
{
2014-11-19 06:12:41 +00:00
weechat_hook_completion("tox_profiles", "profile",
2014-09-28 01:29:34 +00:00
twc_completion_profile,
(void *)(intptr_t)TWC_ALL_PROFILES, NULL);
2014-11-19 06:12:41 +00:00
weechat_hook_completion("tox_loaded_profiles", "loaded profile",
2014-09-28 01:29:34 +00:00
twc_completion_profile,
(void *)(intptr_t)TWC_LOADED_PROFILES, NULL);
2014-11-19 06:12:41 +00:00
weechat_hook_completion("tox_unloaded_profiles", "unloaded profile",
2014-09-28 01:29:34 +00:00
twc_completion_profile,
(void *)(intptr_t)TWC_UNLOADED_PROFILES, NULL);
2014-11-19 06:12:41 +00:00
weechat_hook_completion("tox_friend_tox_id", "friend Tox ID",
2014-10-04 22:24:52 +00:00
twc_completion_friend,
(void *)(intptr_t)TWC_COMPLETE_FRIEND_ID, NULL);
2014-11-19 06:12:41 +00:00
weechat_hook_completion("tox_friend_name", "friend name",
2014-10-04 22:24:52 +00:00
twc_completion_friend,
(void *)(intptr_t)TWC_COMPLETE_FRIEND_NAME, NULL);
2014-09-28 01:29:34 +00:00
}