mud.lua (1420B)
1 local DataHandler 2 local mud_socket 3 4 local LastAddress 5 local LastPort 6 7 function mud.send( data ) 8 mud.last_command_time = mud.now() 9 socket.send( mud_socket, data ) 10 end 11 12 function mud.disconnect() 13 mud.print( "\n#s> Disconnected!" ) 14 mud.connected = false 15 socket.close( mud_socket ) 16 mud_socket = nil 17 end 18 19 function mud.connect( address, port ) 20 if mud.connected then 21 mud.print( "\n#s> Already connected! (%s:%d)", LastAddress, LastPort ) 22 23 return 24 end 25 26 if not address then 27 if not LastAddress then 28 mud.print( "\n#s> I need an address..." ) 29 30 return 31 end 32 33 address = LastAddress 34 port = LastPort 35 end 36 37 mud.print( "\n#s> Connecting to %s:%d...", address, port ) 38 39 local sock, err = socket.connect( address, port, function( sock, data ) 40 if data then 41 DataHandler( data ) 42 else 43 mud.disconnect() 44 end 45 end ) 46 47 if not sock then 48 mud.print( "\n#s> Connection failed: %s", err ) 49 return 50 end 51 52 mud_socket = sock 53 54 LastAddress = address 55 LastPort = port 56 57 mud.connected = true 58 mud.last_command_time = mud.now() 59 end 60 61 mud.alias( "/con", { 62 [ "^$" ] = function() 63 mud.connect() 64 end, 65 66 [ "^(%S+)%s+(%d+)$" ] = function( address, port ) 67 mud.connect( address, port ) 68 end, 69 }, "<ip> <port>" ) 70 71 mud.alias( "/dc", function() 72 if mud.connected then 73 mud.disconnect() 74 else 75 mud.print( "\n#s> You're not connected..." ) 76 end 77 end ) 78 79 return { 80 init = function( dataHandler ) 81 DataHandler = dataHandler 82 end, 83 }