Skip to content

Commit

Permalink
re-enable send ban unban
Browse files Browse the repository at this point in the history
and also remove the dot from the bork dir inside the config dir
  • Loading branch information
kristoff-it committed Jan 16, 2024
1 parent af07f7f commit b30496f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 65 deletions.
17 changes: 8 additions & 9 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn borkStart(alloc: std.mem.Allocator, config: BorkConfig, token: []const u8) !v
var ch = Channel(Event).init(&buf);

var remote_server: remote.Server = undefined;
remote_server.init(alloc, &ch) catch |err| {
remote_server.init(alloc, config, token, &ch) catch |err| {
switch (err) {
// error.AddressInUse => {
// std.debug.print(
Expand Down Expand Up @@ -314,11 +314,10 @@ fn getConfigAndToken(gpa: std.mem.Allocator, check_token: bool) !ConfigAndToken
try folders.open(gpa, .executable_dir, .{}) orelse
std.fs.cwd();

// Ensure existence of .bork/
try config_base.makePath(".bork");
try config_base.makePath("bork");

const config: BorkConfig = config: {
const file = config_base.openFile(".bork/config.json", .{}) catch |err| switch (err) {
const file = config_base.openFile("bork/config.json", .{}) catch |err| switch (err) {
else => return err,
error.FileNotFound => break :config try createConfig(gpa, config_base),
};
Expand All @@ -335,7 +334,7 @@ fn getConfigAndToken(gpa: std.mem.Allocator, check_token: bool) !ConfigAndToken
};

const token: []const u8 = token: {
const file = config_base.openFile(".bork/token.secret", .{}) catch |err| switch (err) {
const file = config_base.openFile("bork/token.secret", .{}) catch |err| switch (err) {
else => return err,
error.FileNotFound => {
break :token try createToken(gpa, config_base, .new);
Expand Down Expand Up @@ -477,7 +476,7 @@ fn createConfig(
};

// create the config file
var file = try config_base.createFile(".bork/config.json", .{});
var file = try config_base.createFile("bork/config.json", .{});
try std.json.stringify(result, .{}, file.writer());
return result;
}
Expand Down Expand Up @@ -544,7 +543,7 @@ fn createToken(
std.os.exit(1);
}

var token_file = try config_base.createFile(".bork/token.secret", .{});
var token_file = try config_base.createFile("bork/token.secret", .{});
defer token_file.close();

try token_file.writer().print("{s}\n", .{tok});
Expand Down Expand Up @@ -573,10 +572,10 @@ fn setupLogging(gpa: std.mem.Allocator) !void {
try folders.open(gpa, .executable_dir, .{}) orelse
std.fs.cwd();

try cache_base.makePath(".bork");
try cache_base.makePath("bork");

const log_name = if (options.local) "bork-local.log" else "bork.log";
const log_path = try std.fmt.allocPrint(gpa, ".bork/{s}", .{log_name});
const log_path = try std.fmt.allocPrint(gpa, "bork/{s}", .{log_name});

log_file = try cache_base.createFile(log_path, .{ .truncate = false });
const end = try log_file.?.getEndPos();
Expand Down
118 changes: 62 additions & 56 deletions src/remote/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub const Event = union(enum) {
},
};

config: BorkConfig,
token: []const u8,
listener: std.net.StreamServer,
alloc: std.mem.Allocator,
ch: *Channel(GlobalEventUnion),
Expand All @@ -30,9 +32,13 @@ thread: std.Thread,
pub fn init(
self: *Server,
alloc: std.mem.Allocator,
config: BorkConfig,
token: []const u8,
ch: *Channel(GlobalEventUnion),
) !void {
self.alloc = alloc;
self.config = config;
self.token = token;
self.ch = ch;

const tmp_dir_path = try folders.getPath(alloc, .cache) orelse "/tmp";
Expand Down Expand Up @@ -87,24 +93,24 @@ fn handle(self: *Server, stream: std.net.Stream) !void {
};
defer std.log.debug("remote cmd: {s}", .{cmd});

// if (std.mem.eql(u8, cmd, "SEND")) {
// const msg = stream.reader().readUntilDelimiterAlloc(self.alloc, '\n', 4096) catch |err| {
// std.log.debug("remote could read: {}", .{err});
// return;
// };
// defer self.alloc.free(msg);

// std.log.debug("remote msg: {s}", .{msg});

// // Since sending the message from the main connection
// // makes it so that twitch doesn't echo it back, we're
// // opening a one-off connection to send the message.
// // This way we don't have to implement locally emote
// // parsing.
// var twitch_conn = Network.connect(self.alloc, self.config.nick, self.token) catch return;
// defer twitch_conn.close();
// twitch_conn.writer().print("PRIVMSG #{s} :{s}\n", .{ self.config.nick, msg }) catch return;
// }
if (std.mem.eql(u8, cmd, "SEND")) {
const msg = stream.reader().readUntilDelimiterAlloc(self.alloc, '\n', 4096) catch |err| {
std.log.debug("remote could read: {}", .{err});
return;
};
defer self.alloc.free(msg);

std.log.debug("remote msg: {s}", .{msg});

// Since sending the message from the main connection
// makes it so that twitch doesn't echo it back, we're
// opening a one-off connection to send the message.
// This way we don't have to implement locally emote
// parsing.
var twitch_conn = Network.connect(self.alloc, self.config.nick, self.token) catch return;
defer twitch_conn.close();
twitch_conn.writer().print("PRIVMSG #{s} :{s}\n", .{ self.config.nick, msg }) catch return;
}

if (std.mem.eql(u8, cmd, "QUIT")) {
self.ch.put(GlobalEventUnion{ .remote = .quit });
Expand All @@ -118,44 +124,44 @@ fn handle(self: *Server, stream: std.net.Stream) !void {
self.ch.put(GlobalEventUnion{ .remote = .{ .links = stream } });
}

// if (std.mem.eql(u8, cmd, "BAN")) {
// const user = stream.reader().readUntilDelimiterAlloc(self.alloc, '\n', 4096) catch |err| {
// std.log.debug("remote could read: {}", .{err});
// return;
// };

// defer self.alloc.free(user);

// std.log.debug("remote msg: {s}", .{user});

// // Since sending the message from the main connection
// // makes it so that twitch doesn't echo it back, we're
// // opening a one-off connection to send the message.
// // This way we don't have to implement locally emote
// // parsing.
// var twitch_conn = Network.connect(self.alloc, self.config.nick, self.token) catch return;
// defer twitch_conn.close();
// twitch_conn.writer().print("PRIVMSG #{s} :/ban {s}\n", .{ self.config.nick, user }) catch return;
// }

// if (std.mem.eql(u8, cmd, "UNBAN")) {
// const user = stream.reader().readUntilDelimiterAlloc(self.alloc, '\n', 4096) catch |err| {
// std.log.debug("remote could read: {}", .{err});
// return;
// };
// defer self.alloc.free(user);

// std.log.debug("remote msg: {s}", .{user});

// // Since sending the message from the main connection
// // makes it so that twitch doesn't echo it back, we're
// // opening a one-off connection to send the message.
// // This way we don't have to implement locally emote
// // parsing.
// var twitch_conn = Network.connect(self.alloc, self.config.nick, self.token) catch return;
// defer twitch_conn.close();
// twitch_conn.writer().print("PRIVMSG #{s} :/ban {s}\n", .{ self.config.nick, user }) catch return;
// }
if (std.mem.eql(u8, cmd, "BAN")) {
const user = stream.reader().readUntilDelimiterAlloc(self.alloc, '\n', 4096) catch |err| {
std.log.debug("remote could read: {}", .{err});
return;
};

defer self.alloc.free(user);

std.log.debug("remote msg: {s}", .{user});

// Since sending the message from the main connection
// makes it so that twitch doesn't echo it back, we're
// opening a one-off connection to send the message.
// This way we don't have to implement locally emote
// parsing.
var twitch_conn = Network.connect(self.alloc, self.config.nick, self.token) catch return;
defer twitch_conn.close();
twitch_conn.writer().print("PRIVMSG #{s} :/ban {s}\n", .{ self.config.nick, user }) catch return;
}

if (std.mem.eql(u8, cmd, "UNBAN")) {
const user = stream.reader().readUntilDelimiterAlloc(self.alloc, '\n', 4096) catch |err| {
std.log.debug("remote could read: {}", .{err});
return;
};
defer self.alloc.free(user);

std.log.debug("remote msg: {s}", .{user});

// Since sending the message from the main connection
// makes it so that twitch doesn't echo it back, we're
// opening a one-off connection to send the message.
// This way we don't have to implement locally emote
// parsing.
var twitch_conn = Network.connect(self.alloc, self.config.nick, self.token) catch return;
defer twitch_conn.close();
twitch_conn.writer().print("PRIVMSG #{s} :/ban {s}\n", .{ self.config.nick, user }) catch return;
}

if (std.mem.eql(u8, cmd, "AFK")) {
const reader = stream.reader();
Expand Down

0 comments on commit b30496f

Please sign in to comment.