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;}
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",
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"}',
28 // sends a message and waits 1 second before sending the next message
30 socket_write($GLOBALS['socket'], $str . "\r\n");
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'])) {
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)));
52 echo "Connected OK.\n";
54 send('user a a a :commits bot for guppy');
56 sleep(4); // wait another 4 seconds before joining the channel
57 // Join channel. Uncomment if it's +n.
58 # send('join #guppy');
60 // Get the posted data.
61 $data = json_decode($_REQUEST['payload']);
65 msg(sprintf('%s pushed %s..%s (%d commit%s) to %s in %s:',
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',
76 $commit->author
->name
,
78 tinyfy($commit->url
)));
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',
87 $data->repository->name,
91 shorten_ref($data->ref),
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',
107 $data->repository->name,
109 ($e == $count - 1 ? ' - ' . tinyfy($commit->url) : '')
115 // The bot disconnects at end of script.