This repository was archived by the owner on Apr 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.class.php
More file actions
164 lines (145 loc) · 4.42 KB
/
client.class.php
File metadata and controls
164 lines (145 loc) · 4.42 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/*
* ircc - a handy, portable console irc client
*
* Copyright (C) 2008 Robin Burchell <w00t@inspircd.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*/
class Client
{
public $irc; /* server connection */
public $output; /* ncurses stuff */
private $nick; /* stores nickname, passed on new connection creation */
private $username; /* stores username, passed on new connection creation */
/*
* Initiates a shutdown of the client, sends QUIT to all connections, then shuts down ncurses, etc.
*/
public function shutdown($msg = "")
{
$this->irc->squit($msg);
$this->output = null;
die();
}
/*
* Polls all file descriptors for activity and hits appropriate callbacks if activity has been detected.
*/
public function Poll()
{
/*
* CAREFUL:
* Optimisers, note! This is something that catches a lot of people out.
* select() syscall modifies the arrays it is passed, so we MUST regenerate the array for each call.
* NOT doing this is suicide, and will give you hard to track down bugs/problems etc. (fds "disappearing" etc).
*/
$aRead = $aWrite = $aExcept = array(); // XXX we should eventually poll for write too.
$aRead[] = $this->output->stdin;
if ($this->irc->sp)
$aRead[] = $this->irc->sp;
/*
* It's annoying to have to @suppress warnings on stream_select(), but PHP raises E_NOTICE if select
* is interrupted by a signal etc, and I have no way of trapping that.
*
* Except, that in their infinite wisdom, not only have they created a nonsensical error on this condition
* (think SIGWINCH in a console application, you idiots!), but it also is un-suppressable!
*
* The error handler will suppress it instead, but YUCK.
*/
$iStreams = stream_select($aRead, $aWrite, $aExcept, 1);
/*
* If no streams have activity, there's no point in checking what callbacks to hit. Duh.
*/
if ($iStreams == 0)
return;
foreach ($aRead as $iSocket)
{
if ($iSocket == $this->output->stdin)
{
// stdin ready, hit the callback
$this->callback_process_stdin();
}
else
{
// irc callback
$this->irc->procline();
}
}
}
/*
* Callback to read and process stdin whenever there is input available.
* XXX eventually this (and all other sockets) should be wrapped in a class with methods for callbacks.
*/
public function callback_process_stdin()
{
if (($input = $this->output->GetUserInput()))
{
//we have a line of input
if(substr($input, 0, 1) == "/")
{
// Tear off /
$input = substr($input, 1);
// This is all ugly, really. Backwards compatibility.
$ex = Utils::ParseLine($input);
$cmd = strtolower($ex[0]);
$msg = implode($ex, " ");
$msgf = implode(array_slice($ex, 1), " "); // same as $msg, except without the command prefix.
// XXX this doesn't allow for aliases yet.
if (file_exists("commands/" . $cmd . ".command.inc.php"))
include("commands/" . $cmd . ".command.inc.php");
else
$this->irc->sendline($cmd." ".$msgf);
}
else
{
$this->irc->say($input);
}
}
}
/*
* ..it's a bird, ..it's a plane..
* .. no, moron, it's a constructor.
*/
public function __construct()
{
$sStatus = "Status";
$this->output = new ncurse($this);
$this->output->SetDisplayVar("nick", ""); // Bit of a hack. Stops the AddBuffer below exploding things.
$this->output->AddBuffer($sStatus); // Create status buffer. ALWAYS at position 0.
$this->irc = new irc($this);
$this->output->Output(BUFFER_STATUS, IRCC_VER . " - irc client");
$sMotd = file_get_contents("ircc.motd");
$aLines = explode("\n", $sMotd);
foreach ($aLines as $sLine)
{
$this->output->Output(BUFFER_STATUS, $sLine);
}
$this->username = 'ircc';
if(!empty($_ENV['LOGNAME']))
{
$this->username = $_ENV['LOGNAME'];
}
elseif(!empty($_ENV['USER']))
{
$this->username = $_ENV['USER'];
}
$this->nick = $this->username;
}
public function Run()
{
while (true)
{
// poll() may hang a while until activity on stdin or IRC
$this->Poll();
// Update time display.
$this->output->setuserinput();
// Pop an error off and display to the user, if there is one.
// Only display one to avoid flooding.
global $aErrors;
if (($sMsg = array_pop($aErrors)))
$this->output->Output(BUFFER_CURRENT, $sMsg);
}
}
}
?>