-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
128 lines (107 loc) · 3.92 KB
/
main.js
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
const fs = require('fs');
const os = require('os');
// get the username of the current user
const username = os.userInfo().username;
console.log('Hello, ' + username + '!');
let filePath = "";
let lastKnownSize = 0;
let latestStateChange = null;
let prevState = null;
// Get the current operating system
const platform = os.platform();
if (platform === 'win32') {
console.log('Running on Windows');
filePath = `C:/Users/${username}/AppData/Roaming/Microsoft/Teams/logs.txt`;
} else if (platform === 'darwin') {
console.log('Running on macOS');
filePath = `/Users/${username}/Library/Application Support/Microsoft/Teams/logs.txt`;
} else if (platform === 'linux') {
console.log('Running on Linux');
filePath = `/home/${username}/.config/Microsoft/Microsoft Teams/logs.txt`;
} else {
console.log(`Unknown platform: ${platform}. You will need to manually specify the path to the log file within the script code.`);
process.exit(1);
}
// Function to extract the new state from the log line
function extractNewState(line) {
const regex = /current state: (\w+)\s->\s(\w+)/;
const match = line.match(regex);
if (match) {
return match[2]; // Extract the new state from the second capture group
}
return null; // Return null if the line does not match the expected pattern
}
//* Startup *//
// Read the initial content of the file
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading file: ${err}`);
return;
}
const lines = data.split('\n');
console.log('Log file loaded, current lines count: ' + lines.length)
lines.forEach(line => {
if (line.includes('current state')) {
const newState = extractNewState(line);
if (newState) {
latestStateChange = newState;
}
}
});
if (latestStateChange) {
console.log(`Latest state change on startup: ${latestStateChange}`);
prevState = latestStateChange;
// Do something with the latest state change here
}
console.log('Watching file for changes...\n')
// Watch the file for changes
fs.watchFile(filePath, (curr, prev) => {
// Check if the file was modified
if (curr.mtime > prev.mtime) {
// Read the updated content since the last known size
fs.open(filePath, 'r', (err, fd) => {
if (err) {
console.error(`Error opening file: ${err}`);
return;
}
fs.fstat(fd, (err, stats) => {
if (err) {
console.error(`Error getting file stats: ${err}`);
fs.close(fd, () => { }); // Close the file descriptor
return;
}
const fileSize = stats.size;
const bufferSize = fileSize - lastKnownSize;
if (bufferSize > 0) {
const buffer = Buffer.alloc(bufferSize);
fs.read(fd, buffer, 0, bufferSize, lastKnownSize, (err, bytesRead, buffer) => {
if (err) {
console.error(`Error reading file: ${err}`);
} else {
const newContent = buffer.toString('utf8');
const lines = newContent.split('\n');
lines.forEach(line => {
if (line.includes('current state')) {
const newState = extractNewState(line);
if (newState) {
latestStateChange = newState;
}
}
});
if (latestStateChange && latestStateChange !== prevState) {
console.log(`Latest state change detected: ${latestStateChange}`);
prevState = latestStateChange;
// Do something with the latest state change here
}
}
fs.close(fd, () => { }); // Close the file descriptor
});
} else {
fs.close(fd, () => { }); // Close the file descriptor
}
lastKnownSize = fileSize; // Update the last known file size
});
});
}
});
});