-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathChannel.swift
79 lines (59 loc) · 1.89 KB
/
Channel.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
// Noze.io: miniirc
class Channel {
let serverID = "noze.io"
let name : String
var welcome : String
var operators = [ String ]()
var sessions = [ Session ]()
var memberNicks : [ String ] {
return sessions.filter { $0.nick != nil }.map { $0.nick! }
}
init(name: String, welcome: String? = nil) {
self.name = name
self.welcome = welcome ?? "Welcome to \(name)!"
}
// MARK: - Sending Messages
func sendMessage(source s: String, message: String) {
send(source: s, command: "PRIVMSG", name, message)
}
func send(source s: String, command: String, _ args: String...) {
for otherUser in sessions {
guard otherUser.nick! != s else { continue }
_ = otherUser.send(source: s, command: command, args: args)
}
}
// MARK: - Joining & Leaving
func join(session s: Session) {
let joinedAlready = sessions.contains { $0 === s }
guard !joinedAlready else {
print("joined already?!: \(s)")
return
}
sessions.append(s)
sendWelcome(session: s)
send(source: s.nick!, command: "JOIN", name)
}
func part(session s: Session) {
#if swift(>=5)
let idxO = sessions.firstIndex(where: { $0 === s })
#else
let idxO = sessions.index(where: { $0 === s })
#endif
if let idx = idxO {
print("leaving channel \(name): \(s)")
sessions.remove(at: idx)
send(source: s.nick!, command: "PART", name)
}
}
// MARK: - Welcome
func sendWelcome(session s: Session) {
let nick = s.nick ?? "<unknown>"
let ms = memberNicks.joined(separator: " ")
s.send(source: serverID, command: 332, nick, name, welcome)
s.send(source: serverID, command: 353, nick, "=", name, ms)
s.send(source: serverID, command: 366, nick, name,"End of /NAMES list.")
}
}
var nameToChannel : [ String : Channel ] = [
"#noze": Channel(name: "#noze")
]