]> Nishi Git Mirror - libw3.git/commitdiff
irc client
authornishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Wed, 21 Feb 2024 16:09:32 +0000 (16:09 +0000)
committernishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Wed, 21 Feb 2024 16:09:32 +0000 (16:09 +0000)
git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@246 d27a3e52-49c5-7645-884c-6793ebffc270

Library/Core.c
Library/IRC.c
Library/W3IRC.h
W3Version.h.m4

index f120cf323dbb0a537b7084feb7a83d5286a5daec..12c1fb122fda8172f1cdc15915600e4555da3203 100644 (file)
@@ -94,6 +94,8 @@ 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){
+               ssl = true;
        }
        w3->props = NULL;
        w3->method = NULL;
index ff9b4eba8622d137f3bce5533ca254a556a9b456..10fd975c4ec900ca30ac232261169a6ddfff1691 100644 (file)
 extern int strcasecmp(const char* s1, const char* s2);
 #endif
 
-void __W3_IRC_Request(struct W3* w3) { __W3_Debug("LibW3-IRC", "Sending the request"); }
+void __W3_IRC_Request(struct W3* w3) {
+       if(__W3_Get_Prop(w3, "IRC_USERNAME") == NULL || __W3_Get_Prop(w3, "IRC_NICKNAME") == NULL) {
+               __W3_Debug("LibW3-IRC", "Set the username/nickname at least");
+               void* funcptr = __W3_Get_Event(w3, "error");
+               if(funcptr != NULL) {
+                       void (*func)(struct W3*, const char*) = (void (*)(struct W3*, const char*))funcptr;
+                       func(w3, "did-not-auth");
+               }
+               return;
+       }
+       char* username = __W3_Get_Prop(w3, "IRC_USERNAME");
+       char* nickname = __W3_Get_Prop(w3, "IRC_NICKNAME");
+       char* realname = __W3_Get_Prop(w3, "IRC_REALNAME");
+       char* servername = __W3_Get_Prop(w3, "IRC_SERVERNAME");
+       char* hostname = __W3_Get_Prop(w3, "IRC_HOSTNAME");
+       if(realname == NULL) realname = username;
+       if(servername == NULL) servername = username;
+       if(hostname == NULL) hostname = username;
+       __W3_Auto_Write(w3, "USER ", 5);
+       __W3_Auto_Write(w3, username, strlen(username));
+       __W3_Auto_Write(w3, " ", 1);
+       __W3_Auto_Write(w3, hostname, strlen(hostname));
+       __W3_Auto_Write(w3, " ", 1);
+       __W3_Auto_Write(w3, servername, strlen(servername));
+       __W3_Auto_Write(w3, " ", 1);
+       __W3_Auto_Write(w3, realname, strlen(realname));
+       __W3_Auto_Write(w3, "\r\n", 2);
+       __W3_Auto_Write(w3, "NICK ", 5);
+       __W3_Auto_Write(w3, nickname, strlen(nickname));
+       __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
+                        */
+       /* <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
+        */
+       char* prefix = malloc(1);
+       prefix[0] = 0;
+       char* command = malloc(1);
+       command[0] = 0;
+       char* params = malloc(1);
+       params[0] = 0;
+       char* cbuf = malloc(2);
+       cbuf[1] = 0;
+       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] == ':'){
+                                       phase = 1;
+                               }
+                       }else if(phase == 1){
+                               if(buf[i] == ' '){
+                                       phase = 2;
+                               }else{
+                                       cbuf[0] = buf[i];
+                                       char* tmp = prefix;
+                                       prefix = __W3_Concat(tmp, cbuf);
+                                       free(tmp);
+                               }
+                       }else if(phase == 2){
+                               if(buf[i] == ' '){
+                                       phase = 3;
+                               }else{
+                                       cbuf[0] = buf[i];
+                                       char* tmp = command;
+                                       command = __W3_Concat(tmp, cbuf);
+                                       free(tmp);
+                               }
+                       }else if(phase == 3){
+                               if(buf[i] == '\n'){
+                                       phase = 0;
+
+                                       /* Parse commands here */
+                                       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] == ':'){
+                                                               username = params;
+                                                               content = params + j + 1;
+                                                               break;
+                                                       }else if(params[j] == ' '){
+                                                               params[j] = 0;
+                                                       }
+                                               }
+                                               if(username != NULL && content != NULL){
+                                                       if(content[0] == 1 && content[strlen(content) - 1] == 1){
+                                                               /* CTCP Request */
+                                                       }
+                                               }
+                                       }
+
+                                       prefix = malloc(1);
+                                       prefix[0] = 0;
+                                       command = malloc(1);
+                                       command[0] = 0;
+                                       params = malloc(1);
+                                       params[0] = 0;
+                                       cbuf = malloc(2);
+                                       cbuf[1] = 0;
+                               }else if(buf[i] != '\r'){
+                                       cbuf[0] = buf[i];
+                                       char* tmp = params;
+                                       params = __W3_Concat(tmp, cbuf);
+                                       free(tmp);
+                               }
+                       }
+               }
+       }
+       free(cbuf);
+       free(params);
+       free(command);
+       free(prefix);
+       free(buf);
+}
+
+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_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_Nickname(struct W3* w3, const char* nickname){
+       __W3_Add_Prop(w3, "IRC_NICKNAME", nickname);
+}
index 2269b9d34fc5d99dc5d828863bba191f9f24a67b..bdd4b8bf6fb4ef148be4d323d0cac0f4ce51eb9e 100644 (file)
@@ -9,6 +9,11 @@ extern "C" {
 #include "W3Core.h"
 
 void __W3_IRC_Request(struct W3* w3);
+void W3_IRC_Set_Username(struct W3* w3, const char* username);
+void W3_IRC_Set_Hostname(struct W3* w3, const char* hostname);
+void W3_IRC_Set_Realname(struct W3* w3, const char* realname);
+void W3_IRC_Set_Servername(struct W3* w3, const char* servername);
+void W3_IRC_Set_Nickname(struct W3* w3, const char* nickname);
 void W3_IRC_Send_Request(struct W3* w3);
 void W3_IRC_Disconnect(struct W3* w3);
 
index 3e483cc2a8d32aa6e31323da87dfa90904971b8c..64c581dda384cab757f98eabc67a30b501b8e84b 100644 (file)
@@ -6,7 +6,7 @@
 extern "C" {
 #endif
 
-#define LIBW3_VERSION "2.17" \
+#define LIBW3_VERSION "2.17A" \
 SUFFIX
 
 ifdef(`HTTP_SUPPORT', `#define LIBW3_HTTP_SUPPORT', `')