line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Convos::Core::Util; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Convos::Core::Util - Utility functions for Convos |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
L can export any of the L. |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=cut |
12
|
|
|
|
|
|
|
|
13
|
37
|
|
|
37
|
|
16354
|
use Mojo::Base 'Exporter'; |
|
37
|
|
|
|
|
7542
|
|
|
37
|
|
|
|
|
193
|
|
14
|
|
|
|
|
|
|
|
15
|
37
|
|
|
37
|
|
6106
|
no warnings "utf8"; |
|
37
|
|
|
|
|
62
|
|
|
37
|
|
|
|
|
1211
|
|
16
|
37
|
|
|
37
|
|
654
|
use Mojo::Log; |
|
37
|
|
|
|
|
45560
|
|
|
37
|
|
|
|
|
236
|
|
17
|
37
|
|
|
37
|
|
1398
|
use Mojo::UserAgent; |
|
37
|
|
|
|
|
201930
|
|
|
37
|
|
|
|
|
235
|
|
18
|
37
|
|
|
37
|
|
1951
|
use Parse::IRC (); |
|
37
|
|
|
|
|
4133
|
|
|
37
|
|
|
|
|
660
|
|
19
|
37
|
|
|
37
|
|
959
|
use Unicode::UTF8 'decode_utf8'; |
|
37
|
|
|
|
|
918
|
|
|
37
|
|
|
|
|
1849
|
|
20
|
37
|
|
|
37
|
|
20601
|
use Time::Piece; |
|
37
|
|
|
|
|
240320
|
|
|
37
|
|
|
|
|
155
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $SERVER_NAME_RE = qr{(?:\w+\.[^:/]+|localhost|loopback):?\d*}; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our @EXPORT_OK = qw( as_id format_time id_as logf pretty_server_name $SERVER_NAME_RE ); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 FUNCTIONS |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head2 as_id |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
$id = as_id @str; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
This method will convert the input to a string which can be used as id |
33
|
|
|
|
|
|
|
attribute in your HTML doc. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
It will convert non-word characters to ":hex" and join C<@str> with ":00". |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=cut |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub as_id { |
40
|
8
|
|
|
|
|
8
|
join ':00', map { |
41
|
8
|
|
|
|
|
12
|
local $_ = $_; # local $_ is for changing constants and not changing input |
42
|
8
|
|
|
|
|
14
|
s/:/:3a/g; |
43
|
8
|
|
|
|
|
21
|
s/([^\w:-])/{ sprintf ':%02x', ord $1 }/ge; |
|
6
|
|
|
|
|
5
|
|
|
6
|
|
|
|
|
23
|
|
44
|
8
|
|
|
|
|
25
|
$_; |
45
|
4
|
|
|
4
|
1
|
950
|
} grep { length $_; } @_; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head2 format_time |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
$str = format_time $timestamp, $format; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=cut |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub format_time { |
55
|
1
|
|
|
1
|
1
|
17
|
my $date = localtime shift; |
56
|
1
|
|
|
|
|
67
|
my $format = shift; |
57
|
|
|
|
|
|
|
|
58
|
1
|
|
|
|
|
5
|
return decode_utf8($date->strftime($format)); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head2 id_as |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
@str = id_as $id; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Reverse of L. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=cut |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub id_as { |
70
|
7
|
|
|
|
|
23
|
map { |
71
|
3
|
|
|
3
|
1
|
12
|
s/:(\w\w)/{ chr hex $1 }/ge; |
|
7
|
|
|
|
|
7
|
|
|
7
|
|
|
|
|
20
|
|
72
|
7
|
|
|
|
|
25
|
$_; |
73
|
|
|
|
|
|
|
} split /:00/, $_[0]; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head2 logf |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
$c->logf($level => $format, @args); |
79
|
|
|
|
|
|
|
$c->logf(debug => 'yay %s', \%data); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Used to log more complex datastructures and to prevent logging C. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=cut |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub logf { |
86
|
37
|
|
|
37
|
|
15791
|
use Data::Dumper; |
|
37
|
|
|
|
|
60
|
|
|
37
|
|
|
|
|
15526
|
|
87
|
0
|
|
|
0
|
1
|
0
|
my ($self, $level, $format, @args) = @_; |
88
|
0
|
|
0
|
|
|
0
|
my $log = $self->{app}{log} || $self->{log} || Mojo::Log->new; |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
0
|
local $Data::Dumper::Maxdepth = 2; |
91
|
0
|
|
|
|
|
0
|
local $Data::Dumper::Indent = 0; |
92
|
0
|
|
|
|
|
0
|
local $Data::Dumper::Terse = 1; |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
0
|
for my $arg (@args) { |
95
|
0
|
0
|
|
|
|
0
|
if (ref($arg) =~ /^\w+$/) { |
|
|
0
|
|
|
|
|
|
96
|
0
|
|
|
|
|
0
|
$arg = Dumper($arg); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
elsif (!defined $arg) { |
99
|
0
|
|
|
|
|
0
|
$arg = '__UNDEF__'; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
0
|
$log->$level(sprintf $format, @args); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head2 pretty_server_name |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
$str = pretty_server_name($server); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Removes "ssl\.", "irc.", "chat." from the beginning and ".com", ".org", ... |
111
|
|
|
|
|
|
|
from the end. Converts all non word and "_" to "-". Also removes the port. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Also has special handling for $servers matching... |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
$server | $str |
116
|
|
|
|
|
|
|
-------------|------- |
117
|
|
|
|
|
|
|
irc.perl.org | magnet |
118
|
|
|
|
|
|
|
efnet | efnet |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=cut |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub pretty_server_name { |
123
|
6
|
|
|
6
|
1
|
584
|
my ($name) = @_; |
124
|
|
|
|
|
|
|
|
125
|
6
|
50
|
|
|
|
15
|
return '' unless defined $name; |
126
|
6
|
100
|
|
|
|
32
|
return 'magnet' if $name =~ /\birc\.perl\.org\b/i; # also match ssl.irc.perl.org |
127
|
3
|
100
|
|
|
|
14
|
return 'efnet' if $name =~ /\befnet\b/i; |
128
|
|
|
|
|
|
|
|
129
|
2
|
|
|
|
|
11
|
$name =~ s!^(irc|chat)\.!!; # remove common prefixes from server name |
130
|
2
|
|
|
|
|
5
|
$name =~ s!:\d+$!!; # remove port |
131
|
2
|
|
|
|
|
20
|
$name =~ s!\.\w{2,3}$!!; # remove .com, .no, ... |
132
|
2
|
|
|
|
|
4
|
$name =~ s![\W_]+!-!g; # make pretty url |
133
|
2
|
|
|
|
|
7
|
$name; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 AUTHOR |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Jan Henning Thorsen - C |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=cut |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
1; |