From: nishi Date: Fri, 23 Feb 2024 11:35:17 +0000 (+0000) Subject: new properties X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=c441dde880fd98d329469ed75bbbc715d8ff2708;p=libw3.git new properties git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@249 d27a3e52-49c5-7645-884c-6793ebffc270 --- diff --git a/Library/Core.c b/Library/Core.c index 12c1fb1..de40505 100644 --- a/Library/Core.c +++ b/Library/Core.c @@ -94,7 +94,7 @@ struct W3* W3_Create(const char* protocol, const char* hostname, int port) { ssl = true; } else if(strcmp(protocol, "gophers") == 0) { ssl = true; - } else if(strcmp(protocol, "ircs") == 0){ + } else if(strcmp(protocol, "ircs") == 0) { ssl = true; } w3->props = NULL; @@ -105,6 +105,8 @@ struct W3* W3_Create(const char* protocol, const char* hostname, int port) { w3->size = 0; w3->data = NULL; w3->sock = -1; + w3->reading = false; + w3->writing = false; w3->readsize = 512; w3->port = port; w3->protocol = __W3_Strdup(protocol); diff --git a/Library/IRC.c b/Library/IRC.c index 2a4d329..b63dde8 100644 --- a/Library/IRC.c +++ b/Library/IRC.c @@ -22,7 +22,7 @@ void __W3_IRC_Request(struct W3* w3) { } return; } - if(__W3_Get_Prop(w3, "IRC_VERSION") == NULL){ + if(__W3_Get_Prop(w3, "IRC_VERSION") == NULL) { __W3_Add_Prop(w3, "IRC_VERSION", "LibW3 " LIBW3_VERSION); } char* username = __W3_Get_Prop(w3, "IRC_USERNAME"); @@ -45,28 +45,28 @@ void __W3_IRC_Request(struct W3* w3) { __W3_Auto_Write(w3, "NICK ", 5); __W3_Auto_Write(w3, nickname, strlen(nickname)); __W3_Auto_Write(w3, "\r\n", 2); - if(__W3_Get_Prop(w3, "IRC_PASSWORD") != NULL){ + if(__W3_Get_Prop(w3, "IRC_PASSWORD") != NULL) { __W3_Auto_Write(w3, "PASS :", 6); __W3_Auto_Write(w3, __W3_Get_Prop(w3, "IRC_PASSWORD"), strlen(__W3_Get_Prop(w3, "IRC_PASSWORD"))); __W3_Auto_Write(w3, "\r\n", 2); } char* buf = malloc(w3->readsize); - int phase = 0; /* 0: before the prefix - * 1: prefix - * 2: command - * 3: parms - */ + int phase = 0; /* 0: before the prefix + * 1: prefix + * 2: command + * 3: parms + */ /* ::= [':' ] * ::= | [ '!' ] [ '@' ] * ::= { } | * ::= ' ' { ' ' } * ::= [ ':' | ] - * + * * ::= * ::= - * + * * ::= CR LF * * https://datatracker.ietf.org/doc/html/rfc1459#section-2.3.1 @@ -80,93 +80,95 @@ void __W3_IRC_Request(struct W3* w3) { char* cbuf = malloc(2); cbuf[1] = 0; bool colon = false; - while(true){ + while(true) { int len = __W3_Auto_Read(w3, buf, w3->readsize); if(len <= 0) break; int i; - for(i = 0; i < len; i++){ - if(phase == 0){ - if(buf[i] == ':'){ + for(i = 0; i < len; i++) { + if(phase == 0) { + if(buf[i] == ':') { phase = 1; - }else{ + } else { phase = 2; cbuf[0] = buf[i]; char* tmp = command; command = __W3_Concat(tmp, cbuf); free(tmp); } - }else if(phase == 1){ - if(buf[i] == ' '){ + } else if(phase == 1) { + if(buf[i] == ' ') { phase = 2; - }else{ + } else { cbuf[0] = buf[i]; char* tmp = prefix; prefix = __W3_Concat(tmp, cbuf); free(tmp); } - }else if(phase == 2){ - if(buf[i] == ' '){ + } else if(phase == 2) { + if(buf[i] == ' ') { phase = 3; - }else{ + } else { cbuf[0] = buf[i]; char* tmp = command; command = __W3_Concat(tmp, cbuf); free(tmp); } - }else if(phase == 3){ - if(buf[i] == '\n'){ + } else if(phase == 3) { + if(buf[i] == '\n') { phase = 0; /* Parse commands here */ - if(strcasecmp(command, "PRIVMSG") == 0){ + if(strcasecmp(command, "PRIVMSG") == 0) { int j; bool msgcont = false; char* username = NULL; char* content = NULL; - for(j = 0; params[j] != 0; j++){ - if(params[j] == ' '){ + for(j = 0; params[j] != 0; j++) { + if(params[j] == ' ') { params[j] = 0; username = params; content = params + j + 1; break; } } - if(username != NULL && content != NULL){ - if(content[0] == 1 && content[strlen(content) - 1] == 1){ + if(username != NULL && content != NULL) { + if(content[0] == 1 && content[strlen(content) - 1] == 1) { char* sentfrom = __W3_Strdup(prefix); int j; - for(j = 0; sentfrom[j] != 0; j++){ - if(sentfrom[j] == '!'){ + for(j = 0; sentfrom[j] != 0; j++) { + if(sentfrom[j] == '!') { sentfrom[j] = 0; break; } } /* CTCP Request */ - if(strcasecmp(content, "\x01" "VERSION" "\x01") == 0){ + if(strcasecmp(content, "\x01" + "VERSION" + "\x01") == 0) { __W3_Auto_Write(w3, "NOTICE ", 7); __W3_Auto_Write(w3, prefix, strlen(prefix)); __W3_Auto_Write(w3, " :", 2); __W3_Auto_Write(w3, __W3_Get_Prop(w3, "IRC_VERSION"), strlen(__W3_Get_Prop(w3, "IRC_VERSION"))); __W3_Auto_Write(w3, "\r\n", 2); - }else{ + } else { /* client is sending the non-standard CTCP ... */ } free(sentfrom); } void* funcptr = __W3_Get_Event(w3, "message"); - if(funcptr != NULL){ - void(*func)(struct W3* w3, char* on, char* message) = (void(*)(struct W3* w3, char* from, char* message))funcptr; + if(funcptr != NULL) { + void (*func)(struct W3 * w3, char* on, char* message) = (void (*)(struct W3 * w3, char* from, char* message)) funcptr; func(w3, username[0] == '#' ? username : prefix, content); } } - }else if(strcasecmp(command, "PING") == 0){ + } else if(strcasecmp(command, "PING") == 0) { __W3_Auto_Write(w3, "PONG ", 5); __W3_Auto_Write(w3, params, strlen(params)); __W3_Auto_Write(w3, "\r\n", 2); - }else{ + } else { void* funcptr = __W3_Get_Event(w3, "unknown"); - if(funcptr != NULL){ - void(*func)(struct W3* w3, char* prefix, char* command, char* data) = (void(*)(struct W3* w3, char* prefix, char* command, char* data))funcptr; + if(funcptr != NULL) { + void (*func)(struct W3 * w3, char* prefix, char* command, char* data) = (void (*)(struct W3 * w3, char* prefix, char* command, char* data)) funcptr; func(w3, prefix, command, params); } } @@ -180,13 +182,13 @@ void __W3_IRC_Request(struct W3* w3) { cbuf = malloc(2); cbuf[1] = 0; colon = false; - }else if(buf[i] != '\r'){ + } else if(buf[i] != '\r') { cbuf[0] = buf[i]; - if(cbuf[0] != ':' || colon){ + if(cbuf[0] != ':' || colon) { char* tmp = params; params = __W3_Concat(tmp, cbuf); free(tmp); - }else if(cbuf[0] == ':'){ + } else if(cbuf[0] == ':') { colon = true; } } @@ -200,31 +202,19 @@ void __W3_IRC_Request(struct W3* w3) { free(buf); } -void W3_IRC_Set_Username(struct W3* w3, const char* username){ - __W3_Add_Prop(w3, "IRC_USERNAME", username); -} +void W3_IRC_Set_Username(struct W3* w3, const char* username) { __W3_Add_Prop(w3, "IRC_USERNAME", username); } -void W3_IRC_Set_Hostname(struct W3* w3, const char* hostname){ - __W3_Add_Prop(w3, "IRC_HOSTNAME", hostname); -} +void W3_IRC_Set_Hostname(struct W3* w3, const char* hostname) { __W3_Add_Prop(w3, "IRC_HOSTNAME", hostname); } -void W3_IRC_Set_Realname(struct W3* w3, const char* realname){ - __W3_Add_Prop(w3, "IRC_REALNAME", realname); -} +void W3_IRC_Set_Realname(struct W3* w3, const char* realname) { __W3_Add_Prop(w3, "IRC_REALNAME", realname); } -void W3_IRC_Set_Servername(struct W3* w3, const char* servername){ - __W3_Add_Prop(w3, "IRC_SERVERNAME", servername); -} +void W3_IRC_Set_Servername(struct W3* w3, const char* servername) { __W3_Add_Prop(w3, "IRC_SERVERNAME", servername); } -void W3_IRC_Set_Nickname(struct W3* w3, const char* nickname){ - __W3_Add_Prop(w3, "IRC_NICKNAME", nickname); -} +void W3_IRC_Set_Nickname(struct W3* w3, const char* nickname) { __W3_Add_Prop(w3, "IRC_NICKNAME", nickname); } -void W3_IRC_Set_Password(struct W3* w3, const char* password){ - __W3_Add_Prop(w3, "IRC_PASSWORD", password); -} +void W3_IRC_Set_Password(struct W3* w3, const char* password) { __W3_Add_Prop(w3, "IRC_PASSWORD", password); } -void W3_IRC_Send_Request(struct W3* w3){ +void W3_IRC_Send_Request(struct W3* w3) { __W3_Auto_Write(w3, w3->method, strlen(w3->method)); __W3_Auto_Write(w3, " ", 1); __W3_Auto_Write(w3, w3->path, strlen(w3->path)); diff --git a/Library/Util.c b/Library/Util.c index 75360e1..fc3fd59 100644 --- a/Library/Util.c +++ b/Library/Util.c @@ -59,27 +59,39 @@ char* __W3_Strdup(const char* str) { } unsigned long __W3_Auto_Write(struct W3* w3, char* data, unsigned long length) { + while(w3->writing) + ; + w3->writing = true; + unsigned long code; #ifdef SSL_SUPPORT if(w3->ssl != NULL) { - return SSL_write(w3->ssl, data, length); + code = SSL_write(w3->ssl, data, length); } else { - return send(w3->sock, data, length, 0); + code = send(w3->sock, data, length, 0); } #else - return send(w3->sock, data, length, 0); + code = send(w3->sock, data, length, 0); #endif + w3->writing = false; + return code; } unsigned long __W3_Auto_Read(struct W3* w3, char* data, unsigned long length) { + while(w3->reading) + ; + w3->reading = true; + unsigned long code; #ifdef SSL_SUPPORT if(w3->ssl != NULL) { - return SSL_read(w3->ssl, data, length); + code = SSL_read(w3->ssl, data, length); } else { - return recv(w3->sock, data, length, 0); + code = recv(w3->sock, data, length, 0); } #else - return recv(w3->sock, data, length, 0); + code = recv(w3->sock, data, length, 0); #endif + w3->reading = false; + return code; } void* __W3_Get_Event(struct W3* w3, const char* eventname) { diff --git a/Library/W3Core.h b/Library/W3Core.h index 5e51368..7b6fc9d 100644 --- a/Library/W3Core.h +++ b/Library/W3Core.h @@ -32,6 +32,9 @@ struct W3 { void* tcl_in; /* Used in Tcl */ void* ssl; /* Actually SSL*, NULL if no SSL */ void* ssl_ctx; /* Actually SSL_CTX* */ + + bool reading; + bool writing; }; int W3_Library_Init(void); /* Initialize the Library */ diff --git a/W3Version.h.m4 b/W3Version.h.m4 index b8366d7..8342490 100644 --- a/W3Version.h.m4 +++ b/W3Version.h.m4 @@ -6,7 +6,7 @@ extern "C" { #endif -#define LIBW3_VERSION "2.17C" \ +#define LIBW3_VERSION "2.18" \ SUFFIX ifdef(`HTTP_SUPPORT', `#define LIBW3_HTTP_SUPPORT', `')