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.
 
 
 
 

122 lines
3.8 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_codeberg.h"
#define MOD_NAME "mod_codeberg"
#define MOD_VERSION "0.0.0-dev"
#define LOG_DOMAIN "vulpes-mod-codeberg"
#define MOD_HELP_PREFIX "!help"
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#define TCP_IP_PORT 7000
#define HELP_TXT "Module " MOD_NAME " - " MOD_VERSION "\n\
Module für Codeberg\n"
static void _show_help(vulpes_ctx_t *vulpes_ctx, const lutjanus_xmpp_adr_t* from, const char *const message);
static void start_server(vulpes_ctx_t *vulpes_ctx);
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 );
start_server(vulpes_ctx);
return VULPES_SUCCESS;
}
vulpes_err_code_t module_unload(vulpes_ctx_t *vulpes_ctx) {
g_log(LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Unloading %s (%s)...", MOD_NAME, MOD_VERSION );
return VULPES_SUCCESS;
}
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);
}
static void start_server(vulpes_ctx_t *vulpes_ctx) {
g_log(LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Starte Codeberg Server", MOD_NAME, MOD_VERSION );
pid_t pid = fork();
if( pid == 0 ) {
int fd_socket = socket(AF_INET, SOCK_STREAM, 0);
if ( fd_socket == -1 ) {
perror("Socket");
g_log(LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Socket: %s", strerror(fd_socket));
return;
}
struct sockaddr_in serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(TCP_IP_PORT);
if (bind(fd_socket, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) == -1) {
g_log(LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Bind:" );
return;
}
if ( listen(fd_socket,3) == -1 ) {
g_log(LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Listen: ");
}
while ( TRUE ) {
g_log(LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Start accept...");
struct sockaddr_in cli_addr;
socklen_t client_len = sizeof(cli_addr);
int clientfd = accept(fd_socket, (struct sockaddr *) &cli_addr, &client_len);
if ( clientfd == -1 ) {
g_log(LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Accept:");
return;
}
pid_t pid = fork();
if( pid == 0 ) {
g_log(LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Start Client process");
char buffer[1024];
bzero(buffer,1024);
int n;
n = read(clientfd,buffer,1024-1);
if ( n < 0 ) {
g_log(LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Read:");
return;
}
//lutjanus_xmpp_adr_t* operator_muc = mod_api_get_operator_muc(vulpes_ctx);
//mod_api_send_groupchat_message(vulpes_ctx, operator_muc, buffer);
char* response = "HTTP/1.1 204 OK\n\n";
write(clientfd, response, strlen(response));
close(clientfd);
g_log(LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Client process closed");
}
}
}
}