diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..f09c9d2 --- /dev/null +++ b/.clang-format @@ -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 diff --git a/Bot/bot.c b/Bot/bot.c index 807f52e..4ff0b0a 100644 --- a/Bot/bot.c +++ b/Bot/bot.c @@ -42,11 +42,9 @@ extern int ircport; int ok_sock; struct sockaddr_in ok_addr; -void ok_close(int sock){ - close(sock); -} +void ok_close(int sock) { close(sock); } -void ok_bot_kill(int sig){ +void ok_bot_kill(int sig) { fprintf(stderr, "Shutdown\n"); ircfw_socket_send_cmd(ok_sock, NULL, "QUIT :Shutdown (Signal)"); exit(1); @@ -60,22 +58,18 @@ bool ok_is_number(const char* str) { return true; } -char* ok_null(const char* str){ - return str == NULL ? "(null)" : (char*)str; -} +char* ok_null(const 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* b = *b_; return atoi(a->d_name) - atoi(b->d_name); } -int nodots(const struct dirent* d){ - return (strcmp(d->d_name, "..") == 0 || strcmp(d->d_name, ".") == 0) ? 0 : 1; -} +int nodots(const struct dirent* d) { return (strcmp(d->d_name, "..") == 0 || strcmp(d->d_name, ".") == 0) ? 0 : 1; } -void ok_bot(void){ - if((ok_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0){ +void ok_bot(void) { + if((ok_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { fprintf(stderr, "Socket creation failure\n"); return; } @@ -92,8 +86,8 @@ void ok_bot(void){ ok_close(ok_sock); 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"); ok_close(ok_sock); return; @@ -104,7 +98,7 @@ void ok_bot(void){ char* construct = malloc(1025); - if(ircpass != NULL && strlen(ircpass) > 0){ + if(ircpass != NULL && strlen(ircpass) > 0) { sprintf(construct, "PASS :%s", ircpass); ircfw_socket_send_cmd(ok_sock, NULL, construct); } @@ -122,47 +116,49 @@ void ok_bot(void){ pollfds[0].fd = ok_sock; pollfds[0].events = POLLIN | POLLPRI; - while(1){ + while(1) { int r = poll(pollfds, 1, 100); - if(!(r > 0 && pollfds[0].revents & POLLIN)){ + if(!(r > 0 && pollfds[0].revents & POLLIN)) { /* 100ms sleep, technically */ uint64_t count = 0; FILE* f = fopen(nntpcount, "rb"); - if(f != NULL){ + if(f != NULL) { fread(&count, sizeof(count), 1, f); fclose(f); } struct dirent** list; int n = scandir(nntppath, &list, nodots, namesort); - if(n >= 0){ + if(n >= 0) { int i; - for(i = 0; i < n; i++){ - if(!sendable){ + for(i = 0; i < n; i++) { + if(!sendable) { free(list[i]); continue; } - if(count <= atoi(list[i]->d_name)){ + if(count <= atoi(list[i]->d_name)) { sprintf(construct, "%s/%s", nntppath, list[i]->d_name); - if(ok_news_read(construct) == 0){ - if(strcmp(news_entry.from, nntpfrom) != 0){ - char* tmp = ok_strcat3("PRIVMSG ", ircchan, " :\x03" "07[USENET] ~ "); + if(ok_news_read(construct) == 0) { + if(strcmp(news_entry.from, nntpfrom) != 0) { + char* tmp = ok_strcat3("PRIVMSG ", ircchan, + " :\x03" + "07[USENET] ~ "); char* temp = ok_strcat3(tmp, news_entry.from, "\x03 "); free(tmp); int j; int incr = 0; - for(j = 0;; j++){ - if(news_entry.content[j] == 0 || news_entry.content[j] == '\n'){ + for(j = 0;; j++) { + if(news_entry.content[j] == 0 || news_entry.content[j] == '\n') { char* line = malloc(j - incr + 1); line[j - incr] = 0; memcpy(line, news_entry.content + incr, j - incr); - - if(strlen(line) > 0){ + + if(strlen(line) > 0) { char* msg = ok_strcat(temp, line); ircfw_socket_send_cmd(ok_sock, NULL, msg); free(msg); usleep(1000 * 100); /* Sleep for 100ms */ } - + free(line); incr = j + 1; if(news_entry.content[j] == 0) break; @@ -170,7 +166,7 @@ void ok_bot(void){ } free(temp); } - }else{ + } else { fprintf(stderr, "Could not read %s\n", construct); } } @@ -179,9 +175,9 @@ void ok_bot(void){ count = atoi(list[i - 1]->d_name) + 1; free(list); } - if(sendable){ + if(sendable) { f = fopen(nntpcount, "wb"); - if(f != NULL){ + if(f != NULL) { fwrite(&count, sizeof(count), 1, f); fclose(f); } @@ -189,40 +185,41 @@ void ok_bot(void){ continue; } int st = ircfw_socket_read_cmd(ok_sock); - if(st != 0){ + if(st != 0) { fprintf(stderr, "Bad response\n"); 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); - if(!is_in && 400 <= res && res <= 599){ + if(!is_in && 400 <= res && res <= 599) { fprintf(stderr, "Bad response\n"); return; - }else if(400 <= res && res <= 599){ + } else if(400 <= res && res <= 599) { fprintf(stderr, "Ignored error: %d\n", res); continue; } - if(res == 376){ + if(res == 376) { is_in = true; fprintf(stderr, "Login successful\n"); sprintf(construct, "JOIN :%s", ircchan); ircfw_socket_send_cmd(ok_sock, NULL, construct); - }else if(res == 331 || res == 332){ + } else if(res == 331 || res == 332) { sendable = true; } - }else{ - if(strcasecmp(ircfw_message.command, "PING") == 0){ + } else { + if(strcasecmp(ircfw_message.command, "PING") == 0) { fprintf(stderr, "Ping request\n"); sprintf(construct, "PONG :%s", ok_null(ircfw_message.prefix)); 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** params = ircfw_message.params; int len = 0; if(params != NULL) { int i; - for(i = 0; params[i] != NULL; i++); + for(i = 0; params[i] != NULL; i++) + ; len = i; } if(prefix != NULL && len == 2) { @@ -236,15 +233,15 @@ void ok_bot(void){ break; } } - if(msg[0] == 1 && msg[strlen(msg) - 1] == 1){ + if(msg[0] == 1 && msg[strlen(msg) - 1] == 1) { /* 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); ircfw_socket_send_cmd(ok_sock, NULL, construct); } - }else if(sentin[0] == '#'){ + } else if(sentin[0] == '#') { /* 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); ircfw_socket_send_cmd(ok_sock, NULL, construct); } @@ -252,8 +249,8 @@ void ok_bot(void){ free(nick); } - }else if(strcasecmp(ircfw_message.command, "ERROR") == 0){ - if(ircfw_message.params != NULL){ + } else if(strcasecmp(ircfw_message.command, "ERROR") == 0) { + if(ircfw_message.params != NULL) { int i; for(i = 0; ircfw_message.params[i] != NULL; i++) fprintf(stderr, "ERROR: %s\n", ircfw_message.params[i]); } diff --git a/Bot/main.c b/Bot/main.c index 062c498..904687c 100644 --- a/Bot/main.c +++ b/Bot/main.c @@ -25,7 +25,7 @@ char* ircreal; char* ircpass; int ircport = 6667; -int main(){ +int main() { printf("Okuu starting up\n"); nntpserver = getenv("NNTPSERVER"); @@ -41,48 +41,48 @@ int main(){ ircnick = getenv("IRCNICK"); ircreal = getenv("IRCREAL"); ircpass = getenv("IRCPASS"); - if(getenv("NNTPPORT") != NULL){ + if(getenv("NNTPPORT") != NULL) { nntpport = atoi(getenv("NNTPPORT")); } - if(getenv("IRCPORT") != NULL){ + if(getenv("IRCPORT") != NULL) { ircport = atoi(getenv("IRCPORT")); } bool bad = false; - if(nntpserver == NULL){ + if(nntpserver == NULL) { fprintf(stderr, "Set NNTPSERVER\n"); bad = true; } - if(nntppath == NULL){ + if(nntppath == NULL) { fprintf(stderr, "Set NNTPPATH\n"); bad = true; } - if(nntpcount == NULL){ + if(nntpcount == NULL) { fprintf(stderr, "Set NNTPCOUNT\n"); bad = true; } - if(nntpfrom == NULL){ + if(nntpfrom == NULL) { fprintf(stderr, "Set NNTPFROM\n"); bad = true; } - if(nntpgroup == NULL){ + if(nntpgroup == NULL) { fprintf(stderr, "Set NNTPGROUP\n"); bad = true; } - if(ircserver == NULL){ + if(ircserver == NULL) { fprintf(stderr, "Set IRCSERVER\n"); bad = true; } - if(ircchan == NULL){ + if(ircchan == NULL) { fprintf(stderr, "Set IRCCHAN\n"); bad = true; } - if(ircuser == NULL){ + if(ircuser == NULL) { fprintf(stderr, "Set IRCUSER\n"); bad = true; } if(ircnick == NULL) ircnick = ircuser; if(ircreal == NULL) ircreal = ircuser; - if(bad){ + if(bad) { return 1; } ok_news_init(); diff --git a/Bot/news.c b/Bot/news.c index 7a92547..f2700e0 100644 --- a/Bot/news.c +++ b/Bot/news.c @@ -33,23 +33,23 @@ struct news_entry news_entry; void ok_close(int sock); -void ok_news_init(void){ +void ok_news_init(void) { news_entry.from = NULL; news_entry.content = NULL; } -int ok_news_read(const char* path){ - if(news_entry.from != NULL){ +int ok_news_read(const char* path) { + if(news_entry.from != NULL) { free(news_entry.from); news_entry.from = NULL; } - if(news_entry.content != NULL){ + if(news_entry.content != NULL) { free(news_entry.content); news_entry.content = NULL; } struct stat s; - if(stat(path, &s) == 0){ + if(stat(path, &s) == 0) { char* boundary = NULL; char* buffer = malloc(s.st_size + 1); buffer[s.st_size] = 0; @@ -64,59 +64,60 @@ int ok_news_read(const char* path){ bool header = true; bool ignore = false; bool bheader = false; - for(i = 0; i < s.st_size; i++){ - if(buffer[i] == '\r'){ - if(buffer[i + 1] == '\n'){ + for(i = 0; i < s.st_size; i++) { + if(buffer[i] == '\r') { + if(buffer[i + 1] == '\n') { /* newline */ i++; - if(!header){ + if(!header) { char* line = malloc(i - 1 - incr + 1); line[i - 1 - incr] = 0; memcpy(line, buffer + incr, i - 1 - incr); - if(strcmp(line, ".") == 0){ + if(strcmp(line, ".") == 0) { free(line); break; - }else{ + } else { char* ln = line; - if(line[0] == '.'){ + if(line[0] == '.') { ln++; } - if(news_entry.content == NULL){ + if(news_entry.content == NULL) { news_entry.content = malloc(1); news_entry.content[0] = 0; } - if(boundary != NULL && strcmp(ln, boundary) == 0){ + if(boundary != NULL && strcmp(ln, boundary) == 0) { bheader = true; ignore = true; - }else if(boundary != NULL && bheader && strlen(ln) == 0){ + } else if(boundary != NULL && bheader && strlen(ln) == 0) { bheader = false; free(line); incr = i + 1; newline = true; continue; - }else if(boundary != NULL && bheader){ + } else if(boundary != NULL && bheader) { int j; - for(j = 0; j < strlen(ln); j++){ - if(ln[j] == ':'){ + for(j = 0; j < strlen(ln); j++) { + if(ln[j] == ':') { ln[j] = 0; - if(strcasecmp(ln, "Content-Type") == 0){ + if(strcasecmp(ln, "Content-Type") == 0) { ignore = false; j++; - for(; ln[j] != 0 && (ln[j] == ' ' || ln[j] == '\t'); j++); - if(ln[j] != 0){ + for(; ln[j] != 0 && (ln[j] == ' ' || ln[j] == '\t'); j++) + ; + if(ln[j] != 0) { char* v = ln + j; int k; - for(k = 0; v[k] != 0; k++){ - if(v[k] == ';'){ + for(k = 0; v[k] != 0; k++) { + if(v[k] == ';') { v[k] = 0; break; } } - if(strcasecmp(v, "text/plain") == 0){ - }else{ + if(strcasecmp(v, "text/plain") == 0) { + } else { ignore = true; } } @@ -126,7 +127,7 @@ int ok_news_read(const char* path){ } } - if(!ignore && !bheader){ + if(!ignore && !bheader) { char* tmp = news_entry.content; news_entry.content = ok_strcat3(tmp, ln, "\n"); free(tmp); @@ -134,9 +135,9 @@ int ok_news_read(const char* path){ } free(line); - }else if(newline){ + } else if(newline) { header = false; - }else{ + } else { char* line = malloc(i - 1 - incr + 1); line[i - 1 - incr] = 0; memcpy(line, buffer + incr, i - 1 - incr); @@ -146,40 +147,41 @@ int ok_news_read(const char* path){ l = ok_strcat(tmp, line); free(tmp); 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); l = ok_strdup(line); al = true; } - if(al){ + if(al) { char* ln = ok_strdup(l); int j; - for(j = 0; ln[j] != 0; j++){ - if(ln[j] == ':'){ + for(j = 0; ln[j] != 0; j++) { + if(ln[j] == ':') { char* key = ln; char* value = ""; ln[j] = 0; 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(strcasecmp(key, "From") == 0){ + if(strcasecmp(key, "From") == 0) { if(news_entry.from != NULL) free(news_entry.from); news_entry.from = ok_strdup(value); - }else if(strcasecmp(key, "Content-Type") == 0){ + } else if(strcasecmp(key, "Content-Type") == 0) { int k = 0; int incr2 = 0; - for(k = 0; k <= strlen(value); k++){ - if(value[k] == ';' || value[k] == 0){ + for(k = 0; k <= strlen(value); k++) { + if(value[k] == ';' || value[k] == 0) { char* attr = malloc(k - incr2 + 1); attr[k - incr2] = 0; memcpy(attr, value + incr2, k - incr2); int in; - for(in = 0; attr[in] != 0; in++){ - if(attr[in] == '='){ + for(in = 0; attr[in] != 0; in++) { + if(attr[in] == '=') { attr[in] = 0; - if(strcasecmp(attr, "boundary") == 0){ + if(strcasecmp(attr, "boundary") == 0) { boundary = ok_strcat("--", attr + in + 1 + 1); boundary[strlen(attr + in + 1 + 1) - 1 + 2] = 0; ignore = true; @@ -191,7 +193,8 @@ int ok_news_read(const char* path){ free(attr); k++; - for(; value[k] != 0 && (value[k] == ' ' || value[k] == '\t'); k++); + for(; value[k] != 0 && (value[k] == ' ' || value[k] == '\t'); k++) + ; incr2 = k; } } @@ -205,10 +208,10 @@ int ok_news_read(const char* path){ } incr = i + 1; newline = true; - }else{ + } else { newline = false; } - }else{ + } else { newline = false; } } @@ -217,25 +220,25 @@ int ok_news_read(const char* path){ free(buffer); if(boundary != NULL) free(boundary); return 0; - }else{ + } else { return 1; } } -int ok_news_parse(int sock){ +int ok_news_parse(int sock) { char c; int sta = 0; bool st = false; - while(1){ - if(recv(sock, &c, 1, 0) <= 0){ + while(1) { + if(recv(sock, &c, 1, 0) <= 0) { return -1; } if(c == '\n') break; - if(!st){ - if('0' <= c && c <= '9'){ + if(!st) { + if('0' <= c && c <= '9') { sta *= 10; sta += c - '0'; - }else if(c == ' '){ + } else if(c == ' ') { st = true; } } @@ -243,10 +246,10 @@ int ok_news_parse(int sock){ 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; 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"); return 1; } @@ -264,7 +267,7 @@ int ok_news_write(const char* nick, const char* message){ 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"); ok_close(nt_sock); return 1; @@ -273,30 +276,30 @@ int ok_news_write(const char* nick, const char* message){ int sta; sta = ok_news_parse(nt_sock); - if(sta == 200 || sta == 201){ + if(sta == 200 || sta == 201) { char construct[1024]; - if(nntpuser != NULL){ + if(nntpuser != NULL) { sprintf(construct, "AUTHINFO USER %s\r\n", nntpuser); send(nt_sock, construct, strlen(construct), 0); sta = ok_news_parse(nt_sock); - if(sta != 381){ + if(sta != 381) { goto cleanup; } } - if(nntppass != NULL){ + if(nntppass != NULL) { sprintf(construct, "AUTHINFO PASS %s\r\n", nntppass); send(nt_sock, construct, strlen(construct), 0); sta = ok_news_parse(nt_sock); - if(sta != 281){ + if(sta != 281) { 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); - if(sta == 200 || sta == 201){ + if(sta == 200 || sta == 201) { send(nt_sock, "POST\r\n", 4 + 2, 0); sta = ok_news_parse(nt_sock); - if(sta == 340){ + if(sta == 340) { sprintf(construct, "From: %s\r\n", nntpfrom); send(nt_sock, construct, strlen(construct), 0); sprintf(construct, "Newsgroups: %s\r\n", nntpgroup); @@ -309,12 +312,12 @@ int ok_news_write(const char* nick, const char* message){ char c; int i; bool first = true; - for(i = 0; message[i] != 0; i++){ - if(message[i] == '\n'){ + for(i = 0; message[i] != 0; i++) { + if(message[i] == '\n') { send(nt_sock, "\r\n", 2, 0); first = true; - }else{ - if(first && message[i] == '.'){ + } else { + if(first && message[i] == '.') { 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); sta = ok_news_parse(nt_sock); if(sta != 205) goto cleanup; - }else{ + } else { goto cleanup; } - }else{ + } else { goto cleanup; } - }else{ + } else { goto cleanup; } diff --git a/Bot/util.c b/Bot/util.c index 7cf29fb..3122e1f 100644 --- a/Bot/util.c +++ b/Bot/util.c @@ -5,7 +5,7 @@ #include #include -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); memcpy(str, a, strlen(a)); memcpy(str + strlen(a), b, strlen(b)); @@ -13,13 +13,11 @@ char* ok_strcat(const char* a, const char* b){ 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* str = ok_strcat(tmp, c); free(tmp); return str; } -char* ok_strdup(const char* a){ - return ok_strcat(a, ""); -} +char* ok_strdup(const char* a) { return ok_strcat(a, ""); } diff --git a/Makefile b/Makefile index f3c6860..7cc7e05 100644 --- a/Makefile +++ b/Makefile @@ -5,10 +5,13 @@ PWD = `pwd` FLAGS = PWD=$(PWD) PLATFORM=$(PLATFORM) -.PHONY: all clean ./Bot +.PHONY: all format clean ./Bot ./Bot:: $(MAKE) -C $@ $(FLAGS) +format: + clang-format -i --verbose `find . -name "*.c" -or -name "*.h"` + clean: $(MAKE) -C ./Bot $(FLAGS) clean