-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathirc.inc
More file actions
executable file
·183 lines (164 loc) · 3.77 KB
/
irc.inc
File metadata and controls
executable file
·183 lines (164 loc) · 3.77 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
class irc {
protected $socket;
protected $TokenBucket;
public function __construct() {
$this->TokenBucket = new TokenBucket(23, 1.73);
}
/**
* Get the socket variable (access function)
*
* @return mixed socket variable
*/
public function getSocket() {
return $this->socket;
}
/**
* Set the socket variable (access function)
*
* @param mixed $socket socket variable
*/
public function setSocket($socket) {
$this->socket = $socket;
}
/**
* Connects to an IRC server
*
* @param mixed $address Address of server
* @param mixed $port Port of server
*/
public function connect($address, $port) {
$this->socket = fsockopen($address, $port);
}
/**
* Set blocking socket
*
* @param boolean $boolean Yes or no to blocking socket (default yes)
*/
public function setBlocking($boolean) {
socket_set_blocking($this->socket, $boolean);
}
/**
* Sends raw data
*
* @param mixed $data Data to send
*/
public function sendData($data) {
while ( !$this->TokenBucket->consume(1) ) {
sleep(0.3);
}
fwrite($this->socket, $data . "\n");
}
/**
* Sends a message to a room or user
*
* @param mixed $recipient An IRC room or user
* @param mixed $message The message to be sent (requires a licked stamp!)
*/
public function say($recipient, $message) {
$this->sendData("PRIVMSG $recipient :$message");
}
/**
* Sends a notice to a user
*
* @param mixed $recipient The user
* @param mixed $message The notice to be sent
*/
public function notice($recipient, $message) {
$this->sendData("NOTICE $recipient :$message");
}
/**
* Sends password to NickServ
*
* @param mixed $password Password
*/
public function sendPassword($password) {
if (!empty($password)) {
$this->sendData("PASS $password");
}
}
/**
* Manipulate user modes in channel
*
* @param mixed $channel Channel
* @param mixed $operation Modes to add/take
* @param mixed $user User to operate on
*/
public function mode($channel, $operation, $user) {
$this->sendData("MODE $channel $operation $user");
}
/**
* Sends the irc ident
*
* @param mixed $nick Nick
* @param mixed $mode Mode
* @param mixed $unused This is unused
* @param mixed $realname Realname
*/
public function sendIdent($nick, $mode, $unused, $realname) {
$this->sendData("NICK $nick");
// USER <user> <mode> <unused> <realname> (RFC 2812)
$this->sendData("USER $nick $mode $unused :$realname");
}
/**
* Joins the specified IRC channel
*
* @param mixed $channel Channel
*/
public function joinChannel($channel) {
$this->sendData("JOIN $channel");
}
/**
* Parts the specified IRC channel
*
* @param mixed $channel Channel
*/
public function partChannel($channel) {
$this->sendData("PART $channel");
}
/**
* Retrieves Data (essentially fgets)
*
* @param mixed $length Max length
* @return mixed Raw data
*/
public function getData($length=0) {
if (empty($length)) {
return fgets($this->socket);
} else {
return fgets($this->socket, $length);
}
}
/**
* Explode the data into words
*
* @param mixed $data getData() raw output
* @return array Array of previously raw data
*/
public function explodeData($data) {
return $data=explode(' ', $data);
}
/**
* Keep IRC bot alive, respond to ping events
*
* @param array $explodeData explodeData() return
*/
public function PingPong($explodeData) {
if ($explodeData["0"] == "PING") {
$this->sendData('PONG ' . $explodeData["1"]);
}
}
/**
* Return true or false if event is occuring
*
* @param mixed $event Event Id
* @param $explodeData explodeData() return
* @return boolean True or false answer
*/
public function event($event, $explodeData) {
if (!empty($explodeData["1"]) && $explodeData["1"] == $event) {
return true;
}
}
}
?>