This repository has been archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
105 lines (81 loc) · 3.36 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
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
/*---------------------------------------------------------------
:: sails-boilerplate
-> adapter
---------------------------------------------------------------*/
var async = require('async');
var irc = require('irc');
var adapter = module.exports = {
// Set to true if this adapter supports (or requires) things like data types, validations, keys, etc.
// If true, the schema for models using this adapter will be automatically synced when the server starts.
// Not terribly relevant if not using a non-SQL / non-schema-ed data store
syncable: false,
// Keep track of the different configurations
configurations: {},
// This method runs when a model is initially registered at server start time
registerCollection: function(collection, cb) {
// Require that the host, nick, and channels were specified
if (!collection.channel) return cb('No channel specified (e.g. #sailsjs');
if (!collection.nick) return cb('No nick specified (e.g. mikermcneil');
if (!collection.host) return cb('No host specified (e.g. zelazny.freenode.net');
// Disable blueprint pubsub
// collection.silent = true;
// Connect/join and save reference to configuration
connect(collection, cb);
},
// The following methods are optional
////////////////////////////////////////////////////////////
// Optional hook fired when a model is unregistered, typically at server halt
// useful for tearing down remaining open connections, etc.
teardown: function(cb) {
cb();
},
// Send a message to the IRC channel
create: function (collectionName, options, cb) {
if (!options.message) return cb('Please create({ message: "your message" }).');
var client = adapter.configurations[collectionName];
var activeChannel = adapter.configurations[collectionName]._activeChannel;
var activeNick = adapter.configurations[collectionName]._activeNick;
client.say(activeChannel, options.message);
cb(null, {
from: activeNick,
to: activeChannel,
message: options.message
});
},
// Always return an empty set of messages
find: function (collectionName, options, cb) {
cb(null, []);
}
};
////////////// //////////////////////////////////////////
////////////// Private Methods //////////////////////////////////////////
////////////// //////////////////////////////////////////
function connect(collection, cb) {
var client = new irc.Client(collection.host, collection.nick, {
channels: [collection.channel]
});
// If the client encounters an error, wait, then attempt to reconnect
client.addListener('error', onError);
function onError (message) {
sails.log.error("IRCAdapter.onError");
sails.log.error(message);
connect(collection);
}
// Listen for incoming chats
client.addListener('message', function (from, to, message) {
// Fire collection's onCreate method if it exists
if (collection.publishCreate) {
collection.publishCreate({
from: from,
to: to,
message: message
});
}
});
// Save reference to client
adapter.configurations[collection.identity] = client;
// Also save reference to active channel and nick
adapter.configurations[collection.identity]._activeChannel = collection.channel;
adapter.configurations[collection.identity]._activeNick = collection.nick;
if (cb) return cb(null, client);
}