-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkrappy.php
183 lines (164 loc) · 7.43 KB
/
krappy.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
<?
// Krappy IRC Logger v3.3
$server = "irc.rizon.net";
$port = "6667";
$channel = "#testchannel"; // Channel to connect to
$nickbot = "Krappy"; // Bot nickname
$initialpath = ""; // Path to store log files
date_default_timezone_set("Europe/Madrid"); // Set time zone
// Arrays to substitute UTF-8 special characters to ISO one using str_replace
$utfchars = array("á", "é", "Ã", "ó", "ú", "ñ", "Ã", "É", "Ã", "Ó", "Ú", "Ñ");
$isochars = array("á", "é", "í", "ó", "ú", "ñ", "Á", "É", "Í", "Ó", "Ú", "Ñ");
// HTML Header
$starthtml='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="es-ES" xml:lang="es-ES" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>IRC log for channel '.$channel.'</title>
<link rel="stylesheet" href="../../logstyle.css" type="text/css" />
</head>
<body>
';
// HTML footer
$endhtml='</body>
</html>';
while (1)
{
$connect = fsockopen($server, $port, $errno, $errstr, 30); // Make the connection
$firstmsg = explode(' ',fgets ($connect,2048)); // We get the first message and split it in parts using space as a separator
$servername = $firstmsg[0]; // First item in the array is the server name (preceded by :)
fputs($connect, "USER $nickbot $nickbot $nickbot : $nickbot\n"); // Server identification
fputs($connect, "NICK $nickbot -\r\n"); // Server identification
if ($connected==0)
{
set_time_limit(0); // To avoid the script closing because of timeout
fputs($connect,"JOIN $channel\n");
fputs($connect,"privmsg $channel :Krappy IRC Logger started!\n");
$connected = 1;
}
$date = date("Y-m-d"); // Get current date
$year = date("Y"); // Get current year
$month = date("m"); // Get current month
// Test if the directory for log storage exists
if (@opendir("logs/$year")==false)
{
mkdir("logs/$year",0777); // Create directory if it doesn't exist
}
if (@opendir("logs/$year/$month")==false)
{
mkdir("logs/$year/$month",0777); // Create directory if it doesn't exist
}
$path = $initialpath."logs/$year/$month/";
$logfilename = $path."log".$date.".html"; // Create filename for log file
if ( @is_file ( $logfilename ) == false ) { // If log file doesn't exist
$open = fopen($logfilename,"a+"); // Open logfile to add text...
fwrite($open,$starthtml); // ...and write HTML header
} else {
$open = fopen($logfilename,"a+"); // If it exists, open logfile to add text
}
while (!feof($connect)) // While connection doesn't die
{
$log = fgets($connect,2048);
$parts = explode(' ',$log, 3);
$sender = $parts[0]; // Get message sender's name
if ($date != date("Y-m-d")) // Test if date has changed
{
fwrite($open,$endhtml); // Write HTML footer
fclose($open); // Close old logfile
$date = date("Y-m-d"); // Get new date
$year = date("Y");
$month = date("m");
// Test if the directory for log storage exists
if (@opendir("logs/$year")==false)
{
mkdir("logs/$year",0777); // Create directory if it doesn't exist
}
if (@opendir("logs/$year/$month/")==false)
{
mkdir("logs/$year/$month/",0777); // Create directory if it doesn't exist
}
$path = $initialpath."logs/$year/$month/";
$logfilename = $path."log".$date.".html"; // Create filename for new log file
$open = fopen($logfilename,"a+"); // Open new logfile
fwrite($open,$starthtml); // Write HTML header
}
if (substr($log, 0, 6) == "PING :") // Check if server sent a ping...
{
fputs($connect,"PONG :".substr($log, 6)."\n\r"); // ...and reply with a pong
}
elseif (($sender == $servername) || ($log == ""))
{
// If it is the server that sends the message, or message is empty, don't log it
}
else // This block contains everything related to writing of the final log fileN
{
$usermask=explode("!",$sender);
$nickname=substr($usermask[0],1);
if ($parts[1]=="NICK") // If user changes nickname
{
$finallogline='<span class="timestamp">'.date("[H:i:s]").'</span> '.'<span class="nickchange">*** '.$nickname.' changes nickname to '.htmlspecialchars(str_replace($utfchars, $isochars,substr($parts[2],1)), ENT_QUOTES).'</span><br/>
';
fwrite($open, $finallogline); // Write to log file
}
elseif ($parts[1]=="PRIVMSG") // If user writes a normal message
{
if ( substr($parts[2],strlen($channel)+2,strlen($channel)+1) == "ACTION") // Check if /me command was used
{
$finallogline='<span class="timestamp">'.date("[H:i:s] ").'</span> '.'<span class="me">*** '.$nickname.htmlspecialchars(str_replace($utfchars, $isochars,substr($parts[2],strlen($channel)+2+7,-3)), ENT_QUOTES).'</span><br/>
';
fwrite($open, $finallogline); // Write to log file
}
else // Otherwise, message is logged normally
{
$finallogline='<span class="timestamp">'.date("[H:i:s]").'</span> '.'<span class="nick"><'.$nickname.'> </span>'.htmlspecialchars(str_replace($utfchars, $isochars,substr($parts[2],strlen($channel)+2)), ENT_QUOTES).'<br/>
';
fwrite($open, $finallogline); // Write to log file
}
}
elseif ($parts[1]=="PART") // If user leaves the channel
{
$finallogline='<span class="timestamp">'.date("[H:i:s]").'</span> '.'<span class="part">*** '.$nickname.' left the channel </span><br/>
';
fwrite($open, $finallogline); // Write to log file
}
elseif ($parts[1]=="JOIN") // If user joins the channel
{
$finallogline='<span class="timestamp">'.date("[H:i:s]").'</span> '.'<span class="join">*** '.$nickname.' has joined the channel</span><br/>
';
fwrite($open, $finallogline); // Write to log file
}
elseif ($parts[1]=="TOPIC") // If topic is changed
{
$finallogline='<span class="timestamp">'.date("[H:i:s]").'</span> '.'<span class="topic">*** '.$nickname.' changed topic to '.htmlspecialchars(str_replace($utfchars, $isochars,substr($parts[2],strlen($channel)+2)), ENT_QUOTES).'</span><br/>
';
fwrite($open, $finallogline); // Write to log file
}
elseif ($parts[1]=="QUIT") // If user leaves the server
{
$finallogline='<span class="timestamp">'.date("[H:i:s]").'</span> '.'<span class="quit">*** '.$nickname.' left IRC network - '.htmlspecialchars(str_replace($utfchars, $isochars,substr($parts[2],1)), ENT_QUOTES).'</span><br/>
';
fwrite($open, $finallogline); // Write to log file
}
elseif ($parts[1]=="MODE") // If mode is changed
{
$finallogline='<span class="timestamp">'.date("[H:i:s]").'</span> '.'<span class="mode">*** '.$nickname.' changed mode to '.htmlspecialchars(str_replace($utfchars, $isochars,substr($parts[2],strlen($channel)+1)), ENT_QUOTES).'</span><br/>
';
fwrite($open, $finallogline); // Write to log file
}
elseif ($parts[1]=="KICK") // If a user is kicked
{
$finallogline='<span class="timestamp">'.date("[H:i:s]").'</span> '.'<span class="kick">*** '.$nickname.' kicked '.htmlspecialchars(str_replace($utfchars, $isochars,substr($parts[2],strlen($channel)+1)), ENT_QUOTES).'</span><br/>
';
fwrite($open, $finallogline); // Write to log file
}
else
{
// If server reply doesn't match any of the above, it is not logged
}
}
}
fwrite($open,$endhtml); // Write HTML footer
fclose($open); // Close logfile
fclose($connect); // Close connection
}
?>