-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
71 lines (56 loc) · 2.17 KB
/
index.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
const irc = require('irc');
const OrbitDB = require('orbit-db');
const Post = require('ipfs-post');
const Ipfs = require('ipfs-api');
// Usage:
// node index.js <botname> <channel>
const botName = process.argv[2] || 'MrsOrbit'
const channel = process.argv[3] || 'testchannel'
const ipfs = new Ipfs();
let orbitdb;
let db = {};
const ircClient = new irc.Client('irc.freenode.net', botName, {
channels: ["#" + channel],
});
OrbitDB.connect('178.62.241.75:3333', botName, '', ipfs)
.then((res) => orbitdb = res);
const sanitizeChannelName = (ircChannel) => {
let arr = ircChannel.split("")
arr.shift()
return arr.join('');
}
// writing message from irc into orbit db
const createMessage = (ipfs, message, from, to) => {
const sanitizedChannel = sanitizeChannelName(to)
const data = {
content: message,
from: from
};
// adds a message to the db
Post.create(ipfs, Post.Types.Message, data)
.then((post) => db[sanitizedChannel].add(post.Hash))
.catch(e => console.log(e))
}
ircClient.addListener('join', (channelName, nick, message) => {
console.log(nick + ' joined ' + channelName, message)
if(nick === botName) {
// notifications in orbit and irc that connection has happened:
const orbitText = `/me has now connected this channel with IRC.`
const ircText = `has now connected this channel with Orbit.`
createMessage(ipfs, orbitText, nick, channelName)
ircClient.action(channelName, ircText)
// no more joining and sending, putting it all directly in the db. sets db instance per channel name/channel joined
const sanitizedChannel = sanitizeChannelName(channelName)
orbitdb.eventlog(sanitizedChannel)
.then((database) => db[sanitizedChannel] = database)
.catch(e => console.log(e))
}
});
// by writing directly to the db user can be other than MrsOrbit (which is set initially when connecting to OrbitDB), 'from' gets set dynamically now depending on irc username who sends message
ircClient.addListener('message', (from, to, message) => {
console.log(from + ' => ' + to + ': ' + message);
createMessage(ipfs, message, from, to);
});
ircClient.addListener('error', (message) => {
console.log('error: ', message);
});