server/buddies.go (5785B)
1 package main 2 3 import ( 4 "database/sql" 5 "encoding/binary" 6 "fmt" 7 "sort" 8 "strings" 9 ) 10 11 func chatBuddyPacket(id, status uint32, metadata string) []byte { 12 body := binary.BigEndian.AppendUint32(nil, id) 13 body = binary.BigEndian.AppendUint32(body, status) 14 body = binary.BigEndian.AppendUint16(body, uint16(len(metadata))) 15 return chatPacket(40, append(body, metadata...)) 16 } 17 18 func (server *chatState) buddyList(player *chatPerson) []byte { 19 ids := make([]uint32, 0, len(player.buddies)) 20 for id := range player.buddies { 21 ids = append(ids, id) 22 } 23 sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] }) 24 var packets []byte 25 for _, id := range ids { 26 status := uint32(0) 27 if server.clients[id] != nil { 28 status = 1 29 } 30 packets = append(packets, chatNamePacket(id, server.people[id].name)...) 31 packets = append(packets, chatBuddyPacket(id, status, player.buddies[id])...) 32 } 33 return packets 34 } 35 36 func (server *chatState) buddyPresence(id, status uint32) map[*chatClient][]byte { 37 updates := make(map[*chatClient][]byte) 38 for owner, target := range server.clients { 39 if metadata, exists := server.people[owner].buddies[id]; exists { 40 updates[target] = chatBuddyPacket(id, status, metadata) 41 } 42 } 43 return updates 44 } 45 46 func (server *chatState) handleChatBuddy(client *chatClient, kind uint16, body []byte) error { 47 // Order durable mutations and presence snapshots with chat session changes. 48 packet, err := server.changeBuddies(client, kind, body) 49 if err != nil { 50 return err 51 } 52 if len(packet) != 0 { 53 return client.send(packet) 54 } 55 return nil 56 } 57 58 func (server *chatState) changeBuddies(client *chatClient, kind uint16, body []byte) ([]byte, error) { 59 if server.clients[client.id] != client || !server.people[client.id].online { 60 return nil, fmt.Errorf("chat world session ended") 61 } 62 player := server.people[client.id] 63 var id uint32 64 var metadata string 65 remove, clearRecent := kind == 41, false 66 switch kind { 67 case 40, 41: 68 if len(body) < 4 { 69 return nil, fmt.Errorf("invalid buddy identity") 70 } 71 id, body = binary.BigEndian.Uint32(body), body[4:] 72 if kind == 40 { 73 var ok bool 74 metadata, ok = readChatString(&body) 75 if !ok || len(metadata) > 65525 { 76 return nil, fmt.Errorf("invalid buddy metadata") 77 } 78 } 79 if len(body) != 0 { 80 return nil, fmt.Errorf("invalid buddy packet length") 81 } 82 case 120: 83 if len(body) < 2 { 84 return nil, fmt.Errorf("invalid chat command") 85 } 86 count := binary.BigEndian.Uint16(body) 87 body = body[2:] 88 var words []string 89 for range count { 90 word, ok := readChatString(&body) 91 if !ok || strings.ContainsRune(word, 0) { 92 return nil, fmt.Errorf("invalid chat command word") 93 } 94 words = append(words, word) 95 } 96 if len(body) != 0 { 97 return nil, fmt.Errorf("invalid chat command length") 98 } 99 if len(words) == 0 || !strings.EqualFold(words[0], "addbuddy") && !strings.EqualFold(words[0], "rembuddy") { 100 return nil, nil 101 } 102 if len(words) != 2 { 103 return chatPacket(36, appendChatString(nil, "Usage: /cc addbuddy <name> or /cc rembuddy <name|?>")), nil 104 } 105 remove = strings.EqualFold(words[0], "rembuddy") 106 clearRecent = remove && words[1] == "?" 107 metadata = "\x01" 108 if !clearRecent { 109 for _, target := range server.people { 110 if strings.EqualFold(target.name, words[1]) { 111 id = target.id 112 break 113 } 114 } 115 } 116 } 117 118 // The recent-correspondent group is a collection of ordinary buddy rows. 119 var ids []uint32 120 if clearRecent { 121 for target, blob := range player.buddies { 122 if blob == "\x00" { 123 ids = append(ids, target) 124 } 125 } 126 sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] }) 127 } else { 128 if id == client.id { 129 return chatPacket(36, appendChatString(nil, "You cannot add yourself to your friends list.")), nil 130 } 131 if target := server.people[id]; target == nil { 132 return chatPacket(36, appendChatString(nil, "Player not found.")), nil 133 } 134 ids = []uint32{id} 135 } 136 137 // Transfer only changed rows to persistence; publish presence after commit. 138 write := buddyWrite{player: client.id, metadata: metadata, remove: remove} 139 for _, target := range ids { 140 previous, exists := player.buddies[target] 141 if remove && !exists || !remove && exists && previous == metadata { 142 continue 143 } 144 write.targets = append(write.targets, target) 145 } 146 if len(write.targets) != 0 { 147 if err := server.writer.commit(write); err != nil { 148 return nil, err 149 } 150 } 151 var packets []byte 152 for _, target := range ids { 153 if remove { 154 delete(player.buddies, target) 155 packets = append(packets, chatPacket(41, binary.BigEndian.AppendUint32(nil, target))...) 156 continue 157 } 158 if player.buddies == nil { 159 player.buddies = make(map[uint32]string) 160 } 161 player.buddies[target] = metadata 162 status := uint32(0) 163 if server.clients[target] != nil { 164 status = 1 165 } 166 packets = append(packets, chatNamePacket(target, server.people[target].name)...) 167 packets = append(packets, chatBuddyPacket(target, status, metadata)...) 168 } 169 return packets, nil 170 } 171 172 // Buddy metadata is a value owned by the chat transaction, not a live player. 173 type buddyWrite struct { 174 player uint32 175 targets []uint32 176 metadata string 177 remove bool 178 } 179 180 func (write buddyWrite) persist(tx *sql.Tx) error { 181 for _, target := range write.targets { 182 query := "INSERT INTO buddies (player_id, buddy_id, metadata) VALUES (?, ?, ?) ON CONFLICT(player_id, buddy_id) DO UPDATE SET metadata = excluded.metadata" 183 args := []any{write.player, target, []byte(write.metadata)} 184 if write.remove { 185 query, args = "DELETE FROM buddies WHERE player_id = ? AND buddy_id = ?", args[:2] 186 } 187 result, err := tx.Exec(query, args...) 188 if err != nil { 189 return err 190 } 191 if count, err := result.RowsAffected(); err != nil || count != 1 { 192 return fmt.Errorf("buddy mutation affected %d rows: %v", count, err) 193 } 194 } 195 return nil 196 }