-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathirc_class.php
More file actions
executable file
·112 lines (107 loc) · 3.93 KB
/
Copy pathirc_class.php
File metadata and controls
executable file
·112 lines (107 loc) · 3.93 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
<?php
/*
* (с) 2018 Грибов Павел
* http://грибовы.рф *
* Если исходный код найден в сети - значит лицензия GPL v.3 *
* В противном случае - код собственность ГК Яртелесервис, Мультистрим, Телесервис, Телесервис плюс *
*/
class Tirc{
var $debug=false;
var $server="localhost";
var $port=6667;
var $errno0;
var $errstr="";
var $timeout=10;
var $socket;
var $nick="noname";
var $chanel="noname";
/**
* Внутрення функция для логов
*/
private function putlog($st){
if ($this->debug==true) echo date("H-i-s")." : ".trim($st)."\n";
}
/**
* Конструктор класса. Вызывается при создании экземпляра
* @param type $server - хост сервера
* @param type $port - порт сервера
*/
function __construct($server, $port,$debug=false) {
$this->server = $server;
$this->port = $port;
$this->debug=$debug;
$this->putlog("лог в режиме DEBUG");
$this->putlog("сервер $server, порт $port");
}
/**
* Соединение с сервером. Возврат - результат
* @return type
*/
function connect(){
$this->putlog("пробуем соедениться с irc сервером");
$this->socket=stream_socket_client("tcp://$this->server:$this->port", $this->errno, $this->errstr,$this->timeout,STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT);
$this->putlog("возврат: errno: $this->errno, errstr: $this->errstr, socket: $this->socket");
return $this->socket;
}
/**
* Разрыв соединения с сервером. Возврат - результат
*/
function disconnect(){
$this->putlog("пробуем закрыть соединение с сервером $this->socket");
$ret=fclose($this->socket);
$this->putlog("результат: $ret");
}
/**
* Послать команду серверу. Возврат false или количество посланый байт
* @param type $command
* @return type
*/
function send($command){
$this->putlog("пробуем послать команду сервер $command");
$ret=fwrite($this->socket, $command);
$this->putlog("результат: $ret");
return $ret;
}
function Join($chanel){
$this->chanel=$chanel;
$res=$this->send("JOIN :#$chanel\r\n");
return $res;
}
function is_ping($line){
if(strstr($line, 'PING')) {
$this->putlog("получен PING");
return true;
}
}
function pong(){
$this->send("PONG :".$this->server."\r\n");
$this->putlog("отправлен PONG");
}
function is_msg($line){
if(strstr($line, 'PRIVMSG')) return true;
}
function SetNick($nick){
$this->nick=$nick;
$ret=$this->send("NICK $nick\r\n");
return $ret;
}
function User($name){
$ret=$this->send("USER $name $this->server bla :$name\r\n");
return $ret;
}
function listChans(){
$ret=$this->send("LIST\r\n");
return $ret;
}
function sendmessage($target, $msg){
$ret=$this->send("PRIVMSG $target :$msg\r\n");
return $ret;
}
function loop($onMessage){
while (!feof($this->socket)) {
$res = fgets($this->socket, 256); // ждем сообщений
$onMessage($res);
if($this->is_ping($res)) $this->pong();
};
}
}