-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot
executable file
·58 lines (47 loc) · 2.01 KB
/
bot
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
#!/usr/bin/env php
<?php // -*- mode: php; -*-
require_once(__DIR__.'/lib/err.php');
require_once(__DIR__.'/lib/network.php');
$last = @file_get_contents(__DIR__.'/last_message.txt');
$conf = json_decode(file_get_contents(__DIR__.'/config.json'));
if ($conf === NULL) err("Parse error in configuration file!");
// Fetch the Atom feed
$atom = curl_xml_get($conf->feed);
// Walk the array backwards (not using foreach for that reason)
for( $i = count($atom->entry)-1; $i>=0; $i--) {
// Extract entry and skip it if it's already processed
$entry = $atom->entry[$i];
if ($entry->updated <= $last) continue;
// Match atom feed title
preg_match('/(.*) - ([^:]*): (.*)/', $entry->title, $fa);
preg_match('/([^ ]*) ([^ ]*)( \(([^\)]*)\))?/', $fa[2], $fb);
$email = (string)$entry->author->email ?: '-';
// To make it more readable:
$o = [
'project' => $fa[1],
'title' => $fa[3],
'tracker' => $fb[1],
'id' => $fb[2],
'new_status' => @$fb[4],
'link' => $entry->link->attributes()->href,
'nick' => @$conf->nicks->{$email} ?: $entry->author->name,
];
// Format message
if ($o['tracker'] === 'Revision') {
$msg = "Commit to {$o['new_status']} by {$o['nick']}: {$o['title']} {$o['link']}";
} else if ($o['new_status'] === NULL) {
$msg = "Comment to {$o['id']} by {$o['nick']}: {$o['title']} {$o['link']}";
} else {
$msg = "{$o['new_status']} {$o['tracker']} {$o['id']} by {$o['nick']}! {$o['title']} {$o['link']}";
}
$msg_notice = array_key_exists($o['project'], $conf->channels) ?
"NOTICE {$conf->channels->{$o['project']}} :$msg\n" :
"NOTICE {$conf->default_channel} :{$o['project']}: $msg\n";
irc_raw("$msg_notice");
if (@$conf->debug_target) {
// If debug target is set, output to that user/channel as well
irc_raw("NOTICE {$conf->debug_target} :{$o['project']}: $msg\n");
}
}
// Update the pointer
file_put_contents(__DIR__.'/last_message.txt', $atom->entry[0]->updated);