Skip to content

Instantly share code, notes, and snippets.

@jeffmarshall
Created May 31, 2011 17:21
Show Gist options
  • Save jeffmarshall/1000912 to your computer and use it in GitHub Desktop.
Save jeffmarshall/1000912 to your computer and use it in GitHub Desktop.
var datatypes = {
action: /^[-\w]$/,
channel: /^[-_a-z0-9]*$/,
nick: /^[-\w]$/,
message: /^.{1,255}$/,
timestamp: /^\d$/
}
var messagetype = {
join: [
'channel',
],
say: [
'message'
]
}
function valid(datatype, data){
if(isNAN(data) && typeof data != 'string'){
console.error('Datatype Validation Failed: unacceptable data '+ data);
return false;
}
if (datatypes[datatype] == "undefined"){
console.error('Datatype Validation Failed: invalid datatype '+ datatype);
return false;
}
if(typeof data != "string" || data.match(datatypes[datatype]) === null){
console.log('Datatype Validation Failed: '+ datatypes[datatype] +" did not find a match in "+ data);
return false;
}
return true;
}
function Message(action){
this.action = valid(action) ? action;
this.timestamp = new Date().getTime();
this.setMessageAttribute = function(datatype, data){
if(valid(datatype, data)){
this[datatype] = data;
}
}
}
function JoinMessage(channel){
Message.call(this, 'join');
this.channel = channel;
}
function SayMessage(message){
Message.call(this, 'say');
this.message = message;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment