]> jfr.im git - irc/borknet/trunk.git/blob - CreateAccount.java
git-svn-id: https://svn.code.sf.net/p/borknet-dev-com/code/borknet_services/trunk...
[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.setString(4,"1000");
88 pstmt.setString(5,"false");
89 pstmt.setString(6,"0");
90 pstmt.setString(7,"0");
91 pstmt.executeUpdate();
92 }
93 catch ( SQLException e )
94 {
95 System.out.println ( "Error executing sql statement" );
96 System.exit(1);
97 }
98 }
99
100 private String readFromConsole(String question)
101 {
102 System.out.println(question);
103 Scanner in = new Scanner(System.in);
104 return in.nextLine();
105 }
106
107 //con = getConnection ( server, user, password, db);
108 private void closeSqlConnection(Connection con)
109 {
110 try
111 {
112 con.close();
113 }
114 catch(Exception e)
115 {
116 System.out.println("MySQL connection failed to close.");
117 System.exit(1);
118 }
119 }
120
121 public String encrypt(String plaintext)
122 {
123 byte[] defaultBytes = plaintext.getBytes();
124 try
125 {
126 MessageDigest algorithm = MessageDigest.getInstance("MD5");
127 algorithm.reset();
128 algorithm.update(defaultBytes);
129 byte messageDigest[] = algorithm.digest();
130 StringBuffer hexString = new StringBuffer();
131 for (int i=0;i<messageDigest.length;i++)
132 {
133 String hex = Integer.toHexString(0xFF & messageDigest[i]);
134 if(hex.length()==1) hexString.append('0');
135 hexString.append(hex);
136 }
137 return hexString.toString();
138 }
139 catch(NoSuchAlgorithmException e)
140 {
141 System.out.println ( "Error encrypting password." );
142 System.exit(0);
143 return "0";
144 }
145 }
146 }