summaryrefslogtreecommitdiffstats
path: root/krebs/2configs/news.nix
blob: 639cadb3726d7e9145cacd39e1c3640b1404f572 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
{ config, pkgs, ... }:

{
  services.rss-bridge = {
    enable = true;
    whitelist = [ "*" ];
  };
  services.nginx.virtualHosts = {
    rss-bridge = {
      serverAliases = [
        "rss.r"
      ];
    };
    "brockman.r" = {
      serverAliases = [
        "news.r"
      ];
      locations."/api".extraConfig = ''
        proxy_pass http://127.0.0.1:7777/;
        proxy_pass_header Server;
      '';
      locations."= /graph.html".extraConfig = ''
        alias ${pkgs.fetchurl {
          url = "https://raw.githubusercontent.com/kmein/brockman/05d33c8caaaf6255752f9600981974bb58390851/tools/graph.html";
          sha256 = "0iw2vdzj6kzkix1c447ybmc953lns6z4ap6sr9pcib8bany4g43w";
        }};
      '';
      locations."/".extraConfig = ''
        root /var/lib/brockman;
        index brockman.json;
      '';
      extraConfig = ''
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      '';
    };
  };
  systemd.tmpfiles.rules = [
    "d /var/lib/brockman 1750 brockman nginx -"
    "d /run/irc-api 1750 brockman nginx -"
  ];

  systemd.services.brockman-graph = {
    path = [
      pkgs.graphviz
      pkgs.jq
      pkgs.inotifyTools
    ];
    serviceConfig = {
      ExecStart = pkgs.writers.writeDash "brockman-graph" ''

        while :; do
          graphviz="$(cat /var/lib/brockman/brockman.json \
            | jq -r '
              .bots |
              to_entries |
              map(select(.value.extraChannels|length > 1 )) |
              .[] |
              "\"\(.key)\" -> {\(.value.extraChannels|map("\""+.+"\"")|join(" "))}"
          ')"
          echo "digraph news { $graphviz }" | circo -Tsvg > /var/lib/brockman/graph.svg

          inotifywait -q -e MODIFY /var/lib/brockman/brockman.json
        done
      '';
      User = "brockman";
    };
    wantedBy = [ "multi-user.target" ];
  };

  krebs.ergo.openFilesLimit = 16384;
  systemd.services.brockman.bindsTo = [ "ergo.service" ];
  systemd.services.brockman.serviceConfig.LimitNOFILE = 16384;
  systemd.services.brockman.environment.BROCKMAN_LOG_LEVEL = "DEBUG";
  krebs.brockman = {
    enable = true;
    config = {
      irc.host = "localhost";
      channel = "#all";
      shortener = "http://go.r";
      controller = {
        nick = "brockman";
        extraChannels = [ "#all" ];
      };
      bots = {};
    };
  };

  krebs.reaktor2.api = {
    hostname = "localhost";
    port = "6667";
    nick = "api";
    API.listen = "inet://127.0.0.1:7777";
    plugins = [
      {
        plugin = "register";
        config = {
          channels = [
            "#all"
          ];
        };
      }
    ];
  };
  krebs.reaktor2.news = let
    name = "candyman";
  in {
    hostname = "localhost";
    port = "6667";
    nick = name;
    plugins = [
      {
        plugin = "register";
        config = {
          channels = [
            "#all"
            "#aluhut"
            "#news"
            "#lasstube"
          ];
        };
      }
      {
        plugin = "system";
        config = {
          hooks.PRIVMSG = [
            {
              activate = "match";
              pattern = "^${name}:\\s*(\\S*)(?:\\s+(.*\\S))?\\s*$";
              command = 1;
              arguments = [2];
              commands = {
                add-reddit.filename = pkgs.writeDash "add-reddit" ''
                  set -euf
                  if [ "$#" -ne 1 ]; then
                    echo 'usage: ${name}: add-reddit $reddit_channel'
                    exit 1
                  fi
                  reddit_channel=$(echo "$1" | ${pkgs.jq}/bin/jq -Rr '[match("(\\S+)\\s*";"g").captures[].string][0]')
                  echo "brockman: add r_$reddit_channel http://rss.r/?action=display&bridge=Reddit&context=single&r=$reddit_channel&format=Atom"
                '';
                add-telegram.filename = pkgs.writeDash "add-telegram" ''
                  set -euf
                  if [ "$#" -ne 1 ]; then
                    echo 'usage: ${name}: add-telegram $telegram_user'
                    exit 1
                  fi
                  telegram_user=$(echo "$1" | ${pkgs.jq}/bin/jq -Rr '[match("(\\S+)\\s*";"g").captures[].string][0]')
                  echo "brockman: add t_$telegram_user http://rss.r/?action=display&bridge=Telegram&username=$telegram_user&format=Mrss"
                '';
                add-youtube.filename = pkgs.writeDash "add-youtube" ''
                  set -euf
                  if [ "$#" -ne 1 ]; then
                    echo 'usage: ${name}: add-youtube $nick $channel/video/stream/id'
                    exit 1
                  fi
                  youtube_nick=$(echo "$1" | ${pkgs.jq}/bin/jq -Rr '[match("(\\S+)\\s*";"g").captures[].string][0]')
                  youtube_url=$(echo "$1" | ${pkgs.jq}/bin/jq -Rr '[match("(\\S+)\\s*";"g").captures[].string][1]')
                  if [ ''${#youtube_url} -eq 24 ]; then
                    youtube_id=$youtube_url
                  else
                    youtube_id=$(${pkgs.youtube-dl}/bin/youtube-dl --max-downloads 1 -j "$youtube_url" | ${pkgs.jq}/bin/jq -r '.channel_id')
                  fi
                  echo "brockman: add yt_$youtube_nick http://rss.r/?action=display&bridge=Youtube&context=By+channel+id&c=$youtube_id&duration_min=&duration_max=&format=Mrss"
                '';
                add-twitch.filename = pkgs.writeDash "add-twitch" ''
                  set -euf
                  if [ "$#" -ne 1 ]; then
                    echo 'usage: ${name}: add-twitch $handle'
                    exit 1
                  fi
                  twitch_nick=$(echo "$1" | ${pkgs.jq}/bin/jq -Rr '[match("(\\S+)\\s*";"g").captures[].string][0]')
                  echo "brockman: add twitch_$twitch_nick http://rss.r/?action=display&bridge=Twitch&channel=$twitch_nick&type=all&format=Atom"
                '';
                add-twitter.filename = pkgs.writeDash "add-twitter" ''
                  set -euf
                  if [ "$#" -ne 1 ]; then
                    echo 'usage: ${name}: add-twitter $handle'
                    exit 1
                  fi
                  twitter_nick=$(echo "$1" | ${pkgs.jq}/bin/jq -Rr '[match("(\\S+)\\s*";"g").captures[].string][0]')
                  echo "brockman: add tw_$twitter_nick http://rss.r/?action=display&bridge=Twitch&channel=$twitter_nick&type=all&format=Atom"
                '';
                search.filename = pkgs.writeDash "search" ''
                  set -euf
                  if [ "$#" -ne 1 ]; then
                    echo 'usage: ${name}: search $searchterm'
                    exit 1
                  fi
                  searchterm=$(echo "$1" | ${pkgs.jq}/bin/jq -Rr '[match("(\\S+)\\s*";"g").captures[].string][0]')
                  ${pkgs.curl}/bin/curl -Ss "https://feedsearch.dev/api/v1/search?url=$searchterm&info=true&favicon=false" |
                    ${pkgs.jq}/bin/jq '.[].url'
                '';
              };
            }
          ];
        };
      }
    ];
  };
}