]> jfr.im git - irc/borknet/trunk.git/blob - CreateAccount.java
Q: Removed the '!' at the end of the line directly giving the userpassword
[irc/borknet/trunk.git] / CreateAccount.java
1 /**
2 #
3 # BorkNet Services Core
4 #
5
6 #
7 # Copyright (C) 2004 Ozafy - ozafy@borknet.org - http://www.borknet.org
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #
23 */
24 import java.sql.*;
25 import java.util.*;
26 import java.io.*;
27 import java.security.*;
28 class CreateAccount
29 {
30 public static void main(String args[])
31 {
32 CreateAccount ca = new CreateAccount();
33 }
34
35 public CreateAccount()
36 {
37 testDriver();
38 Connection con = openSqlConnection();
39 createUserAccount(con);
40 closeSqlConnection(con);
41 System.out.println("Useraccount added!");
42 System.exit(0);
43 }
44
45 private void testDriver()
46 {
47 try
48 {
49 Class.forName ( "org.gjt.mm.mysql.Driver" );
50 }
51 catch ( java.lang.ClassNotFoundException e )
52 {
53 System.out.println("MySQL JDBC Driver not found!");
54 System.exit(1);
55 }
56 }
57
58 private Connection openSqlConnection()
59 {
60 try
61 {
62 String server = readFromConsole("SQL Server host:");
63 String username = readFromConsole("SQL username:");
64 String password = readFromConsole("password:");
65 String database = readFromConsole("Database:");
66 String url = "jdbc:mysql://"+server+"/"+database+"?user="+username+"&password="+password;
67 Connection con = DriverManager.getConnection(url);
68 return con;
69 }
70 catch(Exception e)
71 {
72 System.out.println("Could not open SQL connection!");
73 System.exit(1);
74 return null;
75 }
76 }
77
78 private void createUserAccount(Connection con)
79 {
80 try
81 {
82 PreparedStatement pstmt;
83 pstmt = con.prepareStatement("INSERT INTO auths VALUES (?,?,?,?,?,?,?,?,?)");
84 pstmt.setString(1,readFromConsole("Bot Account Username:"));
85 pstmt.setString(2,encrypt(readFromConsole("Bot Account Password:")));
86 pstmt.setString(3,readFromConsole("Bot Account E-Mail:"));
87 pstmt.setInt(4,1000);
88 pstmt.setBoolean(5,false);
89 pstmt.setLong(6,0);
90 pstmt.setString(7,"0");
91 pstmt.setString(8,"0");
92 pstmt.setString(9,"0");
93 pstmt.executeUpdate();
94 }
95 catch ( SQLException e )
96 {
97 System.out.println ( "Error executing sql statement" );
98 StackTraceElement[] te = e.getStackTrace();
99 System.out.println(e.toString());
100 for(StackTraceElement el : te)
101 {
102 System.out.println("\tat "+el.getClassName()+"."+el.getMethodName()+"("+el.getFileName()+":"+el.getLineNumber()+")");
103 }
104 System.exit(1);
105 }
106 }
107
108 private String readFromConsole(String question)
109 {
110 System.out.println(question);
111 Scanner in = new Scanner(System.in);
112 return in.nextLine();
113 }
114
115 //con = getConnection ( server, user, password, db);
116 private void closeSqlConnection(Connection con)
117 {
118 try
119 {
120 con.close();
121 }
122 catch(Exception e)
123 {
124 System.out.println("MySQL connection failed to close.");
125 System.exit(1);
126 }
127 }
128
129 public String encrypt(String plaintext)
130 {
131 byte[] defaultBytes = plaintext.getBytes();
132 try
133 {
134 MessageDigest algorithm = MessageDigest.getInstance("MD5");
135 algorithm.reset();
136 algorithm.update(defaultBytes);
137 byte messageDigest[] = algorithm.digest();
138 StringBuffer hexString = new StringBuffer();
139 for (int i=0;i<messageDigest.length;i++)
140 {
141 String hex = Integer.toHexString(0xFF & messageDigest[i]);
142 if(hex.length()==1) hexString.append('0');
143 hexString.append(hex);
144 }
145 return hexString.toString();
146 }
147 catch(NoSuchAlgorithmException e)
148 {
149 System.out.println ( "Error encrypting password." );
150 System.exit(0);
151 return "0";
152 }
153 }
154 }