mirror of
https://github.com/dadada/inbox2matrix.git
synced 2025-06-07 17:43:57 +02:00
Add matrix bot and temporary inbox refresh
This commit is contained in:
parent
25b543cc57
commit
1579a8022d
2 changed files with 95 additions and 11 deletions
103
inbox2matrix.ts
103
inbox2matrix.ts
|
@ -1,16 +1,32 @@
|
||||||
import process = require('process');
|
|
||||||
import Imap = require('imap');
|
import Imap = require('imap');
|
||||||
import { inspect } from 'util';
|
|
||||||
import config = require('config');
|
import config = require('config');
|
||||||
|
import matrix = require("matrix-js-sdk");
|
||||||
|
|
||||||
let account = config.get('account');
|
let accounts = config.get('accounts');
|
||||||
|
|
||||||
var imap = new Imap(account);
|
let myUserId = accounts.matrix.user;
|
||||||
|
let myRoom = accounts.matrix.room;
|
||||||
|
|
||||||
|
let client = matrix.createClient({
|
||||||
|
baseUrl: accounts.matrix.baseUrl,
|
||||||
|
accessToken: accounts.matrix.accessToken,
|
||||||
|
userId: myUserId
|
||||||
|
});
|
||||||
|
|
||||||
|
var imap = new Imap(accounts.imap);
|
||||||
|
|
||||||
function openInbox(cb) {
|
function openInbox(cb) {
|
||||||
imap.openBox('INBOX', true, cb);
|
imap.openBox('INBOX', false, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
client.on("RoomMember.membership", function(event, member) {
|
||||||
|
if (member.membership === "invite" && member.userId === myUserId) {
|
||||||
|
client.joinRoom(member.roomId).done(function() {
|
||||||
|
console.log("Auto-joined %s", member.roomId);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
imap.once('ready', function() {
|
imap.once('ready', function() {
|
||||||
openInbox(function(err, box) {
|
openInbox(function(err, box) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
@ -18,14 +34,69 @@ imap.once('ready', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function fetchAndReport(results) {
|
||||||
|
let fetched = imap.fetch(results, {
|
||||||
|
bodies: ['HEADER.FIELDS (FROM)', 'HEADER.FIELDS (SUBJECT)'],
|
||||||
|
markSeen: true
|
||||||
|
});
|
||||||
|
|
||||||
|
fetched.on('message', (msg, seqno) => {
|
||||||
|
let buffer = '';
|
||||||
|
|
||||||
|
msg.on('body', (stream, info) => {
|
||||||
|
stream.on('data', (chunk) => {
|
||||||
|
buffer += chunk.toString('utf-8').replace(/(\r\n\r\n|\n|\r\n|\r)/g, "\n");
|
||||||
|
});
|
||||||
|
stream.once('end', () => {
|
||||||
|
console.log('Buffer: %s', buffer);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
msg.once('end', () => {
|
||||||
|
let content = {
|
||||||
|
'body': buffer,
|
||||||
|
'msgtype': 'm.text'
|
||||||
|
};
|
||||||
|
|
||||||
|
client.sendEvent(myRoom, "m.room.message", content, "", (err, res) => {
|
||||||
|
console.log(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
fetched.once('error', (err) => {
|
||||||
|
console.log(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
fetched.once('end', function() {
|
||||||
|
console.log('Done fetching all messages!');
|
||||||
|
imap.end();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function processUnseen() {
|
||||||
|
imap.search(['UNSEEN'], (err, results) => {
|
||||||
|
if (err) console.log(err);
|
||||||
|
else {
|
||||||
|
try {
|
||||||
|
fetchAndReport(results);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
imap.once('mail', (numNewMsgs : number) => {
|
imap.once('mail', (numNewMsgs : number) => {
|
||||||
console.log(`You have ${numNewMsgs} mail`);
|
console.log(`You have ${numNewMsgs} mail`);
|
||||||
|
processUnseen();
|
||||||
});
|
});
|
||||||
|
|
||||||
imap.once('update', (seqno : number, info : object) => {
|
imap.once('update', (seqno: number) => {
|
||||||
console.log(info);
|
console.log('Some flags changed externally');
|
||||||
|
processUnseen();
|
||||||
});
|
});
|
||||||
|
|
||||||
imap.once('error', function(err) {
|
imap.once('error', function(err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
imap.end();
|
imap.end();
|
||||||
|
@ -36,3 +107,15 @@ imap.once('end', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
imap.connect();
|
imap.connect();
|
||||||
|
|
||||||
|
client.startClient({initialSyncLimit: 200});
|
||||||
|
|
||||||
|
client.once('sync', function(state, prevState, res) {
|
||||||
|
if(state === 'PREPARED') {
|
||||||
|
console.log("prepared");
|
||||||
|
} else {
|
||||||
|
console.log(state);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
setInterval(processUnseen, 5 * 60 * 1000);
|
||||||
|
|
|
@ -4,8 +4,9 @@
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "inbox2matrix.js",
|
"main": "inbox2matrix.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"config": "^3.0.1",
|
"config": "^3.1.0",
|
||||||
"imap": "^0.8.19",
|
"imap": "^0.8.19",
|
||||||
|
"matrix-js-sdk": "^1.1.0-rc.1",
|
||||||
"process": "^0.11.10"
|
"process": "^0.11.10"
|
||||||
},
|
},
|
||||||
"devDependencies": {},
|
"devDependencies": {},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue