From 86286c9afd6c2538b695308b1613762c8b674453 Mon Sep 17 00:00:00 2001 From: Kitteh Date: Wed, 2 Jun 2021 18:42:46 +0100 Subject: [PATCH] hgoijreiriureo --- src/client.zig | 27 +++++++++++++++----------- src/main.zig | 2 +- src/qtshit/qvariant.zig | 43 ++++++++++++++++++++++++++++++++++++++++- src/qtshit/read.zig | 4 +--- src/qtshit/write.zig | 7 +++++++ 5 files changed, 67 insertions(+), 16 deletions(-) diff --git a/src/client.zig b/src/client.zig index 1c08692..102e36b 100644 --- a/src/client.zig +++ b/src/client.zig @@ -4,7 +4,7 @@ const write = @import("./qtshit/write.zig"); const qvar = @import("./qtshit/qvariant.zig"); fn dumpDebug(name: []const u8, list: std.ArrayList(u8)) !void { - std.debug.print("dumpDebug list len {d}\n", .{list.items.len}); + std.debug.print("dumpDebug list len {d}\n", .{list.items.len}); const file = try std.fs.cwd().createFile( name, @@ -28,7 +28,7 @@ pub const Client = struct { var extra = try read.get_short(s.stream.reader()); var version = try read.get_signed_byte(s.stream.reader()); - std.debug.print("Handshake: flags={d} extra={d} version={d} \n", .{flags, extra, version}); + std.debug.print("Handshake: flags={d} extra={d} version={d} \n", .{ flags, extra, version }); } pub fn quassel_init_packet(s: *Client) !void { var list = std.ArrayList(u8).init(s.allocator); @@ -36,26 +36,32 @@ pub const Client = struct { var map = std.StringHashMap(qvar.QVariant).init(s.allocator); try map.put("MsgType", .{ .string = "ClientInit" }); + try map.put("ProtocolVersion", .{ .int = 10 }); try map.put("ClientVersion", .{ .string = "0.1 (quasselclient)" }); try map.put("ClientDate", .{ .string = "Wed, 02 Jun 2021 17:30:30 +0100" }); try map.put("Features", .{ .int = 0 }); var empty = std.ArrayList([]const u8).init(s.allocator); try map.put("FeatureList", .{ .stringlist = empty }); - try write.add_message(list.writer(), s.allocator, map); + try write.add_message(list.writer(), s.allocator, map); try dumpDebug("ClientInit.bin", list); try s.stream.writer().writeAll(list.items); - std.debug.print("meow \n", .{}); + std.debug.print("\n\nInitPacket: \n", .{}); + + qvar.pretty_print_variant(.{.qmap = map}, 0); + + var size = read.get_int(s.stream.reader()); - std.debug.print("InitResp: size={d} \n", .{size}); - - var varient = read.get_variant(s.stream.reader(), s.allocator); - std.debug.print("InitResp: varient={d} \n", .{varient}); - + //std.debug.print("InitResp: size={d} \n", .{size}); + std.debug.print("\n\nInitResponse: \n", .{}); + var varient = try read.get_variant(s.stream.reader(), s.allocator); + qvar.pretty_print_variant(varient, 0); + //std.debug.print("InitResp: type={s} \n", .{varient.qmap.get("MsgType").?.string}); } + pub fn quassel_login(s: *Client, username: []const u8, password: []const u8) !void { var list = std.ArrayList(u8).init(s.allocator); @@ -65,8 +71,7 @@ pub const Client = struct { try map.put("User", .{ .string = username }); try map.put("Password", .{ .string = password }); - - try write.add_message(list.writer(), s.allocator, map); + try write.add_message(list.writer(), s.allocator, map); try dumpDebug("ClientLogin.bin", list); try s.stream.writer().writeAll(list.items); diff --git a/src/main.zig b/src/main.zig index d640386..4dba731 100644 --- a/src/main.zig +++ b/src/main.zig @@ -7,7 +7,7 @@ const initClient = @import("./client.zig").initClient; pub fn main() !void { const allocator = std.heap.page_allocator; - var sock = try std.net.tcpConnectToHost(allocator, "109.70.144.142", 4242); + var sock = try std.net.tcpConnectToHost(allocator, "127.0.0.1", 64242); var client = initClient(allocator, &sock); try client.handshake(); diff --git a/src/qtshit/qvariant.zig b/src/qtshit/qvariant.zig index 51e4ef3..59c7201 100644 --- a/src/qtshit/qvariant.zig +++ b/src/qtshit/qvariant.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const range = @import("./rangeiter.zig").range; pub const QVariant = union(enum) { int: u32, @@ -50,4 +51,44 @@ pub fn qvariant_type_id(variant: QVariant) !u32 { return 0; }, } -} \ No newline at end of file +} + +pub fn print_indent_level(indentLevel: u64) void { + var iter = range(u64, 0, indentLevel); + while (iter.next()) |i| { + std.debug.print(" ", .{}); + } +} + +pub fn pretty_print_variant(variant: QVariant, indentLevel: u64) void { + print_indent_level(indentLevel); + + switch (variant) { + .byte => |b| { + std.debug.print("Byte: {d}\n", .{b}); + }, + .int => |i| { + std.debug.print("Int: {d}\n", .{i}); + }, + .qmap => |q| { + std.debug.print("QMap:\n", .{}); + var qMapIter = q.iterator(); + while (qMapIter.next()) |v| { + print_indent_level(indentLevel + 1); + std.debug.print("Key: \"{s}\"\n", .{v.key}); + print_indent_level(indentLevel + 1); + std.debug.print("Value:\n", .{}); + pretty_print_variant(v.value, indentLevel + 2); + } + }, + .string => |s| { + std.debug.print("String: \"{s}\"\n", .{s}); + }, + .stringlist => {}, + .bytearray => {}, + .short => |s| { + std.debug.print("Short: \"{d}\"\n", .{s}); + }, + else => {}, + } +} diff --git a/src/qtshit/read.zig b/src/qtshit/read.zig index 152f189..2b519b8 100644 --- a/src/qtshit/read.zig +++ b/src/qtshit/read.zig @@ -38,15 +38,13 @@ pub fn get_string(reader: anytype, allocator: *std.mem.Allocator) ![]u8 { var length = try reader.readIntBig(i32); var chars = @divTrunc(length, 2); -// std.debug.print("get_lenchars: {d} {d} \n", .{length, chars}); - var index: usize = 0; while (true) { if (index == chars) break; const byte = try reader.readIntBig(u16); - try data.append(@intCast(u8, byte)); + try data.append(@truncate(u8, byte)); index += 1; } diff --git a/src/qtshit/write.zig b/src/qtshit/write.zig index 1119908..753b09e 100644 --- a/src/qtshit/write.zig +++ b/src/qtshit/write.zig @@ -37,6 +37,13 @@ pub fn add_stringlist(writer: anytype, allocator: *std.mem.Allocator, strList: s } } +pub fn add_variantlist(writer: anytype, allocator: *std.mem.Allocator, varList: std.ArrayList([]qvar.QVariant)) !void { + try add_int(writer, @intCast(u32, varList.items.len)); + for (varList.items) |var| { + try add_variant(writer, allocator, varList); + } +} + // Call this and Then write your type pub fn add_qvariant_with_id(writer: anytype, type_id: u32) !void { try add_int(writer, type_id);