-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMessageParser.swift
176 lines (142 loc) · 5.58 KB
/
MessageParser.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Noze.io: miniirc
import core
import streams
// MARK: - Message Parser (a through stream)
// Samples:
// NICK noze
// USER noze 0 * :Noze io
// :noze JOIN :#nozechannel
//
// Basic syntax:
// [':' SOURCE]? ' ' COMMAND [' ' ARGS]? [' :' LAST-ARG]?
var line2msg : Transform<String, Message> {
return through2 {
( lines : [ String ], _, done: ( Error?, [Message]? ) -> Void ) in
/// split the line into the source, command and argument parts
func splitIRCLine(line l: String) -> ( String?, String?, [ String ]?) {
// there is probably a better way to do this ...
#if swift(>=3.2)
let chars = l
#else
let chars = l.characters
#endif
var idx = chars.startIndex
var source : String? = nil
var commandString : String? = nil
var arguments : [ String ]? = nil
// parse source
if chars[chars.startIndex] == ":" { // has a source
idx = chars.index(after: idx) // skip colon
#if swift(>=3.2)
let from : String.Index
let to : String.Index
#else
let from : String.CharacterView.Index
let to : String.CharacterView.Index
#endif
if let spaceIdx = chars.index(of: " ", from: idx) {
from = idx
to = spaceIdx
idx = chars.index(after: spaceIdx) // skip space
}
else {
from = idx
to = chars.endIndex
idx = chars.endIndex // done
}
if from != to && chars.index(after: from) != to {
source = String(chars[from..<to])
assert(!source!.isEmpty)
}
}
// parse command (numeric or string)
if idx != chars.endIndex {
var lastArg : String? = nil
let colonIdx = chars.index(of: ":", from: idx);
if colonIdx != nil {
lastArg = String(chars[chars.index(after: colonIdx!)..<chars.endIndex])
}
let allButLast = chars[idx ..< (colonIdx ?? chars.endIndex)]
.split(separator: " ",
omittingEmptySubsequences: true)
.map { return String($0) }
if !allButLast.isEmpty {
commandString = allButLast.first
arguments = []
let from = allButLast.index(after: allButLast.startIndex)
arguments!.append(contentsOf: allButLast[from..<allButLast.endIndex])
if let s = lastArg {
arguments!.append(s)
}
}
if arguments == nil { arguments = [] }
}
return ( source, commandString, arguments )
}
// convert lines to messages
let messages : [ Message ]? = lines.filter { !$0.isEmpty }.map { line in
let ( source, commandString, arguments ) = splitIRCLine(line: line)
// Note: split in two guards to compile with both S2&3
guard let cs = commandString else {
return Message(source: source, command: .Invalid(line))
}
guard let args = arguments else {
return Message(source: source, command: .Invalid(line))
}
let command : Command
// FIXME: this is wrong, too much dupe code
switch cs { // TBD: could also match on (cs, argc)
// Textual also sends: CAP[LS,302], PASS[pwd] if there was a pwd
case "NICK":
command = args.count == 1 ? .NICK(args[0]) : .InvalidArgs(cs, args)
case "USER":
let mask = args.count > 1 ? Int(args[1]) : 0
if args.count == 4 && mask != nil {
let user = UserInfo(username: args[0], usermask: UInt32(mask!),
realname: args[3])
command = .USER(user)
}
else { command = .InvalidArgs(cs, args) }
case "QUIT":
if args.count < 2 { command = .QUIT(args.first) }
else { command = .InvalidArgs(cs, args) }
case "PING":
if args.count == 1 { command = .PING(args[0]) }
else { command = .InvalidArgs(cs, args) }
case "PONG":
if args.count == 1 { command = .PONG(args[0]) }
else { command = .InvalidArgs(cs, args) }
case "JOIN":
if args.count == 1 { command = .JOIN(args[0].split(","), nil) }
else if args.count == 2 {
command = .JOIN(args[0].split(","), args[1].split(","))
}
else { command = .InvalidArgs(cs, args) }
case "PART":
switch args.count {
case 1: command = .PART(args[0].split(","), nil)
case 2: command = .PART(args[0].split(","), args[1])
default: command = .InvalidArgs(cs, args)
}
case "ISON":
command = .ISON(args)
case "LIST":
switch args.count {
case 0: command = .LIST(nil, nil)
case 1: command = .LIST(args[0].split(","), nil)
case 2: command = .LIST(args[0].split(","), args[1].split(","))
default: command = .InvalidArgs(cs, args)
}
case "PRIVMSG":
command = args.count == 2
? .PRIVMSG(args[0], args[1])
: .InvalidArgs(cs, args)
default:
if let n = Int(cs) { command = .UnsupportedNumeric(n, args) }
else { command = .Unsupported(cs, args) }
}
return Message(source: source, command: command)
}
done(nil, messages)
}
}