]> ruin.nu Git - NDIRC.git/blob - IrcContext.pm
Updated -user to account for discord id
[NDIRC.git] / IrcContext.pm
1 #**************************************************************************
2 #   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
4 #   This program is free software; you can redistribute it and/or modify  *
5 #   it under the terms of the GNU General Public License as published by  *
6 #   the Free Software Foundation; either version 2 of the License, or     *
7 #   (at your option) any later version.                                   *
8 #                                                                         *
9 #   This program is distributed in the hope that it will be useful,       *
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 #   GNU General Public License for more details.                          *
13 #                                                                         *
14 #   You should have received a copy of the GNU General Public License     *
15 #   along with this program; if not, write to the                         *
16 #   Free Software Foundation, Inc.,                                       *
17 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18 #**************************************************************************/
19
20 package NDIRC::IrcContext;
21 use strict;
22 use warnings;
23 use feature ':5.10';
24
25 use Moose;
26 use namespace::autoclean;
27 extends "NDIRC::Context";
28
29 use Set::Object ();
30
31 has host => (
32         is => 'ro',
33         isa => 'Str',
34         required => 1
35 );
36
37 has nick => (
38         is => 'ro',
39         isa => 'Str',
40         required => 1
41 );
42
43 has server => (
44         is => 'ro',
45         isa => 'Object',
46         required => 1
47 );
48
49 has reply_string => (
50         is => 'ro',
51         isa => 'Str',
52         required => 1,
53 );
54
55 sub reply {
56         my ($self,$msg) = @_;
57
58         my @command = split / /, $self->reply_string;
59         $self->message(@command, $msg);
60 }
61
62 sub message {
63         my ($self,$command, $target, $msg) = @_;
64
65         $msg =~ s`<b>(.*?)</b>`${\(chr(2))}$1${\(chr(15))}`gi;
66         $msg =~ s`<c(\d+)>(.*?)</c>`${\(chr(3))}$1$2${\(chr(15))}`gi;
67
68         #Split the message, using the, slightly modified, algorithm from splitlong.pl in the irssi distribution.
69         if ($command eq 'privmsg'){
70                 my $lend = ' ...';
71                 my $lstart = '... ';
72                 my $maxlength = $self->server->{msg_length} - bytes::length("privmsg $target :" . $self->server->nick_name());
73                 my $maxlength2 = $maxlength - bytes::length($lend);
74
75                 if (bytes::length($msg) > ($maxlength)) {
76                         my @spltarr;
77
78                         while (bytes::length($msg) > ($maxlength)) {
79                                 my $pos = rindex($msg, " ", $maxlength2);
80                                 push @spltarr, substr($msg, 0, ($pos < ($maxlength/10 + 4)) ? $maxlength2  : $pos)  . $lend;
81                                 $msg = $lstart . substr($msg, ($pos < ($maxlength/10 + 4)) ? $maxlength2 : $pos+1);
82                         }
83
84                         push @spltarr, $msg;
85                         for (@spltarr) {
86                                 $self->command($command, $target, $_);
87                         }
88                         return;
89                 }
90         }
91         $self->command($command, $target, $msg);
92 }
93
94 sub command {
95         my ($self,@command) = @_;
96
97         $self->server->yield(@command);
98 }
99
100 sub _build_uid {
101         my ($self) = @_;
102
103         my $query = $self->model->prepare(q{
104 SELECT uid FROM users
105 WHERE hostmask = $1
106                 });
107         $query->execute($self->host);
108
109         if (my ($uid) = $query->fetchrow_array){
110                 $query->finish;
111                 return $uid;
112         }
113         return -4;
114 }
115
116 sub valuecolor {
117         shift @_;
118         my $s = $_;
119         $s = $_[1] if $#_ >= 1;
120         $s = "" unless defined $s;
121         return "<c05>$s</c>" if $s eq 'Hostile';
122         return "<c03>$s</c>" if $s eq 'Friendly';
123         return "<c12>$s</c>" if $s eq 'Nap' or $s eq 'NAP';
124         return "<b>$s</b>" if $_[0];
125         return $s;
126 }
127
128 __PACKAGE__->meta->make_immutable;
129 1;