Advertisement
Guest User

Untitled

a guest
Jan 12th, 2012
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.66 KB | None | 0 0
  1.         $filename = 'users.txt'
  2.        
  3.         # Create a new hash
  4.         $users = Hash.new
  5.        
  6.         # Open and read the contents into the hash
  7.         def load
  8.             # Create a new file if it does not exist
  9.             unless File.exist? $filename
  10.                 # Open and immedietly  close it (the reason for the block)
  11.                 File.open($filename, 'w') {}
  12.             end
  13.             # Open the file and iterate over each line
  14.             File.open($filename, 'r') do |file|
  15.                 # Add all of the pairs to the hash
  16.                 while (line = file.gets)
  17.                     user, rank, userpassword = line.split
  18.                         $users[user] = { :rank => rank, :password => userpassword }
  19.                         end
  20.             end
  21.         end
  22.  
  23.         # Write the contents of the hash to a file
  24.         def save
  25.             File.open($filename, 'w') do |file|
  26.                 $users.each do |user, rank, userpassword|
  27.                     file.puts("#{user} #{rank} #{userpassword}")
  28.                 end
  29.             end
  30.         end
  31.        
  32.         # Remove a user
  33.         def remove(nick)
  34.             $users.delete nick
  35.         end
  36.  
  37.         # Add a new user to the hash if not already registered
  38.         def register(nick, userpassword)
  39.             puts "Received !register from #{nick}"
  40.             if $users.key? nick
  41.                 send "PRIVMSG #{nick} :You are already registered!"
  42.                
  43.             else
  44.                 if !userpassword
  45.                     send "PRIVMSG #{nick} :Correct syntax is: !register <password>"
  46.                 else
  47.                     $users[nick] ||= {}
  48.                     $users[nick][:rank] = 1
  49.                     $users[nick][:password] = userpassword
  50.                     send "PRIVMSG #{nick} :You have successfully registered!"
  51.                 end
  52.             end
  53.         end
  54.        
  55.         # Get a users rank
  56.         def rank(nick)
  57.             puts "Received !rank from #{nick}"
  58.             if $users.key? nick
  59.                 send "PRIVMSG #{nick} :#{$users[:rank]}"
  60.             else
  61.                 send "PRIVMSG #{nick} :You are not registered!"
  62.             end
  63.         end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement