Initial import (again)
[srvx.git] / src / hash.h
1 /* hash.h - IRC network state database
2  * Copyright 2000-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 #ifndef HASH_H
20 #define HASH_H
21
22 #include "common.h"
23 #include "dict.h"
24 #include "policer.h"
25
26 #define MODE_CHANOP             0x0001 /* +o USER */
27 #define MODE_VOICE              0x0002 /* +v USER */
28 #define MODE_PRIVATE            0x0004 /* +p */
29 #define MODE_SECRET             0x0008 /* +s */
30 #define MODE_MODERATED          0x0010 /* +m */
31 #define MODE_TOPICLIMIT         0x0020 /* +t */
32 #define MODE_INVITEONLY         0x0040 /* +i */
33 #define MODE_NOPRIVMSGS         0x0080 /* +n */
34 #define MODE_KEY                0x0100 /* +k KEY */
35 #define MODE_BAN                0x0200 /* +b BAN */
36 #define MODE_LIMIT              0x0400 /* +l LIMIT */
37 #define MODE_DELAYJOINS         0x0800 /* +D */
38 #define MODE_REGONLY            0x1000 /* +r */
39 #define MODE_NOCOLORS           0x2000 /* +c */
40 #define MODE_NOCTCPS            0x4000 /* +C */
41 #define MODE_REMOVE             0x80000000
42
43 #define FLAGS_OPER              0x0001 /* Operator +O */
44 #define FLAGS_LOCOP             0x0002 /* Local operator +o */
45 #define FLAGS_INVISIBLE         0x0004 /* invisible +i */
46 #define FLAGS_WALLOP            0x0008 /* receives wallops +w */
47 #define FLAGS_SERVNOTICE        0x0010 /* receives server notices +s */
48 #define FLAGS_DEAF              0x0020 /* deaf +d */
49 #define FLAGS_SERVICE           0x0040 /* cannot be kicked, killed or deoped +k */
50 #define FLAGS_GLOBAL            0x0080 /* receives global messages +g */
51 #define FLAGS_HELPER            0x0100 /* (network?) helper +h */
52 #define FLAGS_PERSISTENT        0x0200 /* for reserved nicks, this isn't just one-shot */
53 #define FLAGS_GAGGED            0x0400 /* for gagged users */
54 #define FLAGS_AWAY              0x0800 /* for away users */
55 #define FLAGS_STAMPED           0x1000 /* for users who have been stamped */
56 #define FLAGS_HIDDEN_HOST       0x2000 /* user's host is masked by their account */
57 #define FLAGS_REGNICK           0x4000 /* user owns their current nick */
58
59 #define IsOper(x)               ((x)->modes & FLAGS_OPER)
60 #define IsService(x)            ((x)->modes & FLAGS_SERVICE)
61 #define IsDeaf(x)               ((x)->modes & FLAGS_DEAF)
62 #define IsInvisible(x)          ((x)->modes & FLAGS_INVISIBLE)
63 #define IsGlobal(x)             ((x)->modes & FLAGS_GLOBAL)
64 #define IsWallOp(x)             ((x)->modes & FLAGS_WALLOP)
65 #define IsServNotice(x)         ((x)->modes & FLAGS_SERVNOTICE)
66 #define IsHelperIrcu(x)         ((x)->modes & FLAGS_HELPER)
67 #define IsGagged(x)             ((x)->modes & FLAGS_GAGGED)
68 #define IsPersistent(x)         ((x)->modes & FLAGS_PERSISTENT) 
69 #define IsAway(x)               ((x)->modes & FLAGS_AWAY)
70 #define IsStamped(x)            ((x)->modes & FLAGS_STAMPED)
71 #define IsHiddenHost(x)         ((x)->modes & FLAGS_HIDDEN_HOST)
72 #define IsReggedNick(x)         ((x)->modes & FLAGS_REGNICK)
73 #define IsLocal(x)              ((x)->uplink == self)
74
75 #define NICKLEN         30
76 #define USERLEN         10
77 #define HOSTLEN         63
78 #define REALLEN         50
79 #define TOPICLEN        250
80 #define CHANNELLEN      200
81
82 #define MAXMODEPARAMS   6
83 #define MAXBANS         45
84
85 /* IDLEN is 6 because it takes 5.33 Base64 digits to store 32 bytes. */
86 #define IDLEN           6
87
88 DECLARE_LIST(userList, struct userNode*);
89 DECLARE_LIST(modeList, struct modeNode*);
90 DECLARE_LIST(banList, struct banNode*);
91 DECLARE_LIST(channelList, struct chanNode*);
92 DECLARE_LIST(serverList, struct server*);
93
94 struct userNode {
95     char *nick;                   /* Unique name of the client, nick or host */
96     char ident[USERLEN + 1];      /* Per-host identification for user */
97     char info[REALLEN + 1];       /* Free form additional client information */
98     char hostname[HOSTLEN + 1];   /* DNS name or IP address */
99 #ifdef WITH_PROTOCOL_P10
100     char numeric[COMBO_NUMERIC_LEN+1];
101     unsigned int num_local : 18;
102 #endif
103     unsigned int dead : 1;        /* Is user waiting to be recycled? */
104     struct in_addr ip;            /* User's IP address */
105     long modes;                   /* user flags +isw etc... */
106
107     time_t timestamp;             /* Time of last nick change */
108     struct server *uplink;        /* Server that user is connected to */
109     struct modeList channels;     /* Vector of channels user is in */
110
111     /* from nickserv */
112     struct handle_info *handle_info;
113     struct userNode *next_authed;
114     struct policer auth_policer;
115 };
116
117 struct chanNode {
118     chan_mode_t modes;
119     unsigned int limit, locks;
120     char key[KEYLEN + 1];
121     time_t timestamp; /* creation time */
122   
123     char topic[TOPICLEN + 1];
124     char topic_nick[NICKLEN + 1];
125     time_t topic_time;
126
127     struct modeList members;
128     struct banList banlist;
129     struct policer join_policer;
130     unsigned int join_flooded : 1;
131     unsigned int bad_channel : 1;
132
133     struct chanData *channel_info;
134     struct channel_help *channel_help;
135     char name[1];
136 };
137
138 struct banNode {
139     char ban[NICKLEN + USERLEN + HOSTLEN + 3]; /* 1 for '\0', 1 for ! and 1 for @ = 3 */
140     char who[NICKLEN + 1]; /* who set ban */
141     time_t set; /* time ban was set */
142 };
143
144 struct modeNode {
145     struct chanNode *channel;
146     struct userNode *user;
147     long modes;
148     time_t idle_since;
149 };
150
151 #define SERVERNAMEMAX 64
152 #define SERVERDESCRIPTMAX 128
153
154 struct server {
155     char name[SERVERNAMEMAX+1];
156     time_t boot;
157     time_t link;
158     char description[SERVERDESCRIPTMAX+1];
159 #ifdef WITH_PROTOCOL_P10
160     char numeric[COMBO_NUMERIC_LEN+1];
161     unsigned int num_mask;
162 #endif
163     unsigned int hops, clients, max_clients;
164     unsigned int burst : 1, self_burst : 1;
165     struct server *uplink;
166 #ifdef WITH_PROTOCOL_P10
167     struct userNode **users; /* flat indexed by numeric */
168 #else
169     dict_t users; /* indexed by nick */
170 #endif
171     struct serverList children;
172 };
173
174 extern struct server *self;
175 extern dict_t channels;
176 extern dict_t clients;
177 extern dict_t servers;
178 extern unsigned int max_clients, invis_clients;
179 extern time_t max_clients_time;
180 extern struct userList curr_opers, curr_helpers;
181
182 struct server* GetServerH(const char *name); /* using full name */
183 struct userNode* GetUserH(const char *nick);   /* using nick */
184 struct chanNode* GetChannel(const char *name);
185 struct modeNode* GetUserMode(struct chanNode* channel, struct userNode* user);
186
187 typedef void (*server_link_func_t) (struct server *server);
188 void reg_server_link_func(server_link_func_t handler);
189
190 typedef int (*new_user_func_t) (struct userNode *user);
191 void reg_new_user_func(new_user_func_t handler);
192 typedef void (*del_user_func_t) (struct userNode *user, struct userNode *killer, const char *why);
193 void reg_del_user_func(del_user_func_t handler);
194 void unreg_del_user_func(del_user_func_t handler);
195 void ReintroduceUser(struct userNode* user);
196 typedef void (*nick_change_func_t)(struct userNode *user, const char *old_nick);
197 void reg_nick_change_func(nick_change_func_t handler);
198 void NickChange(struct userNode* user, const char *new_nick, int no_announce);
199
200 typedef void (*account_func_t) (struct userNode *user, const char *stamp);
201 void reg_account_func(account_func_t handler);
202 void call_account_func(struct userNode *user, const char *stamp);
203 void StampUser(struct userNode *user, const char *stamp);
204
205 typedef void (*new_channel_func_t) (struct chanNode *chan);
206 void reg_new_channel_func(new_channel_func_t handler);
207 typedef int (*join_func_t) (struct modeNode *mNode);
208 void reg_join_func(join_func_t handler);
209 typedef void (*del_channel_func_t) (struct chanNode *chan);
210 void reg_del_channel_func(del_channel_func_t handler);
211
212 struct chanNode* AddChannel(const char *name, time_t time_, const char *modes, char *banlist);
213 void LockChannel(struct chanNode *channel);
214 void UnlockChannel(struct chanNode *channel);
215
216 struct modeNode* AddChannelUser(struct userNode* user, struct chanNode* channel);
217
218 typedef void (*part_func_t) (struct userNode *user, struct chanNode *chan, const char *reason);
219 void reg_part_func(part_func_t handler);
220 void unreg_part_func(part_func_t handler);
221 void DelChannelUser(struct userNode* user, struct chanNode* channel, const char *reason, int deleting);
222 void KickChannelUser(struct userNode* target, struct chanNode* channel, struct userNode *kicker, const char *why);
223
224 typedef void (*kick_func_t) (struct userNode *kicker, struct userNode *user, struct chanNode *chan);
225 void reg_kick_func(kick_func_t handler);
226 void ChannelUserKicked(struct userNode* kicker, struct userNode* victim, struct chanNode* channel);
227
228 int ChannelBanExists(struct chanNode *channel, const char *ban);
229
230 typedef int (*topic_func_t)(struct userNode *who, struct chanNode *chan, const char *old_topic);
231 void reg_topic_func(topic_func_t handler);
232 void SetChannelTopic(struct chanNode *channel, struct userNode *user, const char *topic, int announce);
233
234 void init_structs(void);
235
236 #endif