Add my_host setting
[guppy-commits.git] / commits.php
blob577157d3a0a11835e0b9c51086913ae6c1cb528a
1 <?php
2 // By <knittl89@gmail.com>, gry <gry.does.not.check.email@gmail.com>, Kays
3 // GPL <http://www.gnu.org/licenses/>
6 function tinyfy ($url) { return $url ? file_get_contents('http://tinyurl.com/api-create.php?url='.$url) : $url;}
8 $set_my_host = "";
9 $set_channels_no_color = array ('#wedonotlikecolors','#plaintextfans');
10 $set_network = 'chat.freenode.net';
11 $set_channels_to_join = array ('#+n_channel','$another_+n_channel');
12 $set_channels_color = array ('#color_fans');
13 $set_nick = 'myprojcommits';
14 # To have the bot auth to Services set this to 'servicesaccountname:servicespassword' or just 'servicespassword'
15 # (This can also be useful at some private servers)
16 $set_password = '';
18 // sends a message and waits 1 second before sending the next message
19 function send($str) {
20 socket_write($GLOBALS['socket'], $str . "\r\n");
21 sleep(1);
23 // send message to channel
24 function msg($msg, $channel) {
25 send(sprintf('PRIVMSG %s :%s',$channel, str_replace("\r\n",'',$msg)));
27 function abbr($id, $len=7) {
28 return substr($id, 0, $len);
30 function shorten_ref($ref, $ns = 'refs/heads/') {
31 return preg_replace('#^'.$ns.'#', '', $ref);
33 if(isset($_REQUEST['payload'])) {
34 //Connect to IRC.
35 $socket = socket_create(AF_INET6, SOCK_STREAM, SOL_TCP);
36 socket_bind($socket,$set_my_host);
37 echo 'Attempting to connect ...';
38 $result = socket_connect($socket, $set_network, 6667);
39 if ($result === false) {
40 printf("socket_connect() failed.\nReason: (%s)\n", socket_strerror(socket_last_error($socket)));
41 } else {
42 echo "Connected OK.\n";
44 send('user bot a a :A repo.or.cz commits bot');
45 send('pass '.$set_password);
46 send('nick commits');
47 sleep(4); // wait another 4 seconds before joining channels
48 // Join channels
49 foreach ($set_channels_to_join as $channel){
50 send('join '.$channel);
52 sleep(1);
53 // Get the posted data.
54 $data = json_decode($_REQUEST['payload']);
57 // No colors version.
58 foreach ($set_channels_no_color as $channel) {
59 // Branch + push ID.
60 msg(sprintf('%s pushed %s..%s (%d commit%s) to %s in %s:',
61 $data->pusher->name,
62 abbr($data->before), abbr($data->after),
63 count($data->commits), count($data->commits)!=1?'s':'',
64 shorten_ref($data->ref),
65 $data->repository->full_name),$channel);
66 // Each commit information.
67 foreach(array_reverse($data->commits) as $commit) {
68 msg(sprintf('* %s %s (%s, %s) %s',
69 abbr($commit->id),
70 $commit->message,
71 $commit->author->name,
72 $commit->timestamp,
73 tinyfy($commit->url)),$channel);
77 /*
78 * Colored version.
81 foreach ($set_channels_color as $channel) {
83 msg(sprintf('%1$s%2$s:%8$s %3$s%4$s%8$s %5$s%6$s%8$s * %7$s %1$s:%8$s',
84 "\x02",
85 $data->repository->name,
86 "\x0303",
87 $data->pusher->name,
88 "\x0307",
89 shorten_ref($data->ref),
90 abbr($data->after),
91 "\x0f"
92 ),$channel);
94 # for each commit
95 foreach(array_reverse($data->commits) as $commit) {
96 preg_match_all('#.{0,100}[^ ]+ ?#', $commit->message, $cmessages, PREG_PATTERN_ORDER);
98 $count = count($cmessages[0]);
100 if (!$count) continue;
102 for ($e = 0; $e < $count; $e++) {
103 msg(sprintf('%1$s%2$s:%1$s %3$s%4$s',
104 "\x02",
105 $data->repository->name,
106 $cmessages[0][$e],
107 ($e == $count - 1 ? ' - ' . tinyfy($commit->url) : '')
108 ),$channel);
114 // The bot disconnects at end of script.
115 send('QUIT');
124 // json dummy data
125 /*array (
126 'payload' => '{
127 "repository":{
128 "owner":{"email":"user@gshellz.org","name":""},
129 "pull_url":"git://repo.or.cz/guppy.git",
130 "url":"http://repo.or.cz/w/guppy.git",
131 "name":"guppy","full_name":"guppy.git"},
132 "after":"32080e9a5fe22134c9089d6cbd29150d71f47570",
133 "commits":[{"added":[],
134 "author":{"email":"user@gshellz.org","name":"user"},
135 "modified":["5"],"message":"test commit",
136 "timestamp":"2011-05-10 15:30:29",
137 "removed":[],
138 "url":"http://repo.or.cz/w/guppy.git/commit/32080e9a5fe22134c9089d6cbd29150d71f47570",
139 "id":"32080e9a5fe22134c9089d6cbd29150d71f47570"}],
140 "pusher":{"email":"user@gshellz.org","name":"user"},
141 "ref":"refs/heads/master",
142 "before":"f0473eaa9f10d524d2e0d7ece127b10a6840e53a"}',