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 pathAigisIRC.php
158 lines (128 loc) · 4.19 KB
/
AigisIRC.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
<?php
// AigisIRC
// GitHub: https://github.com/Joaquin-V/AigisIRC
// Create some constants.
// Aigis configuration directory in home.
define('AIGIS_HOME', getenv('HOME').'/.config/aigis');
if(!file_exists(AIGIS_HOME))
mkdir(AIGIS_HOME, 0755, true);
if(!is_dir(AIGIS_HOME)){
unlink(AIGIS_HOME);
mkdir(AIGIS_HOME, 0755, true);
}
// usr directory for random files like AigisURL/TextDB databases.
define('AIGIS_USR', AIGIS_HOME.'/usr');
if(!file_exists(AIGIS_USR))
mkdir(AIGIS_USR, 0755, true);
// Home directory for plugins.
define('AIGIS_HOMEPLG', AIGIS_HOME.'/plugins');
if(!file_exists(AIGIS_HOMEPLG))
mkdir(AIGIS_HOMEPLG, 0755, true);
// plugins/config for plugin configuration files.
define('PLUGIRC_CONFIG', 'plugins/config');
define('PLUGIRC_HOMECFG', AIGIS_HOMEPLG.'/config');
if(!file_exists(PLUGIRC_HOMECFG))
mkdir(PLUGIRC_HOMECFG, 0755, true);
// Require all of the modules.
require_once "ConnIRC.php";
require_once "UserIRC.php";
require_once "MessIRCManager.php";
require_once "FontIRC.php";
require_once "PlugIRC.php";
class AigisIRCException extends Exception {}
class AigisIRC{
// Version constants.
const AIGISIRC_VERSION = '1.00';
const AIGISIRC_VERNAME = '1.0 Pallas Athena';
const AIGISIRC_GITHUB = "https://github.com/MakotoYuki/AigisIRC/";
// IRC modules.
private $ConnIRC = null;
private $PlugIRC = null;
private $MessIRCManager = null;
private $UserIRC = null;
private $LangIRC = null;
// Bot information.
private $botNick = "";
private $altNick = "";
private $nsPass = null;
// Misc. information.
private $startTime = 0;
private $lastMsg = 0;
private $lastConn = null;
private $lastRegg = null;
public function __construct($botNick, $networkName, $altNick = "LunarBot"){
// Pass variables to the private object vars.
$this->botNick = $botNick;
$this->altNick = $altNick;
$this->network = $networkName;
// Start UserIRC and MessIRCManager
$this->MessIRCManager = new MessIRCManager($this, $botNick);
$this->UserIRC = new UserIRC($this, $botNick);
$this->PlugIRC = new PlugIRC($this);
$this->startTime = time();
}
public function setConnInfo($host, $port = 6667){
$this->ConnIRC = new ConnIRC($this, $this->network, $this->botNick, $host, $port);
return $this->ConnIRC;
}
public function nsIdentify(){
if(isset($this->nsPass))
$this->ConnIRC->send("PRIVMSG NickServ :IDENTIFY " . $this->nsPass);
}
// Loop cycle.
public function loopCycle(){
$lastPing = time();
if(!$this->ConnIRC->connected())
$this->ConnIRC->connect();
if($sockread = $this->ConnIRC->read()){
$this->lastMsg = time();
$MessIRC = $this->MessIRCManager->getMessage($sockread);
consoleSend($sockread, "ConnIRC", "info"); /* Uncomment to see the world through bot eyes. */
// Tell ConnIRC to parse RAW input.
if($MessIRC->getType() == "raw")
$this->ConnIRC->parseRaw($MessIRC);
// Ping-pong
if( $MessIRC->getType() == "ping")
$this->ConnIRC->send("PONG " . $MessIRC->getMessage());
$type = $MessIRC->getType();
if(method_exists($this->UserIRC, $type))
$this->UserIRC->$type($MessIRC);
try{
$this->PlugIRC->pluginSendAll($type, $MessIRC);
}catch(Exception $e){
if(get_class($e) == "NoticeException")
return;
else
consoleSend($e->getMessage(), "PlugIRC", "warning");
}
}
// Ping timeouts.
$time = time();
if($time - $this->lastMsg >= ConnIRC::ACTIVITY_TIMEOUT && $time - $this->lastConn >= ConnIRC::RECONNECT_DELAY){
consoleSend("Disconnected for ping timeout.","ConnIRC","warning");
$this->ConnIRC->connect();
}
}
// Returns a variable in the object.
public function getAigisVar($varname){
if(isset($this->$varname))
return $this->$varname;
return null;
}
// Sets an object's variable.
public function setAigisVar($varname, $value){
$this->$varname = $value;
}
public static function getConfig($network){
if(!file_exists(AIGIS_HOME."/aigis.conf")){
if(file_exists("config/aigis.conf"))
$configF = parse_ini_file("config/aigis.conf", true);
else throw new Exception("Config file not found.");
}else $configF = parse_ini_file(AIGIS_HOME."/aigis.conf", true);
if(!isset($configF[$network]))
throw new Exception("Unknown network: $network");
$config = array_merge($configF['Global'], $configF[$network]);
$config['network'] = $network;
return $config;
}
}