change: [][0] to [0][]
[guppy-commits.git] / commits.php
blobabfd186b877601b0a445caa90d6e43b07c8839a8
1 <?php
2 // By <knittl89@gmail.com>, gry <gry.does.not.check.email@gmail.com>, Kays
3 // GPL <http://www.gnu.org/licenses/>
5 function tinyfy ($url) { return $url ? file_get_contents('http://tinyurl.com/api-create.php?url='.$url) : $url;}
7 // json dummy data
8 /*array (
9 'payload' => '{
10 "repository":{
11 "owner":{"email":"user@gshellz.org","name":""},
12 "pull_url":"git://repo.or.cz/guppy.git",
13 "url":"http://repo.or.cz/w/guppy.git",
14 "name":"guppy","full_name":"guppy.git"},
15 "after":"32080e9a5fe22134c9089d6cbd29150d71f47570",
16 "commits":[{"added":[],
17 "author":{"email":"user@gshellz.org","name":"user"},
18 "modified":["5"],"message":"test commit",
19 "timestamp":"2011-05-10 15:30:29",
20 "removed":[],
21 "url":"http://repo.or.cz/w/guppy.git/commit/32080e9a5fe22134c9089d6cbd29150d71f47570",
22 "id":"32080e9a5fe22134c9089d6cbd29150d71f47570"}],
23 "pusher":{"email":"user@gshellz.org","name":"user"},
24 "ref":"refs/heads/master",
25 "before":"f0473eaa9f10d524d2e0d7ece127b10a6840e53a"}',
26 )*/
27 // sends a message and waits 1 second before sending the next message
28 function send($str) {
29 socket_write($GLOBALS['socket'], $str . "\r\n");
30 sleep(1);
32 // send message to channel
33 function msg($msg, $channel) {
34 send(sprintf('PRIVMSG %s :%s', "#guppy", str_replace("\r\n",'',$msg)));
36 function abbr($id, $len=7) {
37 return substr($id, 0, $len);
39 function shorten_ref($ref, $ns = 'refs/heads/') {
40 return preg_replace('#^'.$ns.'#', '', $ref);
42 if(isset($_REQUEST['payload'])) {
43 //Connect to IRC.
44 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
45 echo 'Attempting to connect ...';
46 $result = socket_connect($socket, 'irc.freenode.net', 6667);
47 if ($result === false) {
48 printf("socket_connect() failed.\nReason: (%s)\n", socket_strerror(socket_last_error($socket)));
49 } else {
50 echo "Connected OK.\n";
52 send('user a a a :commits bot for guppy');
53 send('nick commits');
54 sleep(4); // wait another 4 seconds before joining the channel
55 // Join channel.
56 # send('join #botters');
57 sleep(1);
58 // Get the posted data.
59 $data = json_decode($_REQUEST['payload']);
61 // Branch + push ID.
62 msg(sprintf('%s pushed %s..%s (%d commit%s) to %s in %s:',
63 $data->pusher->name,
64 abbr($data->before), abbr($data->after),
65 count($data->commits), count($data->commits)!=1?'s':'',
66 shorten_ref($data->ref),
67 $data->repository->full_name));
68 // Each commit information.
69 foreach(array_reverse($data->commits) as $commit) {
70 msg(sprintf('* %s %s (%s, %s) %s',
71 abbr($commit->id),
72 $commit->message,
73 $commit->author->name,
74 $commit->timestamp,
75 $commit->url));
79 ## modified
80 foreach(array_reverse($data->commits) as $commit) {
81 preg_match_all('#.{0,100}[^ ]+ ?#', $commit->message, $cmessages, PREG_PATTERN_ORDER);
83 $count = count($cmessages);
85 if (!$count) continue;
87 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",
88 "\x02",
89 $data->repository->name,
90 "\x0303",
91 $data->pusher->name,
92 "\x0307",
93 shorten_ref($data->ref),
94 abbr($data->after),
95 "\x0f"
96 ));
98 for ($e = 0; $e < $count; $e++) {
99 msg(sprintf('%1$s%2$s:%1$s %3$s%4$s',
100 "\x02",
101 $data->repository->name,
102 $cmessages[0][$e],
103 ($e == $count - 1 ? ' - ' . tinyfy($commit->url) : '')
107 // The bot disconnects at end of script.
108 send('QUIT');