an experimental irc client

irc: allow plaintext connections

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>

+38 -7
+28 -7
src/irc.zig
··· 444 444 server: []const u8, 445 445 network_id: ?[]const u8 = null, 446 446 name: ?[]const u8 = null, 447 + tls: bool = true, 447 448 }; 448 449 449 450 pub const Capabilities = struct { ··· 503 504 504 505 pub fn deinit(self: *Client) void { 505 506 self.should_close = true; 506 - _ = self.client.writeEnd(self.stream, "PING zirc\r\n", true) catch |err| { 507 - log.err("couldn't close tls conn: {}", .{err}); 508 - }; 507 + if (self.config.tls) { 508 + _ = self.client.writeEnd(self.stream, "PING zirc\r\n", true) catch |err| { 509 + log.err("couldn't close tls conn: {}", .{err}); 510 + }; 511 + } 509 512 self.stream.close(); 510 513 // id gets allocated in the main thread. We need to deallocate it here if 511 514 // we have one ··· 544 547 } 545 548 } 546 549 550 + pub fn read(self: *Client, buf: []u8) !usize { 551 + switch (self.config.tls) { 552 + true => return self.client.read(self.stream, buf), 553 + false => return self.stream.read(buf), 554 + } 555 + } 556 + 547 557 pub fn readLoop(self: *Client) !void { 548 558 var delay: u64 = 1 * std.time.ns_per_s; 549 559 ··· 582 592 var start: usize = 0; 583 593 584 594 while (true) { 585 - const n = self.client.read(self.stream, buf[start..]) catch |err| { 595 + const n = self.read(buf[start..]) catch |err| { 586 596 if (err != error.WouldBlock) break; 587 597 const now = std.time.milliTimestamp(); 588 598 if (now - last_msg > keep_alive + max_rt) { ··· 599 609 } 600 610 continue; 601 611 }; 612 + log.debug("read {d}", .{n}); 602 613 if (n == 0) continue; 603 614 last_msg = std.time.milliTimestamp(); 604 615 var i: usize = 0; ··· 639 650 640 651 pub fn write(self: *Client, buf: []const u8) !void { 641 652 log.debug("[->{s}] {s}", .{ self.config.name orelse self.config.server, buf[0 .. buf.len - 2] }); 642 - try self.client.writeAll(self.stream, buf); 653 + switch (self.config.tls) { 654 + true => try self.client.writeAll(self.stream, buf), 655 + false => try self.stream.writeAll(buf), 656 + } 643 657 } 644 658 645 659 pub fn connect(self: *Client) !void { 646 - self.stream = try std.net.tcpConnectToHost(self.alloc, self.config.server, 6697); 647 - self.client = try tls.Client.init(self.stream, self.app.bundle, self.config.server); 660 + switch (self.config.tls) { 661 + true => { 662 + self.stream = try std.net.tcpConnectToHost(self.alloc, self.config.server, 6697); 663 + self.client = try tls.Client.init(self.stream, self.app.bundle, self.config.server); 664 + }, 665 + false => { 666 + self.stream = try std.net.tcpConnectToHost(self.alloc, self.config.server, 6667); 667 + }, 668 + } 648 669 649 670 var buf: [4096]u8 = undefined; 650 671
+10
src/lua.zig
··· 60 60 lua.argCheck(lua_type == .string, 1, "expected a string for field 'server'"); 61 61 const server = lua.toString(-1) catch unreachable; 62 62 63 + lua_type = lua.getField(1, "tls"); 64 + const tls: bool = switch (lua_type) { 65 + .nil => true, 66 + .boolean => lua.toBoolean(-1), 67 + else => lua.raiseErrorStr("expected a boolean for field 'tls'", .{}), 68 + }; 69 + // lua.argCheck(lua_type == .boolean, 1, "expected a bool for field 'tls'"); 70 + // const tls = lua.toBoolean(-1) catch unreachable; 71 + 63 72 const cfg: Client.Config = .{ 64 73 .server = server, 65 74 .user = user, 66 75 .nick = nick, 67 76 .password = password, 68 77 .real_name = real_name, 78 + .tls = tls, 69 79 }; 70 80 app.loop.?.postEvent(.{ .connect = cfg }); 71 81 return 0;