shithub: mez

Download patch

ref: 102dfb94673a2b45bdfecb43129159058c06284f
parent: 34148b407ec9d5516c7f07958434fe409bb17fcd
author: spew <spew@palas>
date: Wed Jan 22 19:11:32 EST 2025

cleanup

--- a/mez.c
+++ b/mez.c
@@ -4,6 +4,11 @@
 #include <mp.h>
 #include <libsec.h>
 
+typedef struct App App;
+struct App {
+	char *host, *user, *passwd;
+};
+
 void
 usage(void)
 {
@@ -14,7 +19,7 @@
 void
 error(char *s)
 {
-	fprint(2, "%s: fatal error: %s: %r\n", argv0, s);
+	fprint(2, "%s: error: %s: %r\n", argv0, s);
 	exits(s);
 }
 
@@ -30,7 +35,7 @@
 }
 
 void
-vmsg(int fd, char *fmt, va_list v)
+vwmsg(int fd, char *fmt, va_list v)
 {
 	static char buf[8192];
 	char *e;
@@ -43,90 +48,102 @@
 }
 
 void
-msg(int fd, char *fmt, ...)
+wmsg(int fd, char *fmt, ...)
 {
 	va_list v;
 
 	va_start(v, fmt);
-	vmsg(fd, fmt, v);
+	vwmsg(fd, fmt, v);
 }
 
 char*
-rtmsg(int fd, char *fmt, ...)
+rmsg(int fd)
 {
-	static char buf[8192];
 	int bytes;
-	va_list v;
+	static char buf[8192];
 
-	va_start(v, fmt);
-	vmsg(fd, fmt, v);
 	bytes = read(fd, buf, sizeof(buf));
 	buf[bytes] = '\0';
 	return buf;
 }
 
-void
-main(int argc, char **argv)
+char*
+wrmsg(int fd, char *fmt, ...)
 {
-	TLSconn conn;
-	int fd, bytes, i;
-	UserPasswd *up;
+	va_list v;
+
+	va_start(v, fmt);
+	vwmsg(fd, fmt, v);
+	return rmsg(fd);
+}
+
+int
+connect(App *a, int netid)
+{
+	int fd, bytes;
+	char b64[512];
 	uchar buf[512];
-	char *user, *host, b64[512], in[8192];
+	TLSconn *conn;
 
-	user = getenv("user");
-	ARGBEGIN{
-	default:
-		usage();
-	case 'u':
-		user = EARGF(usage());
-		break;
-	}ARGEND
-	if (argc != 1)
-		usage();
-	host = argv[0];
-	up = auth_getuserpasswd(auth_getkey, "proto=pass server=%s service=irc user=%s", host, user);
-	fd = dial(netmkaddr(host, "tcp", "6697"), nil, nil, nil);
+	fd = dial(netmkaddr(a->host, "tcp", "6697"), nil, nil, nil);
 	if(fd == -1)
 		error("Could not connect");
-	fd = tlsClient(fd, &conn);
-	if(fd == -1)
+	conn = emallocz(sizeof(*conn), 1);
+	fd = tlsClient(fd, conn);
+	free(conn);
+	if(fd == -1){
 		error("Could not negotiate tls connection");
+	}
 
-	msg(fd, "CAP LS 302");
-	msg(fd, "NICK %s", user);
-	print(rtmsg(fd, "USER %s 0 * %s", user, user));
-	//:domain.dom CAP * LS :draft/chathistory batch soju.im/account-required draft/read-marker setname draft/pre-away message-tags away-notify soju.im/bouncer-networks-notify soju.im/search account-notify cap-notify server-time invite-notify draft/extended-monitor extended-join multi-prefix soju.im/read extended-monitor chghost account-tag soju.im/bouncer-networks echo-message soju.im/webpush sasl=PLAIN soju.im/no-implicit-names draft/no-implicit-names
+	wmsg(fd, "CAP LS 302");
+	wmsg(fd, "NICK %s", a->user);
+	print(wrmsg(fd, "USER %s 0 * %s", a->user, a->user));
 
-	print(rtmsg(fd, "CAP REQ :sasl"));
-	//:domain.dom CAP * ACK sasl
+	print(wrmsg(fd, "CAP REQ :sasl"));
 
-	print(rtmsg(fd, "AUTHENTICATE PLAIN\r\n"));
-	//AUTHENTICATE +
+	print(wrmsg(fd, "AUTHENTICATE PLAIN\r\n"));
 
-	bytes = snprint((char*)buf, sizeof(buf), "%c%s%c%s", '\0', user, '\0', up->passwd);
-	free(up);
+	bytes = snprint((char*)buf, sizeof(buf), "%c%s%c%s", '\0', a->user, '\0', a->passwd);
 	enc64(b64, sizeof(b64), buf, bytes);
-	print(rtmsg(fd, "AUTHENTICATE %s\r\n", b64));
-	//:domain.dom 903 * :SASL authentication successful
+	print(wrmsg(fd, "AUTHENTICATE %s\r\n", b64));
 
-	print(rtmsg(fd, "CAP END"));
-	//:tlaloc.cbza.org 001 spew :Welcome to soju, spew, etc.
-	for(i = 0; i < 4; ++i){
-		bytes = read(fd, in, sizeof(in));
-		in[bytes] = '\0';
-		print(in);
+	if(netid < 0){
+		wmsg(fd, "CAP END");
+		wmsg(fd, "BOUNCER LISTNETWORKS");
+		return fd;
 	}
+	wmsg(fd, "BOUNCER BIND %d", netid);
+	wmsg(fd, "CAP END");
+	return fd;
+}
 
-	print(rtmsg(fd, "BOUNCER LISTNETWORKS"));
-	//BOUNCER NETWORK 1 etc.
-	for(i = 0; i < 3; ++i){
-		bytes = read(fd, in, sizeof(in));
-		in[bytes] = '\0';
-		print(in);
-	}
+void
+main(int argc, char **argv)
+{
+	App app;
+	UserPasswd *up;
+	int fd;
 
-	// So then I need to make a fresh connection and do the same then and then do BOUNCER BIND before I do CAP END (which will end registration)
+	app.user = getenv("user");
+	ARGBEGIN{
+	default:
+		usage();
+	case 'u':
+		app.user = EARGF(usage());
+		break;
+	}ARGEND
+	if (argc != 1)
+		usage();
+	app.host = argv[0];
+	up = auth_getuserpasswd(auth_getkey, "proto=pass server=%s service=irc user=%s", app.host, app.user);
+	if(up == nil)
+		error("could not get a password");
+	app.passwd = up->passwd;
+	fd = connect(&app, 1);
+	for(int i = 0; i < 100; i++){
+		print(rmsg(fd));
+	}
+	free(up);
 	close(fd);
 	exits(0);
 }