git-svn-id: file:///raid/svn-personal/okuu/trunk@2 7d206d2a-66c2-044b-96de-ba755a9b3ba8
This commit is contained in:
Nishi 2024-09-09 13:22:05 +00:00
parent 366736259f
commit dd30fa1766
6 changed files with 267 additions and 0 deletions

19
Bot/Makefile Normal file
View File

@ -0,0 +1,19 @@
# $Id$
include $(PWD)/Platform/$(PLATFORM).mk
.PHONY: all clean
.SUFFIXES: .c .o
OBJS = main.o ircfw.o
all: okuu$(EXEC)
okuu$(EXEC): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
.c.o:
$(CC) $(CFLAGS) -c -o $@ $<
clean:
rm -f *.o *.exe okuu

165
Bot/ircfw.c Normal file
View File

@ -0,0 +1,165 @@
/* $Id$ */
#define IRCFW_SRC
#include "ircfw.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
struct ircfw_message ircfw_message;
char* ircfw_strcat(const char* a, const char* b) {
char* str = malloc(strlen(a) + strlen(b) + 1);
memcpy(str, a, strlen(a));
memcpy(str + strlen(a), b, strlen(b));
str[strlen(a) + strlen(b)] = 0;
return str;
}
char* ircfw_strcat3(const char* a, const char* b, const char* c) {
char* tmp = ircfw_strcat(a, b);
char* str = ircfw_strcat(tmp, c);
free(tmp);
return str;
}
char* ircfw_strdup(const char* str) { return ircfw_strcat(str, ""); }
void ircfw_init(void) {
ircfw_message.prefix = NULL;
ircfw_message.params = NULL;
ircfw_message.command = NULL;
}
void ircfw_parse_params(const char* str) {
ircfw_message.params = malloc(sizeof(*ircfw_message.params));
ircfw_message.params[0] = NULL;
int i;
int incr = 0;
bool until_end = false;
char* dup = ircfw_strdup(str);
for(i = 0;; i++) {
if(dup[i] == 0 || (!until_end && dup[i] == ' ')) {
char oldc = dup[i];
dup[i] = 0;
char* param = ircfw_strdup(dup + incr);
char** old_params = ircfw_message.params;
int j;
for(j = 0; old_params[j] != NULL; j++)
;
ircfw_message.params = malloc(sizeof(*ircfw_message.params) * (2 + j));
for(j = 0; old_params[j] != NULL; j++) {
ircfw_message.params[j] = old_params[j];
}
ircfw_message.params[j] = param;
ircfw_message.params[j + 1] = NULL;
free(old_params);
incr = i + 1;
if(oldc == 0) break;
} else if(dup[i] == ':' && !until_end) {
until_end = true;
incr = i + 1;
}
}
free(dup);
}
int ircfw_socket_send_cmd(int sock, const char* name, const char* cmd) {
char* str = ircfw_strcat(cmd, "\r\n");
if(name != NULL) {
char* old = str;
char* tmp = ircfw_strcat3(":", name, " ");
str = ircfw_strcat(tmp, old);
free(old);
free(tmp);
}
int st = send(sock, str, strlen(str), 0);
free(str);
return st < 0 ? 1 : 0;
}
int ircfw_socket_read_cmd(int sock) {
char c[2];
c[1] = 0;
char* str = malloc(1);
str[0] = 0;
bool err = false;
bool end = false;
while(1) {
int s = recv(sock, c, 1, 0);
if(s <= 0) {
err = true;
break;
}
if(c[0] == '\n') {
end = true;
break;
} else if(c[0] != '\r') {
char* tmp = str;
str = ircfw_strcat(tmp, c);
free(tmp);
}
}
if(ircfw_message.prefix != NULL) free(ircfw_message.prefix);
if(ircfw_message.params != NULL) {
int i;
for(i = 0; ircfw_message.params[i] != NULL; i++) {
free(ircfw_message.params[i]);
}
free(ircfw_message.params);
}
if(ircfw_message.command != NULL) free(ircfw_message.command);
ircfw_message.prefix = NULL;
ircfw_message.params = NULL;
ircfw_message.command = NULL;
if(str[0] == ':') {
int i;
for(i = 0; str[i] != 0; i++) {
if(str[i] == ' ') {
str[i] = 0;
ircfw_message.prefix = ircfw_strdup(str + 1);
i++;
int start = i;
for(;; i++) {
if(str[i] == ' ' || str[i] == 0) {
char oldc = str[i];
str[i] = 0;
ircfw_message.command = ircfw_strdup(str + start);
if(oldc != 0) {
i++;
ircfw_parse_params(str + i);
}
break;
}
}
break;
}
}
} else {
int i;
for(i = 0; str[i] != 0; i++) {
if(str[i] == ' ' || str[i] == 0) {
char oldc = str[i];
str[i] = 0;
ircfw_message.command = ircfw_strdup(str);
if(oldc != 0) {
i++;
ircfw_parse_params(str + i);
}
break;
}
}
}
free(str);
return err ? 1 : 0;
}

22
Bot/ircfw.h Normal file
View File

@ -0,0 +1,22 @@
/* $Id$ */
#ifndef __IRCFW_H__
#define __IRCFW_H__
#define IRCFW_VERSION "1.00"
struct ircfw_message {
char* prefix;
char* command;
char** params;
};
void ircfw_init(void);
int ircfw_socket_send_cmd(int sock, const char* name, const char* cmd);
int ircfw_socket_read_cmd(int sock);
#ifndef IRCFW_SRC
extern struct ircfw_message ircfw_message;
#endif
#endif

40
Bot/main.c Normal file
View File

@ -0,0 +1,40 @@
/* $Id$ */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
char* nntpserver;
char* nntpuser;
char* nntppass;
int nntpport = 119;
char* ircserver;
int ircport = 6669;
int main(){
printf("Okuu starting up\n");
nntpserver = getenv("NNTPSERVER");
nntpuser = getenv("NNTPUSER");
nntppass = getenv("NNTPPASS");
ircserver = getenv("IRCSERVER");
if(getenv("NNTPPORT") != NULL){
nntpport = atoi(getenv("NNTPPORT"));
}
if(getenv("IRCPORT") != NULL){
ircport = atoi(getenv("IRCPORT"));
}
bool bad = false;
if(nntpserver == NULL){
fprintf(stderr, "Set NNTPSERVER\n");
bad = true;
}
if(ircserver == NULL){
fprintf(stderr, "Set IRCSERVER\n");
bad = true;
}
if(bad){
return 1;
}
}

14
Makefile Normal file
View File

@ -0,0 +1,14 @@
# $Id$
PLATFORM = generic
PWD = `pwd`
FLAGS = PWD=$(PWD) PLATFORM=$(PLATFORM)
.PHONY: all clean ./Bot
./Bot::
$(MAKE) -C $@ $(FLAGS)
clean:
$(MAKE) -C ./Bot $(FLAGS) clean

7
Platform/generic.mk Normal file
View File

@ -0,0 +1,7 @@
# $Id$
CC = cc
CFLAGS = -g -std=c99
LDFLAGS =
LIBS =
EXEC =