no change. looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo...
[guppy-commits.git] / commits.php
blobbc866c6b1438dc9cf28fcb82999dd60ef50155f7
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 echo 'Attempting to connect ...';
47 $result = socket_connect($socket, 'irc.freenode.net', 6667);
48 if ($result === false) {
49 printf("socket_connect() failed.\nReason: (%s)\n", socket_strerror(socket_last_error($socket)));
50 } else {
51 echo "Connected OK.\n";
53 send('user a a a :commits bot for guppy');
54 send('nick commits');
55 sleep(4); // wait another 4 seconds before joining the channel
56 // Join channel.
57 # send('join #botters');
58 sleep(1);
59 // Get the posted data.
60 $data = json_decode($_REQUEST['payload']);
62 // Branch + push ID.
63 msg(sprintf('%s pushed %s..%s (%d commit%s) to %s in %s:',
64 $data->pusher->name,
65 abbr($data->before), abbr($data->after),
66 count($data->commits), count($data->commits)!=1?'s':'',
67 shorten_ref($data->ref),
68 $data->repository->full_name));
69 // Each commit information.
70 foreach(array_reverse($data->commits) as $commit) {
71 msg(sprintf('* %s %s (%s, %s) %s',
72 abbr($commit->id),
73 $commit->message,
74 $commit->author->name,
75 $commit->timestamp,
76 $commit->url));
80 ## modified
81 foreach(array_reverse($data->commits) as $commit) {
82 preg_match_all('#.{0,100}[^ ]+ ?#', $commit->message, $cmessages, PREG_PATTERN_ORDER);
84 $count = count($cmessages);
86 if (!$count) continue;
88 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',
89 "\x02",
90 $data->repository->name,
91 "\x0303",
92 $data->pusher->name,
93 "\x0307",
94 shorten_ref($data->ref),
95 abbr($data->after),
96 "\x0f"
97 ));
99 for ($e = 0; $e < $count; $e++) {
100 msg(sprintf('%1$s%2$s:%1$s %3$s%4$s',
101 "\x02",
102 $data->repository->name,
103 $cmessages[0][$e],
104 ($e == $count - 1 ? ' - ' . tinyfy($commit->url) : '')
108 // The bot disconnects at end of script.
109 send('QUIT');