diff --git a/src/client.zig b/src/client.zig index f6c9268..c2e0fde 100644 --- a/src/client.zig +++ b/src/client.zig @@ -50,19 +50,25 @@ pub const Client = struct { try map.put("ClientVersion", .{ .string = "0.1 (quasselclient)" }); try map.put("ClientDate", .{ .string = "0" }); - try write.add_qvariantmap(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); } pub fn quassel_login(s: *Client, username: []const u8, password: []const u8) !void { + var list = std.ArrayList(u8).init(s.allocator); + var map = std.StringHashMap(qvar.QVariant).init(s.allocator); try map.put("MsgType", .{ .string = "ClientLogin" }); try map.put("User", .{ .string = username }); try map.put("Password", .{ .string = password }); - try write.add_qvariantmap(s.stream.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 ebb4d54..a7ee4ee 100644 --- a/src/main.zig +++ b/src/main.zig @@ -13,5 +13,4 @@ pub fn main() !void { try client.handshake(); try client.quassel_init_packet(); try client.quassel_login("z", "password"); - } diff --git a/src/qtshit/write.zig b/src/qtshit/write.zig index a9f8f5c..78f1925 100644 --- a/src/qtshit/write.zig +++ b/src/qtshit/write.zig @@ -18,15 +18,22 @@ pub fn add_bytearray(writer: anytype, array: std.ArrayList(u8)) !void { try writer.writeAll(array.items); } -pub fn add_string(writer: anytype, str: []const u8) !void { - try writer.writeAll(str); +pub fn add_string(writer: anytype, allocator: *std.mem.Allocator, str: []const u8) !void { + var ut16Str = try allocator.alloc(u16, str.len); + defer allocator.free(ut16Str); + for (str) |character, index| { + ut16Str[index] = std.mem.nativeToBig(u16, @as(u16, character)); + } + + try writer.writeAll(std.mem.sliceAsBytes(ut16Str)); try writer.writeByte('\x00'); + } -pub fn add_stringlist(writer: anytype, strList: std.ArrayList([]const u8)) !void { +pub fn add_stringlist(writer: anytype, allocator: *std.mem.Allocator, strList: std.ArrayList([]const u8)) !void { try add_int(writer, @intCast(u32, strList.items.len)); for (strList.items) |string| { - try add_string(writer, string); + try add_string(writer, allocator, string); } } @@ -48,10 +55,10 @@ pub fn add_qvariant(writer: anytype, allocator: *std.mem.Allocator, variant: qva try add_qvariantmap(writer, allocator, out); }, .string => |out| { - try add_string(writer, out); + try add_string(writer, allocator, out); }, .stringlist => |out| { - try add_stringlist(writer, out); + try add_stringlist(writer, allocator, out); }, .bytearray => |out| { try add_bytearray(writer, out); @@ -69,12 +76,10 @@ pub fn add_qvariantmap(writer: anytype, allocator: *std.mem.Allocator, map: std. var data = std.ArrayList(u8).init(allocator); var writeIterator = map.iterator(); while (writeIterator.next()) |entry| { - try add_string(data.writer(), entry.key); + try add_string(data.writer(), allocator, entry.key); try add_qvariant(data.writer(), allocator, entry.value); } - // lengths o the mesage - try add_int(writer, @intCast(u32, data.items.len) + 4 + 1 + 4); // qvariantmap type try add_int(writer, 8); // 4 // validity? @@ -85,3 +90,13 @@ pub fn add_qvariantmap(writer: anytype, allocator: *std.mem.Allocator, map: std. try writer.writeAll(data.items); return; } + +pub fn add_message(writer: anytype, allocator: *std.mem.Allocator, map: std.StringHashMap(qvar.QVariant)) (@TypeOf(writer).Error || std.os.WriteError || error{OutOfMemory})!void { + var data = std.ArrayList(u8).init(allocator); + try add_qvariantmap(data.writer(), allocator, map); + + try add_int(writer, @intCast(u32, data.items.len) * 8); + + try writer.writeAll(data.items); + return; +}