6 function origin($origin, $key) {
7 $origin = parse_origin($origin);
11 class Robot
extends Session
14 protected $connected = false;
15 protected $joined = array();
16 protected $work = array();
18 function __construct($config) {
19 $this->configure($config);
20 parent
::__construct($this->config
->nick
, $this->config
->user
, $this->config
->real);
24 printf("Connecting to %s...\n", $this->config
->host
);
25 $this->doConnect($this->config
->ipv6
, $this->config
->host
, $this->config
->port ?
: 6667);
27 for ( stream_set_blocking(STDIN
, 0), $i = 0, $x = ["–","\\","|","/"];
28 false !== ($fds = @parent
::run(array(STDIN
), null, 1));
30 if (!$this->isConnected()) {
31 printf(" %s \r", $x[$i%4
]);
34 switch ($command = fgets(STDIN
)) {
45 $this->doRaw($command);
55 function isConnected() {
56 return $this->connected
;
59 function disconnect() {
60 $this->connected
= false;
64 function configure($config) {
65 $this->config
= (object) (parse_ini_file($config, true) +
["config" => $config]);
69 printf("Reloading config...\n");
70 $this->configure($this->config
->config
);
75 foreach ($this->joined
as $channel) {
76 $this->doNames($channel);
80 function eventName($event) {
84 $rfl = new \
ReflectionExtension("ircclient");
85 $map = array_map(function($v) {
86 return substr($v, 11);
92 return isset($map[$event]) ?
$map[$event] : "UNKNOWN_$event";
96 if (!empty($this->work
)) {
97 printf("Checking work queue (%d)\n", $this->eventName($event), count($this->work
));
98 list($cb_func, $cb_args) = array_shift($this->work
);
99 call_user_func_array($cb_func, $cb_args);
103 function onError($origin, array $args) {
104 fprintf(STDERR
, "ERROR: %s %s\n", $origin, implode(" ", $args));
107 function onNumeric($origin, $event, array $args) {
108 static $buf = array();
112 if ((list(,, $channel, $users) = $args)) {
113 $buf["names"][$channel] = array_merge((array) $buf["names"][$channel], explode(" ", $users));
118 if ((list(, $channel) = $args)) {
119 foreach ($buf["names"][$channel] as $user) {
120 if ($user{0} === "@") {
121 $user = substr($user, 1);
123 if ($user !== $this->config
->nick
) {
124 printf("Adding work: WHOIS %s\n", $user);
132 $buf["names"][$channel] = array();
136 if ((list(, $nick, $user, $host, , $real) = $args)) {
137 $this->op(substr($user, 1) ."@" . $host);
142 fprintf(STDERR
, "DEBUG: %s %s %s\n", $origin, $this->eventName($event), "<".implode("> <", $args).">");
145 function onConnect($origin, array $args) {
147 $this->connected
= true;
148 printf("Connected to %s as %s\n", $origin, $nick);
154 foreach (array_filter((array) $this->config
, "is_array") as $channel => $config) {
155 $nopart[] = $channel;
156 printf("Joining %s...\n", $channel);
157 $this->doJoin($channel, strlen($config["pass"]) ?
$config["pass"] : NULL);
159 foreach (array_diff($this->joined
, $nopart) as $channel) {
160 printf("Leaving %s...\n", $channel);
161 $this->doPart($channel);
165 function op($channel, $origin) {
166 if (preg_match($this->config
->{$channel}["oper"], $origin)) {
167 $nick = origin($origin, "nick");
168 printf("Set +o %s\n", $nick);
169 $this->doChannelMode($channel, "+o $nick");
173 function onPart($origin, array $args) {
174 $nick = origin($origin, "nick");
176 if ($nick == $this->config
->nick
&& (list($channel) = $args)) {
177 printf("Left %s\n", $channel);
178 unset($this->joined
[array_search($this->joined
, $channel, true)]);
182 function onJoin($origin, array $args) {
183 list($channel) = $args;
184 $nick = origin($origin, "nick");
186 if ($nick === $this->config
->nick
) {
187 printf("Joined %s\n", $channel);
188 $this->joined
[] = $channel;
190 printf("%s joined %s\n", $origin, $channel);
191 $this->op($channel, $origin);
195 function onMode($origin, array $args) {
196 if (count($args) >= 3) {
197 list($channel, $mode, $users) = $args;
199 if ($mode === "+o") {
200 printf("Got +o %s\n", $users);
202 if (preg_match(sprintf("/\b%s\b/", preg_quote($this->config
->nick
)), $users)) {
203 $this->doNames($channel);