erdhe/xmpp_bot.c

234 lines
7.9 KiB
C
Raw Normal View History

2024-10-06 18:06:38 +02:00
#include <strophe.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SERVER "whatevermaybe.net"
#define ROOM_JID "archlinux@chat.hax.al"
#define BOT_NICKNAME "erdhe"
#define BOT_JID "bot@hax.al"
#define BOT_PASSWORD "passwordfield"
2024-10-06 19:06:09 +02:00
#define DEFAULT_WELCOME_MSG "Welcome to the room, %s!"
#define CMD_PREFIX ".erdha wm"
2024-10-06 18:06:38 +02:00
/* Add UNUSED macro to suppress warnings */
#define UNUSED(x) (void)(x)
/* List to track users who have already received the whisper */
typedef struct UserList {
char **users;
int count;
int size;
} UserList;
2024-10-06 19:06:09 +02:00
/* Global variables */
2024-10-06 18:06:38 +02:00
UserList userlist;
int bot_joined = 0;
2024-10-06 19:06:09 +02:00
char welcome_message[512];
/* Function prototypes */
void send_whisper(xmpp_conn_t * const conn, xmpp_ctx_t *ctx, const char *to_jid, const char *message);
int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata);
2024-10-06 18:06:38 +02:00
2024-10-06 19:06:09 +02:00
/* User list functions */
2024-10-06 18:06:38 +02:00
void userlist_add(UserList *list, const char *jid) {
if (list->count >= list->size) {
list->size *= 2;
list->users = realloc(list->users, list->size * sizeof(char *));
}
list->users[list->count] = strdup(jid);
list->count++;
}
int userlist_contains(UserList *list, const char *jid) {
for (int i = 0; i < list->count; i++) {
if (strcmp(list->users[i], jid) == 0) {
return 1;
}
}
return 0;
}
2024-10-06 19:06:09 +02:00
/* Extract nickname from full JID */
const char *get_nickname(const char *full_jid) {
const char *slash = strrchr(full_jid, '/');
return slash ? slash + 1 : full_jid;
}
/* Send a formatted welcome message */
void send_welcome_message(xmpp_conn_t * const conn, xmpp_ctx_t *ctx, const char *to_jid) {
const char *nickname = get_nickname(to_jid);
char formatted_msg[1024];
snprintf(formatted_msg, sizeof(formatted_msg), welcome_message, nickname);
send_whisper(conn, ctx, to_jid, formatted_msg);
}
2024-10-06 18:06:38 +02:00
/* Send a whisper message */
void send_whisper(xmpp_conn_t * const conn, xmpp_ctx_t *ctx, const char *to_jid, const char *message) {
xmpp_stanza_t *msg = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(msg, "message");
xmpp_stanza_set_type(msg, "chat");
xmpp_stanza_set_attribute(msg, "to", to_jid);
xmpp_stanza_t *body = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(body, "body");
xmpp_stanza_t *text = xmpp_stanza_new(ctx);
xmpp_stanza_set_text(text, message);
xmpp_stanza_add_child(body, text);
xmpp_stanza_add_child(msg, body);
xmpp_stanza_release(text);
xmpp_stanza_release(body);
xmpp_send(conn, msg);
xmpp_stanza_release(msg);
fprintf(stderr, "Sent whisper to %s: %s\n", to_jid, message);
}
2024-10-06 19:06:09 +02:00
/* Check if user is admin or owner */
int is_admin_or_owner(const char *affiliation) {
return (strcmp(affiliation, "owner") == 0 || strcmp(affiliation, "admin") == 0);
}
2024-10-06 18:06:38 +02:00
/* Handle presence stanzas */
int presence_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) {
2024-10-06 19:06:09 +02:00
const char *from = xmpp_stanza_get_attribute(stanza, "from");
2024-10-06 18:06:38 +02:00
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
if (from && strstr(from, ROOM_JID)) {
const char *resource = strchr(from, '/');
2024-10-06 19:06:09 +02:00
if (resource && strcmp(resource + 1, BOT_NICKNAME) != 0) {
2024-10-06 18:06:38 +02:00
if (bot_joined && !userlist_contains(&userlist, from)) {
2024-10-06 19:06:09 +02:00
send_welcome_message(conn, ctx, from);
2024-10-06 18:06:38 +02:00
userlist_add(&userlist, from);
2024-10-06 19:06:09 +02:00
} else if (!bot_joined) {
2024-10-06 18:06:38 +02:00
userlist_add(&userlist, from);
}
}
}
return 1;
}
2024-10-06 19:06:09 +02:00
/* Handle message stanzas */
int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) {
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
const char *type = xmpp_stanza_get_type(stanza);
const char *from = xmpp_stanza_get_attribute(stanza, "from");
if (type && strcmp(type, "groupchat") == 0) {
xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, "body");
if (body) {
char *message = xmpp_stanza_get_text(body);
if (message && strncmp(message, CMD_PREFIX, strlen(CMD_PREFIX)) == 0) {
/* Check if sender is admin/owner */
xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(stanza, "http://jabber.org/protocol/muc#user");
if (x) {
xmpp_stanza_t *item = xmpp_stanza_get_child_by_name(x, "item");
if (item) {
const char *affiliation = xmpp_stanza_get_attribute(item, "affiliation");
if (is_admin_or_owner(affiliation)) {
char *new_msg = message + strlen(CMD_PREFIX) + 1;
strncpy(welcome_message, new_msg, sizeof(welcome_message) - 1);
welcome_message[sizeof(welcome_message) - 1] = '\0';
char response[1024];
snprintf(response, sizeof(response), "Welcome message updated to: %s", welcome_message);
send_whisper(conn, ctx, from, response);
}
}
}
}
if (message) xmpp_free(ctx, message);
}
}
return 1;
}
/* Connection handler */
2024-10-06 18:06:38 +02:00
void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
const int error, xmpp_stream_error_t * const stream_error,
void * const userdata) {
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
if (status == XMPP_CONN_CONNECT) {
fprintf(stderr, "Connected to server %s\n", SERVER);
xmpp_handler_add(conn, presence_handler, NULL, "presence", NULL, ctx);
2024-10-06 19:06:09 +02:00
xmpp_handler_add(conn, message_handler, NULL, "message", NULL, ctx);
2024-10-06 18:06:38 +02:00
xmpp_stanza_t *presence = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(presence, "presence");
char to_buffer[512];
snprintf(to_buffer, sizeof(to_buffer), "%s/%s", ROOM_JID, BOT_NICKNAME);
xmpp_stanza_set_attribute(presence, "to", to_buffer);
xmpp_stanza_t *x = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(x, "x");
xmpp_stanza_set_ns(x, "http://jabber.org/protocol/muc");
xmpp_stanza_add_child(presence, x);
xmpp_stanza_release(x);
xmpp_send(conn, presence);
xmpp_stanza_release(presence);
fprintf(stderr, "Sent presence to join room: %s\n", to_buffer);
bot_joined = 1;
} else {
if (error) {
fprintf(stderr, "Connection error: %d\n", error);
if (stream_error) {
fprintf(stderr, "Stream error type: %d\n", stream_error->type);
}
}
fprintf(stderr, "Disconnected from server\n");
xmpp_stop(ctx);
}
}
/* Main function */
int main(void) {
xmpp_ctx_t *ctx;
xmpp_conn_t *conn;
xmpp_log_t *log;
2024-10-06 19:06:09 +02:00
/* Initialize userlist and welcome message */
userlist.size = 10;
2024-10-06 18:06:38 +02:00
userlist.count = 0;
userlist.users = malloc(userlist.size * sizeof(char *));
2024-10-06 19:06:09 +02:00
strncpy(welcome_message, DEFAULT_WELCOME_MSG, sizeof(welcome_message) - 1);
welcome_message[sizeof(welcome_message) - 1] = '\0';
2024-10-06 18:06:38 +02:00
xmpp_initialize();
2024-10-06 19:06:09 +02:00
log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
2024-10-06 18:06:38 +02:00
ctx = xmpp_ctx_new(NULL, log);
conn = xmpp_conn_new(ctx);
xmpp_conn_set_jid(conn, BOT_JID);
xmpp_conn_set_pass(conn, BOT_PASSWORD);
xmpp_conn_set_flags(conn, XMPP_CONN_FLAG_MANDATORY_TLS);
fprintf(stderr, "Connecting to %s...\n", SERVER);
if (xmpp_connect_client(conn, SERVER, 0, conn_handler, ctx) == XMPP_EOK) {
fprintf(stderr, "Running connection...\n");
xmpp_run(ctx);
} else {
fprintf(stderr, "Failed to start connection\n");
}
xmpp_conn_release(conn);
xmpp_ctx_free(ctx);
xmpp_shutdown();
/* Free the userlist */
for (int i = 0; i < userlist.count; i++) {
free(userlist.users[i]);
}
free(userlist.users);
return 0;
2024-10-06 19:06:09 +02:00
}