#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
 
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
 
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
#!/usr/local/bin/ruby
require 'socket'
 
server = 'ddg.us.tddirc.net'
port = 6667
nick = 'Ruby'
chan = '#hackerthreads'
talky = true
 
lastcommand = Time.new
things = Array.new
users = Array.new
users << 'Cool_Fire'
backupjoined = false
 
def loadusers
	begin
	file = File.new("users.bot", "r")
		while (line = file.gets)
			puts "#{counter}: #{line}"
			counter = counter + 1
		end
	file.close
	rescue => err
		puts "Exception: #{err}"
		err
	end
end
 
def adduser username
	begin
		file = File.new("users.bot", "a")
		file.puts username
		file.close
		rescue => err
		puts "Exception: #{err}"
		err
	end
end
 
def writeuserlist
	begin
		file = File.new("users.bot", "w")
		users.each do |i|
			puts i
		end
 
		file.close
		rescue => err
		puts "Exception: #{err}"
		err
	end
end
 
def isRecent lc
	now = (Time.new - 100)
	return (lc > now)
end
 
s = TCPSocket.new(server, port)
s.puts("NICK #{nick}")
s.puts("USER #{nick} 8 *  : #{nick}")
s.puts("JOIN #{chan}")
 
while line = s.gets
	chunk = Array.new
	chunk = line.split(':')
	overhead = Array.new
	overhead = chunk[1].split('!')
	from = overhead[0]
	msg = chunk[2]
 
	if line =~ /ping/i
		s.puts("PONG :#{chunk[1]}")
		if !backupjoined
			s.puts("JOIN #{chan}")
			backupjoined = true
		end
	end
 
	if (msg =~ /#{nick}/i || (isRecent(lastcommand))) && ((from !~ /#{nick}/i) && (line !~ /ping/i))
		lastcommand = Time.new
 
		if msg =~ /stfu/i || msg =~ /quiet/i || msg =~ /silent/i || msg =~ /silence/i  || msg =~ /talk/i || msg =~ /speak/i
			if talky
				things = ["Fine, I'll be quiet...", "*pouts*", "Tell me when I can talk again ok!?","But I love to talk!"]
				s.puts("PRIVMSG #{chan} :#{things[rand(things.length)]}")
				talky = false
			else
				things = ["Wow, look at how pretty and great everything is! ^_^","This place is so super!", "I love you guys.", "Falalala lala la la!","Yay! =D","I love to talk!"]
				s.puts("PRIVMSG #{chan} :#{things[rand(things.length)]}")
				talky = true
			end
		end
 
		if (msg =~ /hi /i || msg =~ /hey/i || msg =~ /hello/i) && talky
			things = ["Hey, how are you?","Well hello!","Hiya!"]
			s.puts("PRIVMSG #{chan} :#{things[rand(things.length)]}")
		end
 
		if (msg =~ /how are you/i || msg =~ /how are thing/i || msg =~ /how is it/i) && talky
			things = ["I'm so great, thanks for asking!","I'm just fabolous thank you!","Hahaha, this is so great!","I'm dosed up to the eyeballs on anti-depressents!"]
			s.puts("PRIVMSG #{chan} :#{things[rand(things.length)]}")
		end
 
		if msg =~ /op/i || msg =~ /ops/i || msg =~ /oper/i
			if talky
				things = ["Sure thing!", "My pleasure", "I'd love to!","Anything for you!", "Only the best is good enough for you!"]
				s.puts("PRIVMSG #{chan} :#{things[rand(things.length)]}")
			end
			s.puts("MODE #{chan} +o #{from}")
		end
 
		if msg =~ /time/i && talky
			h = DateTime.now.hour
			m = DateTime.now.min
			things = ["According to my watch, it's","It seems to be","Will you look at that! It's already"]
			s.puts("PRIVMSG #{chan} :#{things[rand(things.length)]} #{h}:#{m}.")
		end
 
		if msg =~ /date/i && talky
			things = ["I think it might be a little too soon for dates, don't you?","I do like you, but I'm dating nanobot!","I'm sorry, but I never date people. Please don't feel bad though, you're really nice!"]
			s.puts("PRIVMSG #{chan} :#{things[rand(things.length)]}")
		end
 
		if msg =~ /exit/i || msg =~ /quit/i || msg =~ /shutdown/i || msg =~ /shut down/i
			if talky
				things = ["I'd hate to leave you all, but I'll be back real soon!","It seems I must go now :o","I'll be back!"]
				s.puts("PRIVMSG #{chan} :#{things[rand(things.length)]}")
			end
			s.close
		end
	end
end