e2ed569c8f2db196c6d129404503a93355848e7d
[umurmur.git] / src / voicetarget.c
1 /* Copyright (C) 2009-2014, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2014, Thorvald Natvig <thorvald@natvig.com>
3
4    All rights reserved.
5
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9
10    - Redistributions of source code must retain the above copyright notice,
11      this list of conditions and the following disclaimer.
12    - Redistributions in binary form must reproduce the above copyright notice,
13      this list of conditions and the following disclaimer in the documentation
14      and/or other materials provided with the distribution.
15    - Neither the name of the Developers nor the names of its contributors may
16      be used to endorse or promote products derived from this software without
17      specific prior written permission.
18
19    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include "voicetarget.h"
35 #include "log.h"
36
37 void Voicetarget_add_session(client_t *client, int targetId, int sessionId)
38 {
39         struct dlist *itr;
40         voicetarget_t *vt;
41
42         list_iterate(itr, &client->voicetargets) {
43                 if (targetId == list_get_entry(itr, voicetarget_t, node)->id) {
44                         int i;
45                         vt = list_get_entry(itr, voicetarget_t, node);
46                         for (i = 0; i < TARGET_MAX_SESSIONS; i++) {
47                                 if (vt->sessions[i] == -1) {
48                                         vt->sessions[i] = sessionId;
49                                         Log_debug("Adding session ID %d to voicetarget ID %d", sessionId, targetId);
50                                         return;
51                                 }
52                         }
53                 }
54         }
55 }
56
57 void Voicetarget_add_channel(client_t *client, int targetId, int channelId,
58                                                          bool_t linked, bool_t children)
59 {
60         struct dlist *itr;
61         voicetarget_t *vt;
62
63         list_iterate(itr, &client->voicetargets) {
64                 if (targetId == list_get_entry(itr, voicetarget_t, node)->id) {
65                         int i;
66                         vt = list_get_entry(itr, voicetarget_t, node);
67                         for (i = 0; i < TARGET_MAX_CHANNELS; i++) {
68                                 if (vt->channels[i].channel == -1) {
69                                         vt->channels[i].channel = channelId;
70                                         vt->channels[i].linked = linked;
71                                         vt->channels[i].children = children;
72                                         Log_debug("Adding channel ID %d to voicetarget ID %d", channelId, targetId);
73                                         return;
74                                 }
75                         }
76                 }
77         }
78 }
79
80 void Voicetarget_add_id(client_t *client, int targetId)
81 {
82         voicetarget_t *newtarget;
83         int i;
84
85         Voicetarget_del_id(client, targetId);
86         newtarget = malloc(sizeof(voicetarget_t));
87         if (!newtarget)
88                 Log_fatal("Out of memory");
89         memset(newtarget, 0, sizeof(voicetarget_t));
90         for (i = 0; i < TARGET_MAX_CHANNELS; i++)
91                 newtarget->channels[i].channel = -1;
92         for (i = 0; i < TARGET_MAX_SESSIONS; i++)
93                 newtarget->sessions[i] = -1;
94         newtarget->id = targetId;
95         list_add_tail(&newtarget->node, &client->voicetargets);
96 }
97
98 void Voicetarget_del_id(client_t *client, int targetId)
99 {
100         struct dlist *itr;
101         list_iterate(itr, &client->voicetargets) {
102                 if (targetId == list_get_entry(itr, voicetarget_t, node)->id) {
103                         list_del(&list_get_entry(itr, voicetarget_t, node)->node);
104                         free(list_get_entry(itr, voicetarget_t, node));
105                         Log_debug("Removing voicetarget ID %d", targetId);
106                 }
107         }
108 }
109
110 voicetarget_t *Voicetarget_get_id(client_t *client, int targetId)
111 {
112         struct dlist *itr;
113         list_iterate(itr, &client->voicetargets) {
114                 if (targetId == list_get_entry(itr, voicetarget_t, node)->id) {
115                         return list_get_entry(itr, voicetarget_t, node);
116                 }
117         }
118         return NULL;
119 }
120
121
122 void Voicetarget_free_all(client_t *client)
123 {
124         struct dlist *itr, *save;
125
126         list_iterate_safe(itr, save, &client->voicetargets) {
127                 list_del(&list_get_entry(itr, voicetarget_t, node)->node);
128                 free(list_get_entry(itr, voicetarget_t, node));
129         }
130 }