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;
168 /** Resolvers for looking up this users hostname
170 UserResolver* res_reverse;
172 /** Stored reverse lookup from res_forward
174 std::string stored_host;
176 /** Starts a DNS lookup of the user's IP.
177 * When complete, sets userrec::dns_done to true.
179 void StartDNSLookup();
181 /** The users nickname.
182 * An invalid nickname indicates an unregistered connection prior to the NICK command.
187 /** The users ident reply.
188 * Two characters are added to the user-defined limit to compensate for the tilde etc.
190 char ident[IDENTMAX+2];
192 /** The host displayed to non-opers (used for cloaking etc).
193 * This usually matches the value of userrec::host.
197 /** The users full name.
199 char fullname[MAXGECOS+1];
201 /** The user's mode list.
202 * This is NOT a null terminated string! In the 1.1 version of InspIRCd
203 * this is an array of values in a similar way to channel modes.
204 * A value of 1 in field (modeletter-65) indicates that the mode is
205 * set, for example, to work out if mode +s is set, we check the field
206 * userrec::modes['s'-65] != 0.
207 * The following RFC characters o, w, s, i have constants defined via an
208 * enum, such as UM_SERVERNOTICE and UM_OPETATOR.
212 /** What snomasks are set on this user.
213 * This functions the same as the above modes.
217 /** Channels this user is on, and the permissions they have there
221 /** The server the user is connected to.
225 /** The user's away message.
226 * If this string is empty, the user is not marked as away.
228 char awaymsg[MAXAWAY+1];
230 /** Number of lines the user can place into the buffer
231 * (up to the global NetBufferSize bytes) before they
232 * are disconnected for excess flood
236 /** Number of seconds this user is given to send USER/NICK
237 * If they do not send their details in this time limit they
238 * will be disconnected
240 unsigned int timeout;
242 /** The oper type they logged in as, if they are an oper.
243 * This is used to check permissions in operclasses, so that
244 * we can say 'yay' or 'nay' to any commands they issue.
245 * The value of this is the value of a valid 'type name=' tag.
249 /** True when DNS lookups are completed.
253 /** Number of seconds between PINGs for this user (set from <connect:allow> tag
255 unsigned int pingmax;
257 /** Password specified by the user when they registered.
258 * This is stored even if the <connect> block doesnt need a password, so that
259 * modules may check it.
263 /** User's receive queue.
264 * Lines from the IRCd awaiting processing are stored here.
265 * Upgraded april 2005, old system a bit hairy.
269 /** User's send queue.
270 * Lines waiting to be sent are stored here until their buffer is flushed.
284 /** Initialize the clients sockaddr
285 * @param protocol_family The protocol family of the IP address, AF_INET or AF_INET6
286 * @param ip A human-readable IP address for this user matching the protcol_family
287 * @param port The port number of this user or zero for a remote user
289 void SetSockAddr(int protocol_family, const char* ip, int port);
291 /** Get port number from sockaddr
292 * @return The port number of this user.
296 /** Get protocol family from sockaddr
297 * @return The protocol family of this user, either AF_INET or AF_INET6
299 int GetProtocolFamily();
301 /** Get IP string from sockaddr, using static internal buffer
302 * @return The IP string
304 const char* GetIPString();
306 /** Get IP string from sockaddr, using caller-specified buffer
307 * @param buf A buffer to use
308 * @return The IP string
310 const char* GetIPString(char* buf);
312 /* Write error string
314 std::string WriteError;
316 /** Maximum size this user's sendq can become
320 /** Maximum size this user's recvq can become
324 /** Default constructor
325 * @throw Nothing at present
329 /** Returns the full displayed host of the user
330 * This member function returns the hostname of the user as seen by other users
331 * on the server, in nick!ident&at;host form.
332 * @return The full masked host of the user
334 virtual char* GetFullHost();
336 /** Returns the full real host of the user
337 * This member function returns the hostname of the user as seen by other users
338 * on the server, in nick!ident&at;host form. If any form of hostname cloaking is in operation,
339 * e.g. through a module, then this method will ignore it and return the true hostname.
340 * @return The full real host of the user
342 virtual char* GetFullRealHost();
344 /** Create a displayable mode string for this users snomasks
345 * @return The notice mask character sequence
347 const char* FormatNoticeMasks();
349 /** Process a snomask modifier string, e.g. +abc-de
350 * @param sm A sequence of notice mask characters
351 * @return True if the notice masks were successfully applied
353 bool userrec::ProcessNoticeMasks(const char *sm);
355 /** Returns true if a notice mask is set
356 * @param sm A notice mask character to check
357 * @return True if the notice mask is set
359 bool IsNoticeMaskSet(unsigned char sm);
361 /** Changed a specific notice mask value
362 * @param sm The server notice mask to change
363 * @param value An on/off value for this mask
365 void SetNoticeMask(unsigned char sm, bool value);
367 /** Create a displayable mode string for this users umodes
368 * @param The mode string
370 const char* FormatModes();
372 /** Returns true if a specific mode is set
373 * @param m The user mode
374 * @return True if the mode is set
376 bool IsModeSet(unsigned char m);
378 /** Set a specific usermode to on or off
379 * @param m The user mode
380 * @param value On or off setting of the mode
382 void SetMode(unsigned char m, bool value);
384 /** Returns true if a user is invited to a channel.
385 * @param channel A channel name to look up
386 * @return True if the user is invited to the given channel
388 virtual bool IsInvited(irc::string &channel);
390 /** Adds a channel to a users invite list (invites them to a channel)
391 * @param channel A channel name to add
393 virtual void InviteTo(irc::string &channel);
395 /** Removes a channel from a users invite list.
396 * This member function is called on successfully joining an invite only channel
397 * to which the user has previously been invited, to clear the invitation.
398 * @param channel The channel to remove the invite to
400 virtual void RemoveInvite(irc::string &channel);
402 /** Returns true or false for if a user can execute a privilaged oper command.
403 * This is done by looking up their oper type from userrec::oper, then referencing
404 * this to their oper classes and checking the commands they can execute.
405 * @param command A command (should be all CAPS)
406 * @return True if this user can execute the command
408 bool HasPermission(const std::string &command);
410 /** Calls read() to read some data for this user using their fd.
411 * @param buffer The buffer to read into
412 * @param size The size of data to read
413 * @return The number of bytes read, or -1 if an error occured.
415 int ReadData(void* buffer, size_t size);
417 /** This method adds data to the read buffer of the user.
418 * The buffer can grow to any size within limits of the available memory,
419 * managed by the size of a std::string, however if any individual line in
420 * the buffer grows over 600 bytes in length (which is 88 chars over the
421 * RFC-specified limit per line) then the method will return false and the
422 * text will not be inserted.
423 * @param a The string to add to the users read buffer
424 * @return True if the string was successfully added to the read buffer
426 bool AddBuffer(const std::string &a);
428 /** This method returns true if the buffer contains at least one carriage return
429 * character (e.g. one complete line may be read)
430 * @return True if there is at least one complete line in the users buffer
432 bool BufferIsReady();
434 /** This function clears the entire buffer by setting it to an empty string.
438 /** This method returns the first available string at the tail end of the buffer
439 * and advances the tail end of the buffer past the string. This means it is
440 * a one way operation in a similar way to strtok(), and multiple calls return
441 * multiple lines if they are available. The results of this function if there
442 * are no lines to be read are unknown, always use BufferIsReady() to check if
443 * it is ok to read the buffer before calling GetBuffer().
444 * @return The string at the tail end of this users buffer
446 std::string GetBuffer();
448 /** Sets the write error for a connection. This is done because the actual disconnect
449 * of a client may occur at an inopportune time such as half way through /LIST output.
450 * The WriteErrors of clients are checked at a more ideal time (in the mainloop) and
451 * errored clients purged.
452 * @param error The error string to set.
454 void SetWriteError(const std::string &error);
456 /** Returns the write error which last occured on this connection or an empty string
458 * @return The error string which has occured for this user
460 const char* GetWriteError();
462 /** Adds to the user's write buffer.
463 * You may add any amount of text up to this users sendq value, if you exceed the
464 * sendq value, SetWriteError() will be called to set the users error string to
465 * "SendQ exceeded", and further buffer adds will be dropped.
466 * @param data The data to add to the write buffer
468 void AddWriteBuf(const std::string &data);
470 /** Flushes as much of the user's buffer to the file descriptor as possible.
471 * This function may not always flush the entire buffer, rather instead as much of it
472 * as it possibly can. If the send() call fails to send the entire buffer, the buffer
473 * position is advanced forwards and the rest of the data sent at the next call to
476 void FlushWriteBuf();
478 /** Returns the list of channels this user has been invited to but has not yet joined.
479 * @return A list of channels the user is invited to
481 InvitedList* GetInviteList();
483 /** Creates a wildcard host.
484 * Takes a buffer to use and fills the given buffer with the host in the format *!*@hostname
485 * @return The wildcarded hostname in *!*@host form
487 char* MakeWildHost();
490 * Takes a buffer to use and fills the given buffer with the host in the format nick!user@host
491 * @param Buffer to fill with host information
493 void MakeHost(char* nhost);
495 /** Shuts down and closes the user's socket
499 /** Disconnect a user gracefully
500 * @param user The user to remove
501 * @param r The quit reason
503 static void QuitUser(userrec *user, const std::string &r);
505 /** Add the user to WHOWAS system
509 /** Oper up the user using the given opertype.
510 * This will also give the +o usermode.
511 * @param opertype The oper type to oper as
513 void Oper(const std::string &opertype);
515 /** Use this method to fully connect a user.
516 * This will send the message of the day, check G/K/E lines, etc.
517 * @param Goners If the user is disconnected by this method call, the
518 * value of 'this' will be pushed onto this CullList. This is used by
519 * the core to connect many users in rapid succession without invalidating
522 void FullConnect(CullList* Goners);
524 /** Change this users hash key to a new string.
525 * You should not call this function directly. It is used by the core
526 * to update the users hash entry on a nickchange.
527 * @param New new user_hash key
528 * @return Pointer to userrec in hash (usually 'this')
530 userrec* UpdateNickHash(const char* New);
532 /** Force a nickname change.
533 * If the nickname change fails (for example, because the nick in question
534 * already exists) this function will return false, and you must then either
535 * output an error message, or quit the user for nickname collision.
536 * @param newnick The nickname to change to
537 * @return True if the nickchange was successful.
539 bool ForceNickChange(const char* newnick);
541 /** Add a client to the system.
542 * This will create a new userrec, insert it into the user_hash,
543 * initialize it as not yet registered, and add it to the socket engine.
545 static void AddClient(int socket, int port, bool iscached, insp_inaddr ip);
548 * This will clear the +o usermode and unset the user's oper type
552 /** Return the number of global clones of this user
554 long GlobalCloneCount();
556 /** Return the number of local clones of this user
558 long LocalCloneCount();
562 void Write(const std::string &text);
563 void Write(const char *text, ...);
564 /** FROM server to this user
566 void WriteServ(const std::string& text);
567 void WriteServ(const char* text, ...);
568 /** FROM another user TO this user
570 void WriteFrom(userrec *user, const std::string &text);
571 void WriteFrom(userrec *user, const char* text, ...);
572 /** TO another user FROM this user
574 void WriteTo(userrec *dest, const std::string &data);
575 void WriteTo(userrec *dest, const char *data, ...);
578 /** Default destructor
586 /** Holds whowas related functions and classes
591 /** Used to hold WHOWAS information
593 class WhoWasGroup : public classbase
615 /** Initialize this WhoQasFroup with a user
617 WhoWasGroup(userrec* user);
623 /** A group of users related by nickname
625 typedef std::deque<WhoWasGroup*> whowas_set;
627 /** Sets of users in the whowas system
629 typedef std::map<irc::string,whowas_set*> whowas_users;
631 /** Called every hour by the core to remove expired entries
633 void MaintainWhoWas(time_t TIME);
637 /* Configuration callbacks */
638 bool InitTypes(const char* tag);
639 bool InitClasses(const char* tag);
640 bool DoType(const char* tag, char** entries, void** values, int* types);
641 bool DoClass(const char* tag, char** entries, void** values, int* types);
642 bool DoneClassesAndTypes(const char* tag);