git-svn-id: file:///raid/svn-personal/okuu/trunk@11 7d206d2a-66c2-044b-96de-ba755a9b3ba8
This commit is contained in:
Nishi 2024-09-11 10:24:20 +00:00
parent 376c2bbac6
commit b39a1d1f9d
6 changed files with 152 additions and 136 deletions

15
.clang-format Normal file
View File

@ -0,0 +1,15 @@
---
# $Id$
Language: Cpp
UseTab: Always
TabWidth: 8
IndentWidth: 8
PointerAlignment: Left
ColumnLimit: 1024
AllowShortIfStatementsOnASingleLine: Always
AllowShortBlocksOnASingleLine: Never
AllowShortLoopsOnASingleLine: true
SpaceBeforeParens: Never
AlignEscapedNewlines: DontAlign
SortIncludes: false
AllowShortEnumsOnASingleLine: false

View File

@ -42,11 +42,9 @@ extern int ircport;
int ok_sock; int ok_sock;
struct sockaddr_in ok_addr; struct sockaddr_in ok_addr;
void ok_close(int sock){ void ok_close(int sock) { close(sock); }
close(sock);
}
void ok_bot_kill(int sig){ void ok_bot_kill(int sig) {
fprintf(stderr, "Shutdown\n"); fprintf(stderr, "Shutdown\n");
ircfw_socket_send_cmd(ok_sock, NULL, "QUIT :Shutdown (Signal)"); ircfw_socket_send_cmd(ok_sock, NULL, "QUIT :Shutdown (Signal)");
exit(1); exit(1);
@ -60,22 +58,18 @@ bool ok_is_number(const char* str) {
return true; return true;
} }
char* ok_null(const char* str){ char* ok_null(const char* str) { return str == NULL ? "(null)" : (char*)str; }
return str == NULL ? "(null)" : (char*)str;
}
int namesort(const struct dirent** a_, const struct dirent** b_){ int namesort(const struct dirent** a_, const struct dirent** b_) {
const struct dirent* a = *a_; const struct dirent* a = *a_;
const struct dirent* b = *b_; const struct dirent* b = *b_;
return atoi(a->d_name) - atoi(b->d_name); return atoi(a->d_name) - atoi(b->d_name);
} }
int nodots(const struct dirent* d){ int nodots(const struct dirent* d) { return (strcmp(d->d_name, "..") == 0 || strcmp(d->d_name, ".") == 0) ? 0 : 1; }
return (strcmp(d->d_name, "..") == 0 || strcmp(d->d_name, ".") == 0) ? 0 : 1;
}
void ok_bot(void){ void ok_bot(void) {
if((ok_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0){ if((ok_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "Socket creation failure\n"); fprintf(stderr, "Socket creation failure\n");
return; return;
} }
@ -92,8 +86,8 @@ void ok_bot(void){
ok_close(ok_sock); ok_close(ok_sock);
return; return;
} }
if(connect(ok_sock, (struct sockaddr*)&ok_addr, sizeof(ok_addr)) < 0){ if(connect(ok_sock, (struct sockaddr*)&ok_addr, sizeof(ok_addr)) < 0) {
fprintf(stderr, "Connection failure\n"); fprintf(stderr, "Connection failure\n");
ok_close(ok_sock); ok_close(ok_sock);
return; return;
@ -104,7 +98,7 @@ void ok_bot(void){
char* construct = malloc(1025); char* construct = malloc(1025);
if(ircpass != NULL && strlen(ircpass) > 0){ if(ircpass != NULL && strlen(ircpass) > 0) {
sprintf(construct, "PASS :%s", ircpass); sprintf(construct, "PASS :%s", ircpass);
ircfw_socket_send_cmd(ok_sock, NULL, construct); ircfw_socket_send_cmd(ok_sock, NULL, construct);
} }
@ -122,47 +116,49 @@ void ok_bot(void){
pollfds[0].fd = ok_sock; pollfds[0].fd = ok_sock;
pollfds[0].events = POLLIN | POLLPRI; pollfds[0].events = POLLIN | POLLPRI;
while(1){ while(1) {
int r = poll(pollfds, 1, 100); int r = poll(pollfds, 1, 100);
if(!(r > 0 && pollfds[0].revents & POLLIN)){ if(!(r > 0 && pollfds[0].revents & POLLIN)) {
/* 100ms sleep, technically */ /* 100ms sleep, technically */
uint64_t count = 0; uint64_t count = 0;
FILE* f = fopen(nntpcount, "rb"); FILE* f = fopen(nntpcount, "rb");
if(f != NULL){ if(f != NULL) {
fread(&count, sizeof(count), 1, f); fread(&count, sizeof(count), 1, f);
fclose(f); fclose(f);
} }
struct dirent** list; struct dirent** list;
int n = scandir(nntppath, &list, nodots, namesort); int n = scandir(nntppath, &list, nodots, namesort);
if(n >= 0){ if(n >= 0) {
int i; int i;
for(i = 0; i < n; i++){ for(i = 0; i < n; i++) {
if(!sendable){ if(!sendable) {
free(list[i]); free(list[i]);
continue; continue;
} }
if(count <= atoi(list[i]->d_name)){ if(count <= atoi(list[i]->d_name)) {
sprintf(construct, "%s/%s", nntppath, list[i]->d_name); sprintf(construct, "%s/%s", nntppath, list[i]->d_name);
if(ok_news_read(construct) == 0){ if(ok_news_read(construct) == 0) {
if(strcmp(news_entry.from, nntpfrom) != 0){ if(strcmp(news_entry.from, nntpfrom) != 0) {
char* tmp = ok_strcat3("PRIVMSG ", ircchan, " :\x03" "07[USENET] ~ "); char* tmp = ok_strcat3("PRIVMSG ", ircchan,
" :\x03"
"07[USENET] ~ ");
char* temp = ok_strcat3(tmp, news_entry.from, "\x03 "); char* temp = ok_strcat3(tmp, news_entry.from, "\x03 ");
free(tmp); free(tmp);
int j; int j;
int incr = 0; int incr = 0;
for(j = 0;; j++){ for(j = 0;; j++) {
if(news_entry.content[j] == 0 || news_entry.content[j] == '\n'){ if(news_entry.content[j] == 0 || news_entry.content[j] == '\n') {
char* line = malloc(j - incr + 1); char* line = malloc(j - incr + 1);
line[j - incr] = 0; line[j - incr] = 0;
memcpy(line, news_entry.content + incr, j - incr); memcpy(line, news_entry.content + incr, j - incr);
if(strlen(line) > 0){ if(strlen(line) > 0) {
char* msg = ok_strcat(temp, line); char* msg = ok_strcat(temp, line);
ircfw_socket_send_cmd(ok_sock, NULL, msg); ircfw_socket_send_cmd(ok_sock, NULL, msg);
free(msg); free(msg);
usleep(1000 * 100); /* Sleep for 100ms */ usleep(1000 * 100); /* Sleep for 100ms */
} }
free(line); free(line);
incr = j + 1; incr = j + 1;
if(news_entry.content[j] == 0) break; if(news_entry.content[j] == 0) break;
@ -170,7 +166,7 @@ void ok_bot(void){
} }
free(temp); free(temp);
} }
}else{ } else {
fprintf(stderr, "Could not read %s\n", construct); fprintf(stderr, "Could not read %s\n", construct);
} }
} }
@ -179,9 +175,9 @@ void ok_bot(void){
count = atoi(list[i - 1]->d_name) + 1; count = atoi(list[i - 1]->d_name) + 1;
free(list); free(list);
} }
if(sendable){ if(sendable) {
f = fopen(nntpcount, "wb"); f = fopen(nntpcount, "wb");
if(f != NULL){ if(f != NULL) {
fwrite(&count, sizeof(count), 1, f); fwrite(&count, sizeof(count), 1, f);
fclose(f); fclose(f);
} }
@ -189,40 +185,41 @@ void ok_bot(void){
continue; continue;
} }
int st = ircfw_socket_read_cmd(ok_sock); int st = ircfw_socket_read_cmd(ok_sock);
if(st != 0){ if(st != 0) {
fprintf(stderr, "Bad response\n"); fprintf(stderr, "Bad response\n");
return; return;
} }
if(strlen(ircfw_message.command) == 3 && ok_is_number(ircfw_message.command)){ if(strlen(ircfw_message.command) == 3 && ok_is_number(ircfw_message.command)) {
int res = atoi(ircfw_message.command); int res = atoi(ircfw_message.command);
if(!is_in && 400 <= res && res <= 599){ if(!is_in && 400 <= res && res <= 599) {
fprintf(stderr, "Bad response\n"); fprintf(stderr, "Bad response\n");
return; return;
}else if(400 <= res && res <= 599){ } else if(400 <= res && res <= 599) {
fprintf(stderr, "Ignored error: %d\n", res); fprintf(stderr, "Ignored error: %d\n", res);
continue; continue;
} }
if(res == 376){ if(res == 376) {
is_in = true; is_in = true;
fprintf(stderr, "Login successful\n"); fprintf(stderr, "Login successful\n");
sprintf(construct, "JOIN :%s", ircchan); sprintf(construct, "JOIN :%s", ircchan);
ircfw_socket_send_cmd(ok_sock, NULL, construct); ircfw_socket_send_cmd(ok_sock, NULL, construct);
}else if(res == 331 || res == 332){ } else if(res == 331 || res == 332) {
sendable = true; sendable = true;
} }
}else{ } else {
if(strcasecmp(ircfw_message.command, "PING") == 0){ if(strcasecmp(ircfw_message.command, "PING") == 0) {
fprintf(stderr, "Ping request\n"); fprintf(stderr, "Ping request\n");
sprintf(construct, "PONG :%s", ok_null(ircfw_message.prefix)); sprintf(construct, "PONG :%s", ok_null(ircfw_message.prefix));
ircfw_socket_send_cmd(ok_sock, NULL, construct); ircfw_socket_send_cmd(ok_sock, NULL, construct);
}else if(strcasecmp(ircfw_message.command, "PRIVMSG") == 0){ } else if(strcasecmp(ircfw_message.command, "PRIVMSG") == 0) {
char* prefix = ircfw_message.prefix; char* prefix = ircfw_message.prefix;
char** params = ircfw_message.params; char** params = ircfw_message.params;
int len = 0; int len = 0;
if(params != NULL) { if(params != NULL) {
int i; int i;
for(i = 0; params[i] != NULL; i++); for(i = 0; params[i] != NULL; i++)
;
len = i; len = i;
} }
if(prefix != NULL && len == 2) { if(prefix != NULL && len == 2) {
@ -236,15 +233,15 @@ void ok_bot(void){
break; break;
} }
} }
if(msg[0] == 1 && msg[strlen(msg) - 1] == 1){ if(msg[0] == 1 && msg[strlen(msg) - 1] == 1) {
/* CTCP */ /* CTCP */
if(strcasecmp(msg, "\x01VERSION\x01") == 0){ if(strcasecmp(msg, "\x01VERSION\x01") == 0) {
sprintf(construct, "NOTICE %s :\x01VERSION Okuu %s / IRC Frameworks %s: http://nishi.boats/okuu\x01", nick, OKUU_VERSION, IRCFW_VERSION); sprintf(construct, "NOTICE %s :\x01VERSION Okuu %s / IRC Frameworks %s: http://nishi.boats/okuu\x01", nick, OKUU_VERSION, IRCFW_VERSION);
ircfw_socket_send_cmd(ok_sock, NULL, construct); ircfw_socket_send_cmd(ok_sock, NULL, construct);
} }
}else if(sentin[0] == '#'){ } else if(sentin[0] == '#') {
/* This was sent in channel */ /* This was sent in channel */
if(ok_news_write(nick, msg) != 0){ if(ok_news_write(nick, msg) != 0) {
sprintf(construct, "PRIVMSG %s :Could not send the message to the USENET", sentin); sprintf(construct, "PRIVMSG %s :Could not send the message to the USENET", sentin);
ircfw_socket_send_cmd(ok_sock, NULL, construct); ircfw_socket_send_cmd(ok_sock, NULL, construct);
} }
@ -252,8 +249,8 @@ void ok_bot(void){
free(nick); free(nick);
} }
}else if(strcasecmp(ircfw_message.command, "ERROR") == 0){ } else if(strcasecmp(ircfw_message.command, "ERROR") == 0) {
if(ircfw_message.params != NULL){ if(ircfw_message.params != NULL) {
int i; int i;
for(i = 0; ircfw_message.params[i] != NULL; i++) fprintf(stderr, "ERROR: %s\n", ircfw_message.params[i]); for(i = 0; ircfw_message.params[i] != NULL; i++) fprintf(stderr, "ERROR: %s\n", ircfw_message.params[i]);
} }

View File

@ -25,7 +25,7 @@ char* ircreal;
char* ircpass; char* ircpass;
int ircport = 6667; int ircport = 6667;
int main(){ int main() {
printf("Okuu starting up\n"); printf("Okuu starting up\n");
nntpserver = getenv("NNTPSERVER"); nntpserver = getenv("NNTPSERVER");
@ -41,48 +41,48 @@ int main(){
ircnick = getenv("IRCNICK"); ircnick = getenv("IRCNICK");
ircreal = getenv("IRCREAL"); ircreal = getenv("IRCREAL");
ircpass = getenv("IRCPASS"); ircpass = getenv("IRCPASS");
if(getenv("NNTPPORT") != NULL){ if(getenv("NNTPPORT") != NULL) {
nntpport = atoi(getenv("NNTPPORT")); nntpport = atoi(getenv("NNTPPORT"));
} }
if(getenv("IRCPORT") != NULL){ if(getenv("IRCPORT") != NULL) {
ircport = atoi(getenv("IRCPORT")); ircport = atoi(getenv("IRCPORT"));
} }
bool bad = false; bool bad = false;
if(nntpserver == NULL){ if(nntpserver == NULL) {
fprintf(stderr, "Set NNTPSERVER\n"); fprintf(stderr, "Set NNTPSERVER\n");
bad = true; bad = true;
} }
if(nntppath == NULL){ if(nntppath == NULL) {
fprintf(stderr, "Set NNTPPATH\n"); fprintf(stderr, "Set NNTPPATH\n");
bad = true; bad = true;
} }
if(nntpcount == NULL){ if(nntpcount == NULL) {
fprintf(stderr, "Set NNTPCOUNT\n"); fprintf(stderr, "Set NNTPCOUNT\n");
bad = true; bad = true;
} }
if(nntpfrom == NULL){ if(nntpfrom == NULL) {
fprintf(stderr, "Set NNTPFROM\n"); fprintf(stderr, "Set NNTPFROM\n");
bad = true; bad = true;
} }
if(nntpgroup == NULL){ if(nntpgroup == NULL) {
fprintf(stderr, "Set NNTPGROUP\n"); fprintf(stderr, "Set NNTPGROUP\n");
bad = true; bad = true;
} }
if(ircserver == NULL){ if(ircserver == NULL) {
fprintf(stderr, "Set IRCSERVER\n"); fprintf(stderr, "Set IRCSERVER\n");
bad = true; bad = true;
} }
if(ircchan == NULL){ if(ircchan == NULL) {
fprintf(stderr, "Set IRCCHAN\n"); fprintf(stderr, "Set IRCCHAN\n");
bad = true; bad = true;
} }
if(ircuser == NULL){ if(ircuser == NULL) {
fprintf(stderr, "Set IRCUSER\n"); fprintf(stderr, "Set IRCUSER\n");
bad = true; bad = true;
} }
if(ircnick == NULL) ircnick = ircuser; if(ircnick == NULL) ircnick = ircuser;
if(ircreal == NULL) ircreal = ircuser; if(ircreal == NULL) ircreal = ircuser;
if(bad){ if(bad) {
return 1; return 1;
} }
ok_news_init(); ok_news_init();

View File

@ -33,23 +33,23 @@ struct news_entry news_entry;
void ok_close(int sock); void ok_close(int sock);
void ok_news_init(void){ void ok_news_init(void) {
news_entry.from = NULL; news_entry.from = NULL;
news_entry.content = NULL; news_entry.content = NULL;
} }
int ok_news_read(const char* path){ int ok_news_read(const char* path) {
if(news_entry.from != NULL){ if(news_entry.from != NULL) {
free(news_entry.from); free(news_entry.from);
news_entry.from = NULL; news_entry.from = NULL;
} }
if(news_entry.content != NULL){ if(news_entry.content != NULL) {
free(news_entry.content); free(news_entry.content);
news_entry.content = NULL; news_entry.content = NULL;
} }
struct stat s; struct stat s;
if(stat(path, &s) == 0){ if(stat(path, &s) == 0) {
char* boundary = NULL; char* boundary = NULL;
char* buffer = malloc(s.st_size + 1); char* buffer = malloc(s.st_size + 1);
buffer[s.st_size] = 0; buffer[s.st_size] = 0;
@ -64,59 +64,60 @@ int ok_news_read(const char* path){
bool header = true; bool header = true;
bool ignore = false; bool ignore = false;
bool bheader = false; bool bheader = false;
for(i = 0; i < s.st_size; i++){ for(i = 0; i < s.st_size; i++) {
if(buffer[i] == '\r'){ if(buffer[i] == '\r') {
if(buffer[i + 1] == '\n'){ if(buffer[i + 1] == '\n') {
/* newline */ /* newline */
i++; i++;
if(!header){ if(!header) {
char* line = malloc(i - 1 - incr + 1); char* line = malloc(i - 1 - incr + 1);
line[i - 1 - incr] = 0; line[i - 1 - incr] = 0;
memcpy(line, buffer + incr, i - 1 - incr); memcpy(line, buffer + incr, i - 1 - incr);
if(strcmp(line, ".") == 0){ if(strcmp(line, ".") == 0) {
free(line); free(line);
break; break;
}else{ } else {
char* ln = line; char* ln = line;
if(line[0] == '.'){ if(line[0] == '.') {
ln++; ln++;
} }
if(news_entry.content == NULL){ if(news_entry.content == NULL) {
news_entry.content = malloc(1); news_entry.content = malloc(1);
news_entry.content[0] = 0; news_entry.content[0] = 0;
} }
if(boundary != NULL && strcmp(ln, boundary) == 0){ if(boundary != NULL && strcmp(ln, boundary) == 0) {
bheader = true; bheader = true;
ignore = true; ignore = true;
}else if(boundary != NULL && bheader && strlen(ln) == 0){ } else if(boundary != NULL && bheader && strlen(ln) == 0) {
bheader = false; bheader = false;
free(line); free(line);
incr = i + 1; incr = i + 1;
newline = true; newline = true;
continue; continue;
}else if(boundary != NULL && bheader){ } else if(boundary != NULL && bheader) {
int j; int j;
for(j = 0; j < strlen(ln); j++){ for(j = 0; j < strlen(ln); j++) {
if(ln[j] == ':'){ if(ln[j] == ':') {
ln[j] = 0; ln[j] = 0;
if(strcasecmp(ln, "Content-Type") == 0){ if(strcasecmp(ln, "Content-Type") == 0) {
ignore = false; ignore = false;
j++; j++;
for(; ln[j] != 0 && (ln[j] == ' ' || ln[j] == '\t'); j++); for(; ln[j] != 0 && (ln[j] == ' ' || ln[j] == '\t'); j++)
if(ln[j] != 0){ ;
if(ln[j] != 0) {
char* v = ln + j; char* v = ln + j;
int k; int k;
for(k = 0; v[k] != 0; k++){ for(k = 0; v[k] != 0; k++) {
if(v[k] == ';'){ if(v[k] == ';') {
v[k] = 0; v[k] = 0;
break; break;
} }
} }
if(strcasecmp(v, "text/plain") == 0){ if(strcasecmp(v, "text/plain") == 0) {
}else{ } else {
ignore = true; ignore = true;
} }
} }
@ -126,7 +127,7 @@ int ok_news_read(const char* path){
} }
} }
if(!ignore && !bheader){ if(!ignore && !bheader) {
char* tmp = news_entry.content; char* tmp = news_entry.content;
news_entry.content = ok_strcat3(tmp, ln, "\n"); news_entry.content = ok_strcat3(tmp, ln, "\n");
free(tmp); free(tmp);
@ -134,9 +135,9 @@ int ok_news_read(const char* path){
} }
free(line); free(line);
}else if(newline){ } else if(newline) {
header = false; header = false;
}else{ } else {
char* line = malloc(i - 1 - incr + 1); char* line = malloc(i - 1 - incr + 1);
line[i - 1 - incr] = 0; line[i - 1 - incr] = 0;
memcpy(line, buffer + incr, i - 1 - incr); memcpy(line, buffer + incr, i - 1 - incr);
@ -146,40 +147,41 @@ int ok_news_read(const char* path){
l = ok_strcat(tmp, line); l = ok_strcat(tmp, line);
free(tmp); free(tmp);
bool al = false; bool al = false;
if(('a' <= line[0] && line[0] <= 'z') || ('A' <= line[0] && line[0] <= 'Z')){ if(('a' <= line[0] && line[0] <= 'z') || ('A' <= line[0] && line[0] <= 'Z')) {
free(l); free(l);
l = ok_strdup(line); l = ok_strdup(line);
al = true; al = true;
} }
if(al){ if(al) {
char* ln = ok_strdup(l); char* ln = ok_strdup(l);
int j; int j;
for(j = 0; ln[j] != 0; j++){ for(j = 0; ln[j] != 0; j++) {
if(ln[j] == ':'){ if(ln[j] == ':') {
char* key = ln; char* key = ln;
char* value = ""; char* value = "";
ln[j] = 0; ln[j] = 0;
j++; j++;
for(; ln[j] != 0 && (ln[j] == '\t' || ln[j] == ' '); j++); for(; ln[j] != 0 && (ln[j] == '\t' || ln[j] == ' '); j++)
;
if(ln[j] != 0) value = ln + j; if(ln[j] != 0) value = ln + j;
if(strcasecmp(key, "From") == 0){ if(strcasecmp(key, "From") == 0) {
if(news_entry.from != NULL) free(news_entry.from); if(news_entry.from != NULL) free(news_entry.from);
news_entry.from = ok_strdup(value); news_entry.from = ok_strdup(value);
}else if(strcasecmp(key, "Content-Type") == 0){ } else if(strcasecmp(key, "Content-Type") == 0) {
int k = 0; int k = 0;
int incr2 = 0; int incr2 = 0;
for(k = 0; k <= strlen(value); k++){ for(k = 0; k <= strlen(value); k++) {
if(value[k] == ';' || value[k] == 0){ if(value[k] == ';' || value[k] == 0) {
char* attr = malloc(k - incr2 + 1); char* attr = malloc(k - incr2 + 1);
attr[k - incr2] = 0; attr[k - incr2] = 0;
memcpy(attr, value + incr2, k - incr2); memcpy(attr, value + incr2, k - incr2);
int in; int in;
for(in = 0; attr[in] != 0; in++){ for(in = 0; attr[in] != 0; in++) {
if(attr[in] == '='){ if(attr[in] == '=') {
attr[in] = 0; attr[in] = 0;
if(strcasecmp(attr, "boundary") == 0){ if(strcasecmp(attr, "boundary") == 0) {
boundary = ok_strcat("--", attr + in + 1 + 1); boundary = ok_strcat("--", attr + in + 1 + 1);
boundary[strlen(attr + in + 1 + 1) - 1 + 2] = 0; boundary[strlen(attr + in + 1 + 1) - 1 + 2] = 0;
ignore = true; ignore = true;
@ -191,7 +193,8 @@ int ok_news_read(const char* path){
free(attr); free(attr);
k++; k++;
for(; value[k] != 0 && (value[k] == ' ' || value[k] == '\t'); k++); for(; value[k] != 0 && (value[k] == ' ' || value[k] == '\t'); k++)
;
incr2 = k; incr2 = k;
} }
} }
@ -205,10 +208,10 @@ int ok_news_read(const char* path){
} }
incr = i + 1; incr = i + 1;
newline = true; newline = true;
}else{ } else {
newline = false; newline = false;
} }
}else{ } else {
newline = false; newline = false;
} }
} }
@ -217,25 +220,25 @@ int ok_news_read(const char* path){
free(buffer); free(buffer);
if(boundary != NULL) free(boundary); if(boundary != NULL) free(boundary);
return 0; return 0;
}else{ } else {
return 1; return 1;
} }
} }
int ok_news_parse(int sock){ int ok_news_parse(int sock) {
char c; char c;
int sta = 0; int sta = 0;
bool st = false; bool st = false;
while(1){ while(1) {
if(recv(sock, &c, 1, 0) <= 0){ if(recv(sock, &c, 1, 0) <= 0) {
return -1; return -1;
} }
if(c == '\n') break; if(c == '\n') break;
if(!st){ if(!st) {
if('0' <= c && c <= '9'){ if('0' <= c && c <= '9') {
sta *= 10; sta *= 10;
sta += c - '0'; sta += c - '0';
}else if(c == ' '){ } else if(c == ' ') {
st = true; st = true;
} }
} }
@ -243,10 +246,10 @@ int ok_news_parse(int sock){
return sta == 0 ? -1 : sta; return sta == 0 ? -1 : sta;
} }
int ok_news_write(const char* nick, const char* message){ int ok_news_write(const char* nick, const char* message) {
int nt_sock; int nt_sock;
struct sockaddr_in nt_addr; struct sockaddr_in nt_addr;
if((nt_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0){ if((nt_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "Socket creation failure\n"); fprintf(stderr, "Socket creation failure\n");
return 1; return 1;
} }
@ -264,7 +267,7 @@ int ok_news_write(const char* nick, const char* message){
return 1; return 1;
} }
if(connect(nt_sock, (struct sockaddr*)&nt_addr, sizeof(nt_addr)) < 0){ if(connect(nt_sock, (struct sockaddr*)&nt_addr, sizeof(nt_addr)) < 0) {
fprintf(stderr, "Connection failure\n"); fprintf(stderr, "Connection failure\n");
ok_close(nt_sock); ok_close(nt_sock);
return 1; return 1;
@ -273,30 +276,30 @@ int ok_news_write(const char* nick, const char* message){
int sta; int sta;
sta = ok_news_parse(nt_sock); sta = ok_news_parse(nt_sock);
if(sta == 200 || sta == 201){ if(sta == 200 || sta == 201) {
char construct[1024]; char construct[1024];
if(nntpuser != NULL){ if(nntpuser != NULL) {
sprintf(construct, "AUTHINFO USER %s\r\n", nntpuser); sprintf(construct, "AUTHINFO USER %s\r\n", nntpuser);
send(nt_sock, construct, strlen(construct), 0); send(nt_sock, construct, strlen(construct), 0);
sta = ok_news_parse(nt_sock); sta = ok_news_parse(nt_sock);
if(sta != 381){ if(sta != 381) {
goto cleanup; goto cleanup;
} }
} }
if(nntppass != NULL){ if(nntppass != NULL) {
sprintf(construct, "AUTHINFO PASS %s\r\n", nntppass); sprintf(construct, "AUTHINFO PASS %s\r\n", nntppass);
send(nt_sock, construct, strlen(construct), 0); send(nt_sock, construct, strlen(construct), 0);
sta = ok_news_parse(nt_sock); sta = ok_news_parse(nt_sock);
if(sta != 281){ if(sta != 281) {
goto cleanup; goto cleanup;
} }
} }
send(nt_sock, "MODE READER\r\n", 4 + 1 + 6 + 2, 0); send(nt_sock, "MODE READER\r\n", 4 + 1 + 6 + 2, 0);
sta = ok_news_parse(nt_sock); sta = ok_news_parse(nt_sock);
if(sta == 200 || sta == 201){ if(sta == 200 || sta == 201) {
send(nt_sock, "POST\r\n", 4 + 2, 0); send(nt_sock, "POST\r\n", 4 + 2, 0);
sta = ok_news_parse(nt_sock); sta = ok_news_parse(nt_sock);
if(sta == 340){ if(sta == 340) {
sprintf(construct, "From: %s\r\n", nntpfrom); sprintf(construct, "From: %s\r\n", nntpfrom);
send(nt_sock, construct, strlen(construct), 0); send(nt_sock, construct, strlen(construct), 0);
sprintf(construct, "Newsgroups: %s\r\n", nntpgroup); sprintf(construct, "Newsgroups: %s\r\n", nntpgroup);
@ -309,12 +312,12 @@ int ok_news_write(const char* nick, const char* message){
char c; char c;
int i; int i;
bool first = true; bool first = true;
for(i = 0; message[i] != 0; i++){ for(i = 0; message[i] != 0; i++) {
if(message[i] == '\n'){ if(message[i] == '\n') {
send(nt_sock, "\r\n", 2, 0); send(nt_sock, "\r\n", 2, 0);
first = true; first = true;
}else{ } else {
if(first && message[i] == '.'){ if(first && message[i] == '.') {
send(nt_sock, message + i, 1, 0); send(nt_sock, message + i, 1, 0);
} }
send(nt_sock, message + i, 1, 0); send(nt_sock, message + i, 1, 0);
@ -328,13 +331,13 @@ int ok_news_write(const char* nick, const char* message){
send(nt_sock, "QUIT\r\n", 6, 0); send(nt_sock, "QUIT\r\n", 6, 0);
sta = ok_news_parse(nt_sock); sta = ok_news_parse(nt_sock);
if(sta != 205) goto cleanup; if(sta != 205) goto cleanup;
}else{ } else {
goto cleanup; goto cleanup;
} }
}else{ } else {
goto cleanup; goto cleanup;
} }
}else{ } else {
goto cleanup; goto cleanup;
} }

View File

@ -5,7 +5,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
char* ok_strcat(const char* a, const char* b){ char* ok_strcat(const char* a, const char* b) {
char* str = malloc(strlen(a) + strlen(b) + 1); char* str = malloc(strlen(a) + strlen(b) + 1);
memcpy(str, a, strlen(a)); memcpy(str, a, strlen(a));
memcpy(str + strlen(a), b, strlen(b)); memcpy(str + strlen(a), b, strlen(b));
@ -13,13 +13,11 @@ char* ok_strcat(const char* a, const char* b){
return str; return str;
} }
char* ok_strcat3(const char* a, const char* b, const char* c){ char* ok_strcat3(const char* a, const char* b, const char* c) {
char* tmp = ok_strcat(a, b); char* tmp = ok_strcat(a, b);
char* str = ok_strcat(tmp, c); char* str = ok_strcat(tmp, c);
free(tmp); free(tmp);
return str; return str;
} }
char* ok_strdup(const char* a){ char* ok_strdup(const char* a) { return ok_strcat(a, ""); }
return ok_strcat(a, "");
}

View File

@ -5,10 +5,13 @@ PWD = `pwd`
FLAGS = PWD=$(PWD) PLATFORM=$(PLATFORM) FLAGS = PWD=$(PWD) PLATFORM=$(PLATFORM)
.PHONY: all clean ./Bot .PHONY: all format clean ./Bot
./Bot:: ./Bot::
$(MAKE) -C $@ $(FLAGS) $(MAKE) -C $@ $(FLAGS)
format:
clang-format -i --verbose `find . -name "*.c" -or -name "*.h"`
clean: clean:
$(MAKE) -C ./Bot $(FLAGS) clean $(MAKE) -C ./Bot $(FLAGS) clean