|
@@ -1,89 +1,102 @@
|
1
|
1
|
<?php
|
|
2
|
+///////
|
|
3
|
+// webirc-client - 2013-2020
|
|
4
|
+// (C) Chris Dorman, GPL v3
|
|
5
|
+// https://notabug.org/Pentium44/ircchat
|
|
6
|
+///////
|
|
7
|
+
|
|
8
|
+// irc.php - used to push and pull data from IRC server.
|
|
9
|
+// Currently supports PING / PONG, data receive, and data push
|
|
10
|
+// Done via PHP sockets.
|
2
|
11
|
|
3
|
12
|
// Prevent PHP from stopping the script after 30 sec
|
4
|
13
|
set_time_limit(0);
|
5
|
14
|
|
|
15
|
+// Include variables
|
6
|
16
|
include_once("config.php");
|
7
|
17
|
|
8
|
|
-// Change these values!
|
|
18
|
+// Get username from command line argument / PHP-CLI
|
9
|
19
|
$username = $argv[1];
|
10
|
20
|
|
|
21
|
+// If username isn't set, exit with error.
|
11
|
22
|
if(!isset($username) || $username == "") {
|
12
|
23
|
echo "Username not given...";
|
13
|
24
|
exit(1);
|
14
|
25
|
}
|
15
|
26
|
|
16
|
|
-echo "Starting client...";
|
17
|
|
-
|
18
|
|
-file_put_contents(".$username.socket", "Welcome");
|
19
|
|
-chmod(".$username.socket", 0777);
|
20
|
|
-
|
21
|
|
-file_put_contents(".$username.pingfile", "pong");
|
22
|
|
-chmod(".$username.pingfile", 0777);
|
23
|
|
-
|
24
|
|
-// Opening the socket to the Rizon network
|
|
27
|
+// Create a socket to use
|
25
|
28
|
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
|
26
|
29
|
|
|
30
|
+// Connect to IRC server via socket above
|
27
|
31
|
socket_connect($socket, $server, $port);
|
28
|
32
|
|
29
|
|
-
|
30
|
|
-// Send auth info
|
31
|
|
-// fputs($socket, "PASS " . $password . "\n");
|
32
|
|
-echo "Connected, sending nickname to IRC server...";
|
33
|
|
-
|
|
33
|
+// NICK and USER calls to IRC
|
34
|
34
|
$nickline = "NICK " . $username . "\n";
|
35
|
35
|
$userline = "USER " . $username . " 0 * :" . $username . "'s Bot\n";
|
36
|
36
|
|
|
37
|
+// Pass NICK and USER back to IRC server over socket
|
37
|
38
|
socket_write($socket, $nickline, strlen($nickline));
|
38
|
39
|
socket_write($socket, $userline, strlen($userline));
|
39
|
40
|
|
40
|
|
-// Join channel
|
41
|
|
-foreach($channels as $channel) {
|
42
|
|
- $channelline = "JOIN " . $channel . "\n";
|
43
|
|
- socket_write($socket, $channelline, strlen($channelline));
|
44
|
|
-}
|
|
41
|
+sleep(1);
|
45
|
42
|
|
46
|
|
-// Force an endless while
|
47
|
|
-while (1) {
|
48
|
|
- // Continue the rest of the script here
|
49
|
|
-
|
50
|
|
- while($bytes = socket_recv($socket, $r_data, 2048, MSG_DONTWAIT) !== '') {
|
51
|
|
- if($bytes !== FALSE) {
|
52
|
|
- //$data = socket_read($socket, 2048, PHP_NORMAL_READ);
|
53
|
|
- $data = $r_data;
|
54
|
|
- }
|
55
|
|
- // If client sent something, push it to the IRC server!
|
56
|
|
- if(file_exists(".$username.push")) {
|
57
|
|
- $pushFile = file_get_contents(".$username.push");
|
58
|
|
- socket_write($socket, $pushFile, strlen($pushFile));
|
59
|
|
- unlink(".$username.push");
|
60
|
|
- }
|
|
43
|
+// Continue the rest of the script here
|
|
44
|
+// While script will continue as long as socket continues to be active
|
|
45
|
+while($bytes = socket_recv($socket, $r_data, 2048, MSG_DONTWAIT) !== '') {
|
|
46
|
+ if($bytes !== FALSE) {
|
|
47
|
+ //$data = socket_read($socket, 2048, PHP_NORMAL_READ);
|
|
48
|
+ $data = $r_data;
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ // If client sent something, push it to the IRC server!
|
|
52
|
+ if(file_exists(".$username.push")) {
|
|
53
|
+ // Grab IRC command from server.php
|
|
54
|
+ $pushFile = file_get_contents(".$username.push");
|
|
55
|
+ // Push this / these commands to socket
|
|
56
|
+ socket_write($socket, $pushFile, strlen($pushFile));
|
|
57
|
+ // Remove the push file
|
|
58
|
+ unlink(".$username.push");
|
|
59
|
+ }
|
61
|
60
|
|
62
|
|
- // Check if web client still up, if no pong after 15 seconds, connection closed.
|
63
|
|
- if(!file_exists(".$username.pingfile") || (date("YmdHis.", filemtime(".$username.pingfile"))<(date("YmdHis.", filemtime(".$username.pingfile"))-10))) {
|
64
|
|
- echo "Exiting, leaving IRC server...";
|
65
|
|
- $quitline = "QUIT :Timed out\n";
|
66
|
|
- socket_write($socket, $quitline, strlen($quitline));
|
67
|
|
- socket_close($socket);
|
68
|
|
- exit(1);
|
69
|
|
- }
|
70
|
|
-
|
71
|
|
- if(isset($data)) {
|
72
|
|
- $stringMsg = explode('PRIVMSG', $data);
|
73
|
|
- $socketFileContents = file_get_contents(".$username.socket");
|
74
|
|
- file_put_contents(".$username.socket", $socketFileContents . $data);
|
|
61
|
+ // Check if web client still up, if no pong after 15 seconds, connection closed.
|
|
62
|
+ if(!file_exists(".$username.pingfile")) { // If file is missing, quit
|
|
63
|
+ // Debug logging, check if IRC is exiting properly
|
|
64
|
+ doLog("Exiting, $username logged out...");
|
|
65
|
+ $quitline = "QUIT :$username left the web client\n"; // IRC QUIT line
|
|
66
|
+ socket_write($socket, $quitline, strlen($quitline)); // Push to socket
|
|
67
|
+ socket_close($socket); // Close the socket
|
|
68
|
+ exit(0); // Exit the script, nothing to do.
|
|
69
|
+ } else if (date("YmdHis.", filemtime(".$username.pingfile"))<(date("YmdHis.", filemtime(".$username.pingfile"))-10)) {
|
|
70
|
+ // Debug logging, check if IRC is exiting properly
|
|
71
|
+ doLog("Exiting, $username timed out...");
|
|
72
|
+ $quitline = "QUIT :$username's web session timed out\n"; // IRC QUIT line
|
|
73
|
+ socket_write($socket, $quitline, strlen($quitline)); // Push to socket
|
|
74
|
+ socket_close($socket); // Close the socket
|
|
75
|
+ exit(1); // Exit the script, nothing to do.
|
|
76
|
+ }
|
75
|
77
|
|
76
|
|
- $ex = explode(' ', $data);
|
|
78
|
+ // If data variable is set and buffer has data to recieve
|
|
79
|
+ // RECIEVE IT!
|
|
80
|
+ if(isset($data)) { // If data variable is set, there's data from socket
|
|
81
|
+ $stringMsg = explode('PRIVMSG', $data); // Strip IRC commands
|
|
82
|
+ // Get original contents from socket
|
|
83
|
+ $socketFileContents = file_get_contents(".$username.socket");
|
|
84
|
+ // Push all content to
|
|
85
|
+ file_put_contents(".$username.socket", $socketFileContents . $data);
|
|
86
|
+ $ex = explode(' ', $data);
|
77
|
87
|
|
78
|
|
- // Send PONG back to the server
|
79
|
|
- if ($ex[0] == "PING") {
|
80
|
|
- echo "PONG to IRC server...";
|
81
|
|
- $pongline = "PONG " . $ex[1] . "\n";
|
82
|
|
- socket_write($socket, $pongline, strlen($pongline));
|
83
|
|
- }
|
|
88
|
+ // Send PONG back to the server
|
|
89
|
+ if ($ex[0] == "PING") {
|
|
90
|
+ // Log pong
|
|
91
|
+ doLog("PONG, $username response...");
|
|
92
|
+ $pongline = "PONG " . $ex[1] . "\n"; // PONG IRC CMD
|
|
93
|
+ // Push to IRC server via socket.
|
|
94
|
+ socket_write($socket, $pongline, strlen($pongline));
|
84
|
95
|
}
|
85
|
|
-
|
86
|
|
- usleep(500);
|
87
|
96
|
}
|
|
97
|
+
|
|
98
|
+ // Half second sleep to prevent insane CPU load
|
|
99
|
+ usleep(500);
|
88
|
100
|
}
|
|
101
|
+
|
89
|
102
|
?>
|