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;}
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",
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"}',
27 // sends a message and waits 1 second before sending the next message
29 socket_write($GLOBALS['socket'], $str . "\r\n");
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'])) {
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)));
50 echo "Connected OK.\n";
52 send('user a a a :commits bot for guppy');
54 sleep(4); // wait another 4 seconds before joining the channel
56 # send('join #botters');
58 // Get the posted data.
59 $data = json_decode($_REQUEST['payload']);
62 msg(sprintf('%s pushed %s..%s (%d commit%s) to %s in %s:',
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',
73 $commit->author->name,
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",
89 $data->repository
->name
,
93 shorten_ref($data->ref
),
98 for ($e = 0; $e < $count; $e++
) {
99 msg(sprintf('%1$s%2$s:%1$s %3$s%4$s',
101 $data->repository
->name
,
103 ($e == $count - 1 ?
' - ' . tinyfy($commit->url
) : '')
107 // The bot disconnects at end of script.