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;}
9 $set_channels_no_color = array ('#wedonotlikecolors','#plaintextfans');
10 $set_network = 'chat.freenode.net';
11 $set_channels_to_join = array ('#+n_channel','$another_+n_channel');
12 $set_channels_color = array ('#color_fans');
13 $set_nick = 'myprojcommits';
14 # To have the bot auth to Services set this to 'servicesaccountname:servicespassword' or just 'servicespassword'
15 # (This can also be useful at some private servers)
18 // sends a message and waits 1 second before sending the next message
20 socket_write($GLOBALS['socket'], $str . "\r\n");
23 // send message to channel
24 function msg($msg, $channel) {
25 send(sprintf('PRIVMSG %s :%s',$channel, str_replace("\r\n",'',$msg)));
27 function abbr($id, $len=7) {
28 return substr($id, 0, $len);
30 function shorten_ref($ref, $ns = 'refs/heads/') {
31 return preg_replace('#^'.$ns.'#', '', $ref);
33 if(isset($_REQUEST['payload'])) {
35 $socket = socket_create(AF_INET6
, SOCK_STREAM
, SOL_TCP
);
36 socket_bind($socket,$set_my_host);
37 echo 'Attempting to connect ...';
38 $result = socket_connect($socket, $set_network, 6667);
39 if ($result === false) {
40 printf("socket_connect() failed.\nReason: (%s)\n", socket_strerror(socket_last_error($socket)));
42 echo "Connected OK.\n";
44 send('user bot a a :A repo.or.cz commits bot');
45 send('pass '.$set_password);
47 sleep(4); // wait another 4 seconds before joining channels
49 foreach ($set_channels_to_join as $channel){
50 send('join '.$channel);
53 // Get the posted data.
54 $data = json_decode($_REQUEST['payload']);
58 foreach ($set_channels_no_color as $channel) {
60 msg(sprintf('%s pushed %s..%s (%d commit%s) to %s in %s:',
62 abbr($data->before
), abbr($data->after
),
63 count($data->commits
), count($data->commits
)!=1?
's':'',
64 shorten_ref($data->ref
),
65 $data->repository
->full_name
),$channel);
66 // Each commit information.
67 foreach(array_reverse($data->commits
) as $commit) {
68 msg(sprintf('* %s %s (%s, %s) %s',
71 $commit->author
->name
,
73 tinyfy($commit->url
)),$channel);
81 foreach ($set_channels_color as $channel) {
83 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',
85 $data->repository
->name
,
89 shorten_ref($data->ref
),
95 foreach(array_reverse($data->commits
) as $commit) {
96 preg_match_all('#.{0,100}[^ ]+ ?#', $commit->message
, $cmessages, PREG_PATTERN_ORDER
);
98 $count = count($cmessages[0]);
100 if (!$count) continue;
102 for ($e = 0; $e < $count; $e++
) {
103 msg(sprintf('%1$s%2$s:%1$s %3$s%4$s',
105 $data->repository
->name
,
107 ($e == $count - 1 ?
' - ' . tinyfy($commit->url
) : '')
114 // The bot disconnects at end of script.
128 "owner":{"email":"user@gshellz.org","name":""},
129 "pull_url":"git://repo.or.cz/guppy.git",
130 "url":"http://repo.or.cz/w/guppy.git",
131 "name":"guppy","full_name":"guppy.git"},
132 "after":"32080e9a5fe22134c9089d6cbd29150d71f47570",
133 "commits":[{"added":[],
134 "author":{"email":"user@gshellz.org","name":"user"},
135 "modified":["5"],"message":"test commit",
136 "timestamp":"2011-05-10 15:30:29",
138 "url":"http://repo.or.cz/w/guppy.git/commit/32080e9a5fe22134c9089d6cbd29150d71f47570",
139 "id":"32080e9a5fe22134c9089d6cbd29150d71f47570"}],
140 "pusher":{"email":"user@gshellz.org","name":"user"},
141 "ref":"refs/heads/master",
142 "before":"f0473eaa9f10d524d2e0d7ece127b10a6840e53a"}',