Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove annoying error thrown by parseTextMessageResult #1 #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 36 additions & 34 deletions src/parsing.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,40 +60,42 @@ module.exports = class Parser {
parseTextMessageResult(result) {
const list = result.split("\r\n")
let messages = []
for (let i = 0; i < list.length; i += 2) {
const parts = list[i].replace("+CMGL: ","").split(",")

const index = parseInt(parts[0])
const status = parts[1].trimQuotes()
const sender = parts[2].trimQuotes().decodedUCS2Hex()
const date = parts[4].trimQuotes().replace(/\//g,"-")
const time = parts[5].trimQuotes()
const numberType = parseInt(parts[6])
const textLength = parseInt(parts[7])
const messageText = list[i+1]

// Message Text decode. Can be either UTF-8 or UCS2. We check this by the char byte size
const textBuffer = Buffer.from(messageText,"hex")
const textCharSize = textBuffer.length / textLength
const decodedText = textCharSize == 2 ? messageText.decodedUCS2Hex() : textBuffer.toString("utf-8")

//Time calculate
const timeParts = time.split("+")
const timezone = parseFloat(timeParts[1])/4.0 * 100
const timezonePrefix = timezone > 0 ? "+" : "-"
const timezoneString = timezonePrefix + Math.abs(timezone).toString().padStart(4,"0")
const timeStamp = `20${date}T${timeParts[0]}${timezoneString}`
const senderString = [PhoneNumberType.text, PhoneNumberType.text2].includes(numberType) ? sender.decodedFromAsciiString() : sender
messages.push({
index: index,
status: status,
sender: senderString,
time: new Date(timeStamp),
text: decodedText,
numberType: numberType,
rawHeader: list[i],
rawMessage: messageText,
})
if(list.length > 1){ // Remove annoying error when no messages received
for (let i = 0; i < list.length; i += 2) {
const parts = list[i].replace("+CMGL: ","").split(",")

const index = parseInt(parts[0])
const status = parts[1].trimQuotes()
const sender = parts[2].trimQuotes().decodedUCS2Hex()
const date = parts[4].trimQuotes().replace(/\//g,"-")
const time = parts[5].trimQuotes()
const numberType = parseInt(parts[6])
const textLength = parseInt(parts[7])
const messageText = list[i+1]

// Message Text decode. Can be either UTF-8 or UCS2. We check this by the char byte size
const textBuffer = Buffer.from(messageText,"hex")
const textCharSize = textBuffer.length / textLength
const decodedText = textCharSize == 2 ? messageText.decodedUCS2Hex() : textBuffer.toString("utf-8")

//Time calculate
const timeParts = time.split("+")
const timezone = parseFloat(timeParts[1])/4.0 * 100
const timezonePrefix = timezone > 0 ? "+" : "-"
const timezoneString = timezonePrefix + Math.abs(timezone).toString().padStart(4,"0")
const timeStamp = `20${date}T${timeParts[0]}${timezoneString}`
const senderString = [PhoneNumberType.text, PhoneNumberType.text2].includes(numberType) ? sender.decodedFromAsciiString() : sender
messages.push({
index: index,
status: status,
sender: senderString,
time: new Date(timeStamp),
text: decodedText,
numberType: numberType,
rawHeader: list[i],
rawMessage: messageText,
})
}
}
return messages
}
Expand Down