forked from xPaw/GitHub-WebHook
/
irker.php
162 lines (128 loc) · 3.6 KB
/
irker.php
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
Header( 'Content-Type: text/plain; charset=utf-8' );
// Don't do this in production!
ini_set( 'error_reporting', -1 );
ini_set( 'display_errors', 1 );
http_response_code( 500 );
require __DIR__ . '/config.php';
require __DIR__ . '/GitHub_WebHook.php';
require __DIR__ . '/GitHub_IRC.php';
$Socket = false;
$Hook = new GitHub_WebHook( );
try
{
// This check is optional, you can implement some secret GET param for example
if( !$Hook->ValidateIPAddress() )
{
throw new Exception( 'Unauthorized.' );
}
$Hook->ProcessRequest( );
$RepositoryName = $Hook->GetFullRepositoryName();
echo 'Received ' . $Hook->GetEventType() . ' in repository ' . $RepositoryName . PHP_EOL;
//print_r( $Hook->GetPayload() );
// Format IRC message
$IRC = new GitHub_IRC( $Hook->GetEventType(), $Hook->GetPayload(), 'shorten_url' );
$Message = $IRC->GetMessage();
if( isset( $_GET[ 'strip_colors' ] ) )
{
$Message = strip_colors( $Message );
}
if( empty( $Message ) )
{
throw new Exception( 'Empty message, not sending.' );
}
// Format irker payload
$IrkerPayload = '';
foreach( $Channels as $Channel => $SendTargets )
{
if( !wild( $RepositoryName, $Channel ) )
{
continue;
}
echo 'Matched "' . $RepositoryName . '" as "' . $Channel . '"' . PHP_EOL;
foreach( $SendTargets as $Target )
{
$IrkerPayload .= json_encode( Array(
'to' => $Target,
'privmsg' => $Message
) ) . "\n";
}
}
if( empty( $IrkerPayload ) )
{
throw new Exception( 'Empty payload, not sending.' );
}
// Send to irker
$Socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
if( $Socket === false
|| socket_connect( $Socket, IRKER_HOST, IRKER_PORT ) === false
|| socket_write( $Socket, $IrkerPayload ) === false )
{
throw new Exception( 'Socket error: ' . socket_strerror( socket_last_error() ) );
}
echo 'Payload sent to irker' . PHP_EOL . $IrkerPayload . PHP_EOL;
http_response_code( 202 );
}
catch( GitHubIgnoredEventException $e )
{
http_response_code( 200 );
echo 'This GitHub event is ignored.';
}
catch( GitHubNotImplementedException $e )
{
http_response_code( 501 );
echo 'Unsupported GitHub event: ' . $e->EventName;
}
catch( Exception $e )
{
echo 'Exception: ' . $e->getMessage() . PHP_EOL;
}
// Cleanup
if( $Socket !== false )
{
socket_close( $Socket );
}
function wild( $string, $expression )
{
if( strpos( $expression, '*' ) === false )
{
return strcmp( $expression, $string ) === 0;
}
$expression = preg_quote( $expression, '/' );
$expression = str_replace( '\*', '.*', $expression );
return preg_match( '/^' . $expression . '$/', $string ) === 1;
}
function strip_colors( $message )
{
return preg_replace( "/\x03(\d\d)?/", '', $message );
}
function shorten_url( $url )
{
$c = curl_init( );
curl_setopt_array( $c, [
CURLOPT_USERAGENT => 'https://github.com/xPaw/GitHub-WebHook',
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_URL => 'https://git.io',
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 5,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => http_build_query( [ 'url' => $url ] )
] );
$response = curl_exec( $c );
curl_close( $c );
if( $response !== false )
{
$response = explode( "\r\n", $response );
$key = 'Location: ';
foreach( $response as $header )
{
if( strpos( $header, $key ) === 0 )
{
return substr( $header, strlen( $key ) );
}
}
}
return $url;
}