File: API.pm

package info (click to toggle)
libmdn-perl 2.4-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 240 kB
  • ctags: 73
  • sloc: perl: 780; makefile: 87; ansic: 60
file content (393 lines) | stat: -rw-r--r-- 11,590 bytes parent folder | download
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# $Id: API.pm,v 1.18 2002/02/28 02:21:00 m-kasahr Exp $
#
# Copyright (c) 2001 Japan Network Information Center.  All rights reserved.
#  
# By using this file, you agree to the terms and conditions set forth bellow.
# 
#                      LICENSE TERMS AND CONDITIONS 
# 
# The following License Terms and Conditions apply, unless a different
# license is obtained from Japan Network Information Center ("JPNIC"),
# a Japanese association, Fuundo Bldg., 1-2 Kanda Ogawamachi, Chiyoda-ku,
# Tokyo, Japan.
# 
# 1. Use, Modification and Redistribution (including distribution of any
#    modified or derived work) in source and/or binary forms is permitted
#    under this License Terms and Conditions.
# 
# 2. Redistribution of source code must retain the copyright notices as they
#    appear in each source code file, this License Terms and Conditions.
# 
# 3. Redistribution in binary form must reproduce the Copyright Notice,
#    this License Terms and Conditions, in the documentation and/or other
#    materials provided with the distribution.  For the purposes of binary
#    distribution the "Copyright Notice" refers to the following language:
#    "Copyright (c) Japan Network Information Center.  All rights reserved."
# 
# 4. Neither the name of JPNIC may be used to endorse or promote products
#    derived from this Software without specific prior written approval of
#    JPNIC.
# 
# 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
#    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
#    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JPNIC BE LIABLE
#    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
#    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
# 
# 6. Indemnification by Licensee
#    Any person or entities using and/or redistributing this Software under
#    this License Terms and Conditions shall defend indemnify and hold
#    harmless JPNIC from and against any and all judgements damages,
#    expenses, settlement liabilities, cost and other liabilities of any
#    kind as a result of use and redistribution of this Software or any
#    claim, suite, action, litigation or proceeding by any third party
#    arising out of or relates to this License Terms and Conditions.
# 
# 7. Governing Law, Jurisdiction and Venue
#    This License Terms and Conditions shall be governed by and and
#    construed in accordance with the law of Japan. Any person or entities
#    using and/or redistributing this Software under this License Terms and
#    Conditions hereby agrees and consent to the personal and exclusive
#    jurisdiction and venue of Tokyo District Court of Japan.
#
package MDN::API;

use strict;
use Carp;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD
	    $resconf $error_message $config_file);

use MDN::ResConf;

require Exporter;

@ISA = qw(Exporter);
@EXPORT = qw();
$VERSION = '2.4';

BEGIN {
    $resconf = MDN::ResConf->new()
	or croak "failed to create an MDN::ResConf object";
    if (defined($config_file)) {
	$resconf->load_file($config_file)
	    or croak "cannot load libmdn configuration file";
    } else {
	$resconf->load_file()
	    or croak "cannot load libmdn configuration file";
    }
    $error_message = 'success';
}

sub encode_name {
    my ($package, $from, @actions) = @_;

    push(@actions, 'encode_app') if (@actions == 0);
    my ($localconv, $delimmap, $localmap, $nameprep, $idnconv)
	= (0, 0, 0, 0, 0);

    foreach my $i (@actions) {
	if ($i =~ /^localconv$/i) {
	    $localconv = 1;
	} elsif ($i =~ /^delimmap$/i) {
	    $delimmap = 1;
	} elsif ($i =~ /^localmap$/i) {
	    $localmap = 1;
	} elsif ($i =~ /^nameprep$/i) {
	    $nameprep = 1;
	} elsif ($i =~ /^unascheck$/i) {
	    # ignore
	} elsif ($i =~ /^idnconv$/i) {
	    $idnconv = 1;
	} elsif ($i =~ /^encode_app$/i) {
	    $localconv = 1;
	    $delimmap = 1;
	    $localmap = 1;
	    $nameprep = 1;
	    $idnconv = 1;
	} else {
	    $error_message = "invalid action, $i";
	    return undef;
	}
    }

    my $nameconv_actions = '';
    $nameconv_actions .= 'l' if ($localconv);
    $nameconv_actions .= 'd' if ($delimmap);
    $nameconv_actions .= 'M' if ($localmap);
    $nameconv_actions .= 'N' if ($nameprep);
    $nameconv_actions .= 'I' if ($idnconv);

    my $result = $resconf->nameconv($nameconv_actions, $from);
    $error_message = MDN::ResConf->lasterror() if (!defined($result));

    return $result;
}

sub decode_name {
    my ($package, $from, @actions) = @_;

    push(@actions, 'decode_app') if (@actions == 0);
    my ($idnconv, $nameprep, $localconv) = (0, 0, 0);

    foreach my $i (@actions) {
	if ($i =~ /^idnconv$/i) {
	    $idnconv = 1;
	} elsif ($i =~ /^nameprep$/i) {
	    $nameprep = 1;
	} elsif ($i =~ /^unascheck$/i) {
	    # ignore
	} elsif ($i =~ /^localconv$/i) {
	    $localconv = 1;
	} elsif ($i =~ /^decode_app$/i) {
	    $idnconv = 1;
	    $nameprep = 1;
	    $localconv = 1;
	} else {
	    $error_message = "invalid action, $i";
	    return undef;
	}
    }

    my $nameconv_actions = '';
    $nameconv_actions .= 'i' if ($idnconv);
    $nameconv_actions .= '!N' if ($nameprep);
    $nameconv_actions .= 'L' if ($localconv);

    my $result = $resconf->nameconv($nameconv_actions, $from);
    $error_message = MDN::ResConf->lasterror() if (!defined($result));

    return $result;
}

sub enable {
    my ($package, $on_off) = @_;

    $resconf->enable($on_off);
}

sub lasterror {
    return $error_message;
}

sub local_to_utf8 {
    my ($package, $from) = @_;
    return encode_name($package, $from, 'localconv');
}

sub utf8_to_local {
    my ($package, $from) = @_;
    return decode_name($package, $from, 'localconv');
}

sub delimiter_map {
    my ($package, $from) = @_;
    return encode_name($package, $from, 'delimmap');
}

sub local_map {
    my ($package, $from) = @_;
    return encode_name($package, $from, 'localmap');
}

sub nameprep {
    my ($package, $from) = @_;
    return encode_name($package, $from, 'nameprep');
}

sub nameprep_check {
    my ($package, $from) = @_;
    return decode_name($package, $from, 'nameprep');
}

sub unassigned_check {
    my ($package, $from) = @_;
    return $from;
}

sub utf8_to_idn {
    my ($package, $from) = @_;
    return encode_name($package, $from, 'idnconv');
}

sub idn_to_utf8 {
    my ($package, $from) = @_;
    return decode_name($package, $from, 'idnconv');
}

sub local_to_idn {
    my ($package, $from) = @_;
    return encode_name($package, $from);
}

sub idn_to_local {
    my ($package, $from) = @_;
    return decode_name($package, $from);
}

1;
__END__

=head1 NAME

MDN::API - Perl interface to domain name conversion methods of libmdn

=head1 SYNOPSIS

  use MDN::API;

  $idn_domain_name = MDN::API->encode_name($local_domain_name);
  $local_domain_name = MDN::API->decode_name($idn_domain_name);

=head1 DESCRIPTION

C<MDN::API> provides a Perl object interface to domain name
conversion methods of libmdn, the multilingual domain name library
included in the mDNkit.

This module provides high-level conversion interface to the library.
It can encode or decode a domain name according with libmdn configuration
(e.g. C</usr/local/etc/mdn.conf>).

=head1 CLASS METHODS

Although this module does not provide object interface,
all the functions should be called as class methods,
in order to be consistent with other modules in C<MDN::>.

        MDN::API->encode_name($domain_name);  # OK
        MDN::API::encode_name($domain_name);  # NG

=over 4

=item encode_name($from [, @actions...])

Encodes domain name of C<$from> according with C<@actions>, and returns
the result.

Each element in C<@actions> is a string as follows:

    'localconv'     convert the local encoding string to UTF-8.
    'delimmap'      covnert local delimiters to periods ('.').
    'localmap'      perfrom local mapping.
    'nameprep'      perform NAMEPREP (mapping, normalization,
                    prohibit character check and unassigned
                    codepoint check).
    'idnconv'       convert the UTF-8 string to ACE.

    'encode_app'    perform 'localconv', 'delimmap', 'localmap',
                    'nameprep' and 'idnconv'.  This is the default
                    value of @actions.

Regardless of the order of elements in C<@actions>, the actions are
always performed in the following order; C<'localconv'>, C<'delimmap'>,
C<'localmap'>, C<'nameprep'> and C<'idnconv'>.

If an error occurs, this method returns C<undef>.

=item decode_name($from [, @actions...])

Decodes domain name of C<$from> according with C<@actions>, and returns
the result.  This is the reverse of C<encode_name>.

Each element in C<@actions> is a string as follows:

    'idnconv'       convert the ACE string to UTF-8.
    'nameprep'      inspect if NAMEPREP has been performed to the
                    string.  If hasn't, convert the string to ACE.
    'localconv'     convert the UTF-8 string to the local encoding.

    'decode_app'    perform 'idnconv', 'nameprep' and 'localconv'.
                    This is the default value of @actions.

Regardless of the order of elements in C<@actions>, those actions
are always performed in the following order; C<'idnconv'>,
C<'nameprep'> and C<'localconv'>.

If an error occurs, this method returns C<undef>.

=item enable($on_off)

Enable or disable mulitilingual domain name support provided by
libmdn.
If C<$on_off> is zero, the support is disabled, otherwise enabled.
If this method has not been called, the C<MDN_DISABLE> environment
variabe is used to determine whether mulitilingual domain name
support is enabled.
The support is disabled if that variable is defined, 

In disabled condition, conversion methods (e.g. C<encode_name()>
and C<decode_name()>) simply return a copy of input string as the
result of conversion.

=item lasterror()

Returns the error message string corresponding to the last error
occurred in this module.

=item local_to_utf8($from)

Is equivalent to C<encode_name($from, 'localconv')>.

=item utf8_to_local($from)

Is equivalent to C<decode_name($from, 'localconv')>.

=item delimiter_map($from)

Is equivalent to C<encode_name($from, 'delimmap')>.

=item local_map($from)

Is equivalent to C<encode_name($from, 'localmap')>.

=item nameprep($from)

Is equivalent to C<encode_name($from, 'nameprep')>.

=item nameprep_check($from)

Is equivalent to C<decode_name($from, 'nameprep')>.

=item utf8_to_idn($from)

Is equivalent to C<encode_name($from, 'idnconv')>.

=item idn_to_utf8($from)

Is equivalent to C<decode_name($from, 'idnconv')>.

=item local_to_idn($from)

Is equivalent to C<encode_name($from)>.

=item idn_to_local($from)

Is equivalent to C<decode_name($from)>.

=back

=head1 COMPATIBILITY

Beginning with version 2.4, the action C<'nameprep'> of C<encode_name()>
and C<decode_name()> also performs unassigned codepoint check.
The C<'unascheck'> action and the C<unassigned_check()> method are no
longer supported.

=head1 ISSUE OF HANDLING UNICODE CHARACTERS

See the ``ISSUE OF HANDLING UNICODE CHARACTERS'' section in
L<MDN::UTF8>.

=head1 SEE ALSO

L<mdn.conf(5)>, L<MDN::ResConf>, L<MDN::UTF8>

MDN library specification

=cut