summaryrefslogtreecommitdiff
path: root/bin/create_account.sh
blob: f073a48ba33aca02d6c408afe5009c9e2efe70d9 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/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!'
            STATE=1
        else
            send_email $'Something went wrong when automatically creating your account.\nError message:' "$RESULT"
            STATE=2
        fi
        do_mysql << __EOF__
update $SQL_TABLE set state = $STATE, password = '' where id = $ID
__EOF__
    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.