TelIRC Gateway

By K-Shadow on Dec 19, 2011

This is a Telnet IRC gateway I made in my spare time.
Main server runs on port 90.
Identd server runs on port 113.
The basic commands are:
JOIN
SEND
RAW
And of course, QUIT.

;TelIRC Gateway
;Made by K-Shadow
;Version 0.50 Beta

;Handle connections to the gateway.
on *:SOCKLISTEN:irc: {
  inc %gateway.clients
  ;Assign a name to the new client.
  var %self gateway. $+ $r(1,100000)
  ;Accept the client's connection request.
  sockaccept %self
  ;Output new connection to server log window.
  writeinfo [CLIENT] New connection from $sock(%self).ip $+ .
  ;Send welcome message;
  sockwrite -nt %self Welcome to the TelIRC gateway. To start, send a line starting with USER, $+ $crlf $+ containing the nickname you want to use.
}

;Reading from client connected to the gateway.
on *:SOCKREAD:gateway.*: {
  ;Set a variable for the incoming data to be read from clients connected to the gateway.
  var %buffer
  ;Read data from the socket, and store it into the %buffer var.
  sockread %buffer
  ;Assign a name for the IRC socket for the client.
  var %selfirc irc. $+ $right($sockname,-8)
  ;If there is no %buffer, halt.
  if (!%buffer) halt
  ;Client commands
  tokenize 32 %buffer
  ;Handle client login
  if ($1 == USER) {
    if (!$sock(%selfirc)) {
      var %nick $2
      if ($len(%nick) > 30) {
        sockwrite -nt $sockname Nick is above 30 characters, try again.
        halt
      }
      else {
        goto login
      }
      :login
      var %host $replace(%nick,_,-,`,-,[,-,],-,^,-)
      sockwrite -nt $sockname Now connecting to IRC.
      writeinfo [CLIENT] Client $sock($sockname).ip has initiated a connection to %telirc.server $+ .
      sockrename $sockname gateway. $+ %nick
      var %selfirc irc. $+ %nick
      set %nextident %nick
      inc %irc.clients
      sockopen %selfirc %telirc.server %telirc.serverport
      .timer 1 3 sockwrite -nt %selfirc NICK %nick
      .timer 1 3 sockwrite -nt %selfirc USER %nick * * :TelIRC Gateway
      unset %nick
    }
    else {
      sockwrite -nt $sockname You are already connected to IRC!
    }
  }
  if ($1 == SEND) {
    var %dest $2
    var %text $3-
    sockwrite -nt %selfirc PRIVMSG %dest : $+ %text
    unset %dest
    unset %text
  }
  if ($1 == JOIN) {
    var %chan $2
    sockwrite -nt %selfirc JOIN %chan
    unset %chan
  }
  if ($1 == QUIT) {
    dec %irc.clients
    dec %gateway.clients
    writeinfo [CLIENT] Client $sock($sockname).ip requested to close the connection.
    sockclose irc. $+ $right($sockname,-8)
    sockclose $sockname
  }
  if ($1 == RAW) {
    var %cmd $2
    var %args $3-
    sockwrite -nt %selfirc %cmd %args
    unset %cmd
    unset %args
  }
  unset %buffer
}

;Manages connection to IRC for client.
on *:SOCKREAD:irc.*: {
  ;Set a variable for incoming data to be read from the IRC connection.
  var %buffer
  ;Read data from socket, and store it in %buffer.
  sockread %buffer
  ;If there is no %buffer, halt.
  if (!%buffer) halt
  ;IRC related stuff.
  tokenize 32 %buffer
  if ($1 == PING) {
    var %reply $right($2,-1)
    sockwrite -nt $sockname PONG %reply
    unset %reply
  }
  if ($1 == ERROR) {
    dec %irc.clients
    dec %gateway.clients
    writeinfo [ERROR] Error from client $sock(irc. $+ $right($sockname,-8)).ip $+ 's connection to IRC, closing connection.
    sockclose gateway. $+ $right($sockname,-4)
    sockclose $sockname
  }
  ;CTCP Replies.
  if ($4 == $+(:,$chr(001),VERSION,$chr(001))) sockwrite -nt $sockname NOTICE $parsenick($1) $+(:,$chr(001),VERSION TelIRC Gateway Beta,$chr(001))

  ;Sent data from IRC socket to corresponding client connected to the gateway.
  if ($sock(gateway. $+ $right($sockname,-4))) {
    if ($2 == 372 || $2 == 002 || $2 == 003 || $2 == 004 || $2 == 005 || $1 == PING || $2 == 251 || $2 == 252 || $2 == 254 || $2 == 255 || $2 == 265 || $2 == 266 || $2 == 375 || $2 == 376) { halt }
    if ($2 == 001) {
      sockwrite -nt gateway. $+ $right($sockname,-4) Connected to %telirc.server $+ !
      halt
    }
    if ($2 == NOTICE && $3 == AUTH) halt
    sockwrite -nt gateway. $+ $right($sockname,-4) %buffer
  }
  unset %buffer
}

;If connection to gateway is closed, close the sockets!
on *:SOCKCLOSE:gateway.*: {
  dec %gateway.clients
  writeinfo [CLIENT] Client $sock($sockname).ip closed the connection.
  if ($sock(irc. $+ $right($sockname,-8))) {
    sockclose irc. $+ $right($sockname,-8)
    dec %irc.clients
  }
  sockclose $sockname
}

;If connection to IRC is closed, close the sockets!
on *:SOCKCLOSE:irc.*: {
  dec %irc.clients
  dec %gateway.clients
  writeinfo [ERROR] Error from client $sock(gateway. $+ $right($sockname,-4)).ip $+ 's connection to IRC, closing connection.
  sockclose gateway. $+ $right($sockname,-4)
  sockclose $sockname
}

;TelIRC aliases

;Alias to parse nickname from PRIVMSGs.
alias parsenick {
  var %nick $gettok($1-,1,64)
  var %nick $gettok(%nick,1,33)
  var %nick $right(%nick,-1)
  return %nick
}

;Alias to send a message to clients connected to the gateway.
alias sendall {
  sockwrite -nt gateway.* [BROADCAST] $1-
}

;Alias to output server status to log window.
alias telircstats {
  writeinfo [STATS] Clients connected to gateway: %gateway.clients
  writeinfo [STATS] Clients connected to IRC via gateway: %irc.clients
}

;Alias to clear status window.
alias clearinfo {
  clear @Server
}

;Alias to write to server log window.
alias writeinfo {
  aline -hp @Server $timestamp $+ $chr(32) $1-
}

;Alias to shutdown TelIRC gateway.
alias shutdown {
  sockclose irc.*
  sockclose modnar.*
  sockclose irc
  sockclose identd
  unset %gateway.clients
  unset %irc.clients
  if (%nextident) unset %nextident
  window -c @Server
}

;Alias to start TelIRC gateway.
alias starttelirc {
  if (!%gateway.clients) set %gateway.clients 0
  if (!%irc.clients) set %irc.clients 0
  if (!%telirc.server) set %telirc.server $?="What server will your gateway connect clients to?"
  if (!%telirc.serverport) set %telirc.serverport $?="What port does the server run on? (Usually 6667)"
  if (!%telirc.gatewayport) set %telirc.gatewayport $?="What port will the gateway run on?"
  socklisten irc %telirc.gatewayport
  socklisten identd 113
  if (!$window(@Server)) {
    window @Server
  }
  else {
    clear @Server
    window -a @Server
  }
  writeinfo [START] TelIRC Gateway started on port %telirc.gatewayport $+ .
  writeinfo [START] TelIRC IdentD started on port 113.
}

;TelIRC IdentD

on *:SOCKLISTEN:identd: {
  var %identrequest identd. $+ $r(1,100000)
  sockaccept %identrequest
  writeinfo [IDENTD] New ident request from $sock(%identrequest).ip $+ .
}

on *:SOCKREAD:identd.*: {
  var %request
  sockread %request
  tokenize 32 %request
  if (%nextident) sockwrite -nt $sockname $1 $+ $chr(32) $+ $2 $3 : USERID : UNIX : %nextident
  unset %nextident
  writeinfo [IDENTD] Responded to ident request from $sock($sockname).ip $+ , closing ident request socket.
  .timer 1 1 sockclose $sockname
}

Comments

Sign in to comment.
Frenetic   -  Dec 19, 2011

lol I see

 Respond  
K-Shadow   -  Dec 19, 2011

@Frenetic, I was testing something earlier, forgot I had that.

 Respond  
Jordyk19   -  Dec 19, 2011

@Frenetic Maybe a hidden port :P
@K-Shadow Very nice to see you here :)
Its a really good script you have there.

 Respond  
Frenetic   -  Dec 19, 2011

Ah I see. About to test it.

Um, Is that your IP in there? Lol.

 Respond  
K-Shadow   -  Dec 19, 2011

That's the ident server, forgot to mention that.

 Respond  
Frenetic   -  Dec 19, 2011
  writeinfo [START] TelIRC IdentD started on port 113.

Main server runs on port 90.

Can't make your mind up?

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.