fix possible crash on user deletion
[srvx.git] / src / mod-snoop.c
1 /* mod-snoop.c - User surveillance module (per pomac's spec)
2  * Copyright 2002-2004 srvx Development Team
3  *
4  * This file is part of srvx.
5  *
6  * srvx is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with srvx; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
19  */
20
21 /* Adds new section to srvx.conf:
22  * "modules" {
23  *     "snoop" {
24  *         // Where to send snoop messages?
25  *         "channel" "#wherever";
26  *         // Which bot?
27  *         "bot" "OpServ";
28  *         // Show new users and joins from net joins?  (off by default)
29  *         "show_bursts" "0";
30  *     };
31  * };
32  */
33
34 #include "conf.h"
35 #include "helpfile.h"
36 #include "nickserv.h"
37
38 #ifdef HAVE_NETINET_IN_H
39 #include <netinet/in.h>
40 #endif
41 #ifdef HAVE_ARPA_INET_H
42 #include <arpa/inet.h>
43 #endif
44
45 static struct {
46     struct chanNode *channel;
47     struct userNode *bot;
48     unsigned int show_bursts : 1;
49     unsigned int enabled : 1;
50 } snoop_cfg;
51 static char timestamp[16];
52 const char *snoop_module_deps[] = { NULL };
53
54 static int finalized;
55 int snoop_finalize(void);
56
57 #if defined(GCC_VARMACROS)
58 # define SNOOP(FORMAT, ARGS...) send_channel_message(snoop_cfg.channel, snoop_cfg.bot, "%s "FORMAT, timestamp, ARGS)
59 #elif defined(C99_VARMACROS)
60 # define SNOOP(FORMAT, ...) send_channel_message(snoop_cfg.channel, snoop_cfg.bot, "%s "FORMAT, timestamp, __VA_ARGS__)
61 #endif
62 #define UPDATE_TIMESTAMP() do { time_t feh = now; strftime(timestamp, sizeof(timestamp), "[%H:%M:%S]", localtime(&feh)); } while (0)
63
64 static void
65 snoop_nick_change(struct userNode *user, const char *old_nick) {
66     if (!snoop_cfg.enabled) return;
67     UPDATE_TIMESTAMP();
68     SNOOP("$bNICK$b change %s -> %s", old_nick, user->nick);
69 }
70
71 static int
72 snoop_join(struct modeNode *mNode) {
73     struct userNode *user = mNode->user;
74     struct chanNode *chan = mNode->channel;
75     if (!snoop_cfg.enabled) return 0;
76     if (user->uplink->burst && !snoop_cfg.show_bursts) return 0;
77     UPDATE_TIMESTAMP();
78     if (chan->members.used == 1) {
79         SNOOP("$bCREATE$b %s by %s", chan->name, user->nick);
80     } else {
81         SNOOP("$bJOIN$b %s by %s", chan->name, user->nick);
82     }
83     return 0;
84 }
85
86 static void
87 snoop_part(struct modeNode *mn, const char *reason) {
88     if (!snoop_cfg.enabled) return;
89     if (mn->user->dead) return;
90     UPDATE_TIMESTAMP();
91     SNOOP("$bPART$b %s by %s (%s)", mn->channel->name, mn->user->nick, reason ? reason : "");
92 }
93
94 static void
95 snoop_kick(struct userNode *kicker, struct userNode *victim, struct chanNode *chan) {
96     if (!snoop_cfg.enabled) return;
97     UPDATE_TIMESTAMP();
98     SNOOP("$bKICK$b %s from %s by %s", victim->nick, chan->name, (kicker ? kicker->nick : "some server"));
99 }
100
101 static void
102 snoop_new_user(struct userNode *user) {
103     if (!snoop_cfg.enabled) return;
104     if (user->uplink->burst && !snoop_cfg.show_bursts) return;
105     UPDATE_TIMESTAMP();
106     SNOOP("$bNICK$b %s %s@%s [%s] on %s", user->nick, user->ident, user->hostname, irc_ntoa(&user->ip), user->uplink->name);
107 }
108
109 static void
110 snoop_del_user(struct userNode *user, struct userNode *killer, const char *why) {
111     if (!snoop_cfg.enabled) return;
112     UPDATE_TIMESTAMP();
113     if (killer) {
114         SNOOP("$bKILL$b %s (%s@%s, on %s) by %s (%s)", user->nick, user->ident, user->hostname, user->uplink->name, killer->nick, why);
115     } else {
116         SNOOP("$bQUIT$b %s (%s@%s, on %s) (%s)", user->nick, user->ident, user->hostname, user->uplink->name, why);
117     }
118 }
119
120 static void
121 snoop_auth(struct userNode *user, UNUSED_ARG(struct handle_info *old_handle)) {
122     if (!snoop_cfg.enabled) return;
123     if (user->uplink->burst && !snoop_cfg.show_bursts) return;
124     if (user->handle_info) {
125         UPDATE_TIMESTAMP();
126         SNOOP("$bAUTH$b %s as %s", user->nick, user->handle_info->handle);
127     }
128 }
129
130 static void
131 snoop_conf_read(void) {
132     dict_t node;
133     char *str;
134
135     node = conf_get_data("modules/snoop", RECDB_OBJECT);
136     if (!node)
137         return;
138     str = database_get_data(node, "channel", RECDB_QSTRING);
139     if (!str)
140         return;
141     snoop_cfg.channel = AddChannel(str, now, "+sntim", NULL);
142     if (!snoop_cfg.channel)
143         return;
144     str = database_get_data(node, "show_bursts", RECDB_QSTRING);
145     snoop_cfg.show_bursts = str ? enabled_string(str) : 0;
146     snoop_cfg.enabled = 1;
147     if (finalized)
148         snoop_finalize();
149 }
150
151 void
152 snoop_cleanup(void) {
153     snoop_cfg.enabled = 0;
154     unreg_del_user_func(snoop_del_user);
155 }
156
157 int
158 snoop_init(void) {
159     reg_exit_func(snoop_cleanup);
160     conf_register_reload(snoop_conf_read);
161     reg_nick_change_func(snoop_nick_change);
162     reg_join_func(snoop_join);
163     reg_part_func(snoop_part);
164     reg_kick_func(snoop_kick);
165     reg_new_user_func(snoop_new_user);
166     reg_del_user_func(snoop_del_user);
167     reg_auth_func(snoop_auth);
168     /* Not implemented since hooks don't exist or lack data desired:
169      * chanmode (issuing user not listed)
170      * usermode (no hook)
171      */
172     return 1;
173 }
174
175 int
176 snoop_finalize(void) {
177     struct mod_chanmode change;
178     dict_t node;
179     char *str;
180
181     finalized = 1;
182     node = conf_get_data("modules/snoop", RECDB_OBJECT);
183     if (!node)
184         return 0;
185     str = database_get_data(node, "bot", RECDB_QSTRING);
186     if (!str)
187         return 0;
188     snoop_cfg.bot = GetUserH(str);
189     if (!snoop_cfg.bot)
190         return 0;
191     mod_chanmode_init(&change);
192     change.argc = 1;
193     change.args[0].mode = MODE_CHANOP;
194     change.args[0].u.member = AddChannelUser(snoop_cfg.bot, snoop_cfg.channel);
195     mod_chanmode_announce(snoop_cfg.bot, snoop_cfg.channel, &change);
196     return 1;
197 }