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