-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
141 lines (130 loc) · 4.27 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
{
outputs = {
self,
nixpkgs,
flake-utils,
}:
flake-utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs {inherit system;};
ruby = pkgs.ruby;
in rec {
formatter = pkgs.alejandra;
packages.default = pkgs.writeScriptBin "furpoll" ''
#!/usr/bin/env ruby
URL = "https://www.furaffinity.net/msg/pms/"
COOKIE = File.read(ARGV.shift).strip
SUBJECT = ARGV.shift
FROM = ARGV.shift
TO = ARGV.shift
out = `${pkgs.curl}/bin/curl -s -H"Cookie: #{COOKIE}" #{URL}`
msg = []
if out =~ /class="no-sub"/
msg << "Need login again."
else
count = out.scan(/note-unread/).count
if count > 0
msg << "You've got mail! -- https://www.furaffinity.net/msg/pms/"
end
count = out.scan(/"\d+ Comment Notifications?"/).count
if count > 0
msg << "You've got a comment reply! -- https://www.furaffinity.net/msg/others/#comments"
end
end
if msg.any?
# This can surely not backfire.
IO.popen(["/run/wrappers/bin/sendmail", "-t"], "r+") do |f|
f.puts <<~OUT
Subject: #{SUBJECT}
From: #{FROM}
To: #{TO}
#{msg.join("\n")}
OUT
f.close_write
print f.read
end
end
'';
devShells.default = packages.default;
nixosModules.default = {
config,
lib,
pkgs,
...
}: let
cfg = config.services.furpoll;
inherit (lib) mkIf mkEnableOption mkOption types;
in {
options.services.furpoll = {
enable = mkEnableOption "Enable the furpoll FurAffinity poller";
package = mkOption {
type = types.package;
description = "Package to use for furpoll (defaults to this flake's).";
default = self.packages.${system}.default;
};
user = mkOption {
type = types.str;
description = "User account to run furpoll as (defaults to 'furpoll', which it will create).";
default = "furpoll";
};
group = mkOption {
type = types.str;
description = "Group user account belongs to (defaults to 'furpoll', which it will create).";
default = "furpoll";
};
cookieFile = mkOption {
type = types.path;
description = "Path to file with cookie value to authenticate with FurAffinity.";
};
calendar = mkOption {
type = types.str;
description = "systemd calendar events to fire on.";
default = "*-*-* 1,6,8,19,22:07:00";
};
subject = mkOption {
type = types.str;
default = "furpoll";
description = "Subject: header in emails sent.";
};
from = mkOption {
type = types.str;
description = "From: header in emails sent.";
};
to = mkOption {
type = types.str;
description = "To: header in emails sent.";
};
};
config = mkIf (cfg.enable) (let
ruby = "${pkgs.ruby}/bin/ruby";
furpoll = "${cfg.package}/bin/furpoll";
in {
users.groups.furpoll = mkIf (cfg.group == "furpoll") {};
users.users.furpoll = mkIf (cfg.user == "furpoll") {
description = "furpoll user";
group = cfg.group;
isSystemUser = true;
};
systemd.services.furpoll = {
script = ''
set -eu
# NO CLAIMS ARE MADE AS TO THE SUITABILITY OF THE FOLLOWING
# INTERPOLATIONS FOR ANY PURPOSE, EVEN MERCHANTABILITY(!!).
${ruby} -Eutf-8 ${furpoll} "${cfg.cookieFile}" "${cfg.subject}" "${cfg.from}" "${cfg.to}"
'';
serviceConfig = {
Type = "oneshot";
User = cfg.user;
};
};
systemd.timers.furpoll = {
wantedBy = ["timers.target"];
timerConfig = {
Unit = "furpoll.service";
OnCalendar = cfg.calendar;
Persistent = true;
};
};
});
};
});
}