Update xmpp_bot.c
This commit is contained in:
parent
b4230bce78
commit
9a29af1f18
1 changed files with 71 additions and 13 deletions
84
xmpp_bot.c
84
xmpp_bot.c
|
@ -2,6 +2,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define SERVER "whatevermaybe.net"
|
||||
#define ROOM_JID "archlinux@chat.hax.al"
|
||||
|
@ -28,8 +29,29 @@ 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);
|
||||
void send_room_message(xmpp_conn_t * const conn, xmpp_ctx_t *ctx, const char *message);
|
||||
int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata);
|
||||
|
||||
/* Logging function with timestamp */
|
||||
void log_with_timestamp(const char *format, ...) {
|
||||
time_t now;
|
||||
struct tm *tm_info;
|
||||
char timestamp[26];
|
||||
va_list args;
|
||||
|
||||
time(&now);
|
||||
tm_info = localtime(&now);
|
||||
strftime(timestamp, 26, "%Y-%m-%d %H:%M:%S", tm_info);
|
||||
|
||||
fprintf(stderr, "[%s] ", timestamp);
|
||||
|
||||
va_start(args, format);
|
||||
vfprintf(stderr, format, args);
|
||||
va_end(args);
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
/* User list functions */
|
||||
void userlist_add(UserList *list, const char *jid) {
|
||||
if (list->count >= list->size) {
|
||||
|
@ -63,6 +85,31 @@ void send_welcome_message(xmpp_conn_t * const conn, xmpp_ctx_t *ctx, const char
|
|||
send_whisper(conn, ctx, to_jid, formatted_msg);
|
||||
}
|
||||
|
||||
/* Send a message to the room */
|
||||
void send_room_message(xmpp_conn_t * const conn, xmpp_ctx_t *ctx, const char *message) {
|
||||
xmpp_stanza_t *msg = xmpp_stanza_new(ctx);
|
||||
xmpp_stanza_set_name(msg, "message");
|
||||
xmpp_stanza_set_type(msg, "groupchat");
|
||||
xmpp_stanza_set_attribute(msg, "to", ROOM_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);
|
||||
|
||||
log_with_timestamp("Sent room message: %s", message);
|
||||
}
|
||||
|
||||
/* 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);
|
||||
|
@ -82,12 +129,12 @@ void send_whisper(xmpp_conn_t * const conn, xmpp_ctx_t *ctx, const char *to_jid,
|
|||
xmpp_send(conn, msg);
|
||||
xmpp_stanza_release(msg);
|
||||
|
||||
fprintf(stderr, "Sent whisper to %s: %s\n", to_jid, message);
|
||||
log_with_timestamp("Sent whisper to %s: %s", to_jid, message);
|
||||
}
|
||||
|
||||
/* Check if user is admin or owner */
|
||||
int is_admin_or_owner(const char *affiliation) {
|
||||
return (strcmp(affiliation, "owner") == 0 || strcmp(affiliation, "admin") == 0);
|
||||
return (strcmp(affiliation, "Owners") == 0 || strcmp(affiliation, "Administrators") == 0);
|
||||
}
|
||||
|
||||
/* Handle presence stanzas */
|
||||
|
@ -128,13 +175,24 @@ int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void
|
|||
if (item) {
|
||||
const char *affiliation = xmpp_stanza_get_attribute(item, "affiliation");
|
||||
if (is_admin_or_owner(affiliation)) {
|
||||
const char *nickname = get_nickname(from);
|
||||
char *new_msg = message + strlen(CMD_PREFIX) + 1;
|
||||
char old_msg[512];
|
||||
strncpy(old_msg, welcome_message, sizeof(old_msg) - 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);
|
||||
/* Announce the change to the room */
|
||||
char announcement[1024];
|
||||
snprintf(announcement, sizeof(announcement),
|
||||
"Welcome message changed by %s\nOld: %s\nNew: %s",
|
||||
nickname, old_msg, welcome_message);
|
||||
send_room_message(conn, ctx, announcement);
|
||||
|
||||
/* Log the change */
|
||||
log_with_timestamp("Welcome message changed by %s from '%s' to '%s'",
|
||||
nickname, old_msg, welcome_message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -152,7 +210,7 @@ void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
|
|||
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
|
||||
|
||||
if (status == XMPP_CONN_CONNECT) {
|
||||
fprintf(stderr, "Connected to server %s\n", SERVER);
|
||||
log_with_timestamp("Connected to server %s", SERVER);
|
||||
|
||||
xmpp_handler_add(conn, presence_handler, NULL, "presence", NULL, ctx);
|
||||
xmpp_handler_add(conn, message_handler, NULL, "message", NULL, ctx);
|
||||
|
@ -173,17 +231,17 @@ void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
|
|||
xmpp_send(conn, presence);
|
||||
xmpp_stanza_release(presence);
|
||||
|
||||
fprintf(stderr, "Sent presence to join room: %s\n", to_buffer);
|
||||
log_with_timestamp("Sent presence to join room: %s", to_buffer);
|
||||
bot_joined = 1;
|
||||
|
||||
} else {
|
||||
if (error) {
|
||||
fprintf(stderr, "Connection error: %d\n", error);
|
||||
log_with_timestamp("Connection error: %d", error);
|
||||
if (stream_error) {
|
||||
fprintf(stderr, "Stream error type: %d\n", stream_error->type);
|
||||
log_with_timestamp("Stream error type: %d", stream_error->type);
|
||||
}
|
||||
}
|
||||
fprintf(stderr, "Disconnected from server\n");
|
||||
log_with_timestamp("Disconnected from server");
|
||||
xmpp_stop(ctx);
|
||||
}
|
||||
}
|
||||
|
@ -211,13 +269,13 @@ int main(void) {
|
|||
xmpp_conn_set_pass(conn, BOT_PASSWORD);
|
||||
xmpp_conn_set_flags(conn, XMPP_CONN_FLAG_MANDATORY_TLS);
|
||||
|
||||
fprintf(stderr, "Connecting to %s...\n", SERVER);
|
||||
log_with_timestamp("Connecting to %s...", SERVER);
|
||||
|
||||
if (xmpp_connect_client(conn, SERVER, 0, conn_handler, ctx) == XMPP_EOK) {
|
||||
fprintf(stderr, "Running connection...\n");
|
||||
log_with_timestamp("Running connection...");
|
||||
xmpp_run(ctx);
|
||||
} else {
|
||||
fprintf(stderr, "Failed to start connection\n");
|
||||
log_with_timestamp("Failed to start connection");
|
||||
}
|
||||
|
||||
xmpp_conn_release(conn);
|
||||
|
|
Loading…
Reference in a new issue