-
Notifications
You must be signed in to change notification settings - Fork 14
/
output-socket.js
162 lines (134 loc) · 4.54 KB
/
output-socket.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* The OutputSocket is a facade for the IrcSocket's `raw` method.
*
* This could really be in tennu_modules, except that it is integrated
* into the client. It will be soon enough. First though, upgrade the modules.
*
* It supports the following methods:
*
* Public Methods
* say
* act
* join
* part
* quit
* nick
* mode
* userhost
* whois
* raw
* rawf
*/
var inspect = require('util').inspect;
var format = require('util').format;
var Q = require('q');
var partition = function (array, length) {
var partitions = [];
for (var i = 0, len = array.length; i < len; i += length) {
partitions.push(array.slice(i, i + length));
}
return partitions;
};
var OutputSocket = function (socket, messageHandler, nickname, logger) {
var raw = function (line) {
if (Array.isArray(line)) { line = line.join(" "); }
logger.info("->: " + String(line));
socket.raw(line);
};
var rawf = function () {
raw(format.apply(null, arguments));
};
return {
say : function recur (location, message) {
if (Array.isArray(message)) {
message.forEach(function (msg) {
recur.call(this, location, msg);
});
return;
}
rawf("PRIVMSG %s :%s", location, message);
},
ctcp : function recur (location, type, message) {
if (Array.isArray(message)) {
message.forEach(function (msg) {
recur.call(this, location, type, msg);
});
return;
}
this.say(location, format('\u0001%s %s\u0001', type, message));
},
act: function (location, message) {
this.ctcp(location, "ACTION", message);
},
join : function (channel) {
var deferred = Q.defer();
var unsubscribe = function () {
logger.debug("Join response or timeout occured.");
messageHandler.off('join', onJoin);
};
var onJoin = function (join) {
if (join.nickname !== nickname() || join.channel !== channel) {
return;
}
unsubscribe();
logger.debug("Resolving with join message.");
deferred.resolve(join);
};
messageHandler.on('join', onJoin);
rawf("JOIN :%s", channel);
return undefined; // WIP Code.
return deferred.promise;
},
part : function (channel, reason) {
raw("PART " + channel + (reason ? " :" + reason : ""));
},
nick : function (newNick) {
rawf("NICK %s", newNick);
},
quit : function (reason) {
logger.notice(format("Quitting with reason: %s", reason));
raw("QUIT" + (reason ? " :" + reason : ""));
},
mode : function (target, plus, minus, inArgs) {
var args = ":";
if (plus) {
args += "+" + plus;
}
if (minus) {
args += "-" + minus;
}
if (inArgs) {
args += " " + util.isArray(inArgs) ? inArgs.join(' ') : inArgs;
}
raw(["MODE", target, args]);
},
userhost : function recur (users) {
if (typeof users === 'string') {
rawf("USERHOST :%s", users);
} else if (typeof users === 'array') {
partition(users, 5)
.map(function (hosts) { return hosts.join(' '); })
.map(recur);
} else {
throw new Error("Userhost command takes either a string (a single nick) or an array (of string nicks)");
}
},
whois : function recur (users, server) {
if (typeof users === "array") {
if (users.length > 15) {
partition(users, 15)
.map(function (users) { return users.join(','); })
.map(function (users) { recur(users, server); });
}
} else if (typeof users === 'string') {
raw("WHOIS " + (server ? server + " " : "") + users);
} else {
throw new Error("Whois command takes either a string (a single nick) or an array (of string nicks)");
}
},
raw : raw,
rawf : rawf,
toString : function () { return "[Object IrcOutputSocket]"; }
};
};
module.exports = OutputSocket;