11a0e55050cc5bb12ae3f5cdf6e6cac97aae30f5
[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 extern time_t now;
46 static struct {
47     struct chanNode *channel;
48     struct userNode *bot;
49     unsigned int show_bursts : 1;
50     unsigned int enabled : 1;
51 } snoop_cfg;
52 static char timestamp[16];
53 const char *snoop_module_deps[] = { NULL };
54
55 static int finalized;
56 int snoop_finalize(void);
57
58 #define SNOOP(FORMAT, ARGS...) send_channel_message(snoop_cfg.channel, snoop_cfg.bot, "%s "FORMAT, timestamp , ## ARGS)
59 #define UPDATE_TIMESTAMP() strftime(timestamp, sizeof(timestamp), "[%H:%M:%S]", localtime(&now))
60
61 static void
62 snoop_nick_change(struct userNode *user, const char *old_nick) {
63     if (!snoop_cfg.enabled) return;
64     UPDATE_TIMESTAMP();
65     SNOOP("$bNICK$b change %s -> %s", old_nick, user->nick);
66 }
67
68 static int
69 snoop_join(struct modeNode *mNode) {
70     struct userNode *user = mNode->user;
71     struct chanNode *chan = mNode->channel;
72     if (!snoop_cfg.enabled) return 0;
73     if (user->uplink->burst && !snoop_cfg.show_bursts) return 0;
74     UPDATE_TIMESTAMP();
75     if (chan->members.used == 1) {
76         SNOOP("$bCREATE$b %s by %s", chan->name, user->nick);
77     } else {
78         SNOOP("$bJOIN$b %s by %s", chan->name, user->nick);
79     }
80     return 0;
81 }
82
83 static void
84 snoop_part(struct modeNode *mn, const char *reason) {
85     if (!snoop_cfg.enabled) return;
86     if (mn->user->dead) return;
87     UPDATE_TIMESTAMP();
88     SNOOP("$bPART$b %s by %s (%s)", mn->channel->name, mn->user->nick, reason ? reason : "");
89 }
90
91 static void
92 snoop_kick(struct userNode *kicker, struct userNode *victim, struct chanNode *chan) {
93     if (!snoop_cfg.enabled) return;
94     UPDATE_TIMESTAMP();
95     SNOOP("$bKICK$b %s from %s by %s", victim->nick, chan->name, (kicker ? kicker->nick : "some server"));
96 }
97
98 static int
99 snoop_new_user(struct userNode *user) {
100     if (!snoop_cfg.enabled) return 0;
101     if (user->uplink->burst && !snoop_cfg.show_bursts) return 0;
102     UPDATE_TIMESTAMP();
103     SNOOP("$bNICK$b %s %s@%s [%s] on %s", user->nick, user->ident, user->hostname, irc_ntoa(&user->ip), user->uplink->name);
104     return 0;
105 }
106
107 static void
108 snoop_del_user(struct userNode *user, struct userNode *killer, const char *why) {
109     if (!snoop_cfg.enabled) return;
110     UPDATE_TIMESTAMP();
111     if (killer) {
112         SNOOP("$bKILL$b %s (%s@%s, on %s) by %s (%s)", user->nick, user->ident, user->hostname, user->uplink->name, killer->nick, why);
113     } else {
114         SNOOP("$bQUIT$b %s (%s@%s, on %s) (%s)", user->nick, user->ident, user->hostname, user->uplink->name, why);
115     }
116 }
117
118 static void
119 snoop_auth(struct userNode *user, UNUSED_ARG(struct handle_info *old_handle)) {
120     if (!snoop_cfg.enabled) return;
121     if (user->uplink->burst && !snoop_cfg.show_bursts) return;
122     if (user->handle_info) {
123         UPDATE_TIMESTAMP();
124         SNOOP("$bAUTH$b %s as %s", user->nick, user->handle_info->handle);
125     }
126 }
127
128 static void
129 snoop_conf_read(void) {
130     dict_t node;
131     char *str;
132
133     node = conf_get_data("modules/snoop", RECDB_OBJECT);
134     if (!node)
135         return;
136     str = database_get_data(node, "channel", RECDB_QSTRING);
137     if (!str)
138         return;
139     snoop_cfg.channel = AddChannel(str, now, "+sntim", NULL);
140     if (!snoop_cfg.channel)
141         return;
142     str = database_get_data(node, "show_bursts", RECDB_QSTRING);
143     snoop_cfg.show_bursts = str ? enabled_string(str) : 0;
144     snoop_cfg.enabled = 1;
145     if (finalized)
146         snoop_finalize();
147 }
148
149 void
150 snoop_cleanup(void) {
151     snoop_cfg.enabled = 0;
152     unreg_del_user_func(snoop_del_user);
153 }
154
155 int
156 snoop_init(void) {
157     reg_exit_func(snoop_cleanup);
158     conf_register_reload(snoop_conf_read);
159     reg_nick_change_func(snoop_nick_change);
160     reg_join_func(snoop_join);
161     reg_part_func(snoop_part);
162     reg_kick_func(snoop_kick);
163     reg_new_user_func(snoop_new_user);
164     reg_del_user_func(snoop_del_user);
165     reg_auth_func(snoop_auth);
166     /* Not implemented since hooks don't exist or lack data desired:
167      * chanmode (issuing user not listed)
168      * usermode (no hook)
169      */
170     return 1;
171 }
172
173 int
174 snoop_finalize(void) {
175     struct mod_chanmode change;
176     dict_t node;
177     char *str;
178
179     finalized = 1;
180     node = conf_get_data("modules/snoop", RECDB_OBJECT);
181     if (!node)
182         return 0;
183     str = database_get_data(node, "bot", RECDB_QSTRING);
184     if (!str)
185         return 0;
186     snoop_cfg.bot = GetUserH(str);
187     if (!snoop_cfg.bot)
188         return 0;
189     mod_chanmode_init(&change);
190     change.argc = 1;
191     change.args[0].mode = MODE_CHANOP;
192     change.args[0].u.member = AddChannelUser(snoop_cfg.bot, snoop_cfg.channel);
193     mod_chanmode_announce(snoop_cfg.bot, snoop_cfg.channel, &change);
194     return 1;
195 }