Created
February 5, 2022 14:08
-
-
Save DarthGandalf/c64095f7e98c7a1be439dee4c40b7ca5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use utf8; | |
use HTTP::Response; | |
use JSON; | |
package linker; | |
use base 'ZNC::Module'; | |
sub description { | |
"OpenMW Expandor bot" | |
} | |
sub module_types { | |
$ZNC::CModInfo::UserModule | |
} | |
sub put_chan { | |
my ($self, $chan, $msg) = @_; | |
$self->PutIRC("PRIVMSG $chan :$msg"); | |
} | |
sub OnChanMsg { | |
my ($self, $nick, $chan, $what) = @_; | |
$nick = $nick->GetNick; | |
$chan = $chan->GetName; | |
my $count = 0; | |
#if (my ($type, $num) = $what=~m@(?|\B([#!])(\d+)|https://gitlab.com/OpenMW/openmw/-/(issues|merge_requests)/(\d+))@) { | |
#if (my ($type, $num) = $what=~m@\B([#!])(\d{2,})@) { | |
for (my ($w, $type, $num) = ($what, '', 0); ($type, $num, $w) = $w=~m@\B([#!])(\d{2,})(.*)@ and $count++ < 5;) { | |
if ($num > 0) { | |
if ($type eq '#' or $type eq 'issues') { | |
$type = 'issues'; | |
} else { | |
$type = 'merge_requests'; | |
} | |
$self->CreateSocket('linker::socket', $type, $num, $self->GetNetwork, $chan); | |
} | |
} | |
return $ZNC::CONTINUE; | |
} | |
package linker::socket; | |
use base 'ZNC::Socket'; | |
sub Init { | |
my $self = shift; | |
$self->{type} = shift; | |
$self->{num} = shift; | |
$self->{network} = shift; | |
$self->{chan} = shift; | |
$self->{response} = ''; | |
$self->DisableReadLine; | |
$self->Connect('gitlab.com', 443, ssl=>1); | |
$self->Write("GET https://gitlab.com/api/v4/projects/7107382/$self->{type}/$self->{num} HTTP/1.0\r\n"); | |
$self->Write("Host: gitlab.com\r\n"); | |
$self->Write("\r\n"); | |
} | |
sub OnReadData { | |
my $self = shift; | |
my $data = shift; | |
print "new data |$data|\n"; | |
$self->{response} .= $data; | |
} | |
sub OnDisconnected { | |
my $self = shift; | |
my $response = HTTP::Response->parse($self->{response}); | |
if ($response->is_success) { | |
my $data = JSON->new->utf8->decode($response->decoded_content); | |
$self->{network}->PutIRC("PRIVMSG $self->{chan} :$data->{web_url} “$data->{title}” ($data->{state})"); | |
} else { | |
my $error = $response->status_line; | |
$self->{network}->PutIRC("PRIVMSG $self->{chan} :https://github.com/OpenMW/openmw/-/$self->{type}/$self->{num} – $error"); | |
} | |
} | |
sub OnTimeout { | |
my $self = shift; | |
$self->{network}->PutIRC("PRIVMSG $self->{chan} :gitlab timeout"); | |
} | |
sub OnConnectionRefused { | |
my $self = shift; | |
$self->{network}->PutIRC("PRIVMSG $self->{chan} :gitlab connection refused"); | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment