-
Notifications
You must be signed in to change notification settings - Fork 3
/
tr.jsm
262 lines (246 loc) · 7.29 KB
/
tr.jsm
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// -*- Mode: JavaScript; tab-width: 4 -*- vim:tabstop=4 syntax=javascript:
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/** @fileoverview aucgbot module: Transform text. */
/*jshint es5: true, esnext: true, nonstandard: true */
/*global decodeB64: false, decodeHTML: false, decodeURL: false, encodeB64: false, encodeHTML: false, encodeURL: false, module: false */
module.version = 2.91;
module.UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
module.LOWER = "abcdefghijklmnopqrstuvwxyz";
module.ALPHABET = module.UPPER + module.LOWER;
module.AL_BHED = "YPLTAVKREZGMSHUBXNCDIJFQOWypltavkrezgmshubxncdijfqow";
module.GOOGLERESE = "ynficwlbkuomxsevzpdrjgthaq";
module.DIGITS = "0123456789";
module.cmd_tr = function cmd_tr(e) {
var args = /^"((?:\\")*[^"]+(?:\\"[^"]*)*)" "((?:\\")*[^"]+(?:\\"[^"]*)*)" "((?:\\")*[^"]+(?:\\"[^"]*)*)"$/.exec(e.args);
e.reply(args ? (args.shift(), tr.apply(null, args)) : this.cmd_tr.help);
return true;
};
module.cmd_tr.help = 'Like the UNIX tr utility. Usage: tr "<text>" "<trFromTable>" "<trToTable>"';
module.cmd_rot13 = function cmd_rot13(e) {
var msg = e.args;
if (!msg) {
e.reply(this.cmd_rot13.help);
return true;
}
e.reply(tr(msg, this.ALPHABET, "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"));
return true;
};
module.cmd_rot13.help = "ROT13 text. Usage: rot13 <text>";
module.cmd_rot47 = function cmd_rot47(e) {
var msg = e.args;
if (!msg) {
e.reply(this.cmd_rot47.help);
return true;
}
e.reply(tr(msg, "!\"#$%&\'()*+,-./0123456789:;<=>?@" + this.UPPER + "[\\]^_`" + this.LOWER + "{|}~", "PQRSTUVWXYZ[\\]^_`" + this.LOWER + "{|}~!\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO"));
return true;
};
module.cmd_rot47.help = "ROT47 text. Usage: rot47 <text>";
module.cmd_atbash = function cmd_atbash(e) {
var msg = e.args;
if (!msg) {
e.reply(this.cmd_atbash.help);
return true;
}
e.reply(tr(msg, this.ALPHABET, this.REVUPPER + this.REVLOWER));
return true;
};
module.cmd_atbash.help = "Atbash: a reversed alphabet Caesar cyphar. Usage: atbash <text>";
module.cmd_atbashd = function cmd_atbashd(e) {
var msg = e.args;
if (!msg) {
e.reply(this.cmd_atbashd.help);
return true;
}
e.reply(tr(msg, this.ALPHABET + this.DIGITS, this.REVUPPER + this.REVLOWER + this.DIGITS.reverse()));
return true;
};
module.cmd_atbashd.help = "Atbash + digits: a reversed alphabet Caesar cyphar. Usage: atbashd <text>";
module.cmd_rev = function cmd_rev(e) {
var msg = e.args;
if (!msg) {
e.reply(this.cmd_rev.help);
return true;
}
e.reply(msg.reverse());
return true;
};
module.cmd_rev.help = "Reverse text. Usage: rev <text>";
module.cmd_revword = function cmd_revword(e) {
var msg = e.args;
if (!msg) {
e.reply(this.cmd_revword.help);
return true;
}
e.reply(msg.split(" ").map(String.reverse).join(" "));
return true;
};
module.cmd_revword.help = "Reverse words. Usage: revword <text>";
module.cmd_encode = function cmd_encode(e) {
var args = e.args;
if (!args) {
e.reply(this.cmd_encode.help);
return true;
}
args = args.split(" ");
var type = args.shift().toLowerCase(), msg = args.join(" ");
switch (type) {
case "base64": case "b64":
e.reply(encodeB64(msg));
return true;
case "html":
e.reply(encodeHTML(msg));
return true;
case "url":
e.reply(encodeURL(msg));
return true;
case "uri":
e.reply(encodeURI(msg));
return true;
case "uricomponent":
e.reply(encodeURIComponent(msg));
return true;
case "escape":
e.reply(escape(msg));
return true;
case "charcode": case "dec": case "hex": case "bin":
var s = [];
for (var i = 0; i < msg.length; i++) {
if (type == "bin") {
s.push(msg.charCodeAt(i).toString(2).zfill(8));
} else {
s.push(msg.charCodeAt(i).toString(type == "hex" ? 16 : 10));
}
}
e.reply(s.join(" "));
return true;
case "albhed":
e.reply(tr(msg, this.ALPHABET, this.AL_BHED));
return true;
case "googlerese":
e.reply(tr(msg, this.LOWER, this.GOOGLERESE));
return true;
}
};
module.cmd_encode.help = "Encode stuff. Usage: encode <type> <text>";
module.cmd_decode = function cmd_decode(e) {
var args = e.args;
if (!args) {
e.reply(this.cmd_decode.help);
return true;
}
args = args.split(" ");
var type = args.shift().toLowerCase(), msg = args.join(" ");
switch (type) {
case "base64": case "b64":
e.reply(decodeB64(msg));
return true;
case "html":
e.reply(decodeHTML(msg));
return true;
case "url":
e.reply(decodeURL(msg));
return true;
case "uri":
e.reply(decodeURI(msg));
return true;
case "uricomponent":
e.reply(decodeURIComponent(msg));
return true;
case "escape":
e.reply(unescape(msg));
return true;
case "charcode":
e.reply(String.fromCharCode.apply(null, args));
return true;
case "codepoint": case "codept": case "dec":
e.reply(String.fromCodePoint.apply(null, args));
return true;
case "hex":
if (args.length === 1 && msg.length > 4) {
args = msg.match(/.{1,2}/g);
}
e.reply(String.fromCodePoint.apply(null, args.map(function (x) parseInt(x, 16))));
return true;
case "bin":
e.reply(String.fromCodePoint.apply(null, args.map(function (x) parseInt(x, 2))));
return true;
case "albhed":
e.reply(tr(msg, this.AL_BHED, this.ALPHABET));
return true;
case "googlerese":
e.reply(tr(msg, this.GOOGLERESE, this.LOWER));
return true;
}
};
module.cmd_decode.help = "Decode stuff. Usage: decode <type> <text>";
module.cmd_rainbow = function cmd_rainbow(e) {
var msg = e.args;
if (!msg) {
e.reply(this.cmd_rainbow.help);
return true;
}
function f(n) n < 10 ? "0" + n : n;
var s = "";
for (var i = 0, chr; chr = msg[i]; i++) {
if (this.DIGITS.contains(chr)) {
s += "\003" + f(randint(0, 15)) + chr;
} else {
s += "\003" + randint(0, 15) + chr;
}
}
e.nmsg(s);
return true;
};
module.cmd_rainbow.help = "Rainbows, rainbows everywhere! Usage: rainbow <text>";
// from https://developer.mozilla.org/en/A_re-introduction_to_JavaScript
// henceforth licensed in whatever license the MDN is (probably MPL)
String.reverse = function reverse(str) {
var s = "";
for (var i = str.length - 1; i >= 0; i--)
s += str[i];
return s;
};
String.prototype.reverse = function reverse() String.reverse(this);
module.REVUPPER = module.UPPER.reverse();
module.REVLOWER = module.REVUPPER.toLowerCase();
String.zfill = function zfill(str, l) {
while (str.length < l)
str = "0" + str;
return str;
};
String.prototype.zfill = function zfill(l) String.zfill(this, l);
// shim for ES5: ECMA-262 6th Edition, 15.5.3.3
String.fromCodePoint = function fromCodePoint() {
var points = [];
Array.forEach(arguments, function (offset) {
if (offset < 0x10000)
points.push(offset);
else {
offset -= 0x10000;
points.push(0xD800 | (offset >> 10), 0xDC00 | (offset & 0x3FF));
}
});
return String.fromCharCode.apply(null, points);
};
/**
* Translates text in a similar fashion to the UNIX tr utility.
*
* @param {string} str The string to transform.
* @param {string} frm Table containing characters to replace.
* @param {string} to Table containing characters to replace with.
* @return {string} Transformed string.
*/
function tr(str, frm, to) {
var s = "";
for (var i = 0, j = 0, k = ""; i < str.length; i++) {
if ((j = frm.indexOf(str[i])) == -1)
k = str[i];
else if (!(k = to[j]))
k = "";
s += k;
}
return s;
}