]> Nishi Git Mirror - libw3.git/commitdiff
new properties
authornishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Fri, 23 Feb 2024 11:35:17 +0000 (11:35 +0000)
committernishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Fri, 23 Feb 2024 11:35:17 +0000 (11:35 +0000)
git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@249 d27a3e52-49c5-7645-884c-6793ebffc270

Library/Core.c
Library/IRC.c
Library/Util.c
Library/W3Core.h
W3Version.h.m4

index 12c1fb122fda8172f1cdc15915600e4555da3203..de4050548721a9930d257ba7dda4741d90891745 100644 (file)
@@ -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);
index 2a4d32909568052cb4802c97c70cc05c17ecb25d..b63dde8ec90431eeca608f97d21a5a3af79ba568 100644 (file)
@@ -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
+                       */
        /* <message>  ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
         * <prefix>   ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
         * <command>  ::= <letter> { <letter> } | <number> <number> <number>
         * <SPACE>    ::= ' ' { ' ' }
         * <params>   ::= <SPACE> [ ':' <trailing> | <middle> <params> ]
-        * 
+        *
         * <middle>   ::= <Any *non-empty* sequence of octets not including SPACE
         *                or NUL or CR or LF, the first of which may not be ':'>
         * <trailing> ::= <Any, possibly *empty*, sequence of octets not including
         *                  NUL or CR or LF>
-        * 
+        *
         * <crlf>     ::= 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));
index 75360e14d9249e0912fda8c06656d7e985aefe01..fc3fd5951c1f4cf0f264e54a04be663a0ee60132 100644 (file)
@@ -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) {
index 5e513685af35fbf53315d806f0f633f238fa3096..7b6fc9d7e70cf8182cf1e08fdb6e5e328363c362 100644 (file)
@@ -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 */
index b8366d761f026b77e75f060ed5d6b42bca16df17..8342490a29f18cea49680f15da98ef76526b9b18 100644 (file)
@@ -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', `')