gnu-social/vendor/diogocomposer/xmpphp/examples/cli_longrun_example.php

139 lines
3.8 KiB
PHP
Raw Normal View History

<?php
/**
* @file: XMPPHP Cli example
*
* @info: If this script doesn't work, are you running 64-bit PHP with < 5.2.6?
*/
/**
* Activate full error reporting
* error_reporting(E_ALL & E_STRICT);
*
* XMPPHP Log levels:
*
* LEVEL_ERROR = 0;
* LEVEL_WARNING = 1;
* LEVEL_INFO = 2;
* LEVEL_DEBUG = 3;
* LEVEL_VERBOSE = 4;
*/
2019-11-17 01:51:01 +09:00
$conf = [
'server' => 'im.server.tld',
'port' => 5222,
'username' => 'username',
'password' => 'password',
'proto' => 'xmpphp',
'domain' => 'domain.net',
'printlog' => true,
'loglevel' => XMPPHP\Log::LEVEL_VERBOSE,
];
// Easy and simple for access to variables with their names
extract($conf);
$conn = new XMPPHP\XMPP($server, $port, $username, $password, $proto, $domain, $printlog, $loglevel);
$conn->autoSubscribe();
2019-11-17 01:51:01 +09:00
$vcard_request = [];
try {
$conn->connect();
while (!$conn->isDisconnected()) {
2019-11-17 01:51:01 +09:00
$events = ['message', 'presence', 'end_stream', 'session_start', 'vcard'];
$payloads = $conn->processUntil($events);
foreach ($payloads as $result) {
list($event, $data) = $result;
if (isset($data)) {
extract($data);
}
switch ($event) {
2019-11-17 01:51:01 +09:00
case 'message':
2019-11-17 01:51:01 +09:00
if (!$body) {
break;
}
2019-11-17 01:51:01 +09:00
echo str_repeat('-', 80);
echo "Message from: $from";
2019-11-17 01:51:01 +09:00
if (isset($subject)) {
echo "Subject: $subject";
}
2019-11-17 01:51:01 +09:00
echo $body;
echo str_repeat('-', 80);
2019-11-17 01:51:01 +09:00
$cmd = explode(' ', $body);
$body = "Mi no entender! '$body'";
$conn->message($from, $body, $type);
2019-11-17 01:51:01 +09:00
if (isset($cmd[0])) {
if ($cmd[0] == 'quit') {
$conn->disconnect();
}
2019-11-17 01:51:01 +09:00
if ($cmd[0] == 'break') {
$conn->send('</end>');
}
2019-11-17 01:51:01 +09:00
if ($cmd[0] == 'vcard') {
if (!isset($cmd[1])) {
$cmd[1] = $conn->user;
}
2019-11-17 01:51:01 +09:00
// Take a note which user requested which vcard
$vcard_request[$from] = $cmd[1];
// Request the vcard
$conn->getVCard($cmd[1]);
}
}
break;
2019-11-17 01:51:01 +09:00
case 'presence':
2019-11-17 01:51:01 +09:00
echo "Presence: $from [$show] $status\n";
break;
2019-11-17 01:51:01 +09:00
case 'session_start':
2019-11-17 01:51:01 +09:00
echo "Session start\n";
$conn->getRoster();
$conn->presence('Quasar!');
break;
2019-11-17 01:51:01 +09:00
case 'vcard':
2019-11-17 01:51:01 +09:00
$deliver = array_keys($vcard_request, $from);
$msg = '';
2019-11-17 01:51:01 +09:00
foreach ($data as $key => $item) {
$msg .= $key . ': ';
2019-11-17 01:51:01 +09:00
if (is_array($item)) {
$msg .= "\n";
foreach ($item as $subkey => $subitem) {
$msg .= ' ' . $subkey . ':' . $subitem . "\n";
}
} else {
$msg .= $item . "\n";
}
}
2019-11-17 01:51:01 +09:00
foreach ($deliver as $sendjid) {
unset($vcard_request[$sendjid]);
$conn->message($sendjid, $msg, 'chat');
}
break;
}
}
}
} catch (XMPPHP\Exception $e) {
die($e->getMessage());
}