From 37becdc703e354dcf308bb5e4c48e0a536852291 Mon Sep 17 00:00:00 2001 From: gigirassy Date: Wed, 18 Mar 2026 00:02:23 +0100 Subject: [PATCH] init --- ntfyd.cr | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 ntfyd.cr diff --git a/ntfyd.cr b/ntfyd.cr new file mode 100644 index 0000000..0df1bf8 --- /dev/null +++ b/ntfyd.cr @@ -0,0 +1,62 @@ +require "http/client" +require "json" + +CONFIG = ARGV[0]? || "ntfy.conf" + +struct Msg + include JSON::Serializable + getter event : String + getter message : String? + getter title : String? +end + +def notify(title : String, body : String) + Process.run("notify-send", [title, body], output: STDOUT, error: STDERR) +end + +def handle_stream(url : String) + loop do + begin + HTTP::Client.get(url, headers: HTTP::Headers{ + "Connection" => "keep-alive", + "Cache-Control" => "no-cache", + "Accept" => "application/json" + }) do |res| + io = res.body_io + + while line = io.gets + begin + msg = Msg.from_json(line) + + next unless msg.event == "message" + + title = msg.title || "ntfy" + body = msg.message || "" + + notify(title, body) + + rescue + end + end + end + rescue + sleep 3 + end + end +end + +urls = File.read_lines(CONFIG) + .map(&.strip) + .reject(&.empty?) + .map do |url| + u = url.rstrip('/') + u.ends_with?("/json") ? u : "#{u}/json" + end + +urls.each do |url| + spawn do + handle_stream(url) + end +end + +sleep \ No newline at end of file