-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperlbot-unicode.patch
213 lines (207 loc) · 7.14 KB
/
perlbot-unicode.patch
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
--- Chrisbot/Common.pm.orig 2007-06-14 14:13:40.000000000 -0300
+++ Chrisbot/Common.pm 2007-06-14 14:28:35.000000000 -0300
@@ -15,6 +15,7 @@
use Chrisbot::Util::Math;
use Chrisbot::Util::TempConv;
use Chrisbot::Util::Scramble;
+use Chrisbot::Util::Unicode;
# modules used by perlbot modules (including this one)
use Safe;
@@ -22,6 +23,8 @@
use CGI; # for Vars
use LWP::Simple ();
use Geo::IP;
+use Encode ();
+use Unicode::CharName ();
sub tell
@@ -112,6 +115,15 @@
}
+sub unicode
+{
+
+ my ($self, $char) = @_;
+ Chrisbot::Util::Unicode->info($char);
+
+}
+
+
sub scramble
{
--- Chrisbot/Util/Unicode.pm.orig 1969-12-31 20:00:00.000000000 -0400
+++ Chrisbot/Util/Unicode.pm 2007-06-14 15:00:35.000000000 -0300
@@ -0,0 +1,86 @@
+package Chrisbot::Util::Unicode;
+
+# Copyright (c) 2007 Jason Rhinelander (jagerman@jagerman.com). All rights reserved.
+# This program is free software; you can redistribute it and/or
+# modify it under the same terms as Perl itself.
+
+# UTF-8/Unicode character information by Jason Rhinelander
+
+use strict;
+use warnings;
+
+use Unicode::CharName qw/uname ublock/;
+use Encode qw/encode/;
+
+my %special;
+@special{0 .. 31, 127 .. 159} = qw{
+ NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US DEL
+ PAD HOP BPH NBH IND NEL SSA ESA HTS HTJ VTS PLD PLU RI SS2 SS3 DCS PU1 PU2 STS CCH MW SPA EPA SOS SGCI SCI CSI ST OSC PM APC
+};
+my $regex = qr{
+ (
+ 0 (?:_[1-7]|[0-7])+ # Octal, such as: 020075. _ allowed, but not before a 0 (for some reason, oct() doesn't allow that).
+ |
+ (?:0?x | [Uu]\+?) [[:xdigit:]]+ (?:_[[:xdigit:]]+)* # Hex, such as: 0x203d, x203d, 0x20_3d, etc. single _'s allowed. Also allows the U+0123 system.
+ |
+ 0?b[01]+(?:_[01]+)* # Binary, such as: 0b100000_00111101, b00100000_00111101, b10000000111101, etc. single _'s allowed.
+ )
+ |
+ (\d+) # Regular decimal number, such as 345
+ |
+ ( # Bytes making up a utf8 character
+ [\x00-\x7f] # 1 byte (0-7F)
+ |
+ [\xc2-\xdf][\x80-\xbf] # 2 bytes (80-7FF)
+ |
+ [\xe0-\xef][\x80-\xbf]{2} # 3 bytes (800-FFFF)
+ |
+ [\xf0-\xf3][\x80-\xbf]{3} # 4 bytes (10000-10FFFF)
+ )
+}ix;
+
+sub info
+{
+ my ($self, $unicode) = @_;
+
+ $unicode =~ /^\s*$regex\s*/ or return "That doesn't look like a valid UTF-8 character or unicode codepoint";
+
+ my ($oct, $dec, $char) = ($1, $2, $3);
+ my $cp;
+ if (defined $oct) { $oct =~ s/U\+?/0x/i; $cp = oct $oct }
+ elsif (defined $dec) { $cp = 0+$dec }
+ else {
+ # Manually get the codepoint from the encoded utf8 bytes.
+ # This could be done with Encode::decode_utf8, but it doesn't guarantee that the result will be identical.
+ my @char = split //, $char;
+ if (@char == 1) {
+ # 0b0xxxxxxx -- i.e. also an ASCII character
+ $cp = ord($char);
+ }
+ else {
+ # 0b110yyyyy_10zzzzzz -- 2 bytes for 8-11 bit codepoints (0byyyyyzzzzzz)
+ # 0b1110xxxx_10yyyyyy_10zzzzzz -- 3 bytes for 12-16 bit codepoints (0bxxxxyyyyyyzzzzzz)
+ # 0b11110www_10xxxxxx_10yyyyyy_10zzzzzz -- 4 bytes for 17-21 bit codepoints (0bwwwxxxxxxyyyyyyzzzzzz)
+ #
+ # The first byte has either 5 (2-byte sequence), 4 (3-byte) or 3 (4-byte) significant bits:
+ $cp = ord(shift(@char) & (@char == 1 ? "\x1f" : @char == 2 ? "\xf" : "\x7"));
+ # The remaining bytes all have 6 significant bits
+ $cp = ($cp << 6) + ord(shift(@char) & "\x3f") while @char;
+ }
+ }
+
+ my $utf8_char = encode("utf8", chr $cp);
+ my $char_disp = $special{$cp} || $utf8_char;
+ my $latin1 = '';
+ if (not $special{$cp} and $cp >= 128 and $cp <= 255) {
+ $latin1 = ". ISO-8859-1: " . encode("ISO-8859-1", chr $cp);
+ }
+ my $uname = uname($cp) || 'Unknown/Invalid';
+ my $ublock = ublock($cp) || 'Unknown';
+ my $utf8_encoding = join '', map sprintf('\x%02X', ord), split //, encode("utf8", chr($cp));
+
+ return "\cB$char_disp - $uname\cB. Unicode category: $ublock; codepoint: $cp; UTF-8 encoding: $utf8_encoding$latin1";
+
+}
+
+1;
--- Chrisbot/Util/Unicode.pm.orig 1969-12-31 20:00:00.000000000 -0400
+++ Chrisbot/Util/Unicode.pm 2007-06-14 15:00:35.000000000 -0300
@@ -0,0 +1,86 @@
+package Chrisbot::Util::Unicode;
+
+# Copyright (c) 2007 Jason Rhinelander (jagerman@jagerman.com). All rights reserved.
+# This program is free software; you can redistribute it and/or
+# modify it under the same terms as Perl itself.
+
+# UTF-8/Unicode character information by Jason Rhinelander
+
+use strict;
+use warnings;
+
+use Unicode::CharName qw/uname ublock/;
+use Encode qw/encode/;
+
+my %special;
+@special{0 .. 31, 127 .. 159} = qw{
+ NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US DEL
+ PAD HOP BPH NBH IND NEL SSA ESA HTS HTJ VTS PLD PLU RI SS2 SS3 DCS PU1 PU2 STS CCH MW SPA EPA SOS SGCI SCI CSI ST OSC PM APC
+};
+my $regex = qr{
+ (
+ 0 (?:_[1-7]|[0-7])+ # Octal, such as: 020075. _ allowed, but not before a 0 (for some reason, oct() doesn't allow that).
+ |
+ (?:0?x | [Uu]\+?) [[:xdigit:]]+ (?:_[[:xdigit:]]+)* # Hex, such as: 0x203d, x203d, 0x20_3d, etc. single _'s allowed. Also allows the U+0123 system.
+ |
+ 0?b[01]+(?:_[01]+)* # Binary, such as: 0b100000_00111101, b00100000_00111101, b10000000111101, etc. single _'s allowed.
+ )
+ |
+ (\d+) # Regular decimal number, such as 345
+ |
+ ( # Bytes making up a utf8 character
+ [\x00-\x7f] # 1 byte (0-7F)
+ |
+ [\xc2-\xdf][\x80-\xbf] # 2 bytes (80-7FF)
+ |
+ [\xe0-\xef][\x80-\xbf]{2} # 3 bytes (800-FFFF)
+ |
+ [\xf0-\xf3][\x80-\xbf]{3} # 4 bytes (10000-10FFFF)
+ )
+}ix;
+
+sub info
+{
+ my ($self, $unicode) = @_;
+
+ $unicode =~ /^\s*$regex\s*/ or return "That doesn't look like a valid UTF-8 character or unicode codepoint";
+
+ my ($oct, $dec, $char) = ($1, $2, $3);
+ my $cp;
+ if (defined $oct) { $oct =~ s/U\+?/0x/i; $cp = oct $oct }
+ elsif (defined $dec) { $cp = 0+$dec }
+ else {
+ # Manually get the codepoint from the encoded utf8 bytes.
+ # This could be done with Encode::decode_utf8, but it doesn't guarantee that the result will be identical.
+ my @char = split //, $char;
+ if (@char == 1) {
+ # 0b0xxxxxxx -- i.e. also an ASCII character
+ $cp = ord($char);
+ }
+ else {
+ # 0b110yyyyy_10zzzzzz -- 2 bytes for 8-11 bit codepoints (0byyyyyzzzzzz)
+ # 0b1110xxxx_10yyyyyy_10zzzzzz -- 3 bytes for 12-16 bit codepoints (0bxxxxyyyyyyzzzzzz)
+ # 0b11110www_10xxxxxx_10yyyyyy_10zzzzzz -- 4 bytes for 17-21 bit codepoints (0bwwwxxxxxxyyyyyyzzzzzz)
+ #
+ # The first byte has either 5 (2-byte sequence), 4 (3-byte) or 3 (4-byte) significant bits:
+ $cp = ord(shift(@char) & (@char == 1 ? "\x1f" : @char == 2 ? "\xf" : "\x7"));
+ # The remaining bytes all have 6 significant bits
+ $cp = ($cp << 6) + ord(shift(@char) & "\x3f") while @char;
+ }
+ }
+
+ my $utf8_char = encode("utf8", chr $cp);
+ my $char_disp = $special{$cp} || $utf8_char;
+ my $latin1 = '';
+ if (not $special{$cp} and $cp >= 128 and $cp <= 255) {
+ $latin1 = ". ISO-8859-1: " . encode("ISO-8859-1", chr $cp);
+ }
+ my $uname = uname($cp) || 'Unknown/Invalid';
+ my $ublock = ublock($cp) || 'Unknown';
+ my $utf8_encoding = join '', map sprintf('\x%02X', ord), split //, encode("utf8", chr($cp));
+
+ return "\cB$char_disp - $uname\cB. Unicode category: $ublock; codepoint: $cp; UTF-8 encoding: $utf8_encoding$latin1";
+
+}
+
+1;