shithub: irc.myr

Download patch

ref: 1a4d159fa6bf9bf01a7851c28b1a7519ebfb2461
parent: 92dc1eb4051a5be29f6c8342c4139a7d107a1700
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Feb 23 17:54:00 EST 2018

Improve error handling a bit.

--- a/irc.myr
+++ b/irc.myr
@@ -12,7 +12,7 @@
 	const io	: (irc : irc#, fd : std.fd -> void)
 	const curchan	: (irc : irc# -> chan#)
 	const cursrv	: (irc : irc# -> std.option(server#))
-	const send	: (irc : irc#, srv : server#, fmt : byte[:], args : ... -> void)
+	const send	: (irc : irc#, srv : server#, fmt : byte[:], args : ... -> bool)
 	const status	: (irc : irc#, chan : chan#, fmt : byte[:], args : ... -> void)
 	const chancycle	: (irc : irc#, delta : std.size -> void)
 	const handshake	: (irc : irc#, srv : server# -> bool)
@@ -260,6 +260,7 @@
 	if srv.death == 0
 		srv.death = std.now()
 		status(irc, irc.self, "{} closed: {}", srv.ds, msg)
+		std.close(srv.fd)
 	;;
 }
 
@@ -588,14 +589,17 @@
 }
 
 const send = {irc, srv, fmt, args
-	var s, ap
+	var s, ap, r
 
+	r = true
 	ap = std.vastart(&args)
 	s = std.fmtv(fmt, &ap)
 	if !writeall(srv.fd, s)
 		closed(irc, srv, "failed writing")
+		r = false
 	;;
 	std.slfree(s)
+	-> r
 }
 
 const message = {irc, msg
@@ -606,9 +610,10 @@
 		c = curchan(irc)
 		if c == irc.self
 			status(irc, irc.self, "can't send to status channel")
-		else
-			send(irc, s, "PRIVMSG {} :{}\r\n", c.name, msg)
+		elif send(irc, s, "PRIVMSG {} :{}\r\n", c.name, msg)
 			chanmsg(irc, s, c, s.nick, msg)
+		else
+			status(irc, c, "disconnected")
 		;;
 	| `std.None:
 		status(irc, irc.self, "no connected server")
--