tessel/gprs-sim900 — pull request #17 by facultymatt (2014-06-22 13:21:34+00:00)
Title: [DONT MERGE YET] Refactor Postmaster unsolicited check

I found the unsolicited check to be very, very confusing to follow. This is the beginning of my attempt to refactor it.

The line in question is:

```
if ((self.callback === null || (!self.started && starts.indexOf(data) === -1)) && !(!self.started && self.alternate && self.alternate[2] && self.alternate[0].filter(function(partialStart) {
if (self.debug) {
console.log([data], [partialStart], data.indexOf(partialStart));
}
return data.indexOf(partialStart) === -1;
}).length != self.alternate[0].length)) {
// ^check that we're not using soft logic either...
self.emit('unsolicited', data);
}
```

First I factored out various checks into single functions. Then I combine these functions in a function that returns true/ false.

```
function checkIsUnsolicited() {
if(!hasCallback()) return true;
if(hasCallback() && !hasStarted() && !isDataInStartsArray()) return true;
if(!usingAlternate) return true;
if(!relaxedStartChecking) return true;
if(!isDataInAlternateStartsArray()) return true;
return false;
}
```

There is a lot of logging that I will remove once I'm sure the logic is correct. @johnnyman727 can you take a look and confirm the logic is correct?