class Chat

Public Class Methods

checkLogin( connection ) click to toggle source
# File lib/chatC.rb, line 15
def self.checkLogin ( connection )

print "Login: "
user = self.input
print "Password: "
passwd = self.input

md5passwd = Digest::MD5.new
md5passwd << passwd


connection.query("SELECT * FROM users WHERE username = '#{user}'").each do |data_for_passwd|
if (md5passwd == data_for_passwd["password"])
return user
else
puts "Login failed"
return ""
end
end

end
get_connection(host,username,password,port,database) click to toggle source
# File lib/chatC.rb, line 6
def self.get_connection (host,username,password,port,database)
return Mysql2::Client.new( :host => host,
  :username => username ,
  :password =>  password ,
  :port =>  port ,
  :database => database )
end
get_lastmsgs( connection, table, n) click to toggle source
# File lib/chatC.rb, line 69
def self.get_lastmsgs ( connection, table, n)
connection.query("SELECT * FROM #{table} WHERE id > ( SELECT MAX(id) - #{n} FROM chat)")
end
help() click to toggle source
# File lib/chatC.rb, line 75
def self.help
puts "Help information for work with chat_by_rznvls /n -r = refresh chat information, shows last 5 messages"
end
input() click to toggle source
# File lib/chatC.rb, line 86
def self.input
gets.to_s.strip
end
push_msg(connection, user,msg) click to toggle source
# File lib/chatC.rb, line 65
def self.push_msg(connection, user,msg)
connection.query("INSERT INTO chat (user, msg) VALUES ('#{user}', '#{msg}')")
end
refresh(lastmsgs) click to toggle source
# File lib/chatC.rb, line 79
def self.refresh (lastmsgs)
lastmsgs.each do |row|
        puts row["user"] + ": " + row["msg"]
    end
end
registration(connection) click to toggle source
# File lib/chatC.rb, line 39
def self.registration(connection)
print "Login: "
user = Chat.input
print "Password: "
passwd = Chat.input
md5passwd = Digest::MD5.new
md5passwd << passwd


connection.query("SELECT * FROM users WHERE username = '#{user}'").each do |data_for_reg|
if (user == data_for_reg["username"])
puts "Try another login"
puts user
return ""
end
end
puts "Login succes"
puts user
connection.query("INSERT INTO users (username, password) VALUES ('#{user}', '#{md5passwd}')")
return user


end