]> jfr.im git - solanum.git/blame - src/monitor.c
Remove s_assert definition from ircd_defs.h and add it to its own header.
[solanum.git] / src / monitor.c
CommitLineData
212380e3
AC
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 *
8aba962d 32 * $Id: monitor.c 3520 2007-06-30 22:15:35Z jilles $
212380e3
AC
33 */
34#include "stdinc.h"
212380e3 35#include "client.h"
212380e3
AC
36#include "monitor.h"
37#include "hash.h"
212380e3 38#include "numeric.h"
77d3d2db 39#include "send.h"
212380e3 40
95b3551e 41struct monitor *monitorTable[MONITOR_HASH_SIZE];
423f18a0 42static rb_bh *monitor_heap;
212380e3
AC
43
44void
45init_monitor(void)
46{
423f18a0 47 monitor_heap = rb_bh_create(sizeof(struct monitor), MONITOR_HEAP_SIZE, "monitor_heap");
212380e3
AC
48}
49
50static inline unsigned int
51hash_monitor_nick(const char *name)
52{
95b3551e 53 return fnv_hash_upper((const unsigned char *)name, MONITOR_HASH_BITS);
212380e3
AC
54}
55
56struct monitor *
57find_monitor(const char *name, int add)
58{
95b3551e
AC
59 struct monitor *monptr;
60
61 unsigned int hashv = hash_monitor_nick(name);
62
63 for(monptr = monitorTable[hashv]; monptr; monptr = monptr->hnext)
64 {
65 if(!irccmp(monptr->name, name))
66 return monptr;
67 }
68
69 if(add)
70 {
71 monptr = rb_bh_alloc(monitor_heap);
72 rb_strlcpy(monptr->name, name, sizeof(monptr->name));
73
74 monptr->hnext = monitorTable[hashv];
75 monitorTable[hashv] = monptr;
76
77 return monptr;
78 }
79
212380e3
AC
80 return NULL;
81}
82
95b3551e
AC
83void
84free_monitor(struct monitor *monptr)
85{
86 rb_bh_free(monitor_heap, monptr);
423f18a0
VY
87}
88
212380e3
AC
89/* monitor_signon()
90 *
91 * inputs - client who has just connected
92 * outputs -
93 * side effects - notifies any clients monitoring this nickname that it has
94 * connected to the network
95 */
96void
97monitor_signon(struct Client *client_p)
98{
95b3551e
AC
99 char buf[USERHOST_REPLYLEN];
100 struct monitor *monptr = find_monitor(client_p->name, 0);
101
102 /* noones watching this nick */
103 if(monptr == NULL)
104 return;
105
106 rb_snprintf(buf, sizeof(buf), "%s!%s@%s", client_p->name, client_p->username, client_p->host);
107
8aba962d 108 sendto_monitor(monptr, form_str(RPL_MONONLINE), me.name, "*", buf);
212380e3
AC
109}
110
111/* monitor_signoff()
112 *
113 * inputs - client who is exiting
114 * outputs -
115 * side effects - notifies any clients monitoring this nickname that it has
116 * left the network
117 */
118void
119monitor_signoff(struct Client *client_p)
120{
95b3551e
AC
121 struct monitor *monptr = find_monitor(client_p->name, 0);
122
123 /* noones watching this nick */
124 if(monptr == NULL)
125 return;
126
127 sendto_monitor(monptr, form_str(RPL_MONOFFLINE), me.name, "*",
8aba962d 128 client_p->name);
212380e3
AC
129}
130
131void
132clear_monitor(struct Client *client_p)
133{
95b3551e
AC
134 struct monitor *monptr;
135 rb_dlink_node *ptr, *next_ptr;
136
137 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->monitor_list.head)
138 {
139 monptr = ptr->data;
140
141 rb_dlinkFindDestroy(client_p, &monptr->users);
142 rb_free_rb_dlink_node(ptr);
143 }
144
145 client_p->localClient->monitor_list.head = client_p->localClient->monitor_list.tail = NULL;
212380e3
AC
146 client_p->localClient->monitor_list.length = 0;
147}