web feedback done. add mark in todo
[guppy-commits.git] / commits.php
blobadd98d423df2271fa37b4a634609b6b442800685
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 // json dummy data
9 /*array (
10 'payload' => '{
11 "repository":{
12 "owner":{"email":"user@gshellz.org","name":""},
13 "pull_url":"git://repo.or.cz/guppy.git",
14 "url":"http://repo.or.cz/w/guppy.git",
15 "name":"guppy","full_name":"guppy.git"},
16 "after":"32080e9a5fe22134c9089d6cbd29150d71f47570",
17 "commits":[{"added":[],
18 "author":{"email":"user@gshellz.org","name":"user"},
19 "modified":["5"],"message":"test commit",
20 "timestamp":"2011-05-10 15:30:29",
21 "removed":[],
22 "url":"http://repo.or.cz/w/guppy.git/commit/32080e9a5fe22134c9089d6cbd29150d71f47570",
23 "id":"32080e9a5fe22134c9089d6cbd29150d71f47570"}],
24 "pusher":{"email":"user@gshellz.org","name":"user"},
25 "ref":"refs/heads/master",
26 "before":"f0473eaa9f10d524d2e0d7ece127b10a6840e53a"}',
27 )*/
28 // sends a message and waits 1 second before sending the next message
29 function send($str) {
30 socket_write($GLOBALS['socket'], $str . "\r\n");
31 sleep(1);
33 // send message to channel
34 function msg($msg, $channel) {
35 send(sprintf('PRIVMSG %s :%s', "#guppy", str_replace("\r\n",'',$msg)));
37 function abbr($id, $len=7) {
38 return substr($id, 0, $len);
40 function shorten_ref($ref, $ns = 'refs/heads/') {
41 return preg_replace('#^'.$ns.'#', '', $ref);
43 if(isset($_REQUEST['payload'])) {
44 //Connect to IRC.
45 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
46 // put 'AF_INET6' here to use ipv6 connection
47 echo 'Attempting to connect ...';
48 $result = socket_connect($socket, 'irc.freenode.net', 6667);
49 if ($result === false) {
50 printf("socket_connect() failed.\nReason: (%s)\n", socket_strerror(socket_last_error($socket)));
51 } else {
52 echo "Connected OK.\n";
54 send('user a a a :commits bot for guppy');
55 send('nick commits');
56 sleep(4); // wait another 4 seconds before joining the channel
57 // Join channel. Uncomment if it's +n.
58 # send('join #guppy');
59 sleep(1);
60 // Get the posted data.
61 $data = json_decode($_REQUEST['payload']);
63 // No colors version.
64 // Branch + push ID.
65 msg(sprintf('%s pushed %s..%s (%d commit%s) to %s in %s:',
66 $data->pusher->name,
67 abbr($data->before), abbr($data->after),
68 count($data->commits), count($data->commits)!=1?'s':'',
69 shorten_ref($data->ref),
70 $data->repository->full_name));
71 // Each commit information.
72 foreach(array_reverse($data->commits) as $commit) {
73 msg(sprintf('* %s %s (%s, %s) %s',
74 abbr($commit->id),
75 $commit->message,
76 $commit->author->name,
77 $commit->timestamp,
78 tinyfy($commit->url)));
81 /*
82 * Colored version.
85 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',
86 "\x02",
87 $data->repository->name,
88 "\x0303",
89 $data->pusher->name,
90 "\x0307",
91 shorten_ref($data->ref),
92 abbr($data->after),
93 "\x0f"
94 ));
96 # for each commit
97 foreach(array_reverse($data->commits) as $commit) {
98 preg_match_all('#.{0,100}[^ ]+ ?#', $commit->message, $cmessages, PREG_PATTERN_ORDER);
100 $count = count($cmessages[0]);
102 if (!$count) continue;
104 for ($e = 0; $e < $count; $e++) {
105 msg(sprintf('%1$s%2$s:%1$s %3$s%4$s',
106 "\x02",
107 $data->repository->name,
108 $cmessages[0][$e],
109 ($e == $count - 1 ? ' - ' . tinyfy($commit->url) : '')
115 // The bot disconnects at end of script.
116 send('QUIT');