diff options
-rwxr-xr-x | bin/create_account.sh | 95 | ||||
-rwxr-xr-x | bin/createaccount.rb | 80 |
2 files changed, 95 insertions, 80 deletions
diff --git a/bin/create_account.sh b/bin/create_account.sh new file mode 100755 index 0000000..14fab86 --- /dev/null +++ b/bin/create_account.sh @@ -0,0 +1,95 @@ +#!/bin/bash +# Run every minute + +MAIL_FROM='user@host' +MAIL_FROM_NAME='The Mana World server' +MAIL_SUBJECT='The Mana World account registration' + + +SQL_HOST='localhost' +SQL_USER='user' +SQL_PASSWORD='password' +SQL_DATABASE='db' +SQL_TABLE='table' + +LADMIN_DIR='/path/to/tmwserver/login/' +LADMIN_BIN='tmwa-admin' + +### end configuration + + +mail_command() +{ +# no arguments; takes a message on stdin + /usr/sbin/sendmail -t +} + +die() +{ + echo "$@" + exit 1 +} + +one_word() +{ + test $# -eq 1 || die "Error: evil spaces!" +} + +one_word $SQL_HOST +one_word $SQL_USER +one_word $SQL_PASSWORD +one_word $SQL_DATABASE +one_word $SQL_TABLE + +send_email() +{ +# Note: USERNAME and EMAIL are captured +mail_command << __EOF__ +From: $MAIL_FROM_NAME <$MAIL_FROM> +To: $USERNAME <$EMAIL> +Subject: $MAIL_SUBJECT + +Hello $USERNAME, + +$@ +__EOF__ +} + +do_mysql() +{ + # SQL statements on stdin + mysql --host="$SQL_HOST" --user="$SQL_USER" --password="$SQL_PASSWORD" "$SQL_DATABASE" +} + +frob_accounts() +{ + IFS=$'\t' + while read ID USERNAME PASSWORD EMAIL GENDER + do + if test "$GENDER" = '1'; then GENDER=M; fi + if test "$GENDER" = '2'; then GENDER=F; fi + + RESULT=$(cd $LADMIN_DIR; $LADMIN_BIN <<< "create $USERNAME $GENDER $EMAIL $PASSWORD" 2>&1) + echo RESULT: "$RESULT" + if grep -q 'successfully created' <<< "$RESULT" + then + send_email 'Your account was created successfully. Have fun playing The Mana World!' + do_mysql << __EOF__ +update $SQL_TABLE set state = 1 where id = $ID +__EOF__ + else + send_email $'Something went wrong when automatically creating your account.\nError message:' "$RESULT" + do_mysql << __EOF__ +update $SQL_TABLE set state = 2 where id = $ID +__EOF__ + fi + done +} + +do_mysql << __EOF__ | tail -n+2 | frob_accounts +select id, username, password, email, gender +from $SQL_TABLE +where state = 0 +__EOF__ + +echo Everything is Ok. diff --git a/bin/createaccount.rb b/bin/createaccount.rb deleted file mode 100755 index 0f9228e..0000000 --- a/bin/createaccount.rb +++ /dev/null @@ -1,80 +0,0 @@ -#!/usr/bin/ruby - -require 'mysql' -require 'net/smtp' - -$smtp_server = 'localhost' -$mail_from = 'noreply@themanaworld.org' -$mail_from_name = 'The Mana World server' -$mail_subject = 'The Mana World Account registration' -$mail_body_success = "Your account was created successfully. Have fun playing The Mana World!\n" -$mail_body_error = "The was something wrong with the creation of your account.\n" + - "Error message: " -$mysql_hostname = "localhost" -$mysql_database = "test" -$mysql_username = "test" -$mysql_password = "test123" -$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" - message << "Hello #{username},\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($mysql_hostname, $mysql_username, $mysql_password, $mysql_database) -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 - |