shithub: ircs

Download patch

ref: e57fc06379d7784bf90fadc8ac4440e2d2b2270b
parent: 30d35379c291756e560de78b033c9fc0f3d443b7
author: kemal <kemalinanc8@gmail.com>
date: Sun Oct 3 06:01:48 EDT 2021

ircs: simplify and fix parts of connect()

- tls shouldn't be static and global.
- tls.cert must be freed.
- good idea to have an err goto.
- send the USER command in a way that is more conforming to RFC2812

--- a/main.c
+++ b/main.c
@@ -66,8 +66,6 @@
 static Ioproc *outio;
 static Ioproc *logio;
 
-static TLSconn tls;
-
 static int debug;
 static int timestamps = 1;
 static int usetls;
@@ -707,41 +705,42 @@
 static int
 connect(void)
 {
+	TLSconn tls;
 	UserPasswd *u;
 	char buf[Bufsize];
 	long t;
 	int fd;
 
-	u = nil;
-	if(passwd && usetls){
-		u = auth_getuserpasswd(auth_getkey, "proto=pass service=irc server=%q user=%q", addr, mynick);
+	if(ircfd >= 0)
+		close(ircfd);
+
+	if(passwd){
+		u = auth_getuserpasswd(auth_getkey,
+			"proto=pass service=irc server=%q user=%q", addr, mynick);
 		if(u == nil){
 			perror("auth_getuserpasswd");
 			return -1;
 		}
-	}
-	if(ircfd >= 0)
-		close(ircfd);
+	}else
+		u = nil;
 
 	ircfd = dial(netmkaddr(addr, "tcp", usetls ? "6697" : "6667"), nil, nil, nil);
 	if(ircfd < 0){
 		perror("dial");
-		free(u);
-		return -1;
+		goto err;
 	}
 	if(usetls){
 		memset(&tls, 0, sizeof(tls));
 		fd = tlsClient(ircfd, &tls);
+		free(tls.cert);
 		if(fd < 0){
 			perror("tlsClient");
-			close(ircfd);
-			free(u);
-			return -1;
+			goto err;
 		}
 		ircfd = fd;
 	}
-	t = time(0);
 
+	t = time(0);
 	if(u != nil){
 		snprint(buf, sizeof(buf), "PASS");
 		logsend(buf, t, &mainlog);
@@ -749,7 +748,7 @@
 		ircsend(buf);
 		free(u);
 	}
-	snprint(buf, sizeof(buf), "USER %s foo bar :<nil>", user);
+	snprint(buf, sizeof(buf), "USER %s 0 * :<nil>", user);
 	logsend(buf, t, &mainlog);
 	ircsend(buf);
 
@@ -758,6 +757,10 @@
 	ircsend(buf);
 
 	return 0;
+err:
+	free(u);
+	close(ircfd);
+	return -1;
 }
 
 static void