Skip to content

Commit

Permalink
refactor: fileInfoBuilder() & addToResumeQueue()
Browse files Browse the repository at this point in the history
Both function simplify praseCtcp and checkBeforeDL comprehension
  • Loading branch information
JiPaix committed Aug 23, 2020
1 parent e6db5a0 commit d71a99c
Showing 1 changed file with 44 additions and 40 deletions.
84 changes: 44 additions & 40 deletions src/ctcp_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ export class CtcpParser extends AddJob {
delay: this.timeout,
fileInfo: fileInfo,
})
// this.TOeventMessage(candidate, `couldn't connect to %yellow%` + fileInfo.ip + ':' + fileInfo.port, 6)
// .TOeventType(candidate, 'error')
// .TOstart(candidate, this.timeout, fileInfo)
if (fileInfo.type === 'DCC SEND') {
isResume = this.checkExistingFiles(fileInfo, candidate, resp)
}
Expand All @@ -121,14 +118,22 @@ export class CtcpParser extends AddJob {
}
}
}

private addToResumeQueue(fileInfo: FileInfo, nick: string): void {
this.resumequeue.push({
type: fileInfo.type,
nick: nick,
ip: fileInfo.ip,
length: fileInfo.length,
token: fileInfo.token,
position: fileInfo.position,
port: fileInfo.port,
filePath: fileInfo.filePath,
file: fileInfo.file,
})
}

private checkExistingFiles(
fileInfo: FileInfo,
candidate: Job,
resp: {
[prop: string]: string
}
): boolean {
private checkExistingFiles(fileInfo: FileInfo, candidate: Job, resp: { [prop: string]: string }): boolean {
if (fs.existsSync(fileInfo.filePath) && this.path) {
fileInfo.position = fs.statSync(fileInfo.filePath).size - 8192
if (fileInfo.position < 0) {
Expand All @@ -137,17 +142,7 @@ export class CtcpParser extends AddJob {
fileInfo.length = fileInfo.length - fileInfo.position
const quotedFilename = this.fileNameWithQuotes(fileInfo.file)
this.ctcpRequest(resp.nick, 'DCC RESUME', quotedFilename, fileInfo.port, fileInfo.position, fileInfo.token)
this.resumequeue.push({
type: fileInfo.type,
nick: resp.nick,
ip: fileInfo.ip,
length: fileInfo.length,
token: fileInfo.token,
position: fileInfo.position,
port: fileInfo.port,
filePath: fileInfo.filePath,
file: fileInfo.file,
})
this.addToResumeQueue(fileInfo, resp.nick)
this.__SetupTimeout({
candidate: candidate,
eventType: 'error',
Expand Down Expand Up @@ -177,26 +172,20 @@ export class CtcpParser extends AddJob {
return match
}
}
protected parseCtcp(text: string, nick: string): FileInfo | void {
const parts = this.ctcpMatch(text)
const type = `${parts[0]} ${parts[1]}`
if (type === 'DCC ACCEPT') {
const resume = this.resumequeue.filter(q => q.nick == nick)
this.resumequeue = this.resumequeue.filter(q => q.nick !== nick)
if (resume.length) {
return {
type: `${parts[0]} ${parts[1]}`,
file: parts[2].replace(/"/g, ''),
filePath: resume[0].filePath,
ip: resume[0].ip,
port: resume[0].port,
position: resume[0].position,
length: resume[0].length,
token: resume[0].token,
}

private fileInfoBuilder(parts: RegExpMatchArray, resume?: ResumeQueue): FileInfo {
if (resume) {
return {
type: `${parts[0]} ${parts[1]}`,
file: parts[2].replace(/"/g, ''),
filePath: resume.filePath,
ip: resume.ip,
port: resume.port,
position: resume.position,
length: resume.length,
token: resume.token,
}
}
if (type === 'DCC SEND') {
} else {
return {
type: `${parts[0]} ${parts[1]}`,
file: parts[2].replace(/"/g, ''),
Expand All @@ -210,6 +199,21 @@ export class CtcpParser extends AddJob {
}
}

protected parseCtcp(text: string, nick: string): FileInfo | void {
const parts = this.ctcpMatch(text)
const type = `${parts[0]} ${parts[1]}`
if (type === 'DCC ACCEPT') {
const resume = this.resumequeue.filter(q => q.nick == nick)
this.resumequeue = this.resumequeue.filter(q => q.nick !== nick)
if (resume.length) {
return this.fileInfoBuilder(parts, resume[0])
}
}
if (type === 'DCC SEND') {
return this.fileInfoBuilder(parts)
}
}

protected uint32ToIP(n: number): string {
const byte1 = n & 255,
byte2 = (n >> 8) & 255,
Expand Down

0 comments on commit d71a99c

Please sign in to comment.