Skip to content

Commit

Permalink
Fix account creation
Browse files Browse the repository at this point in the history
  • Loading branch information
markwinter committed Oct 3, 2014
1 parent 2f8623b commit b1c7b84
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 103 deletions.
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<string name="create_register_incog">Register Incognito</string>
<string name="create_skip">Allow anyone to add you using your profile name</string>
<string name="create_must_fill_in">You must fill in all fields</string>
<string name="create_bad_profile_name">Profile name can\'t contain spaces or backslashes/forwardslashes</string>
<string name="create_bad_profile_name">Profile name can\'t contain spaces or slashes or be empty</string>

<!-- Main Activity -->
<string name="main_no_active_chat">No active chat. Start one by choosing a friend from the pane on the left.</string>
Expand Down
243 changes: 145 additions & 98 deletions app/src/main/scala/im/tox/antox/activities/CreateAcccountActivity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,54 +72,166 @@ class CreateAcccountActivity extends ActionBarActivity {
super.onOptionsItemSelected(item)
}

def onClickRegisterAccount(view: View) {
val accountField = findViewById(R.id.create_account_name).asInstanceOf[EditText]
val account = accountField.getText.toString
def validAccountName(account: String): Boolean = {
val pattern = Pattern.compile("\\s")
val pattern2 = Pattern.compile(File.separator)
var matcher = pattern.matcher(account)
val containsSpaces = matcher.find()
matcher = pattern2.matcher(account)
val containsFileSeperator = matcher.find()
if (account == "") {
val context = getApplicationContext
val text = getString(R.string.create_must_fill_in)
val duration = Toast.LENGTH_SHORT
val toast = Toast.makeText(context, text, duration)
toast.show()
} else if (containsSpaces || containsFileSeperator) {
val context = getApplicationContext
val text = getString(R.string.create_bad_profile_name)
val duration = Toast.LENGTH_SHORT
val toast = Toast.makeText(context, text, duration)
toast.show()

if(account == "" || containsSpaces || containsFileSeperator)
return false

true
}

def showBadAccountNameError(): Unit = {
val context = getApplicationContext
val text = getString(R.string.create_bad_profile_name)
val duration = Toast.LENGTH_SHORT
val toast = Toast.makeText(context, text, duration)
toast.show()
}

def onClickRegisterIncogAccount(view: View) {
val accountField = findViewById(R.id.create_account_name).asInstanceOf[EditText]
val account = accountField.getText.toString

if (!validAccountName(account)) {
showBadAccountNameError()
} else {
// Add user to db
val db = new UserDB(this)
db.addUser(account, "")
db.close()

// Create the tox data file for this account and save preferences
var ID = ""
var fileBytes: Array[Byte] = null

try {
val antoxFriendList = new AntoxFriendList()
val callbackHandler = new CallbackHandler(antoxFriendList)
val toxOptions = new ToxOptions(Options.ipv6Enabled, Options.udpEnabled, Options.proxyEnabled)
val jTox = new JTox(antoxFriendList, callbackHandler, toxOptions)
val toxDataFile = new ToxDataFile(this, account)
toxDataFile.saveFile(jTox.save())
ID = jTox.getAddress
fileBytes = toxDataFile.loadFile()

// Save preferences
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
val udpEnabled = preferences.getBoolean("enable_udp", false)
val toxOptions = new ToxOptions(Options.ipv6Enabled, udpEnabled, Options.proxyEnabled)
val editor = preferences.edit()
editor.putString("active_account", account)
editor.putString("nickname", account)
editor.putString("status", "1")
editor.putString("status_message", getResources.getString(R.string.pref_default_status_message))
editor.putString("tox_id", ID)
editor.putBoolean("loggedin", true)
editor.apply()

// Start the activity
val startTox = new Intent(getApplicationContext, classOf[ToxDoService])
getApplicationContext.startService(startTox)
val main = new Intent(getApplicationContext, classOf[MainActivity])
startActivity(main)
setResult(Activity.RESULT_OK)

finish()
} catch {
case e: ToxException => Log.d("CreateAccount", "Failed creating tox data save file")
}
}
}

def onClickRegisterAccount(view: View) {
val accountField = findViewById(R.id.create_account_name).asInstanceOf[EditText]
val account = accountField.getText.toString

if (!validAccountName(account)) {
showBadAccountNameError()
} else {
// Add user to db
val db = new UserDB(this)
db.addUser(account, "")
db.close()

// Create tox data save file
var ID = ""
var fileBytes: Array[Byte] = null

try {
val antoxFriendList = new AntoxFriendList()
val callbackHandler = new CallbackHandler(antoxFriendList)
val toxOptions = new ToxOptions(Options.ipv6Enabled, Options.udpEnabled, Options.proxyEnabled)
val jTox = new JTox(antoxFriendList, callbackHandler, toxOptions)
val toxDataFile = new ToxDataFile(this, account)
toxDataFile.saveFile(jTox.save())
ID = jTox.getAddress
fileBytes = toxDataFile.loadFile()
} catch {
case e: ToxException => Log.d("CreateAccount", e.getMessage)
case e: ToxException => Log.d("CreateAccount", "Failed creating tox data save file")
}

// Register on toxme.se
try {
System.load("/data/data/im.tox.antox/lib/libkaliumjni.so")
} catch {
case e: Exception => Log.d("CreateAccount", "System.load() on kalium failed")
}
val skipRegistration = findViewById(R.id.skip_button).asInstanceOf[CheckBox]
if (!skipRegistration.isChecked) {

val allow = 0
val jsonPost = new JSONPost()
val toxmeThread = new Thread(jsonPost)

try {
val unencryptedPayload = new JSONObject()
unencryptedPayload.put("tox_id", ID)
unencryptedPayload.put("name", account)
unencryptedPayload.put("privacy", allow)
unencryptedPayload.put("bio", "")
val epoch = System.currentTimeMillis() / 1000
unencryptedPayload.put("timestamp", epoch)
val hexEncoder = new Hex()
val rawEncoder = new Raw()
val toxmepk = "5D72C517DF6AEC54F1E977A6B6F25914EA4CF7277A85027CD9F5196DF17E0B13"
val serverPublicKey = hexEncoder.decode(toxmepk)
val ourSecretKey = Array.ofDim[Byte](32)
System.arraycopy(fileBytes, 52, ourSecretKey, 0, 32)
val box = new Box(serverPublicKey, ourSecretKey)
val random = new org.abstractj.kalium.crypto.Random()
var nonce = random.randomBytes(24)
var payloadBytes = box.encrypt(nonce, rawEncoder.decode(unencryptedPayload.toString))
payloadBytes = Base64.encode(payloadBytes, Base64.NO_WRAP)
nonce = Base64.encode(nonce, Base64.NO_WRAP)
val payload = rawEncoder.encode(payloadBytes)
val nonceString = rawEncoder.encode(nonce)
val json = new JSONObject()
json.put("action", 1)
json.put("public_key", ID.substring(0, 64))
json.put("encrypted", payload)
json.put("nonce", nonceString)
jsonPost.setJSON(json.toString)
toxmeThread.start()
toxmeThread.join()
} catch {
case e: JSONException => Log.d("CreateAcccount", "JSON Exception " + e.getMessage)
case e: InterruptedException =>
}

var toastMessage = ""
val context = getApplicationContext
val duration = Toast.LENGTH_SHORT
var toast: Toast = null
val errorCode = jsonPost.getErrorCode

if ("0" == errorCode) {
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
val editor = preferences.edit()
editor.putString("active_account", account)
editor.putString("nickname", account)
editor.putString("status", "online")
editor.putString("status", "1")
editor.putString("status_message", getResources.getString(R.string.pref_default_status_message))
editor.putString("tox_id", ID)
editor.putBoolean("loggedin", true)
Expand All @@ -130,83 +242,18 @@ class CreateAcccountActivity extends ActionBarActivity {
startActivity(main)
setResult(Activity.RESULT_OK)
finish()
} else {
try {
System.load("/data/data/im.tox.antox/lib/libkaliumjni.so")
} catch {
case e: Exception => Log.d("CreateAccount", "System.load() on kalium failed")
}
val allow = 0
val jsonPost = new JSONPost()
val toxmeThread = new Thread(jsonPost)
try {
val unencryptedPayload = new JSONObject()
unencryptedPayload.put("tox_id", ID)
unencryptedPayload.put("name", account)
unencryptedPayload.put("privacy", allow)
unencryptedPayload.put("bio", "")
val epoch = System.currentTimeMillis() / 1000
unencryptedPayload.put("timestamp", epoch)
val hexEncoder = new Hex()
val rawEncoder = new Raw()
val toxmepk = "5D72C517DF6AEC54F1E977A6B6F25914EA4CF7277A85027CD9F5196DF17E0B13"
val serverPublicKey = hexEncoder.decode(toxmepk)
val ourSecretKey = Array.ofDim[Byte](32)
System.arraycopy(fileBytes, 52, ourSecretKey, 0, 32)
val box = new Box(serverPublicKey, ourSecretKey)
val random = new org.abstractj.kalium.crypto.Random()
var nonce = random.randomBytes(24)
var payloadBytes = box.encrypt(nonce, rawEncoder.decode(unencryptedPayload.toString))
payloadBytes = Base64.encode(payloadBytes, Base64.NO_WRAP)
nonce = Base64.encode(nonce, Base64.NO_WRAP)
val payload = rawEncoder.encode(payloadBytes)
val nonceString = rawEncoder.encode(nonce)
val json = new JSONObject()
json.put("action", 1)
json.put("public_key", ID.substring(0, 64))
json.put("encrypted", payload)
json.put("nonce", nonceString)
jsonPost.setJSON(json.toString)
toxmeThread.start()
toxmeThread.join()
} catch {
case e: JSONException => Log.d("CreateAcccount", "JSON Exception " + e.getMessage)
case e: InterruptedException =>
}
var toastMessage = ""
val context = getApplicationContext
val duration = Toast.LENGTH_SHORT
var toast: Toast = null
val errorCode = jsonPost.getErrorCode
if ("0" == errorCode) {
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
val editor = preferences.edit()
editor.putString("active_account", account)
editor.putString("nickname", account)
editor.putString("status", "1")
editor.putString("status_message", getResources.getString(R.string.pref_default_status_message))
editor.putString("tox_id", ID)
editor.putBoolean("loggedin", true)
editor.apply()
val startTox = new Intent(getApplicationContext, classOf[ToxDoService])
getApplicationContext.startService(startTox)
val main = new Intent(getApplicationContext, classOf[MainActivity])
startActivity(main)
setResult(Activity.RESULT_OK)
finish()
} else if ("-25" == errorCode) {
toastMessage = "This name is already taken"
toast = Toast.makeText(context, toastMessage, duration)
toast.show()
} else if ("-26" == errorCode) {
toastMessage = "Internal Antox Error. Please restart and try again"
toast = Toast.makeText(context, toastMessage, duration)
toast.show()
} else if ("-4" == errorCode) {
toastMessage = "You can only register 13 accounts an hour. You have reached this limit"
toast = Toast.makeText(context, toastMessage, duration)
toast.show()
}
} else if ("-25" == errorCode) {
toastMessage = "This name is already taken"
toast = Toast.makeText(context, toastMessage, duration)
toast.show()
} else if ("-26" == errorCode) {
toastMessage = "Internal Antox Error. Please restart and try again"
toast = Toast.makeText(context, toastMessage, duration)
toast.show()
} else if ("-4" == errorCode) {
toastMessage = "You can only register 13 accounts an hour. You have reached this limit"
toast = Toast.makeText(context, toastMessage, duration)
toast.show()
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions app/src/main/scala/im/tox/antox/utils/Options.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package im.tox.antox.utils

//remove if not needed
import scala.collection.JavaConversions._

object Options {

var ipv6Enabled: Boolean = true

var udpEnabled: Boolean = false
Expand Down

0 comments on commit b1c7b84

Please sign in to comment.