/*
######################
sxSockets.mrc v1.5.5
by sprion
######################

24th August 2004

What?
Sockets with proxy and logging capabilities, exactly what mIRC doesn't provide.

For?
People who wrote scripts that made use of sockets and are now thinking of providing firewall support.
People who want to learn with a reference on how to communicate with the 3 protocols. (HTTP, SOCKS4, SOCKS5)
People like you who likes to read tricky codes and interesting ideas that has never came to mind :P

How?
Does the proxy communications for you, then tunnels data for access via signal events/temporary local proxy.


Add?
Many more things to add, that's why I included the autoupdating codes.
 - commenting and cleaning up the codes
 - sxSockets listing
 - sxSockets log viewer
 - sxSockets log replayer (replay the whole socket session!)
Known problems
 - seems a bit unfriendly (especially socket naming)
 - none yet but I know there bound to be, it's complicated materials. And I released without testing much, sorry.
   You find one? you either squash it or mail me about it! Appreciate.


Feedback, Questions + Help?
write me at sprion@lyniq.com



Changes in v1.5.5:
    - Cleaned up codes a bit

Changes in v1.5:
    - Added /sxsockList command.
    - Improved how the logging system works.
    - Fixed a bug that makes SOCKS5 Authentication .. impossible. Now you can use SOCKS5 proxy with
      username + passwords!

    - Now works even more like an mIRC socket.
    - ON SOCKREAD and ON SOCKCLOSE events can be used!
    - Sockets now works 100% faster than before (when using ON SOCKREAD to SXSOCKS.READ signal)

    - Improved and changed $sxsock identifier.
        Now you can specify $sxsock(sprion) instead of $sxsock(sxsocks.sprion) . Both works.
        Parameters are now specified as properties
          $sxsock(sprion).remoteIP instead of $sxsock(sprion,remoteIP)


------------------------------------------------------------


MOREEEEEEEEEE??


 Available commands:

   /sxsock.update - Checks for updates for this addon
 
 
   /sxsockOpen [-d Name] <+h45> <proxy_addr/ip> <port> <target_addr/ip> <port> [user] [pass]
     = creates a socket through a proxy specified, data is logged.
       - A random name is generated if the -d switch is not used, 'sxSocks.' is appended to the name specified.
           i.e. -d Hello
           this creates a socket called 'sxSocks.Hello' for use
       - +h uses the HTTP proxy
       - +4 uses the SOCKS4 proxy
       - +5 uses the SOCKS5 proxy
     
       * only one proxy type can be used at a time.


   /sxsockClose [param] - closes the sxsocket. Parameters works the same as /sockclose
   
   
   /sxsockList [param] - lists scSockets. Parameters works the same as /socklist


   /sxsockForget <sockname> - erases log


   
   ** /sxsockSet [-p Name] <socketname> <data>
     = sets an information attached to the socket
       - -p is an optional switch, creates a CUSTOM variable with the name specified, to store the data
       - if no switch specified, defaults to setting information about the state of the socket.


   
   $sxsock(<sockname>).[parameter]
     = returns the state of the socket
       - when parameter is specified, returns information associated with the socket
          + available parameters: (when you create your CUSTOM variables you can read with this too)
              index - how many times you READ from the socket
              param - the whole parameters used to create the socket - /sxsockopen <all these>
              method - the proxy type
              proxyIP
              proxyPort
              remoteIP
              remotePort
              user - the username specified
              pass - the password specified



   $sxsockRead(<sockname>,[N])
     = reads the last unread line in buffer.
       - Specify N to pick a line from the log.
       - When N is 0, returns total of lines logged.



  ** - you don't usually use this


------------------------------------------------------------

*/
/*
 ############################################
 EXAMPLE (just load this readme into mirc '/load -rs <thisfile>')
 Make a connection to see how
 it works.. try:

 /sxsockopen -d sprion +? <proxyip> <port> singnet.sg.galaxynet.org 6668
 where ? is h or 4 or 5

 * NOTE that you see repeat messages from connection because
   ON SOCKREAD and SXSOCKS.READ signal are both used in this example for you
   to compare and decide which you want to use.
   
   EVENT is faster than SIGNAL
 ############################################
*/

; sxSocks' ON SOCKOPEN signal (use this to replace on sockopen)
on *:signal:sxSocks.open:{
  var %e = echo *
  %e ********************************************
  %e Proxy Type: $2
  %e Remote Address: $3
  %e Proxy Address: $4
  %e Proxy Allocated Port: $5
  %e  $1 socket established successfully!
  %e ********************************************
}


; EVENT sockread (works the same as the signal)
on *:sockread:sprion:{
  if ($sockerr > 0) return
  :nextread
  sockread &read
  echo -e * >> $calc($ticks - %sxsock.ticks) ms - EVENT - ON SOCKREAD - $bvar(&read,0) bytes from $1 : $bvar(&read,1,$bvar(&read,0)).text
  unset %sxsock.ticks
}

; SIGNAL sockread event signal counterpart (slower when more lines are read)
on *:signal:sxSocks.read:{
  ; sxSocks' ON SOCKREAD event
  
  ; IMPORTANT LINE!
  ; this line is always needed, stores the read line to &read binary variable
  bcopy &read 1 $sxsockread($1) 1 -1
  
  echo -e * >> $calc($ticks - %sxsock.ticks) ms - SIGNAL - SXSOCKS.READ - $bvar(&read,0) bytes from $1 : $bvar(&read,1,$bvar(&read,0)).text
  unset %sxsock.ticks
}



; EVENT sockclose event
on *:sockclose:sprion:{
  echo -e * >> ON SOCKCLOSE: CLOSED!
}

; SIGNAL sockclose event signal counterpart
on ^*:signal:sxSocks.close:{
  ; sxSocks' ON SOCKCLOSE event
  echo -e * SXSOCKS.CLOSE: Socket ENDed: $1- (removed log)
  
  ; command to remove log. it's up to u to do this or not..
  sxsockforget $1
}




on *:signal:sxSocks.fail:{
  ; sxSocks' ERROR event
  echo -e * Socket FAILed: $1 - $2-
}

on *:signal:sxSocks.info:{
  ; sxSocks' INFO event
  echo -e * INFO: $1-
}
/*
  #############
   EXAMPLE END
  #############
*/
