1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2024-11-12 03:55:01 -05:00

Allow chars '[' and ']' in ident

This commit is contained in:
Sebastian Kaspari 2010-12-17 23:37:00 +01:00
parent aaa313e805
commit e0b4b3d25d

View File

@ -17,13 +17,22 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with Yaaic. If not, see <http://www.gnu.org/licenses/>. along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.yaaic.activity; package org.yaaic.activity;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.yaaic.R;
import org.yaaic.Yaaic;
import org.yaaic.db.Database;
import org.yaaic.exception.ValidationException;
import org.yaaic.model.Extra;
import org.yaaic.model.Identity;
import org.yaaic.model.Server;
import org.yaaic.model.Status;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
@ -37,15 +46,6 @@ import android.widget.EditText;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.Toast; import android.widget.Toast;
import org.yaaic.R;
import org.yaaic.Yaaic;
import org.yaaic.db.Database;
import org.yaaic.exception.ValidationException;
import org.yaaic.model.Extra;
import org.yaaic.model.Identity;
import org.yaaic.model.Server;
import org.yaaic.model.Status;
/** /**
* Add a new server to the list * Add a new server to the list
* *
@ -56,12 +56,12 @@ public class AddServerActivity extends Activity implements OnClickListener
private static final int REQUEST_CODE_CHANNELS = 1; private static final int REQUEST_CODE_CHANNELS = 1;
private static final int REQUEST_CODE_COMMANDS = 2; private static final int REQUEST_CODE_COMMANDS = 2;
private static final int REQUEST_CODE_ALIASES = 3; private static final int REQUEST_CODE_ALIASES = 3;
private Server server; private Server server;
private ArrayList<String> aliases; private ArrayList<String> aliases;
private ArrayList<String> channels; private ArrayList<String> channels;
private ArrayList<String> commands; private ArrayList<String> commands;
/** /**
* On create * On create
*/ */
@ -69,12 +69,12 @@ public class AddServerActivity extends Activity implements OnClickListener
public void onCreate(Bundle savedInstanceState) public void onCreate(Bundle savedInstanceState)
{ {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.serveradd); setContentView(R.layout.serveradd);
aliases = new ArrayList<String>(); aliases = new ArrayList<String>();
channels = new ArrayList<String>(); channels = new ArrayList<String>();
commands = new ArrayList<String>(); commands = new ArrayList<String>();
((Button) findViewById(R.id.add)).setOnClickListener(this); ((Button) findViewById(R.id.add)).setOnClickListener(this);
((Button) findViewById(R.id.cancel)).setOnClickListener(this); ((Button) findViewById(R.id.cancel)).setOnClickListener(this);
((Button) findViewById(R.id.aliases)).setOnClickListener(this); ((Button) findViewById(R.id.aliases)).setOnClickListener(this);
@ -86,7 +86,7 @@ public class AddServerActivity extends Activity implements OnClickListener
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, charsets); ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, charsets);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter); spinner.setAdapter(adapter);
Bundle extras = getIntent().getExtras(); Bundle extras = getIntent().getExtras();
if (extras != null && extras.containsKey(Extra.SERVER)) { if (extras != null && extras.containsKey(Extra.SERVER)) {
// Request to edit an existing server // Request to edit an existing server
@ -96,18 +96,18 @@ public class AddServerActivity extends Activity implements OnClickListener
this.channels = db.getChannelsByServerId(server.getId()); this.channels = db.getChannelsByServerId(server.getId());
this.commands = db.getCommandsByServerId(server.getId()); this.commands = db.getCommandsByServerId(server.getId());
db.close(); db.close();
// Set server values // Set server values
((EditText) findViewById(R.id.title)).setText(server.getTitle()); ((EditText) findViewById(R.id.title)).setText(server.getTitle());
((EditText) findViewById(R.id.host)).setText(server.getHost()); ((EditText) findViewById(R.id.host)).setText(server.getHost());
((EditText) findViewById(R.id.port)).setText(String.valueOf(server.getPort())); ((EditText) findViewById(R.id.port)).setText(String.valueOf(server.getPort()));
((EditText) findViewById(R.id.password)).setText(server.getPassword()); ((EditText) findViewById(R.id.password)).setText(server.getPassword());
((EditText) findViewById(R.id.nickname)).setText(server.getIdentity().getNickname()); ((EditText) findViewById(R.id.nickname)).setText(server.getIdentity().getNickname());
((EditText) findViewById(R.id.ident)).setText(server.getIdentity().getIdent()); ((EditText) findViewById(R.id.ident)).setText(server.getIdentity().getIdent());
((EditText) findViewById(R.id.realname)).setText(server.getIdentity().getRealName()); ((EditText) findViewById(R.id.realname)).setText(server.getIdentity().getRealName());
((CheckBox) findViewById(R.id.useSSL)).setChecked(server.useSSL()); ((CheckBox) findViewById(R.id.useSSL)).setChecked(server.useSSL());
// Select charset // Select charset
if (server.getCharset() != null) { if (server.getCharset() != null) {
for (int i = 0; i < charsets.length; i++) { for (int i = 0; i < charsets.length; i++) {
@ -118,18 +118,18 @@ public class AddServerActivity extends Activity implements OnClickListener
} }
} }
} }
Uri uri = getIntent().getData(); Uri uri = getIntent().getData();
if (uri != null && uri.getScheme().equals("irc")) { if (uri != null && uri.getScheme().equals("irc")) {
// handling an irc:// uri // handling an irc:// uri
((EditText) findViewById(R.id.host)).setText(uri.getHost()); ((EditText) findViewById(R.id.host)).setText(uri.getHost());
if (uri.getPort() != -1) { if (uri.getPort() != -1) {
((EditText) findViewById(R.id.port)).setText(String.valueOf(uri.getPort())); ((EditText) findViewById(R.id.port)).setText(String.valueOf(uri.getPort()));
} }
} }
} }
/** /**
* On activity result * On activity result
*/ */
@ -139,7 +139,7 @@ public class AddServerActivity extends Activity implements OnClickListener
if (resultCode != RESULT_OK) { if (resultCode != RESULT_OK) {
return; // ignore everything else return; // ignore everything else
} }
switch (requestCode) { switch (requestCode) {
case REQUEST_CODE_ALIASES: case REQUEST_CODE_ALIASES:
aliases.clear(); aliases.clear();
@ -157,6 +157,7 @@ public class AddServerActivity extends Activity implements OnClickListener
/** /**
* On click add server or cancel activity * On click add server or cancel activity
*/ */
@Override
public void onClick(View v) public void onClick(View v)
{ {
switch (v.getId()) { switch (v.getId()) {
@ -189,21 +190,21 @@ public class AddServerActivity extends Activity implements OnClickListener
} catch(ValidationException e) { } catch(ValidationException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
} }
break; break;
case R.id.cancel: case R.id.cancel:
setResult(RESULT_CANCELED); setResult(RESULT_CANCELED);
finish(); finish();
break; break;
} }
} }
/** /**
* Add server to database * Add server to database
*/ */
private void addServer() private void addServer()
{ {
Database db = new Database(this); Database db = new Database(this);
Identity identity = getIdentityFromView(); Identity identity = getIdentityFromView();
long identityId = db.addIdentity( long identityId = db.addIdentity(
identity.getNickname(), identity.getNickname(),
@ -211,7 +212,7 @@ public class AddServerActivity extends Activity implements OnClickListener
identity.getRealName(), identity.getRealName(),
identity.getAliases() identity.getAliases()
); );
Server server = getServerFromView(); Server server = getServerFromView();
long serverId = db.addServer( long serverId = db.addServer(
server.getTitle(), server.getTitle(),
@ -223,30 +224,30 @@ public class AddServerActivity extends Activity implements OnClickListener
identityId, identityId,
server.getCharset() server.getCharset()
); );
db.setChannels((int) serverId, channels); db.setChannels((int) serverId, channels);
db.setCommands((int) serverId, commands); db.setCommands((int) serverId, commands);
db.close(); db.close();
server.setId((int) serverId); server.setId((int) serverId);
server.setIdentity(identity); server.setIdentity(identity);
server.setAutoJoinChannels(channels); server.setAutoJoinChannels(channels);
server.setConnectCommands(commands); server.setConnectCommands(commands);
Yaaic.getInstance().addServer(server); Yaaic.getInstance().addServer(server);
} }
/** /**
* Update server * Update server
*/ */
private void updateServer() private void updateServer()
{ {
Database db = new Database(this); Database db = new Database(this);
int serverId = this.server.getId(); int serverId = this.server.getId();
int identityId = db.getIdentityIdByServerId(serverId); int identityId = db.getIdentityIdByServerId(serverId);
Server server = getServerFromView(); Server server = getServerFromView();
db.updateServer( db.updateServer(
serverId, serverId,
@ -259,7 +260,7 @@ public class AddServerActivity extends Activity implements OnClickListener
identityId, identityId,
server.getCharset() server.getCharset()
); );
Identity identity = getIdentityFromView(); Identity identity = getIdentityFromView();
db.updateIdentity( db.updateIdentity(
identityId, identityId,
@ -268,20 +269,20 @@ public class AddServerActivity extends Activity implements OnClickListener
identity.getNickname(), identity.getNickname(),
identity.getAliases() identity.getAliases()
); );
db.setChannels(serverId, channels); db.setChannels(serverId, channels);
db.setCommands(serverId, commands); db.setCommands(serverId, commands);
db.close(); db.close();
server.setId(this.server.getId()); server.setId(this.server.getId());
server.setIdentity(identity); server.setIdentity(identity);
server.setAutoJoinChannels(channels); server.setAutoJoinChannels(channels);
server.setConnectCommands(commands); server.setConnectCommands(commands);
Yaaic.getInstance().updateServer(server); Yaaic.getInstance().updateServer(server);
} }
/** /**
* Populate a server object from the data in the view * Populate a server object from the data in the view
* *
@ -295,10 +296,10 @@ public class AddServerActivity extends Activity implements OnClickListener
String password = ((EditText) findViewById(R.id.password)).getText().toString().trim(); String password = ((EditText) findViewById(R.id.password)).getText().toString().trim();
String charset = ((Spinner) findViewById(R.id.charset)).getSelectedItem().toString(); String charset = ((Spinner) findViewById(R.id.charset)).getSelectedItem().toString();
Boolean useSSL = ((CheckBox) findViewById(R.id.useSSL)).isChecked(); Boolean useSSL = ((CheckBox) findViewById(R.id.useSSL)).isChecked();
// not in use yet // not in use yet
//boolean autoConnect = ((CheckBox) findViewById(R.id.autoconnect)).isChecked(); //boolean autoConnect = ((CheckBox) findViewById(R.id.autoconnect)).isChecked();
Server server = new Server(); Server server = new Server();
server.setHost(host); server.setHost(host);
server.setPort(port); server.setPort(port);
@ -310,7 +311,7 @@ public class AddServerActivity extends Activity implements OnClickListener
return server; return server;
} }
/** /**
* Populate an identity object from the data in the view * Populate an identity object from the data in the view
* *
@ -321,17 +322,17 @@ public class AddServerActivity extends Activity implements OnClickListener
String nickname = ((EditText) findViewById(R.id.nickname)).getText().toString().trim(); String nickname = ((EditText) findViewById(R.id.nickname)).getText().toString().trim();
String ident = ((EditText) findViewById(R.id.ident)).getText().toString().trim(); String ident = ((EditText) findViewById(R.id.ident)).getText().toString().trim();
String realname = ((EditText) findViewById(R.id.realname)).getText().toString().trim(); String realname = ((EditText) findViewById(R.id.realname)).getText().toString().trim();
Identity identity = new Identity(); Identity identity = new Identity();
identity.setNickname(nickname); identity.setNickname(nickname);
identity.setIdent(ident); identity.setIdent(ident);
identity.setRealName(realname); identity.setRealName(realname);
identity.setAliases(aliases); identity.setAliases(aliases);
return identity; return identity;
} }
/** /**
* Validate the input for a server * Validate the input for a server
* *
@ -343,29 +344,29 @@ public class AddServerActivity extends Activity implements OnClickListener
String host = ((EditText) findViewById(R.id.host)).getText().toString(); String host = ((EditText) findViewById(R.id.host)).getText().toString();
String port = ((EditText) findViewById(R.id.port)).getText().toString(); String port = ((EditText) findViewById(R.id.port)).getText().toString();
String charset = ((Spinner) findViewById(R.id.charset)).getSelectedItem().toString(); String charset = ((Spinner) findViewById(R.id.charset)).getSelectedItem().toString();
if (title.trim().equals("")) { if (title.trim().equals("")) {
throw new ValidationException(getResources().getString(R.string.validation_blank_title)); throw new ValidationException(getResources().getString(R.string.validation_blank_title));
} }
if (host.trim().equals("")) { if (host.trim().equals("")) {
// XXX: We should use some better host validation // XXX: We should use some better host validation
throw new ValidationException(getResources().getString(R.string.validation_blank_host)); throw new ValidationException(getResources().getString(R.string.validation_blank_host));
} }
try { try {
Integer.parseInt(port); Integer.parseInt(port);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new ValidationException(getResources().getString(R.string.validation_invalid_port)); throw new ValidationException(getResources().getString(R.string.validation_invalid_port));
} }
try { try {
"".getBytes(charset); "".getBytes(charset);
} }
catch (UnsupportedEncodingException e) { catch (UnsupportedEncodingException e) {
throw new ValidationException(getResources().getString(R.string.validation_unsupported_charset)); throw new ValidationException(getResources().getString(R.string.validation_unsupported_charset));
} }
Database db = new Database(this); Database db = new Database(this);
if (db.isTitleUsed(title) && (server == null || !server.getTitle().equals(title))) { if (db.isTitleUsed(title) && (server == null || !server.getTitle().equals(title))) {
db.close(); db.close();
@ -373,7 +374,7 @@ public class AddServerActivity extends Activity implements OnClickListener
} }
db.close(); db.close();
} }
/** /**
* Validate the input for a identity * Validate the input for a identity
* *
@ -384,30 +385,30 @@ public class AddServerActivity extends Activity implements OnClickListener
String nickname = ((EditText) findViewById(R.id.nickname)).getText().toString(); String nickname = ((EditText) findViewById(R.id.nickname)).getText().toString();
String ident = ((EditText) findViewById(R.id.ident)).getText().toString(); String ident = ((EditText) findViewById(R.id.ident)).getText().toString();
String realname = ((EditText) findViewById(R.id.realname)).getText().toString(); String realname = ((EditText) findViewById(R.id.realname)).getText().toString();
if (nickname.trim().equals("")) { if (nickname.trim().equals("")) {
throw new ValidationException(getResources().getString(R.string.validation_blank_nickname)); throw new ValidationException(getResources().getString(R.string.validation_blank_nickname));
} }
if (ident.trim().equals("")) { if (ident.trim().equals("")) {
throw new ValidationException(getResources().getString(R.string.validation_blank_ident)); throw new ValidationException(getResources().getString(R.string.validation_blank_ident));
} }
if (realname.trim().equals("")) { if (realname.trim().equals("")) {
throw new ValidationException(getResources().getString(R.string.validation_blank_realname)); throw new ValidationException(getResources().getString(R.string.validation_blank_realname));
} }
// RFC 1459: <nick> ::= <letter> { <letter> | <number> | <special> } // RFC 1459: <nick> ::= <letter> { <letter> | <number> | <special> }
// <special> ::= '-' | '[' | ']' | '\' | '`' | '^' | '{' | '}' // <special> ::= '-' | '[' | ']' | '\' | '`' | '^' | '{' | '}'
// Chars that are not in RFC 1459 but are supported too: // Chars that are not in RFC 1459 but are supported too:
// | and _ // | and _
Pattern nickPattern = Pattern.compile("^[a-zA-Z_][a-zA-Z0-9^\\-`\\[\\]{}|_\\\\]*$"); Pattern nickPattern = Pattern.compile("^[a-zA-Z_][a-zA-Z0-9^\\-`\\[\\]{}|_\\\\]*$");
if (!nickPattern.matcher(nickname).matches()) { if (!nickPattern.matcher(nickname).matches()) {
throw new ValidationException(getResources().getString(R.string.validation_invalid_nickname)); throw new ValidationException(getResources().getString(R.string.validation_invalid_nickname));
} }
// We currently only allow chars and numbers as ident // We currently only allow chars and numbers as ident
Pattern identPattern = Pattern.compile("^[a-zA-Z0-9]+$"); Pattern identPattern = Pattern.compile("^[a-zA-Z0-9\\[\\]]+$");
if (!identPattern.matcher(ident).matches()) { if (!identPattern.matcher(ident).matches()) {
throw new ValidationException(getResources().getString(R.string.validation_invalid_ident)); throw new ValidationException(getResources().getString(R.string.validation_invalid_ident));
} }