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