-
Notifications
You must be signed in to change notification settings - Fork 0
/
dAmnPacket.pm
executable file
·58 lines (47 loc) · 1.81 KB
/
dAmnPacket.pm
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
# Perl dAmnPacket parser - Part of dAmnPearl
# Author: Justin Eittreim <eittreim.justin@live.com>
# Date: Wed Oct 31 2012 23:59
package dAmnPacket;
sub parse {
my $packet = $_[0];
my %self = ();
$self{command} = '';
$self{parameter} = '';
$self{subCommand} = '';
$self{subParameter} = '';
$self{body} = '';
$self{raw} = '';
$self{arguments} = ();
$packet =~ s/\0//;
($self{raw} = $packet) =~ s/\n/\\n/;
my $nl_pos = index($packet, "\n");
my $chunk = substr $packet, 0, $nl_pos;
if ((my $space_pos = index($packet, ' ')) != -1) {
$self{command} = substr $chunk, 0, $space_pos;
$self{parameter} = substr $chunk, $space_pos + 1;
} else {
$self{command} = $chunk;
}
$packet = substr $packet, $nl_pos + 1;
if ((my $nlnl_pos = rindex($packet, "\n\n")) != -1) {
$self{body} = substr $packet, $nlnl_pos + 2;
$packet = substr $packet, 0, $nlnl_pos;
}
my @chunks = split /\n/, $packet;
foreach my $piece (@chunks) {
if (length $piece > 0) {
if ((my $sep_pos = index($piece, '=')) != -1) {
$self{arguments}{substr $piece, 0, $sep_pos} = substr $piece, $sep_pos + 1;
} else {
if (($space_pos = index($packet, ' ')) != -1) {
$self{subCommand} = substr $piece, 0, $space_pos - 1;
$self{subParameter} = substr $piece, $space_pos;
} else {
$self{subCommand} = $piece;
}
}
}
}
return %self;
}
1;