function diaspost_send(&$a, &$b)
{
    $hostname = 'hubzilla ' . '(' . $a->get_hostname() . ')';
    logger('diaspost_send: invoked', LOGGER_DEBUG);
    if ($b['mid'] != $b['parent_mid']) {
        return;
    }
    if (!is_item_normal($b) || $b['item_private'] || $b['created'] !== $b['edited']) {
        return;
    }
    if (!perm_is_allowed($b['uid'], '', 'view_stream')) {
        return;
    }
    if (!strstr($b['postopts'], 'diaspost')) {
        return;
    }
    logger('diaspost_send: prepare posting', LOGGER_DEBUG);
    $diaspost_username = get_pconfig($b['uid'], 'diaspost', 'diaspost_username');
    $diaspost_password = z_unobscure(get_pconfig($b['uid'], 'diaspost', 'diaspost_password'));
    $diaspost_url = get_pconfig($b['uid'], 'diaspost', 'diaspost_url');
    if ($diaspost_url && $diaspost_username && $diaspost_password) {
        logger('diaspost_send: all values seem to be okay', LOGGER_DEBUG);
        require_once 'include/bb2diaspora.php';
        $tag_arr = array();
        $tags = '';
        $x = preg_match_all('/\\#\\[(.*?)\\](.*?)\\[/', $b['tag'], $matches, PREG_SET_ORDER);
        if ($x) {
            foreach ($matches as $mtch) {
                $tag_arr[] = $mtch[2];
            }
        }
        if (count($tag_arr)) {
            $tags = implode(',', $tag_arr);
        }
        $title = $b['title'];
        $body = $b['body'];
        // Insert a newline before and after a quote
        $body = str_ireplace("[quote", "\n\n[quote", $body);
        $body = str_ireplace("[/quote]", "[/quote]\n\n", $body);
        // strip bookmark indicators
        $body = preg_replace('/\\#\\^\\[([zu])rl/i', '[$1rl', $body);
        $body = preg_replace('/\\#\\^http/i', 'http', $body);
        if (intval(get_pconfig($item['uid'], 'system', 'prevent_tag_hijacking'))) {
            $new_tag = html_entity_decode('⋕', ENT_COMPAT, 'UTF-8');
            $new_mention = html_entity_decode('@', ENT_COMPAT, 'UTF-8');
            // #-tags
            $body = preg_replace('/\\#\\[url/i', $new_tag . '[url', $body);
            $body = preg_replace('/\\#\\[zrl/i', $new_tag . '[zrl', $body);
            // @-mentions
            $body = preg_replace('/\\@\\!?\\[url/i', $new_mention . '[url', $body);
            $body = preg_replace('/\\@\\!?\\[zrl/i', $new_mention . '[zrl', $body);
        }
        // remove multiple newlines
        do {
            $oldbody = $body;
            $body = str_replace("\n\n\n", "\n\n", $body);
        } while ($oldbody != $body);
        // convert to markdown
        $body = bb2diaspora($body, false, true);
        // Adding the title
        if (strlen($title)) {
            $body = "## " . html_entity_decode($title) . "\n\n" . $body;
        }
        require_once "addon/diaspost/diasphp.php";
        try {
            logger('diaspost_send: prepare', LOGGER_DEBUG);
            $conn = new Diasphp($diaspost_url);
            logger('diaspost_send: try to log in ' . $diaspost_username, LOGGER_DEBUG);
            $conn->login($diaspost_username, $diaspost_password);
            logger('diaspost_send: try to send ' . $body, LOGGER_DEBUG);
            //throw new Exception('Test');
            $conn->post($body, $hostname);
            logger('diaspost_send: success');
        } catch (Exception $e) {
            logger("diaspost_send: Error submitting the post: " . $e->getMessage());
            //			logger('diaspost_send: requeueing '.$b['uid'], LOGGER_DEBUG);
            //			$r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
            //			if (count($r))
            //				$a->contact = $r[0]["id"];
            //			$s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $body));
            //			require_once('include/queue_fn.php');
            //			add_to_queue($a->contact,NETWORK_DIASPORA2,$s);
            //			notice(t('Diaspost post failed. Queued for retry.').EOL);
        }
    }
}
function diaspora_send(&$a, &$b)
{
    $hostname = $a->get_hostname();
    logger('diaspora_send: invoked');
    if ($b['deleted'] || $b['private'] || $b['created'] !== $b['edited']) {
        return;
    }
    if (!strstr($b['postopts'], 'diaspora')) {
        return;
    }
    if ($b['parent'] != $b['id']) {
        return;
    }
    logger('diaspora_send: prepare posting', LOGGER_DEBUG);
    $diaspora_username = get_pconfig($b['uid'], 'diaspora', 'diaspora_username');
    $diaspora_password = get_pconfig($b['uid'], 'diaspora', 'diaspora_password');
    $diaspora_url = get_pconfig($b['uid'], 'diaspora', 'diaspora_url');
    if ($diaspora_url && $diaspora_username && $diaspora_password) {
        logger('diaspora_send: all values seem to be okay', LOGGER_DEBUG);
        require_once 'include/bb2diaspora.php';
        $tag_arr = array();
        $tags = '';
        $x = preg_match_all('/\\#\\[(.*?)\\](.*?)\\[/', $b['tag'], $matches, PREG_SET_ORDER);
        if ($x) {
            foreach ($matches as $mtch) {
                $tag_arr[] = $mtch[2];
            }
        }
        if (count($tag_arr)) {
            $tags = implode(',', $tag_arr);
        }
        $title = $b['title'];
        $body = $b['body'];
        // Insert a newline before and after a quote
        $body = str_ireplace("[quote", "\n\n[quote", $body);
        $body = str_ireplace("[/quote]", "[/quote]\n\n", $body);
        // Removal of tags and mentions
        // #-tags
        $body = preg_replace('/#\\[url\\=(\\w+.*?)\\](\\w+.*?)\\[\\/url\\]/i', '#$2', $body);
        // @-mentions
        $body = preg_replace('/@\\[url\\=(\\w+.*?)\\](\\w+.*?)\\[\\/url\\]/i', '@$2', $body);
        // remove multiple newlines
        do {
            $oldbody = $body;
            $body = str_replace("\n\n\n", "\n\n", $body);
        } while ($oldbody != $body);
        // convert to markdown
        $body = bb2diaspora($body, false, true);
        // Adding the title
        if (strlen($title)) {
            $body = "## " . html_entity_decode($title) . "\n\n" . $body;
        }
        require_once "addon/diaspora/diasphp.php";
        try {
            logger('diaspora_send: prepare', LOGGER_DEBUG);
            $conn = new Diasphp($diaspora_url);
            logger('diaspora_send: try to log in ' . $diaspora_username, LOGGER_DEBUG);
            $conn->login($diaspora_username, $diaspora_password);
            logger('diaspora_send: try to send ' . $body, LOGGER_DEBUG);
            //throw new Exception('Test');
            $conn->post($body, $hostname);
            logger('diaspora_send: success');
        } catch (Exception $e) {
            logger("diaspora_send: Error submitting the post: " . $e->getMessage());
            logger('diaspora_send: requeueing ' . $b['uid'], LOGGER_DEBUG);
            $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
            if (count($r)) {
                $a->contact = $r[0]["id"];
            }
            $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $body));
            require_once 'include/queue_fn.php';
            add_to_queue($a->contact, NETWORK_DIASPORA2, $s);
            notice(t('Diaspora post failed. Queued for retry.') . EOL);
        }
    }
}