forked from DamianZaremba/davinci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibirc.php
114 lines (94 loc) · 2.32 KB
/
libirc.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
### IRC protocol stuff
class Message {
public $tags;
public $prefix;
public $cmd;
public $params;
function __construct($tags, $prefix, $params) {
$this->tags = $tags;
$this->prefix = $prefix;
$this->params = $params;
}
static function parse($str) {
$tags = array();
$prefix = null;
$params = self::explode($str);
if ($params[0][0] === "@") {
$tags = array_shift($params);
$tags = substr($tags, 1);
$tagv = explode(";", $tags);
$tags = array();
foreach ($tagv as $x) {
@list($k, $v) = explode("=", $x, 2);
$tags[$k] = $v === null ? true : $v;
}
}
if ($params[0][0] === ":") {
$prefix = array_shift($params);
$prefix = MessagePrefix::parse($prefix);
}
$params[0] = strtoupper($params[0]);
return new self($tags, $prefix, $params);
}
static function explode($str) {
$str = rtrim($str, "\r\n");
$tags = null;
if ($str[0] === "@")
list($tags, $str) = explode(" ", $str, 2);
$str = explode(" :", $str, 2);
$params = explode(" ", $str[0]);
if (count($str) > 1)
$params[] = $str[1];
if ($tags !== null)
array_unshift($params, $tags);
return $params;
}
static function implode($params) {
$trailing = array_pop($params);
if (strpos($trailing, " ") !== false
or strpos($trailing, ":") !== false)
$trailing = ":".$trailing;
$params[] = $trailing;
$str = implode(" ", $params) . "\r\n";
return $str;
}
}
class MessagePrefix {
public $nick;
public $user;
public $host;
function __construct($nick, $user, $host) {
$this->nick = $nick;
$this->user = $user;
$this->host = $host;
}
static function parse($prefix) {
if ($prefix === null)
return new self(null, null, null);
$npos = $prefix[0] == ":" ? 1 : 0;
$upos = strpos($prefix, "!", $npos);
$hpos = strpos($prefix, "@", $upos);
if ($upos === false or $hpos === false) {
$nick = null;
$user = null;
$host = substr($prefix, $npos);
} else {
$nick = substr($prefix, $npos, $upos++-$npos);
$user = substr($prefix, $upos, $hpos++-$upos);
$host = substr($prefix, $hpos);
}
return new self($nick, $user, $host);
}
}
function ischannel($target) {
return $target[0] == "#";
}
function nicktolower($nick) {
$nick = strtolower($nick);
$nick = strtr($nick, "[]\\", "{}|");
return $nick;
}
function nickeq($a, $b) {
return nicktolower($a) === nicktolower($b);
}