"When you move to a new town," writes Maynard via the Submit-to-WTF Visual Studio Add-In, "you need to take jobs at less-than reputable firms to get yourself settled in. "
"I cleaned up a lot of filthy code during my time at $company_name, but I couldn't bring myself to alter a single line of this monstrosity of a method. I think my favorite part is when the 'password' and "confirm' are each checked against a regex, and only then does it check if they are the same string."
public void onClick(View v) {
String userString = userfld.getEditableText().toString();
String emailString = emailfld.getEditableText().toString();
String passString = passfld.getEditableText().toString();
String confirmString = confirmfld.getEditableText().toString();
String secretqString = questions[selected];
String secretaString = secretafld.getEditableText().toString();
int error = 0;
if (userString == null || userString.equalsIgnoreCase("")) {
error = 1;
} else if (emailString == null || emailString.equalsIgnoreCase("")) {
error = 2;
} else if (passString == null || passString.equalsIgnoreCase("")) {
error = 3;
} else if (confirmString == null || confirmString.equalsIgnoreCase("")) {
error = 4;
} else if (secretaString == null || secretaString.equalsIgnoreCase("")) {
error = 5;
} else if (!userString.matches("[a-zA-z0-9_]*")){
error = 6;
} else if (!passString.matches("[a-zA-Z0-9]*") || passString.length() < 6) {
error = 7;
} else if (!confirmString.matches("[a-zA-Z0-9]*")) {
error = 8;
} else if (!passString.equals(confirmString)) {
error = 9;
} else if (emailString.indexOf("@") == -1) {
error = 10;
} else {
String lhsString = emailString.substring(0, emailString.indexOf("@"));
String rhsString = emailString.substring(emailString.indexOf("@") + 1, emailString.length());
if (lhsString.equalsIgnoreCase("")) {
error = 10;
} else if (rhsString.equalsIgnoreCase("")) {
error = 10;
}
}
if (error == 0) {
String processRequest = String.format(ServerCommands.ADD_USER, URLEncoder.encode(userString),
URLEncoder.encode(emailString), URLEncoder.encode(secretqString),
URLEncoder.encode(secretaString), StaticUtils.md5(passString));
try {
InputStream is = Networking.getStreamFromURL(processRequest);
StringBuffer buffer = new StringBuffer();
Reader in = new BufferedReader(new InputStreamReader(is));
int ch;
while ((ch = in.read()) > -1) {
buffer.append((char)ch);
}
in.close();
String result = buffer.toString();
if (result.equalsIgnoreCase("0")) {
StaticUtils.toaster(app, "This username has now been registered, you can login using this username now");
this.finish();
} else if (result.equalsIgnoreCase("1")) {
app.displayError(SignUpScreen.this, app.getString(R.string.dialog_title_oops), "This email is already registered, please choose another email");
} else if (result.equalsIgnoreCase("2")) {
app.displayError(SignUpScreen.this, app.getString(R.string.dialog_title_oops), "This username is already registered, please choose another username");
} else {
app.displayError(SignUpScreen.this, app.getString(R.string.dialog_title_oops), "An unknown error occured.");
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (error == 1) {
app.displayError(SignUpScreen.this, app.getString(R.string.dialog_title_oops), "Username field must be filled in.");
} else if (error == 2) {
app.displayError(SignUpScreen.this, app.getString(R.string.dialog_title_oops), "Email field must be filled in.");
} else if (error == 3) {
app.displayError(SignUpScreen.this, app.getString(R.string.dialog_title_oops), "Password field must be filled in.");
} else if (error == 4) {
app.displayError(SignUpScreen.this, app.getString(R.string.dialog_title_oops), "Confirm Password field must be filled in.");
} else if (error == 5) {
app.displayError(SignUpScreen.this, app.getString(R.string.dialog_title_oops), "Secret Answer field must be filled in.");
} else if (error == 6) {
app.displayError(SignUpScreen.this, app.getString(R.string.dialog_title_oops), "Username must only contain alphanumeric characters and _");
} else if (error == 7) {
app.displayError(SignUpScreen.this, app.getString(R.string.dialog_title_oops), "Password must only contain alphanumeric characters, and be at least 6 characters.");
} else if (error == 8) {
app.displayError(SignUpScreen.this, app.getString(R.string.dialog_title_oops), "Confirm Password must only contain alphanumeric characters");
} else if (error == 9) {
app.displayError(SignUpScreen.this, app.getString(R.string.dialog_title_oops), "Password does not match Confirm Password");
} else if (error == 10) {
app.displayError(SignUpScreen.this, app.getString(R.string.dialog_title_oops), "This email does not seem to be valid");
}
}