1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
21 #include "inspircd_config.h"
24 #include "inspstring.h"
25 #include "connection.h"
28 #include "cull_list.h"
42 /** RFC1459 channel modes
45 UM_SERVERNOTICE = 's'-65,
47 UM_INVISIBLE = 'i'-65,
51 enum RegistrationState {
52 REG_NONE = 0, /* Has sent nothing */
53 REG_USER = 1, /* Has sent USER */
54 REG_NICK = 2, /* Has sent NICK */
55 REG_NICKUSER = 3, /* Bitwise combination of REG_NICK and REG_USER */
56 REG_ALL = 7 /* REG_NICKUSER plus next bit along */
59 /** Holds a channel name to which a user has been invited.
61 class Invited : public classbase
69 /** Derived from Resolver, and performs user forward/reverse lookups.
71 class UserResolver : public Resolver
74 /** User this class is 'attached' to.
80 UserResolver(userrec* user, std::string to_resolve, bool forward);
82 void OnLookupComplete(const std::string &result);
83 void OnError(ResolverError e, const std::string &errormessage);
87 /** Holds information relevent to <connect allow> and <connect deny> tags in the config file.
89 class ConnectClass : public classbase
92 /** Type of line, either CC_ALLOW or CC_DENY
95 /** Max time to register the connection in seconds
97 int registration_timeout;
98 /** Number of lines in buffer before excess flood is triggered
101 /** Host mask for this line
104 /** Number of seconds between pings for this line
107 /** (Optional) Password for this line
111 /** Threshold value for flood disconnect
115 /** Maximum size of sendq for users in this class (bytes)
119 /** Maximum size of recvq for users in this class (bytes)
123 /** Local max when connecting by this connection class
127 /** Global max when connecting by this connection class
131 ConnectClass() : registration_timeout(0), flood(0), host(""), pingtime(0), pass(""), threshold(0), sendqmax(0), recvqmax(0)
136 /** Holds a complete list of all channels to which a user has been invited and has not yet joined.
138 typedef std::vector<Invited> InvitedList;
142 /** Holds a complete list of all allow and deny tags from the configuration file (connection classes)
144 typedef std::vector<ConnectClass> ClassVector;
146 /** Typedef for the list of user-channel records for a user
148 typedef std::vector<ucrec*> UserChanList;
150 /** Holds all information about a user
151 * This class stores all information about a user connected to the irc server. Everything about a
152 * connection is stored here primarily, from the user's socket ID (file descriptor) through to the
153 * user's nickname and hostname. Use the Find method of the server class to locate a specific user
156 class userrec : public connection
160 /** A list of channels the user has a pending invite to.
164 /** Resolvers for looking up this users hostname
166 UserResolver* res_forward;
167 UserResolver* res_reverse;
168 std::string stored_host;
170 void StartDNSLookup();
172 /** The users nickname.
173 * An invalid nickname indicates an unregistered connection prior to the NICK command.
178 /** The users ident reply.
179 * Two characters are added to the user-defined limit to compensate for the tilde etc.
181 char ident[IDENTMAX+2];
183 /** The host displayed to non-opers (used for cloaking etc).
184 * This usually matches the value of userrec::host.
188 /** The users full name.
190 char fullname[MAXGECOS+1];
192 /** The user's mode list.
193 * This is NOT a null terminated string! In the 1.1 version of InspIRCd
194 * this is an array of values in a similar way to channel modes.
195 * A value of 1 in field (modeletter-65) indicates that the mode is
196 * set, for example, to work out if mode +s is set, we check the field
197 * userrec::modes['s'-65] != 0.
198 * The following RFC characters o, w, s, i have constants defined via an
199 * enum, such as UM_SERVERNOTICE and UM_OPETATOR.
203 /** What snomasks are set on this user.
204 * This functions the same as the above modes.
210 /** The server the user is connected to.
214 /** The user's away message.
215 * If this string is empty, the user is not marked as away.
217 char awaymsg[MAXAWAY+1];
219 /** Number of lines the user can place into the buffer
220 * (up to the global NetBufferSize bytes) before they
221 * are disconnected for excess flood
225 /** Number of seconds this user is given to send USER/NICK
226 * If they do not send their details in this time limit they
227 * will be disconnected
229 unsigned int timeout;
231 /** The oper type they logged in as, if they are an oper.
232 * This is used to check permissions in operclasses, so that
233 * we can say 'yay' or 'nay' to any commands they issue.
234 * The value of this is the value of a valid 'type name=' tag.
238 /** True when DNS lookups are completed.
242 /** Number of seconds between PINGs for this user (set from <connect:allow> tag
244 unsigned int pingmax;
246 /** Password specified by the user when they registered.
247 * This is stored even if the <connect> block doesnt need a password, so that
248 * modules may check it.
252 /** User's receive queue.
253 * Lines from the IRCd awaiting processing are stored here.
254 * Upgraded april 2005, old system a bit hairy.
258 /** User's send queue.
259 * Lines waiting to be sent are stored here until their buffer is flushed.
273 /** Initialize the clients sockaddr
275 void SetSockAddr(int protocol_family, const char* ip, int port);
277 /** Get port number from sockaddr
281 /** Get protocol family from sockaddr
283 int GetProtocolFamily();
285 /** Get IP string from sockaddr, using static internal buffer
287 const char* GetIPString();
289 /** Get IP string from sockaddr, using caller-specified buffer
291 const char* GetIPString(char* buf);
293 /* Write error string
295 std::string WriteError;
297 /** Maximum size this user's sendq can become
301 /** Maximum size this user's recvq can become
305 /** Default constructor
309 /** Returns the full displayed host of the user
310 * This member function returns the hostname of the user as seen by other users
311 * on the server, in nick!ident&at;host form.
313 virtual char* GetFullHost();
315 /** Returns the full real host of the user
316 * This member function returns the hostname of the user as seen by other users
317 * on the server, in nick!ident&at;host form. If any form of hostname cloaking is in operation,
318 * e.g. through a module, then this method will ignore it and return the true hostname.
320 virtual char* GetFullRealHost();
323 * Create a displayable mode string for this users umodes
325 const char* FormatNoticeMasks();
327 bool userrec::ProcessNoticeMasks(const char *sm);
329 bool IsNoticeMaskSet(unsigned char sm);
331 void SetNoticeMask(unsigned char sm, bool value);
334 * Create a displayable mode string for this users umodes
336 const char* FormatModes();
338 bool IsModeSet(unsigned char m);
340 void SetMode(unsigned char m, bool value);
342 /** Returns true if a user is invited to a channel.
344 virtual bool IsInvited(irc::string &channel);
346 /** Adds a channel to a users invite list (invites them to a channel)
348 virtual void InviteTo(irc::string &channel);
350 /** Removes a channel from a users invite list.
351 * This member function is called on successfully joining an invite only channel
352 * to which the user has previously been invited, to clear the invitation.
354 virtual void RemoveInvite(irc::string &channel);
356 /** Returns true or false for if a user can execute a privilaged oper command.
357 * This is done by looking up their oper type from userrec::oper, then referencing
358 * this to their oper classes and checking the commands they can execute.
360 bool HasPermission(const std::string &command);
362 /** Calls read() to read some data for this user using their fd.
364 int ReadData(void* buffer, size_t size);
366 /** This method adds data to the buffer of the user.
367 * The buffer can grow to any size within limits of the available memory,
368 * managed by the size of a std::string, however if any individual line in
369 * the buffer grows over 600 bytes in length (which is 88 chars over the
370 * RFC-specified limit per line) then the method will return false and the
371 * text will not be inserted.
373 bool AddBuffer(const std::string &a);
375 /** This method returns true if the buffer contains at least one carriage return
376 * character (e.g. one complete line may be read)
378 bool BufferIsReady();
380 /** This function clears the entire buffer by setting it to an empty string.
384 /** This method returns the first available string at the tail end of the buffer
385 * and advances the tail end of the buffer past the string. This means it is
386 * a one way operation in a similar way to strtok(), and multiple calls return
387 * multiple lines if they are available. The results of this function if there
388 * are no lines to be read are unknown, always use BufferIsReady() to check if
389 * it is ok to read the buffer before calling GetBuffer().
391 std::string GetBuffer();
393 /** Sets the write error for a connection. This is done because the actual disconnect
394 * of a client may occur at an inopportune time such as half way through /LIST output.
395 * The WriteErrors of clients are checked at a more ideal time (in the mainloop) and
396 * errored clients purged.
398 void SetWriteError(const std::string &error);
400 /** Returns the write error which last occured on this connection or an empty string
403 const char* GetWriteError();
405 /** Adds to the user's write buffer.
406 * You may add any amount of text up to this users sendq value, if you exceed the
407 * sendq value, SetWriteError() will be called to set the users error string to
408 * "SendQ exceeded", and further buffer adds will be dropped.
410 void AddWriteBuf(const std::string &data);
412 /** Flushes as much of the user's buffer to the file descriptor as possible.
413 * This function may not always flush the entire buffer, rather instead as much of it
414 * as it possibly can. If the send() call fails to send the entire buffer, the buffer
415 * position is advanced forwards and the rest of the data sent at the next call to
418 void FlushWriteBuf();
420 /** Returns the list of channels this user has been invited to but has not yet joined.
422 InvitedList* GetInviteList();
424 /** Creates a wildcard host.
425 * Takes a buffer to use and fills the given buffer with the host in the format *!*@hostname
427 char* MakeWildHost();
430 * Takes a buffer to use and fills the given buffer with the host in the format nick!user@host
432 void MakeHost(char* nhost);
434 /** Shuts down and closes the user's socket
438 /** Disconnect a user gracefully
440 static void QuitUser(userrec *user, const std::string &r);
444 void Oper(const std::string &opertype);
446 void FullConnect(CullList* Goners);
447 userrec* UpdateNickHash(const char* New);
448 bool ForceNickChange(const char* newnick);
452 long GlobalCloneCount();
453 long LocalCloneCount();
455 /** Default destructor
460 /** Used to hold WHOWAS information
462 class WhoWasGroup : public classbase
472 WhoWasGroup(userrec* user);
476 typedef std::deque<WhoWasGroup*> whowas_set;
477 typedef std::map<irc::string,whowas_set*> whowas_users;
479 void MaintainWhoWas(time_t TIME);
480 void AddClient(int socket, int port, bool iscached, insp_inaddr ip4);
482 /* Configuration callbacks */
483 bool InitTypes(const char* tag);
484 bool InitClasses(const char* tag);
485 bool DoType(const char* tag, char** entries, void** values, int* types);
486 bool DoClass(const char* tag, char** entries, void** values, int* types);
487 bool DoneClassesAndTypes(const char* tag);