All pastes #898226 Raw Edit

Unnamed

public lua v1 · immutable
·published 2008-02-10 04:28 UTC
rendered paste body
require "socket";require "lfs";DEBUG = true;Socket = nil;Commands = { };Config = {	Server = "irc.gamesurge.net",	Port = 6667,	User = "Xera",	Nick = "LuaBot",	Channel = "luahelp",};function Log( LogType, LogMessage, LogToFile, ... )	local TypeList = {		recv = "<< %s",		send = ">> %s",		info = "(Info) %s",		error = "Error: %s",	};	if DEBUG then		TypeList[ "debug" ] = "Debug: %s";	end		if not TypeList[ LogType ] then		return false;	end	local Str = "";		if not ... then		Str = TypeList[ LogType ]:format( LogMessage );	else		Str = TypeList[ LogType ]:format( LogMessage:format( ... ) );	end		print( Str );		if LogToFile then		local File = io.open( "log.txt", "a" );		File:write( "\n" .. Str );		File:close( );	end	endfunction Send( String, ... )	if ... then		String = String:format( ... );	end		assert( String:len( ) <= 510, "max string length is 510 bytes." );		Socket:send( String .. "\r\n" );	endfunction Say( Channel, Message )	local Split = { };		local M = Message;		while string.len( M ) + string.len( "PRIVMSG " .. Channel .. " :" ) > 510 do		table.insert( Split, string.sub( M, 1, 510 ) );		M = Split[ #Split ];		print( M );	end		if #Split == 0 then		Send( "PRIVMSG %s :%s", Channel, Message );	else		for k, v in pairs ( Split ) do			Send( "PRIVMSG %s :%s", Channel, v );		end	endendfunction Receive( )	local Read = Socket:receive( "*line" );		if not Read then		return;	end		if DEBUG then		print( tostring( Read ) );	end		if string.find( Read, "Checking Ident" ) then		Socket:send( "USER Xera test lol :Lua Bot\n" );		Socket:send( "NICK " .. Config.Nick .. "\n" );		return;	end		if string.find( Read, "PING :" ) then		local Reply = "PONG " .. string.sub( Read, string.find( Read, ":" ) + 1 );		Socket:send( Reply .. "\r\n" );		return;	end		if string.find( Read, "001" ) and not JoinedChannel then		Socket:send( "JOIN #" .. Config.Channel .. "\r\n" );		JoinedChannel = true;		return;	end		local Parse = { 		HostStartPos = string.find( Read, "!" ),		MessageStartPos = string.find( Read, ":", 2 ),		HostMaskStartPos = string.find( Read, "@" ),		HostMaskEndPos = string.find( Read, " " ),		TypeEndPos = string.find( Read, " ", string.find( Read, " " ) + 1 ),		ChanStartPos = string.find( Read, "#" )or string.find( Read, Config.Nick )	};		if not Parse.HostStartPos or not Parse.MessageStartPos or not Parse.HostMaskStartPos or not Parse.HostMaskEndPos or not Parse.TypeEndPos or not Parse.ChanStartPos then		return;	end		local Parsed = { 		Message = string.sub( Read, Parse.MessageStartPos + 1 ),		Nick = string.sub( Read, 2, Parse.HostStartPos - 1 ),		Host = string.gsub( string.sub( Read, Parse.HostMaskStartPos + 1, Parse.HostMaskEndPos ), " ", "" ),		Channel = string.sub( Read, Parse.ChanStartPos, Parse.MessageStartPos - 2 ),		Type = string.sub( Read, Parse.HostMaskEndPos + 1, Parse.TypeEndPos - 1 )	};		Log( "recv", "[%s] (%s) %s %s: %s", true, os.date( "%x %X" ), Parsed.Type, Parsed.Channel, Parsed.Nick, Parsed.Message );		if string.sub( Parsed.Message, 1, 1 ) == "."  then		local CommandEnd = string.find( Parsed.Message, " " );		if not CommandEnd then			return;		end		local CommandString = string.sub( Parsed.Message, 2, CommandEnd - 1 );		if CommandString and Commands[ CommandString ] then			Parsed.Message = string.sub( Parsed.Message, CommandEnd + 1 );			Command.Run( CommandString, Parsed );		end	end	endfunction Connect( Server, Port )	Socket = socket.tcp( );		local Result, Error = Socket:connect( Server, Port );		if not Result then		Log( "error", "Unable to connect: %s", false, Error );		return false;	end		return true;	enddofile( "commands.lua" );if Connect( Config.Server, Config.Port ) then	while Socket and Socket:getpeername( ) do		Receive( );	endend