Vulpes - XMPP Bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

93 lines
3.1 KiB

/*
* vim: expandtab:ts=2:sts=2:sw=2
*
* Copyright (C) 2021 DebXWoody <stefan@devlug.de>
*
* This file is part of vulpes.
*
* vulpes 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.
*
* vulpes 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 buteo. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mod_echo.h"
#define MOD_NAME "mod_echo"
#define MOD_VERSION "0.0.0-dev"
#define LOG_DOMAIN "vulpes-mod-echo"
#define MOD_HELP_PREFIX "!help"
#define HELP_TXT "Module " MOD_NAME " - " MOD_VERSION "\n\
Der Bot wiederholt was man sagt\n"
static void _show_help(vulpes_ctx_t *vulpes_ctx, const lutjanus_xmpp_adr_t* from, const char *const message);
static GString *_getDateAndTime();
vulpes_err_code_t module_load(vulpes_ctx_t *vulpes_ctx) {
g_log(LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Loading %s (%s)...", MOD_NAME, MOD_VERSION );
return VULPES_SUCCESS;
}
vulpes_err_code_t module_unload(vulpes_ctx_t *vulpes_ctx) {
return VULPES_SUCCESS;
}
vulpes_err_code_t module_tricker(vulpes_ctx_t *vulpes_ctx) {
GString* now = _getDateAndTime();
lutjanus_xmpp_adr_t* operator_muc = mod_api_get_operator_muc(vulpes_ctx);
mod_api_send_groupchat_message(vulpes_ctx, operator_muc, now->str);
}
vulpes_err_code_t chat(vulpes_ctx_t *vulpes_ctx, const lutjanus_xmpp_adr_t* from, const char *const message) {
g_log(LOG_DOMAIN, G_LOG_LEVEL_INFO, "Incoming messsage from %s: %s", from, message);
if ( g_str_has_prefix (message, MOD_HELP_PREFIX ) ) {
_show_help(vulpes_ctx, from,message);
return VULPES_SUCCESS;
}
if ( g_str_has_prefix (message, "!" ) ) {
return VULPES_SUCCESS;
}
GString* echo = g_string_new("Echo bot: ");
g_string_append(echo, message);
return mod_api_send_chat_message(vulpes_ctx, from, echo->str);
}
vulpes_err_code_t groupchat_command(vulpes_ctx_t *vulpes_ctx, const lutjanus_xmpp_adr_t* from, const char *const message) {
g_log(LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Incoming groupchat command %s: %s", lutjanus_xmpp_adr_get_full(from)->str, message);
if ( !g_str_has_prefix (message, "!echo" ) ) {
return VULPES_SUCCESS;
}
GString* echo = g_string_new("Echo bot: ");
g_string_append(echo, message);
GString* to = lutjanus_xmpp_adr_get_bare(from);
return mod_api_send_groupchat_message(vulpes_ctx, lutjanus_xmpp_adr_new_parse(to), echo->str);
}
static void _show_help(vulpes_ctx_t *vulpes_ctx, const lutjanus_xmpp_adr_t* from, const char *const message) {
mod_api_send_chat_message(vulpes_ctx, from, HELP_TXT);
}
GString *_getDateAndTime() {
GDateTime *now = g_date_time_new_now_local();
char *date = g_date_time_format(now, "%A, %d. %B %Y %R");
GString *result = g_string_new(date);
g_log(LOG_DOMAIN, G_LOG_LEVEL_INFO, "Now: %s ", result->str);
return result;
}