-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.mjs
55 lines (51 loc) · 1.5 KB
/
index.mjs
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
import fs from "fs";
import notifier from "node-notifier";
import path from "path";
import sqlite3 from "sqlite3";
import sqlite from "sqlite";
import child_process from "child_process";
import util from "util";
const { open } = sqlite;
const HOME = process.env.HOME;
const dbFilePath = path.join(HOME, "/Library/Messages/chat.db");
const walFilePath = path.join(HOME, "/Library/Messages/chat.db-wal");
const SMS_RE = new RegExp(/\s(\d{4,8})/);
var lastNotifiedCode;
async function copyLatestShortcode(db) {
let rows = await db.all(
"SELECT * FROM `message` WHERE service != 'iMessage' ORDER BY date DESC LIMIT 5"
);
for (var i = 0; i < rows.length; i++) {
let row = rows[i];
let match = SMS_RE.exec(row.text);
if (match) {
let code = match[1];
if (code === lastNotifiedCode) {
break;
}
lastNotifiedCode = code;
var proc = child_process.spawn("pbcopy");
proc.stdin.write(code);
proc.stdin.end();
let result = await util.promisify(notifier.notify)({
title: "New code copied",
message: `Code is: ${code}`,
wait: true
});
break;
}
}
}
open({
filename: dbFilePath,
driver: sqlite3.Database,
mode: sqlite3.OPEN_READONLY
}).then(async db => {
console.log("watching", walFilePath);
fs.watchFile(walFilePath, async (curr, prev) => {
console.log(`${walFilePath} file Changed`);
await copyLatestShortcode(db);
});
}).catch((err) => {
console.error("Couldn't open DB file", dbFilePath, err);
});