-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path01-channels.php
91 lines (70 loc) · 2.83 KB
/
01-channels.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
use Clue\React\Quassel\Factory;
use Clue\React\Quassel\Client;
use Clue\React\Quassel\Io\Protocol;
require __DIR__ . '/../vendor/autoload.php';
$host = '127.0.0.1';
if (isset($argv[1])) { $host = $argv[1]; }
echo 'Server: ' . $host . PHP_EOL;
echo 'User name: ';
$user = trim(fgets(STDIN));
echo 'Password: ';
$pass = trim(fgets(STDIN));
$factory = new Factory();
$uri = rawurlencode($user) . ':' . rawurlencode($pass) . '@' . $host;
$factory->createClient($uri)->then(function (Client $client) {
var_dump('CONNECTED');
$await = array();
$client->on('data', function ($message) use ($client, &$await) {
// session initialized => initialize all networks
if (isset($message->MsgType) && $message->MsgType === 'SessionInit') {
var_dump('session initialized');
foreach ($message->SessionState->NetworkIds as $nid) {
var_dump('requesting Network for ' . $nid . ', this may take a few seconds');
$client->writeInitRequest("Network", $nid);
$await[$nid] = true;
}
if ($await) {
var_dump('initialization completed, now waiting for network information');
} else {
var_dump('no networks found');
$client->close();
}
return;
}
// network information received
if (isset($message[0]) && $message[0] === Protocol::REQUEST_INITDATA && $message[1] === 'Network') {
// print network information except for huge users/channels list
$info = clone $message[3];
//unset($info->IrcUsersAndChannels);
echo json_encode($info, JSON_PRETTY_PRINT) . PHP_EOL;
// print names of all known channels on this network (if connected)
if ($message[3]->IrcUsersAndChannels->Channels) {
foreach ($message[3]->IrcUsersAndChannels->Channels as $channel) {
echo $channel->name . PHP_EOL;
}
} else {
echo 'No channels in ' . $message[3]->networkName . PHP_EOL;
}
// close connection after showing all networks
$id = $message[2];
unset($await[$id]);
if (!$await) {
var_dump('network information received');
$client->close();
}
return;
}
// ignore initial CoreInfo reporting connected clients for Quassel v0.13+
if (is_array($message) && isset($message[1]) && $message[1] === 'CoreInfo') {
return;
}
echo 'received unhandled: ' . json_encode($message, JSON_PRETTY_PRINT) . PHP_EOL;
});
$client->on('error', 'printf');
$client->on('close', function () {
echo 'Connection closed' . PHP_EOL;
});
})->then(null, function ($e) {
echo $e;
});