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.

186 lines
4.6 KiB
C++

4 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/.
#include <stdint.h>
4 years ago
#include <string.h>
4 years ago
#include <strophe.h>
4 years ago
#include <weechat/weechat-plugin.h>
3 years ago
#include "plugin.hh"
#include "account.hh"
#include "channel.hh"
3 years ago
#include "buffer.hh"
4 years ago
void buffer__get_account_and_channel(struct t_gui_buffer *buffer,
weechat::account **account,
weechat::channel **channel)
4 years ago
{
if (!buffer)
return;
*account = nullptr;
*channel = nullptr;
4 years ago
4 years ago
/* look for a account or channel using this buffer */
for (auto& ptr_account : weechat::accounts)
4 years ago
{
if (ptr_account.second.buffer == buffer)
4 years ago
{
if (account)
*account = &ptr_account.second;
4 years ago
return;
}
for (auto& ptr_channel : ptr_account.second.channels)
4 years ago
{
if (ptr_channel.second.buffer == buffer)
4 years ago
{
if (account)
*account = &ptr_account.second;
4 years ago
if (channel)
*channel = &ptr_channel.second;
4 years ago
return;
}
}
}
}
4 years ago
char *buffer__typing_bar_cb(const void *pointer, void *data,
struct t_gui_bar_item *item,
struct t_gui_window *window,
struct t_gui_buffer *buffer,
struct t_hashtable *extra_info)
4 years ago
{
weechat::account *account;
weechat::channel *channel;
4 years ago
char notification[256];
unsigned typecount;
(void) pointer;
(void) data;
(void) item;
(void) window;
(void) extra_info;
account = NULL;
channel = NULL;
buffer__get_account_and_channel(buffer, &account, &channel);
if (!channel)
4 years ago
return strndup("", 0);
4 years ago
typecount = 0;
for (auto& ptr_typing : channel->typings)
4 years ago
{
switch (++typecount)
{
case 1:
strcpy(notification, ptr_typing.name);
4 years ago
break;
case 2:
strcat(notification, ", ");
strcat(notification, ptr_typing.name);
4 years ago
break;
case 3:
default:
strcpy(notification, "Several people");
break;
}
}
if (typecount)
{
strcat(notification, NG_(" is typing...",
" are typing...",
typecount));
return strdup(notification);
}
else
{
4 years ago
return strndup("", 0);
4 years ago
}
}
int buffer__nickcmp_cb(const void *pointer, void *data,
4 years ago
struct t_gui_buffer *buffer,
const char *nick1,
const char *nick2)
4 years ago
{
weechat::account *account;
4 years ago
(void) data;
if (pointer)
account = (weechat::account *)pointer;
4 years ago
else
buffer__get_account_and_channel(buffer, &account, NULL);
if (account)
{
return weechat_strcasecmp(nick1, nick2);
}
else
{
return weechat_strcasecmp(nick1, nick2);
}
}
int buffer__close_cb(const void *pointer, void *data,
4 years ago
struct t_gui_buffer *buffer)
4 years ago
{
struct t_weechat_plugin *buffer_plugin = NULL;
weechat::account *ptr_account = NULL;
weechat::channel *ptr_channel = NULL;
4 years ago
(void) pointer;
(void) data;
3 years ago
buffer_plugin = (struct t_weechat_plugin*)weechat_buffer_get_pointer(buffer, "plugin");
4 years ago
if (buffer_plugin != weechat_plugin)
return WEECHAT_RC_OK;
buffer__get_account_and_channel(buffer, &ptr_account, &ptr_channel);
const char* type = weechat_buffer_get_string(buffer, "localvar_type");
if (weechat_strcasecmp(type, "server") == 0)
4 years ago
{
4 years ago
if (ptr_account)
4 years ago
{
if (ptr_account->connected())
4 years ago
{
ptr_account->disconnect(0);
4 years ago
}
4 years ago
4 years ago
ptr_account->buffer = NULL;
}
}
else if (weechat_strcasecmp(type, "channel") == 0)
{
if (ptr_account && ptr_channel)
{
if (ptr_account->connected())
4 years ago
{
ptr_account->channels.erase(ptr_channel->name);
4 years ago
}
}
}
else if (weechat_strcasecmp(type, "private") == 0)
{
if (ptr_account && ptr_channel)
{
if (ptr_account->connected())
4 years ago
{
ptr_account->channels.erase(ptr_channel->name);
4 years ago
}
}
}
else
{
4 years ago
}
return WEECHAT_RC_OK;
}