summaryrefslogtreecommitdiff
path: root/modules/statusbot/statusbot.js
blob: 0fd9ce59a57cf9a3ce029603b87e70113c2b1f00 (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
// Format of the required matrix.json file:
// {
//   "user_id": "@statusbot:tomsmeding.com",
//   "password": "PASSWORD OF THE USER",
//   "room_id": "!ROOMID:tomsmeding.com"
// }

// Furthermore, accounts.json is required in the standard format ([[user, pass]])

// API:
// POST /statusbot
// { "sender": "who are you", "text": "content of the status message" }

// State in this file: the above matrix.json plus:
// {
//   config: the above matrix.json,
//   home_server: "tomsmeding.com",  // nominal server in the user id
//   matrix_server: "matrix.tomsmeding.com",  // actual matrix server address, without port
//   access_token: "some access token",
//   req_counter: 1  // counter for transaction requests to the matrix server
// }

// Saves persist state under key "access_tokens":
// {
//   "@statusbot:tomsmeding.com|!ROOMID:tomsmeding.com": ["access token", req_counter: int]
// }

const cmn = require("../$common.js");
const fs = require("fs");
const https = require("https");
const URL = require("url");
const bodyParser = require("body-parser");

let moddir = null;

const persist = require("node-persist").create({
  dir: cmn.persistdir + "/statusbot",
  continuous: false,
  interval: false
});
persist.initSync();

let persistTokens = persist.getItemSync("access_tokens");
if (!persistTokens) {
  persistTokens = {};
  persist.setItemSync("access_tokens", persistTokens);
}

// cb: (status: int, body: string) => ()
function fetch(method, headers, hostname, path, data, cb) {
  const req = https.request({method, headers, hostname, path}, res => {
    let body = "";
    res.on("data", data => { body += data; });
    res.on("end", () => cb(res.statusCode, body));
  });
  req.on("error", () => cb(null, null))
  req.write(data);
  req.end();
}

// cb: (state) => ()
function augmentConfig(config, cb) {
  const m = config.user_id.match(/^@[^:]+:(.*)$/);
  if (!m) {
    throw new Error("statusbot: Cannot parse matrix ID (must be in @user:domain format)");
  }
  const home_server = m[1];

  const pkey = `${config.user_id}|${config.room_id}`
  let access_token = undefined;
  let req_counter = undefined;
  if (pkey in persistTokens) {
    [access_token, req_counter] = persistTokens[pkey];
  }

  fetch("GET", {}, home_server, "/.well-known/matrix/server", "", (status, body) => {
    if (status != 200) {
      throw new Error(`statusbot: Failed getting https://${home_server}/.well-known/matrix/server`);
    }
    const mserver = JSON.parse(body)["m.server"];
    const m = mserver.match(/^([^:]*)(:443)?$/);
    if (!m) {
      throw new Error(`statusbot: Matrix server port not 443 (sorry): <${mserver}>`);
    }
    const matrix_server = m[1];
    cb({config, home_server, matrix_server, access_token, req_counter});
  });
}

function updatePersist(state) {
  persistTokens[`${state.config.user_id}|${state.config.room_id}`] = [state.access_token, state.req_counter];
  persist.setItemSync("access_tokens", persistTokens);
}

// Sets access token in state.
// cb: (success: bool, body: string) => ()
function matrixLogin(state, cb) {
  const data = JSON.stringify({
    type: "m.login.password",
    identifier: {type: "m.id.user", user: state.config.user_id},
    password: state.config.password,
  });
  fetch("POST", {}, state.matrix_server, "/_matrix/client/v3/login", data, (status, body) => {
    if (status != 200) { cb(false, body); return; }
    try {
      const response = JSON.parse(body);
      state.access_token = response.access_token;
      state.req_counter = 1;
      updatePersist(state);
      cb(true, body);
    } catch (e) {
      cb(false, body);
    }
  });
}

// cb: (status: int, body: string) => ()
// Status 401: access token invalid
// Status 200: success
// Anything else: ? (see body?)
function matrixSendMsg(state, text, cb) {
  if (state.access_token == undefined) { cb(401); return; }

  const headers = {Authorization: `Bearer ${state.access_token}`};
  const url = `/_matrix/client/v3/rooms/${state.config.room_id}/send/m.room.message/${state.req_counter}`;
  state.req_counter++;
  updatePersist(state);  // req_counter changed

  const data = JSON.stringify({
    msgtype: "m.text",
    body: text,
  });

  fetch("PUT", headers, state.matrix_server, url, data, (status, body) => {
    cb(status, body);
  });
}

// cb: () => ()
function logFailure(message, cb) {
  fs.appendFile(moddir + "/faillog.txt", `[${new Date().toISOString()}] ${message}\n`, err => {
    if (err) console.error(err);
    cb();
  });
}

// Tries to send a message, trying login if the access token is invalid.
// cb: (success: bool) => ()
function matrixSendMsgLogin(state, text, cb) {
  matrixSendMsg(state, text, (status, body) => {
    switch (status) {
      case 200:
        cb(true);
        break;

      case 401:
        matrixLogin(state, (success, body) => {
          if (!success) {
            logFailure(`Failed to log in: ${body}`, () => cb(false));
            return;
          }

          matrixSendMsg(state, text, (status, body) => {
            switch (status) {
              case 200: cb(true); return;
              case 401: logFailure(`401 even after login: ${body}`, () => cb(false)); break;
              default: logFailure(`Failed to send message: ${body}`, () => cb(false)); break;
            }
          });
        });
        break;

      default:
        logFailure(`Failed to send message: ${body}`, () => cb(false));
        break;
    }
  })
}

module.exports = function(app, io, _moddir) {
  moddir = _moddir;

  const config = require("./matrix.json");
  const accounts = require("./accounts.json");

  augmentConfig(config, state => {
    app.post("/statusbot", bodyParser.json(), cmn.authgen(accounts), (req, res) => {
      if (typeof req.body.sender != "string" || typeof req.body.text != "string") {
        return res.sendStatus(400);
      }
      matrixSendMsgLogin(state, `[${req.body.sender}] ${req.body.text}`, success => {
        if (success) res.sendStatus(200);
        else res.sendStatus(503);  // service unavailable
      });
    });
  });
};