X-Git-Url: http://www.git.cypherpunks.ru/?p=goircd.git;a=blobdiff_plain;f=daemon.go;h=9a5ae85e75bec66c8ac8d6b33e5e66a5ec2bd927;hp=57797c7128199b80fbeb976a31d48e017601e8b8;hb=7afceda9cb6352809a0a3123c6aae1234329088b;hpb=158ab243b29e99bfbe7a78ee48662fb132b47704 diff --git a/daemon.go b/daemon.go index 57797c7..9a5ae85 100644 --- a/daemon.go +++ b/daemon.go @@ -1,6 +1,6 @@ /* goircd -- minimalistic simple Internet Relay Chat (IRC) server -Copyright (C) 2014-2015 Sergey Matveev +Copyright (C) 2014-2017 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -40,18 +40,23 @@ const ( var ( RENickname = regexp.MustCompile("^[a-zA-Z0-9-]{1,24}$") + clients map[*Client]struct{} = make(map[*Client]struct{}) + clientsM sync.RWMutex + rooms map[string]*Room = make(map[string]*Room) + roomsM sync.RWMutex roomsGroup sync.WaitGroup - - clients map[*Client]struct{} = make(map[*Client]struct{}) + roomSinks map[*Room]chan ClientEvent = make(map[*Room]chan ClientEvent) ) func SendLusers(client *Client) { lusers := 0 + clientsM.RLock() for client := range clients { if client.registered { lusers++ } } + clientsM.RUnlock() client.ReplyNicknamed("251", fmt.Sprintf("There are %d users and 0 invisible on 1 servers", lusers)) } @@ -67,7 +72,7 @@ func SendMotd(client *Client) { return } client.ReplyNicknamed("375", "- "+*hostname+" Message of the day -") - for _, s := range strings.Split(strings.Trim(string(motdText), "\n"), "\n") { + for _, s := range strings.Split(strings.TrimSuffix(string(motdText), "\n"), "\n") { client.ReplyNicknamed("372", "- "+s) } client.ReplyNicknamed("376", "End of /MOTD command") @@ -82,11 +87,14 @@ func SendWhois(client *Client, nicknames []string) { var subscriber *Client for _, nickname := range nicknames { nickname = strings.ToLower(nickname) + clientsM.RLock() for c = range clients { if strings.ToLower(*c.nickname) == nickname { + clientsM.RUnlock() goto Found } } + clientsM.RUnlock() client.ReplyNoNickChan(nickname) continue Found: @@ -101,6 +109,7 @@ func SendWhois(client *Client, nicknames []string) { client.ReplyNicknamed("301", *c.nickname, *c.away) } subscriptions = make([]string, 0) + roomsM.RLock() for _, room = range rooms { for subscriber = range room.members { if *subscriber.nickname == nickname { @@ -108,6 +117,7 @@ func SendWhois(client *Client, nicknames []string) { } } } + roomsM.RUnlock() sort.Strings(subscriptions) client.ReplyNicknamed("319", *c.nickname, strings.Join(subscriptions, " ")) client.ReplyNicknamed("318", *c.nickname, "End of /WHOIS list") @@ -121,14 +131,17 @@ func SendList(client *Client, cols []string) { rs = strings.Split(strings.Split(cols[1], " ")[0], ",") } else { rs = make([]string, 0) + roomsM.RLock() for r = range rooms { rs = append(rs, r) } + roomsM.RUnlock() } sort.Strings(rs) var room *Room var found bool for _, r = range rs { + roomsM.RLock() if room, found = rooms[r]; found { client.ReplyNicknamed( "322", @@ -137,6 +150,7 @@ func SendList(client *Client, cols []string) { *room.topic, ) } + roomsM.RUnlock() } client.ReplyNicknamed("323", "End of /LIST") } @@ -153,7 +167,8 @@ func ClientRegister(client *Client, cmd string, cols []string) { client.ReplyNotEnoughParameters("PASS") return } - client.password = &cols[1] + password := strings.TrimPrefix(cols[1], ":") + client.password = &password case "NICK": if len(cols) == 1 || len(cols[1]) < 1 { client.ReplyParts("431", "No nickname given") @@ -162,12 +177,16 @@ func ClientRegister(client *Client, cmd string, cols []string) { nickname := cols[1] // Compatibility with some clients prepending colons to nickname nickname = strings.TrimPrefix(nickname, ":") + nickname = strings.ToLower(nickname) + clientsM.RLock() for existingClient := range clients { if *existingClient.nickname == nickname { + clientsM.RUnlock() client.ReplyParts("433", "*", nickname, "Nickname is already in use") return } } + clientsM.RUnlock() if !RENickname.MatchString(nickname) { client.ReplyParts("432", "*", cols[1], "Erroneous nickname") return @@ -226,8 +245,10 @@ func ClientRegister(client *Client, cmd string, cols []string) { func RoomRegister(name string) (*Room, chan ClientEvent) { roomNew := NewRoom(name) roomSink := make(chan ClientEvent) + roomsM.Lock() rooms[name] = roomNew roomSinks[roomNew] = roomSink + roomsM.Unlock() go roomNew.Processor(roomSink) roomsGroup.Add(1) return roomNew, roomSink @@ -256,8 +277,10 @@ func HandlerJoin(client *Client, cmd string) { } else { key = "" } + roomsM.RLock() for roomExisting, roomSink = range roomSinks { if room == *roomExisting.name { + roomsM.RUnlock() if (*roomExisting.key != "") && (*roomExisting.key != key) { goto Denied } @@ -265,6 +288,7 @@ func HandlerJoin(client *Client, cmd string) { goto Joined } } + roomsM.RUnlock() roomNew, roomSink = RoomRegister(room) log.Println("Room", roomNew, "created") if key != "" { @@ -292,6 +316,7 @@ func Processor(events chan ClientEvent, finished chan struct{}) { client := event.client switch event.eventType { case EventTick: + clientsM.RLock() for c := range clients { if c.recvTimestamp.Add(PingTimeout).Before(now) { log.Println(c, "ping timeout") @@ -308,20 +333,39 @@ func Processor(events chan ClientEvent, finished chan struct{}) { } } } + clientsM.RUnlock() + roomsM.Lock() + for rn, r := range rooms { + if *statedir == "" && len(r.members) == 0 { + log.Println(rn, "emptied room") + delete(rooms, rn) + close(roomSinks[r]) + delete(roomSinks, r) + } + } + roomsM.Unlock() case EventTerm: + roomsM.RLock() for _, sink := range roomSinks { sink <- ClientEvent{eventType: EventTerm} } + roomsM.RUnlock() roomsGroup.Wait() close(finished) return case EventNew: + clientsM.Lock() clients[client] = struct{}{} + clientsM.Unlock() case EventDel: + clientsM.Lock() delete(clients, client) + clientsM.Unlock() + roomsM.RLock() for _, roomSink := range roomSinks { roomSink <- event } + roomsM.RUnlock() case EventMsg: cols := strings.SplitN(event.text, " ", 2) cmd := strings.ToUpper(cols[0]) @@ -337,6 +381,9 @@ func Processor(events chan ClientEvent, finished chan struct{}) { ClientRegister(client, cmd, cols) continue } + if client != nil { + client.recvTimestamp = now + } switch cmd { case "AWAY": if len(cols) == 1 { @@ -372,9 +419,11 @@ func Processor(events chan ClientEvent, finished chan struct{}) { continue } room := cols[0] + roomsM.RLock() r, found := rooms[room] if !found { client.ReplyNoChannel(room) + roomsM.RUnlock() continue } if len(cols) == 1 { @@ -382,6 +431,7 @@ func Processor(events chan ClientEvent, finished chan struct{}) { } else { roomSinks[r] <- ClientEvent{client, EventMode, cols[1]} } + roomsM.RUnlock() case "MOTD": SendMotd(client) case "PART": @@ -390,14 +440,15 @@ func Processor(events chan ClientEvent, finished chan struct{}) { continue } rs := strings.Split(cols[1], " ")[0] + roomsM.RLock() for _, room := range strings.Split(rs, ",") { if r, found := rooms[room]; found { roomSinks[r] <- ClientEvent{client, EventDel, ""} } else { client.ReplyNoChannel(room) - continue } } + roomsM.RUnlock() case "PING": if len(cols) == 1 { client.ReplyNicknamed("409", "No origin specified") @@ -418,6 +469,7 @@ func Processor(events chan ClientEvent, finished chan struct{}) { } msg := "" target := strings.ToLower(cols[0]) + clientsM.RLock() for c := range clients { if *c.nickname == target { msg = fmt.Sprintf(":%s %s %s %s", client, cmd, *c.nickname, cols[1]) @@ -428,9 +480,11 @@ func Processor(events chan ClientEvent, finished chan struct{}) { break } } + clientsM.RUnlock() if msg != "" { continue } + roomsM.RLock() if r, found := rooms[target]; found { roomSinks[r] <- ClientEvent{ client, @@ -440,13 +494,16 @@ func Processor(events chan ClientEvent, finished chan struct{}) { } else { client.ReplyNoNickChan(target) } + roomsM.RUnlock() case "TOPIC": if len(cols) == 1 { client.ReplyNotEnoughParameters("TOPIC") continue } cols = strings.SplitN(cols[1], " ", 2) + roomsM.RLock() r, found := rooms[cols[0]] + roomsM.RUnlock() if !found { client.ReplyNoChannel(cols[0]) continue @@ -457,18 +514,22 @@ func Processor(events chan ClientEvent, finished chan struct{}) { } else { change = "" } + roomsM.RLock() roomSinks[r] <- ClientEvent{client, EventTopic, change} + roomsM.RUnlock() case "WHO": if len(cols) == 1 || len(cols[1]) < 1 { client.ReplyNotEnoughParameters("WHO") continue } room := strings.Split(cols[1], " ")[0] + roomsM.RLock() if r, found := rooms[room]; found { roomSinks[r] <- ClientEvent{client, EventWho, ""} } else { client.ReplyNoChannel(room) } + roomsM.RUnlock() case "WHOIS": if len(cols) == 1 || len(cols[1]) < 1 { client.ReplyNotEnoughParameters("WHOIS") @@ -477,6 +538,24 @@ func Processor(events chan ClientEvent, finished chan struct{}) { cols := strings.Split(cols[1], " ") nicknames := strings.Split(cols[len(cols)-1], ",") SendWhois(client, nicknames) + case "ISON": + if len(cols) == 1 || len(cols[1]) < 1 { + client.ReplyNotEnoughParameters("ISON") + continue + } + nicksKnown := make(map[string]struct{}) + clientsM.RLock() + for c := range clients { + nicksKnown[*c.nickname] = struct{}{} + } + clientsM.RUnlock() + var nicksExists []string + for _, nickname := range strings.Split(cols[1], " ") { + if _, exists := nicksKnown[nickname]; exists { + nicksExists = append(nicksExists, nickname) + } + } + client.ReplyNicknamed("303", strings.Join(nicksExists, " ")) case "VERSION": var debug string if *verbose { @@ -489,8 +568,5 @@ func Processor(events chan ClientEvent, finished chan struct{}) { client.ReplyNicknamed("421", cmd, "Unknown command") } } - if client != nil { - client.recvTimestamp = now - } } }