| 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 |  | 15673 | use Mojo::Base 'Exporter'; | 
|  | 37 |  |  |  |  | 6943 |  | 
|  | 37 |  |  |  |  | 201 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 37 |  |  | 37 |  | 5960 | no warnings "utf8"; | 
|  | 37 |  |  |  |  | 61 |  | 
|  | 37 |  |  |  |  | 1132 |  | 
| 16 | 37 |  |  | 37 |  | 610 | use Mojo::Log; | 
|  | 37 |  |  |  |  | 43433 |  | 
|  | 37 |  |  |  |  | 247 |  | 
| 17 | 37 |  |  | 37 |  | 1294 | use Mojo::UserAgent; | 
|  | 37 |  |  |  |  | 179978 |  | 
|  | 37 |  |  |  |  | 220 |  | 
| 18 | 37 |  |  | 37 |  | 1635 | use Parse::IRC (); | 
|  | 37 |  |  |  |  | 3991 |  | 
|  | 37 |  |  |  |  | 566 |  | 
| 19 | 37 |  |  | 37 |  | 984 | use Unicode::UTF8 'decode_utf8'; | 
|  | 37 |  |  |  |  | 1021 |  | 
|  | 37 |  |  |  |  | 1906 |  | 
| 20 | 37 |  |  | 37 |  | 19713 | use Time::Piece; | 
|  | 37 |  |  |  |  | 230804 |  | 
|  | 37 |  |  |  |  | 167 |  | 
| 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 |  |  |  |  | 7 | join ':00', map { | 
| 41 | 8 |  |  |  |  | 11 | local $_ = $_;    # local $_ is for changing constants and not changing input | 
| 42 | 8 |  |  |  |  | 13 | s/:/:3a/g; | 
| 43 | 8 |  |  |  |  | 19 | s/([^\w:-])/{ sprintf ':%02x', ord $1 }/ge; | 
|  | 6 |  |  |  |  | 5 |  | 
|  | 6 |  |  |  |  | 23 |  | 
| 44 | 8 |  |  |  |  | 28 | $_; | 
| 45 | 4 |  |  | 4 | 1 | 586 | } 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 | 18 | my $date   = localtime shift; | 
| 56 | 1 |  |  |  |  | 69 | 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 |  |  |  |  | 18 | map { | 
| 71 | 3 |  |  | 3 | 1 | 12 | s/:(\w\w)/{ chr hex $1 }/ge; | 
|  | 7 |  |  |  |  | 9 |  | 
|  | 7 |  |  |  |  | 21 |  | 
| 72 | 7 |  |  |  |  | 21 | $_; | 
| 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 |  | 16066 | use Data::Dumper; | 
|  | 37 |  |  |  |  | 60 |  | 
|  | 37 |  |  |  |  | 14991 |  | 
| 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 | 386 | my ($name) = @_; | 
| 124 |  |  |  |  |  |  |  | 
| 125 | 6 | 50 |  |  |  | 13 | return '' unless defined $name; | 
| 126 | 6 | 100 |  |  |  | 30 | return 'magnet' if $name =~ /\birc\.perl\.org\b/i;    # also match ssl.irc.perl.org | 
| 127 | 3 | 100 |  |  |  | 13 | return 'efnet'  if $name =~ /\befnet\b/i; | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 2 |  |  |  |  | 12 | $name =~ s!^(irc|chat)\.!!;                           # remove common prefixes from server name | 
| 130 | 2 |  |  |  |  | 6 | $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; |