Removed null-pointer dereference in low mem.
[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 #include "memory.h"
37
38 void Voicetarget_add_session(client_t *client, int targetId, int sessionId)
39 {
40         struct dlist *itr;
41         voicetarget_t *vt;
42
43         list_iterate(itr, &client->voicetargets) {
44                 if (targetId == list_get_entry(itr, voicetarget_t, node)->id) {
45                         int i;
46                         vt = list_get_entry(itr, voicetarget_t, node);
47                         for (i = 0; i < TARGET_MAX_SESSIONS; i++) {
48                                 if (vt->sessions[i] == -1) {
49                                         vt->sessions[i] = sessionId;
50                                         Log_debug("Adding session ID %d to voicetarget ID %d", sessionId, targetId);
51                                         return;
52                                 }
53                         }
54                 }
55         }
56 }
57
58 void Voicetarget_add_channel(client_t *client, int targetId, int channelId,
59                                                          bool_t linked, bool_t children)
60 {
61         struct dlist *itr;
62         voicetarget_t *vt;
63
64         list_iterate(itr, &client->voicetargets) {
65                 if (targetId == list_get_entry(itr, voicetarget_t, node)->id) {
66                         int i;
67                         vt = list_get_entry(itr, voicetarget_t, node);
68                         for (i = 0; i < TARGET_MAX_CHANNELS; i++) {
69                                 if (vt->channels[i].channel == -1) {
70                                         vt->channels[i].channel = channelId;
71                                         vt->channels[i].linked = linked;
72                                         vt->channels[i].children = children;
73                                         Log_debug("Adding channel ID %d to voicetarget ID %d", channelId, targetId);
74                                         return;
75                                 }
76                         }
77                 }
78         }
79 }
80
81 void Voicetarget_add_id(client_t *client, int targetId)
82 {
83         voicetarget_t *newtarget;
84         int i;
85
86         Voicetarget_del_id(client, targetId);
87         newtarget = Memory_safeCalloc(1, sizeof(voicetarget_t));
88         for (i = 0; i < TARGET_MAX_CHANNELS; i++)
89                 newtarget->channels[i].channel = -1;
90         for (i = 0; i < TARGET_MAX_SESSIONS; i++)
91                 newtarget->sessions[i] = -1;
92         newtarget->id = targetId;
93         list_add_tail(&newtarget->node, &client->voicetargets);
94 }
95
96 void Voicetarget_del_id(client_t *client, int targetId)
97 {
98         struct dlist *itr;
99         list_iterate(itr, &client->voicetargets) {
100                 if (targetId == list_get_entry(itr, voicetarget_t, node)->id) {
101                         list_del(&list_get_entry(itr, voicetarget_t, node)->node);
102                         free(list_get_entry(itr, voicetarget_t, node));
103                         Log_debug("Removing voicetarget ID %d", targetId);
104                 }
105         }
106 }
107
108 voicetarget_t *Voicetarget_get_id(client_t *client, int targetId)
109 {
110         struct dlist *itr;
111         list_iterate(itr, &client->voicetargets) {
112                 if (targetId == list_get_entry(itr, voicetarget_t, node)->id) {
113                         return list_get_entry(itr, voicetarget_t, node);
114                 }
115         }
116         return NULL;
117 }
118
119
120 void Voicetarget_free_all(client_t *client)
121 {
122         struct dlist *itr, *save;
123
124         list_iterate_safe(itr, save, &client->voicetargets) {
125                 list_del(&list_get_entry(itr, voicetarget_t, node)->node);
126                 free(list_get_entry(itr, voicetarget_t, node));
127         }
128 }