|
| 1 | +package ts3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +type ChannelGroup struct { |
| 10 | + Id int64 |
| 11 | + Name string |
| 12 | + Type GroupType // found in servergroup.go |
| 13 | + // IconId int |
| 14 | + // SaveDb int |
| 15 | + // SortId int |
| 16 | + // NameMode int |
| 17 | +} |
| 18 | + |
| 19 | +// Get a list of channel groups on the server |
| 20 | +func (TSClient *Conn) ChannelGroups() (*QueryResponse, *[]ChannelGroup, error) { |
| 21 | + var channelGroups []ChannelGroup |
| 22 | + |
| 23 | + res, body, err := TSClient.Exec("channelgrouplist") |
| 24 | + if err != nil || !res.IsSuccess { |
| 25 | + Log(Error, "Failed to get channel groups from TS server \n%v \n%v", res, err) |
| 26 | + return res, nil, err |
| 27 | + } |
| 28 | + |
| 29 | + groups := strings.Split(body, "|") |
| 30 | + for i := 0; i < len(groups); i++ { |
| 31 | + seg := strings.Split(groups[i], " ") |
| 32 | + |
| 33 | + // Get the channel group ID |
| 34 | + cgid, err := strconv.ParseInt(GetVal(seg[0]), 10, 64) |
| 35 | + if err != nil { |
| 36 | + Log(Error, "Failed to parse the Group ID \n%v", err) |
| 37 | + return nil, nil, err |
| 38 | + } |
| 39 | + |
| 40 | + // Get the Group Type ID so we can convert it to an enum later |
| 41 | + groupTypeId, err := strconv.ParseInt(GetVal(seg[2]), 10, 64) |
| 42 | + if err != nil { |
| 43 | + Log(Error, "Failed to parse the Group Type \n%v", err) |
| 44 | + } |
| 45 | + |
| 46 | + channelGroups = append(channelGroups, ChannelGroup{ |
| 47 | + Id: cgid, |
| 48 | + Type: GroupType(groupTypeId), |
| 49 | + Name: Decode(GetVal(seg[1])), |
| 50 | + }) |
| 51 | + } |
| 52 | + |
| 53 | + return res, &channelGroups, nil |
| 54 | +} |
| 55 | + |
| 56 | +// Add a client to a specific channel group for a given channel |
| 57 | +func (TSClient *Conn) SetChannelGroup(cgid int, cid int, cldbid int) (*QueryResponse, error) { |
| 58 | + res, _, err := TSClient.Exec("setclientchannelgroup cgid=%v cid=%v cldbid=%v", cgid, cid, cldbid) |
| 59 | + if err != nil || !res.IsSuccess { |
| 60 | + Log(Error, "Unable to update client channel group {clientDbId: %v, channelId: %v, channelGroupId: %v}. \n%v \n%v", cldbid, cid, cgid, res, err) |
| 61 | + return res, err |
| 62 | + } |
| 63 | + |
| 64 | + return res, nil |
| 65 | +} |
| 66 | + |
| 67 | +// Set a user back to the default channel group. The default channel group is a group that: |
| 68 | +// a) has the name "guest" && b) is a RegularGroup type |
| 69 | +func (TSClient *Conn) ResetChannelGroup(cid int, cldbid int) (*QueryResponse, error) { |
| 70 | + res, groups, err := TSClient.ChannelGroups() |
| 71 | + if err != nil || !res.IsSuccess { |
| 72 | + Log(Error, "Failed to get avaliable channel groups \n%v \n%v", res, err) |
| 73 | + return res, err |
| 74 | + } |
| 75 | + |
| 76 | + // Find the group ID of the guest channel group |
| 77 | + var cgid int64 |
| 78 | + for _, group := range *groups { |
| 79 | + if group.Name == "Guest" && group.Type == RegularGroup { |
| 80 | + cgid = group.Id |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Set the channel group |
| 85 | + res, err = TSClient.SetChannelGroup(int(cgid), cid, cldbid) |
| 86 | + if err != nil || !res.IsSuccess { |
| 87 | + Log(Error, "Failed to reset the users channel group \n%v \n%v", res, err) |
| 88 | + return res, err |
| 89 | + } |
| 90 | + |
| 91 | + return res, nil |
| 92 | +} |
| 93 | + |
| 94 | +// Return the members of a specific channel group for a given channel |
| 95 | +func (TSClient *Conn) ChannelGroupMembers(cgid int, cid int) (*QueryResponse, []User, error) { |
| 96 | + users := []User{} |
| 97 | + |
| 98 | + // Get a list of channel group members for a given channel |
| 99 | + res, body, err := TSClient.Exec("channelgroupclientlist cid=%v cgid=%v", cid, cgid) |
| 100 | + if err != nil || !res.IsSuccess { |
| 101 | + Log(Error, "Failed to get the list of channel group members {cid: %v, cgid: %v} \n%v \n%v", cid, cgid, res, err) |
| 102 | + return res, nil, err |
| 103 | + } |
| 104 | + |
| 105 | + // Map of active clients using their DatabaseID as the map key |
| 106 | + sessions, err := TSClient.ActiveClients() |
| 107 | + if err != nil { |
| 108 | + Log(Error, "Fauked to get the list of active clients \n%v \n%v", res, err) |
| 109 | + return nil, nil, err |
| 110 | + } |
| 111 | + |
| 112 | + clients := strings.Split(body, "|") |
| 113 | + for i := 0; i < len(clients); i++ { |
| 114 | + parts := strings.Split(clients[i], " ") |
| 115 | + |
| 116 | + // Parse the client database id |
| 117 | + cldbid, err := strconv.ParseInt(GetVal(parts[1]), 10, 64) |
| 118 | + if err != nil { |
| 119 | + Log(Error, "Failed to parse the client database ID \n%v", err) |
| 120 | + return nil, nil, err |
| 121 | + } |
| 122 | + |
| 123 | + // Find the user using their CLDBID |
| 124 | + user, err := TSClient.FindUserByDbId(cldbid) |
| 125 | + if err != nil { |
| 126 | + Log(Error, "Failed to find user using the cldbid %v \n%v", cldbid, err) |
| 127 | + return nil, nil, err |
| 128 | + } |
| 129 | + |
| 130 | + // Assign the CLID (active client IDs) for the users active sessions |
| 131 | + user.ActiveSessionIds = sessions[user.Cldbid] |
| 132 | + |
| 133 | + // Add user object to result array |
| 134 | + users = append(users, *user) |
| 135 | + } |
| 136 | + |
| 137 | + return res, users, nil |
| 138 | +} |
| 139 | + |
| 140 | +// Poke all clients who belong to a given channel group in a specific channel |
| 141 | +func (TSClient *Conn) ChannelGroupPoke(cgid int, cid int, msg string) (*QueryResponse, error) { |
| 142 | + res, body, err := TSClient.ChannelGroupMembers(cgid, cid) |
| 143 | + if err != nil || !res.IsSuccess { |
| 144 | + Log(Error, "Failed to get channel group members \n%v \n%v", res, err) |
| 145 | + return res, err |
| 146 | + } |
| 147 | + |
| 148 | + var successful int = 0 |
| 149 | + var attempted int = 0 |
| 150 | + |
| 151 | + for _, user := range body { |
| 152 | + for i := 0; i < len(user.ActiveSessionIds); i++ { |
| 153 | + res, err := TSClient.PokeUser(int(user.ActiveSessionIds[i]), msg) |
| 154 | + if err != nil { |
| 155 | + Log(Error, "Failed to poke %v \n%v \n%v", user.Nickname, res, err) |
| 156 | + } |
| 157 | + |
| 158 | + // Increase counters |
| 159 | + attempted++ |
| 160 | + if res.IsSuccess { |
| 161 | + successful++ |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + return &QueryResponse{ |
| 167 | + Id: -1, |
| 168 | + Msg: fmt.Sprintf("%v out of %v clients sucesfully poked", successful, attempted), |
| 169 | + IsSuccess: true, |
| 170 | + }, nil |
| 171 | +} |
0 commit comments