-
Notifications
You must be signed in to change notification settings - Fork 3
/
irctest.js
180 lines (159 loc) · 3.2 KB
/
irctest.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
var irc = require("./lib/irc");
var exec = require("child_process").exec;
function system(cmd, cb)
{
if (!cb || typeof cb != 'function')
{
return "NOOB";
}
exec(cmd, function (error, stdout, stderr)
{
cb(stdout + stderr);
});
}
// GMAIL QUOTA
var CP = [
[ 1199433600000, 6283 ],
[ 1224486000000, 7254 ],
[ 2144908800000, 10996 ],
[ 2147328000000, 43008 ],
[ 46893711600000, Number.MAX_VALUE ]
];
function gmailQuota() {
var now = (new Date()).getTime();
var i;
for (i = 0; i < CP.length; i++) {
if (now < CP[i][0]) {
break;
}
}
if (i == CP.length) {
return CP[i - 1][1];
} else {
var ts = CP[i - 1][0];
var bs = CP[i - 1][1];
return format(((now-ts) / (CP[i][0]-ts) * (CP[i][1]-bs)) + bs);
}
}
var PAD = '.000000';
function format(num) {
var str = String(num);
var dot = str.indexOf('.');
if (dot < 0) {
return str + PAD;
} if (PAD.length > (str.length - dot)) {
return str + PAD.substring(str.length - dot);
} else {
return str.substring(0, dot + PAD.length);
}
}
// END GMAIL QUOTA
var $ = undefined;
irc.Channel.prototype._eval = function (code, service, bot, user, lastres)
{
var res = undefined;
var _ = lastres;
with (this)
{
res = eval(code);
}
return res;
}
var lastResult = undefined;
var s = new irc.Service(new irc.UnrealProtocol({ password: 'nodejs', numeric: 78, serviceHost: 'service.node.js' }));
var done = false;
function cleanup()
{
if (done) { return; }
done = true;
s.disconnect("User request");
}
with (s)
{
connect("localhost", 6667, function ()
{
process.once('SIGINT', cleanup);
process.once('SIGTERM', cleanup);
process.once('exit', cleanup);
$ = function (x)
{
if (x.length < 1)
{
return;
}
if (x[0] == '#')
{
var c = findChannel(x);
if (!irc.Channel.isChannel(c))
{
return null;
}
return c;
}
else
{
var u = findUser(x, true);
if (!irc.User.isUser(u))
{
var c = findChannel('#' + x);
if (!irc.Channel.isChannel(c))
{
return null;
}
return c;
}
return u;
}
}
var b = createBot("nodebot", "nodejs", "node.js bot", "bot.node.js");
b.join("#mave");
b.on('test', function ()
{
b.say("#mave", "hi there, I got it!");
});
b.on('userChannelJoin', function (user, channel)
{
channel.say(b, "Welcome, " + user.nickname[0] + "-" + user.nickname.substr(1) + "!");
});
on('rawPRIVMSG', function (origin, params)
{
var u = origin.user();
if (!irc.User.isUser(u))
{
return;
}
var chan = params[0];
if (chan[0] != "#")
{
return;
}
var c = findChannel(chan);
if (!irc.Channel.isChannel(c))
{
return;
}
var msg = params[1];
var trigger = "%";
if (msg.substr(0, trigger.length) == trigger)
{
try
{
var e = c._eval(msg.substr(trigger.length).trim(), s, b, u, lastResult);
lastResult = e;
if (e != undefined)
{
b.say(chan, e);
}
}
catch (e)
{
b.say(chan, "Error: " + e.message);
}
}
else if (msg.trim().toLowerCase() == "!gmail")
{
b.say(chan, 'Right now, Google Mail offers ' + String(gmailQuota()) + ' MB of free storage space.');
}
});
});
}