xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / inc / wh / http / matrix.inc.php (3ee2091c202c02377523b3d04ca9ea8432aa18ce) (4,401B) (mode 100644) [raw]
<?php

function rg_wh_matrix_fill_vars(&$rg, &$junk)
{
	$a = $rg['wh']['idata'];

	$a['user'] = trim(rg_var_str('wh::idata::user'));
	$a['pass'] = trim(rg_var_str('wh::idata::pass'));
	$a['room'] = trim(rg_var_str('wh::idata::room'));
	$a['bot_name'] = trim(rg_var_str('wh::idata::bot_name'));

	$rg['wh']['idata'] = $a;
}

function rg_wh_matrix_send($db, $ev)
{
	rg_log_ml('DEBUG: wh_matrix_send: ev: ' . print_r($ev, TRUE));

	$ret = FALSE;
	while (1) {
		// common
		// Enforce content type
		$ev['wh']['idata']['content_type'] = 'application/json';
		$ev['wh']['idata']['url'] = rtrim($ev['wh']['idata']['url'], '/');

		// login
		// https://matrix.org/docs/spec/client_server/latest#login
		$ev1 = $ev;
		$ev1['wh']['idata']['url'] .= '/_matrix/client/r0/login';
		$b = array(
			'type' => 'm.login.password',
			'identifier' => array(
				'type' => 'm.id.user',
				'user' => $ev1['wh']['idata']['user']
			),
			'password' => $ev1['wh']['idata']['pass'],
			'device_id' => 'RocketGitBot-' . $ev1['wh']['id'],
			'initial_device_display_name' => $ev1['wh']['idata']['bot_name']
		);
		$ev1['wh']['idata']['final'] = json_encode($b);

		rg_wh_http_send_helper($db, $ev1);
		// TODO: what to do with the error code?
		rg_log_ml('DEBUG: helper returned: ' . print_r($ev1['wh'], TRUE));
		$j = @json_decode($ev1['wh']['out']['body'], TRUE);
		rg_log_ml('DEBUG: JSON: ' . print_r($j, TRUE));
		if ($j === NULL) {
			rg_log('DEBUG: cannot decode json; what to do? TODO');
			break;
		}
		if (isset($j['errcode'])) {
			rg_log('Error returned: ' . $j['errcode']);
			break;
		}
		if (!isset($j['access_token'])) {
			rg_log('no access_token field!'); // TODO
			break;
		}
		$at = $j['access_token'];


		// join
		// https://matrix.org/docs/spec/client_server/r0.2.0#id157
		rg_log_ml('DEBUG: now, joining room: ' . print_r($ev, TRUE));
		$ev2 = $ev;
		$ev2['wh']['idata']['url'] .= '/_matrix/client/r0/join/'
			. rawurlencode($ev2['wh']['idata']['room'])
			. '?access_token=' . $at;
		$b = array();
		$ev2['wh']['idata']['final'] = json_encode($b);

		rg_wh_http_send_helper($db, $ev2);
		rg_log_ml('DEBUG: helper returned: ' . print_r($ev2['wh']['out'], TRUE));


		// message
		// https://matrix.org/docs/spec/client_server/latest#id320
		rg_log_ml('DEBUG: now, sending the message ev: ' . print_r($ev, TRUE));
		$ev2 = $ev;
		$ev2['wh']['idata']['url'] .= '/_matrix/client/r0/rooms/'
			. rawurlencode($ev2['wh']['idata']['room'])
			. '/send/m.room.message?access_token=' . $at;
		$b = array(
			'msgtype' => 'm.text',
			'body' => $ev2['wh']['idata']['final']
		);
		$ev2['wh']['idata']['final'] = json_encode($b);

		rg_wh_http_send_helper($db, $ev2);
		rg_log_ml('DEBUG: helper returned: ' . print_r($ev2['wh']['out'], TRUE));
		// TODO: what to do with the error code?
		rg_log_ml('DEBUG: helper returned: ' . print_r($ev2['wh'], TRUE));
		$j = @json_decode($ev2['wh']['out']['body'], TRUE);
		rg_log_ml('DEBUG: JSON: ' . print_r($j, TRUE));
		if ($j === NULL) {
			rg_log('DEBUG: cannot decode json; what to do? TODO');
			break;
		}
		if (isset($j['errcode'])) {
			rg_log('Error returned: ' . $j['errcode']);
			break;
		}

		$ret = array();
		break;
	}

	return array();
}

function rg_wh_matrix_add_form_pre($db, &$rg)
{
	$a = $rg['wh']['idata'];
	rg_log_ml('DEBUG: wh_matrix_add_form_pre: ' . print_r($a, TRUE));

	if (!isset($rg['wh']['description']))
		$rg['wh']['description'] = 'Matrix.org integration';

	$a['itype'] = 2;
	$a['itype_force'] = 1;
	$a['url_example'] = 'https://matrix.example.tld';

	if (!isset($a['events']))
		$a['events'] = 'P';

	if (!isset($a['custom_body']))
		$a['custom_body'] = 'Hook ##hook_id## reports: ##ip## pushed into'
		. ' repo ##repo##, branch ##branch##'
		. ' (commit id ##commit##, url ##commit_url##)';

	if (!isset($a['user']))
		$a['user'] = '';

	if (!isset($a['pass']))
		$a['pass'] = '';

	if (!isset($a['room']))
		$a['room'] = '!';

	if (!isset($a['bot_name']))
		$a['bot_name'] = 'RocketGit bot';

	$rg['wh']['idata'] = $a;
}

function rg_wh_matrix_add_form_post($db, &$rg)
{
	$rg['HTML:custom_form'] .= rg_template('user/settings/wh/http/matrix/form.html',
		$rg, TRUE /*xss*/);
}

$cb = array(
	'fill_vars' => 'rg_wh_matrix_fill_vars',
	'send' => 'rg_wh_matrix_send',
	'add_form_pre' => 'rg_wh_matrix_add_form_pre',
	'add_form_post' => 'rg_wh_matrix_add_form_post'
);

rg_wh_register_subtype('http', 'matrix', 'Matrix.org', $cb);

Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master