Files
stabber/src/server/prime.c
jabber.developer2 fdb615a213 feat: Add stbbr_for_presence_to for MUC join presence matching
- Add prime_for_presence_to() and prime_get_for_presence_to() in prime.c
- Add presence_to_callback() in server.c to handle MUC join presence
- Export stbbr_for_presence_to() in stabber.h
- Add stanzas_debug_last() for debugging failed verifications
2025-12-06 15:06:02 +03:00

144 lines
3.3 KiB
C

/*
* prime.c
*
* Copyright (C) 2015 James Booth <boothj5@gmail.com>
*
* This file is part of Stabber.
*
* Stabber 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.
*
* Stabber 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 Stabber. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
#include <string.h>
#include "server/stanza.h"
#include "server/stanzas.h"
#include "server/log.h"
static char *required_passwd = NULL;
static GHashTable *idstubs = NULL;
static GHashTable *querystubs = NULL;
static GHashTable *presence_to_stubs = NULL;
void
prime_init(void)
{
required_passwd = strdup("password");
idstubs = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
querystubs = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)stanza_free);
presence_to_stubs = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)stanza_free);
}
void
prime_free_all(void)
{
free(required_passwd);
required_passwd = NULL;
if (idstubs) {
g_hash_table_destroy(idstubs);
}
idstubs = NULL;
if (querystubs) {
g_hash_table_destroy(querystubs);
}
querystubs = NULL;
if (presence_to_stubs) {
g_hash_table_destroy(presence_to_stubs);
}
presence_to_stubs = NULL;
}
void
prime_required_passwd(char *password)
{
log_println(STBBR_LOGDEBUG, "Received auth password: %s", password);
free(required_passwd);
required_passwd = strdup(password);
}
char*
prime_get_passwd(void)
{
return required_passwd;
}
void
prime_for_id(const char *id, char *stream)
{
if (!idstubs) {
return;
}
log_println(STBBR_LOGDEBUG, "Received stub for id: %s, stanza: %s", id, stream);
g_hash_table_insert(idstubs, strdup(id), strdup(stream));
}
char*
prime_get_for_id(const char *id)
{
return g_hash_table_lookup(idstubs, id);
}
void
prime_for_query(const char *query, char *stream)
{
if (!querystubs) {
return;
}
log_println(STBBR_LOGDEBUG, "Received stub for query: %s, stanza: %s", query, stream);
XMPPStanza *stanza = stanza_parse(stream);
g_hash_table_insert(querystubs, strdup(query), stanza);
}
XMPPStanza*
prime_get_for_query(const char *query)
{
if (!querystubs) {
return NULL;
}
return g_hash_table_lookup(querystubs, query);
}
void
prime_for_presence_to(const char *to_jid, char *stream)
{
if (!presence_to_stubs) {
return;
}
log_println(STBBR_LOGDEBUG, "Received stub for presence to: %s, stanza: %s", to_jid, stream);
XMPPStanza *stanza = stanza_parse(stream);
g_hash_table_insert(presence_to_stubs, strdup(to_jid), stanza);
}
XMPPStanza*
prime_get_for_presence_to(const char *to_jid)
{
if (!presence_to_stubs) {
return NULL;
}
return g_hash_table_lookup(presence_to_stubs, to_jid);
}