summaryrefslogtreecommitdiff
path: root/bin/createaccount.rb
blob: 45a8223b8de86d4834c04f409404b3c65b1fea08 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/ruby

require 'mysql'
require 'net/smtp'

$smtp_server       = 'localhost'
$mail_from         = 'registration@themanaworld.org'
$mail_from_name    = 'The ManaWorld team'
$mail_subject      = 'The Mana World Account registration'
$mail_body_success = "Your account was created successfully. Have fun playing themanaworld.\n"
$mail_body_error   = "The was something wrong with the creation of your account.\n" + 
                     "Error message: "
$create_script     = "/path/to/script"

##############################################################################

returns = [
	{'message' => "successfully created",        'status' => :SUCCESS, 'final_state' => 1 },		
	{'message' => "Same account already exists", 'status' => :FAILED, 'final_state' => 2 },
	{'message' => "Email is too short",          'status' => :FAILED, 'final_state' => 2 },
	{'message' => "Email is too long",           'status' => :FAILED, 'final_state' => 2 },
	{'message' => "Invalid email",               'status' => :FAILED, 'final_state' => 2 },
	{'message' => "Account name is too short",   'status' => :FAILED, 'final_state' => 2 },
	{'message' => "Account name is too long",    'status' => :FAILED, 'final_state' => 2 },
	{'message' => "Illegal character",           'status' => :FAILED, 'final_state' => 2 },
	{'message' => "command not found",           'status' => :FAILED, 'final_state' => 2 },
]

##############################################################################

def send_mail(email, username, status, errm)
	message = "From: #{$mail_from_name} <#{$mail_from}>\n" +
		"To: #{username} <#{email}>\n" +
		"Subject: #{$mail_subject}\n\n"

	if status == :SUCCESS then
		message << $mail_body_success
	elsif status == :FAILED then
		message << $mail_body_error << errm
	end

	Net::SMTP.start($smtp_server) do |smtp|
  		smtp.send_message(message, $mail_from, email)
	end
end

##############################################################################

db = Mysql::new("localhost", "test", "test123", "test")
db.query("SELECT id, username, password, email, gender 
          FROM   tmw_accounts 
          WHERE  state = 0").each do |id, username, password, email, g|
	begin
		gender = case g.to_i
				when 1 then "M"
				when 2 then "F"
			  end

		# insert the right command here
		retval = `#{$create_script} create #{username} #{gender} #{email} #{password}`
		# parse the return value
		returns.each do |retcode|
			if retval.include? retcode['message'] then
				send_mail( email, username, retcode['status'], retcode['message'] )
				db.query("UPDATE tmw_accounts SET STATE = #{retcode['final_state']} WHERE id = #{id}")
			end 			
		end
	rescue
		puts "ERROR occured"
		puts $!
		db.query("UPDATE tmw_accounts SET STATE = 2 WHERE id = #{id}")	
	end
end
db.close