]> jfr.im git - solanum.git/blame - ircd/monitor.c
Merge pull request #140 from viatsko/remove-snprintf
[solanum.git] / ircd / monitor.c
CommitLineData
5f8fb56d
AW
1/*
2 * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
3 * monitor.c - Code for server-side notify lists
4 *
5 * Copyright (C) 2005 Lee Hardy <lee -at- leeh.co.uk>
6 * Copyright (C) 2005 ircd-ratbox development team
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * 1.Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * 2.Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3.The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * $Id: monitor.c 3520 2007-06-30 22:15:35Z jilles $
33 */
34#include "stdinc.h"
35#include "client.h"
36#include "monitor.h"
37#include "hash.h"
38#include "numeric.h"
39#include "send.h"
3de22662 40#include "irc_radixtree.h"
5f8fb56d 41
3de22662 42static struct irc_radixtree *monitor_tree;
5f8fb56d
AW
43
44void
45init_monitor(void)
46{
3de22662 47 monitor_tree = irc_radixtree_create("monitor lists", irc_radixtree_irccasecanon);
5f8fb56d
AW
48}
49
50struct monitor *
51find_monitor(const char *name, int add)
52{
53 struct monitor *monptr;
5f8fb56d 54
3de22662
AC
55 monptr = irc_radixtree_retrieve(monitor_tree, name);
56 if (monptr != NULL)
57 return monptr;
5f8fb56d
AW
58
59 if(add)
60 {
3de22662 61 monptr = rb_malloc(sizeof(*monptr));
5f8fb56d 62 rb_strlcpy(monptr->name, name, sizeof(monptr->name));
3de22662 63 irc_radixtree_add(monitor_tree, monptr->name, monptr);
5f8fb56d 64
5f8fb56d
AW
65 return monptr;
66 }
67
68 return NULL;
69}
70
71void
72free_monitor(struct monitor *monptr)
73{
74 if (rb_dlink_list_length(&monptr->users) > 0)
75 return;
76
3de22662
AC
77 irc_radixtree_delete(monitor_tree, monptr->name);
78 rb_free(monptr);
5f8fb56d
AW
79}
80
81/* monitor_signon()
82 *
83 * inputs - client who has just connected
84 * outputs -
85 * side effects - notifies any clients monitoring this nickname that it has
86 * connected to the network
87 */
88void
89monitor_signon(struct Client *client_p)
90{
91 char buf[USERHOST_REPLYLEN];
92 struct monitor *monptr = find_monitor(client_p->name, 0);
93
94 /* noones watching this nick */
95 if(monptr == NULL)
96 return;
97
5203cba5 98 snprintf(buf, sizeof(buf), "%s!%s@%s", client_p->name, client_p->username, client_p->host);
5f8fb56d
AW
99
100 sendto_monitor(monptr, form_str(RPL_MONONLINE), me.name, "*", buf);
101}
102
103/* monitor_signoff()
104 *
105 * inputs - client who is exiting
106 * outputs -
107 * side effects - notifies any clients monitoring this nickname that it has
108 * left the network
109 */
110void
111monitor_signoff(struct Client *client_p)
112{
113 struct monitor *monptr = find_monitor(client_p->name, 0);
114
115 /* noones watching this nick */
116 if(monptr == NULL)
117 return;
118
119 sendto_monitor(monptr, form_str(RPL_MONOFFLINE), me.name, "*",
120 client_p->name);
121}
122
123void
124clear_monitor(struct Client *client_p)
125{
126 struct monitor *monptr;
127 rb_dlink_node *ptr, *next_ptr;
128
129 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->monitor_list.head)
130 {
131 monptr = ptr->data;
132
133 rb_dlinkFindDestroy(client_p, &monptr->users);
134 rb_free_rb_dlink_node(ptr);
135
7a40c9a5 136 free_monitor(monptr);
5f8fb56d
AW
137 }
138
139 client_p->localClient->monitor_list.head = client_p->localClient->monitor_list.tail = NULL;
140 client_p->localClient->monitor_list.length = 0;
141}