This repository has been archived by the owner on Feb 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserIRC.php
203 lines (171 loc) · 6.17 KB
/
UserIRC.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
require_once "UserIRC_User.php";
require_once "UserIRC_Channel.php";
class UserIRC{
// UserIRC
// Part of AigisIRC (https://github.com/Joaquin-V/AigisIRC)
private $AigisIRC = null;
private $users = array();
private $self = null;
private $channels = array();
private $PMchan = null;
public function __construct(AigisIRC $AigisIRC, $nickSelf){
$this->AigisIRC = $AigisIRC;
// Start instance of self user.
$this->self = new UserIRC_User($nickSelf . "[email protected]");
}
// UserIRC::getUser($address) - Gets the user object of an address (or makes one if non-existant.)
// @param $address string User's address.
// @return User object.
// @throws Exception if $address doesn't match the regex.
public function getUser($address){
//Seperate the nick, ident and hostmask.
if(preg_match("/(.*)!(.*)@(.*)/", $address, $matches)){
$nick = $matches[1];
$ident = $matches[2];
$host = $matches[3];
}else $nick = $address;
//Check if the nick is the same as the bot's and if so, return the self user.
if($nick == $this->nickSelf())
return $this->self;
//If not, check if there is a user object for that nick.
elseif(isset($this->users[$nick]))
return $this->users[$nick];
//If there isn't, create a user object.
$this->users[$nick] = new UserIRC_User($address);
return $this->users[$nick];
}
// UserIRC::getChannel($channel) - Gets a channel object of a specified channel.
// @param string $channel Channel name.
public function getChannel($channel){
if(!isset($this->channels[$channel]))
$this->channels[$channel] = new UserIRC_Channel($channel);
return $this->channels[$channel];
}
public function getSelf(){
return $this->self;
}
// Parse server messages.
public function raw(MessIRC $MessIRC){
$raw = $MessIRC->getRaw();
switch($raw){
case 353:
$params = $MessIRC->getParams();
array_shift($params);
$channel = array_shift($params);
foreach($params as $address){
if(preg_match('/([:~&@%+]*)([^!]+)(?:!([^@]+)@(.+))?/', $address, $match)){
@list(, $modes, $nick, $ident, $host) = $match;
$modes = str_split($modes);
$modeLetters = array('~' => "q", '&' => "a", '@' => "o", '%' => "h", '+' => "v");
$modecmd = "+";
foreach($modes as $modechr){
if(isset($modeLetters[$modechr]))
$modecmd .= $modeLetters[$modechr];
}
$user = $this->getUser("$nick!$ident@$host");
$chan = $this->getChannel($channel);
$user->addChan($channel);
$chan->join($nick);
if($modecmd != "+")
$user->mode($channel, $modecmd);
}
}
break;
}
}
public function privmsg(MessIRC $MessIRC){
if($MessIRC->getNick() == $this->nickSelf())
consoleSend("[Force-spoken] ".$MessIRC->getReplyTarget()." -> ".$MessIRC->getMessage(), "ConnIRC", "send");
if($MessIRC->inChannel()){
$chan = $this->getChannel($MessIRC->getReplyTarget());
$chan->addMessage($MessIRC);
}
$user = $this->getUser($MessIRC->getNick());
$user->addMessage($MessIRC);
}
public function join(MessIRC $MessIRC){
if($MessIRC->getNick() == $this->nickSelf())
consoleSend("Joined ".$MessIRC->getReplyTarget(), "ConnIRC", "info");
$user = $this->getUser($MessIRC->getNick());
$chan = $this->getChannel($MessIRC->getReplyTarget());
$user->addChan($MessIRC->getReplyTarget());
$user->updateHost($MessIRC);
$chan->join($MessIRC->getNick());
}
public function nick(MessIRC $MessIRC){
$nick = $MessIRC->getNick();
$user = $this->getUser($nick);
$newNick = $MessIRC->getMessage();
$user->setNick($newNick);
$this->users[$newNick] = $user;
unset($this->users[$nick]);
}
public function part(MessIRC $MessIRC){
if($MessIRC->getNick() == $this->nickSelf())
consoleSend("Parted ".$MessIRC->getReplyTarget()." (".$MessIRC->getMessage().")", "ConnIRC", "info");
$user = $this->getUser($MessIRC->getNick());
$chan = $this->getChannel($MessIRC->getReplyTarget());
$user->rmChan($MessIRC->getReplyTarget());
$chan->part($MessIRC->getNick());
}
public function kick(MessIRC $MessIRC){
$nick = $MessIRC->getNick();
$reason = explode(" ", MessIRC::strSince($MessIRC->getArrayMsg(), 3));
$kicked = array_shift($reason);
$reason = MessIRC::stripCol(implode(" ",$reason));
if($kicked == $this->nickSelf()){
// Uncomment for automatic rejoin on kick.
// consoleSend("Kicked from ".$MessIRC->getReplyTarget()." by ".$MessIRC->getNick().". Reason: ".$reason, "ConnIRC", "warning");
// $this->AigisIRC->getAigisVar("ConnIRC")->join($MessIRC->getReplyTarget());
}
$user = $this->getUser($kicked);
$chan = $this->getChannel($MessIRC->getReplyTarget());
$user->rmChan($MessIRC->getReplyTarget());
$chan->part($kicked);
}
public function quit(MessIRC $MessIRC){
$nick = $MessIRC->getNick();
$user = $this->getUser($nick);
$chans = $user->getChans();
foreach($chans as $chan){
$channel = $this->getChannel($chan);
$channel->part($nick);
}
if(isset($this->users[$nick]))
unset($this->users[$nick]);
}
public function mode(MessIRC $MessIRC){
$modeArray = $MessIRC->getArrayMsg();
if(!isset($modeArray[4]))
return; //Channel modes that don't change admin settings. They're not that important.
elseif(strpos($modeArray[2], '#') === false)
return; //User modes. Not really useful for a bot (much less one that will most likely not be an IRCOp.)
else{ //Channel modes that affect user admin settings (+vhoaq.)
if(preg_match_all('/(\+|\-)([ahoqv]+)/', $modeArray[3], $regex)){
$channel = $modeArray[2];
// Pointer to shift through nicks in the mode command.
$letterPointer = 0;
// Loop through both modes being added (+) and modes being revoked (-).
for($modePointer = 0; isset($regex[0][$modePointer]); $modePointer++){
$modeSymbol = $regex[1][$modePointer];
$modeLetters= $regex[2][$modePointer];
// Split the letters into an array and individually add modes to the user objects. This lets us add modes to lines that manage multiple users in one go.
$letterArray= str_split($modeLetters);
foreach($letterArray as $letter){
$modeCommand = $modeSymbol.$letter;
$nick = $modeArray[$letterPointer+4];
$user = $this->getUser($nick);
$user->mode($channel, $modeCommand);
$letterPointer++;
}
}
}
}
}
public function nickSelf(){
$nick = $this->AigisIRC->getAigisVar("botNick");
$this->self->setNick($nick);
return $nick;
}
}