aboutsummaryrefslogtreecommitdiffstats
path: root/hypervoice.php
blob: 90b29fd09e8b47128f305801fdb1b18695fd4da4 (plain)
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
<?php
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

set_time_limit(0);
const NEW_LINE = "\n";
$isDaemon = (bool)($argv[1] ?? false);

// ---

function startsWith($haystack, $needle) {
   $length = strlen($needle);
   return (substr($haystack, 0, $length) === $needle);
}

function contains($haystack, $needle) {
  if (strpos($haystack, $needle) > -1) {
    return true;
  }
  else {
    return false;
  }
}

// ---

function funcSendCommand($cmd) {
  sleep(2);
  fputs($GLOBALS['irc'], $cmd  . NEW_LINE);
  funcOutput($cmd, 'snd');
}

function funcExtractNick ($fullUsername) {
  return  preg_replace("/\:(.*)\!(.*)/i", "$1", $fullUsername);
}

function funcOutput($output, $mode) {
  if ($mode != 'err' && $GLOBALS['isDaemon']) {
    return;
  }

  switch ($mode) {
    case 'snd':
      print('Snd> ' . $output . NEW_LINE);
      break;
    case 'rcv':
      print('Rvc> ' . $output);
      break;
    case 'msg':
      print('Msg> ' . $output . NEW_LINE);
      break;
    case 'err':
      print('Err> ' . $output . NEW_LINE);
      break;
    default:
      return;
  }
}
// ---

$ircServer = 'irc.libera.chat';
$ircPort = '6667';
$botNick = 'hypervoice';
$botChannel = '#hyperbola';

$arrayConnectCommands = array(
  "USER $botNick $botNick $botNick $botNick :$botNick",
  "NICK $botNick",
  "PONG",
  "CAP REQ :account-notify extended-join",
  "NICKSERV identify nick password",
  "JOIN $botChannel"
);

$irc = fsockopen($ircServer, $ircPort);

if (!$irc) {
  funcOutput('Something went wrong with the socket', 'err');
  exit(1);
}

foreach ($arrayConnectCommands as $_value) {
  funcSendCommand($_value);
}

// ---

while(1) {
  while ($raw = fgets($irc)) {
    if (startsWith($raw, 'PING')) {
      $lastPing = time();
      funcOutput($raw, 'rcv');
      funcSendCommand('PONG');
    }

    if (startsWith($raw, 'ERROR')) {
      funcOutput($raw, 'rcv');
      exit(1);
    }

    if (startsWith($raw, ':')) {
      $rawEx = explode(' ', $raw);
      funcOutput($raw, 'rcv');
      switch($rawEx[1]) {
        case 'PONG':
          $lastPing = time();
          break;
        case 'JOIN':
          if (!contains($rawEx[0], $botNick) && $rawEx[3] != '*') {
            funcSendCommand("MODE $botChannel +v " . funcExtractNick($rawEx[0]));
          }
          break;
        case 'ACCOUNT':
          funcSendCommand("MODE $botChannel +v " . funcExtractNick($rawEx[0]));
          break;
      }
    }
  }
}

?>