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
71 /** Derived from Resolver, and performs user forward/reverse lookups.
73 class UserResolver : public Resolver
76 /** User this class is 'attached' to.
82 UserResolver(InspIRCd* Instance, userrec* user, std::string to_resolve, bool forward);
84 void OnLookupComplete(const std::string &result);
85 void OnError(ResolverError e, const std::string &errormessage);
89 /** Holds information relevent to <connect allow> and <connect deny> tags in the config file.
91 class ConnectClass : public classbase
94 /** Type of line, either CC_ALLOW or CC_DENY
97 /** Max time to register the connection in seconds
99 int registration_timeout;
100 /** Number of lines in buffer before excess flood is triggered
103 /** Host mask for this line
106 /** Number of seconds between pings for this line
109 /** (Optional) Password for this line
113 /** Threshold value for flood disconnect
117 /** Maximum size of sendq for users in this class (bytes)
121 /** Maximum size of recvq for users in this class (bytes)
125 /** Local max when connecting by this connection class
129 /** Global max when connecting by this connection class
133 ConnectClass() : registration_timeout(0), flood(0), host(""), pingtime(0), pass(""), threshold(0), sendqmax(0), recvqmax(0)
138 /** Holds a complete list of all channels to which a user has been invited and has not yet joined.
140 typedef std::vector<Invited> InvitedList;
144 /** Holds a complete list of all allow and deny tags from the configuration file (connection classes)
146 typedef std::vector<ConnectClass> ClassVector;
148 /** Typedef for the list of user-channel records for a user
150 typedef std::vector<ucrec*> UserChanList;
152 /** Holds all information about a user
153 * This class stores all information about a user connected to the irc server. Everything about a
154 * connection is stored here primarily, from the user's socket ID (file descriptor) through to the
155 * user's nickname and hostname. Use the Find method of the server class to locate a specific user
158 class userrec : public connection
161 /** Pointer to creator
163 InspIRCd* ServerInstance;
165 /** A list of channels the user has a pending invite to.
169 /** Resolvers for looking up this users hostname
171 UserResolver* res_forward;
173 /** Resolvers for looking up this users hostname
175 UserResolver* res_reverse;
177 /** Stored reverse lookup from res_forward
179 std::string stored_host;
181 /** Starts a DNS lookup of the user's IP.
182 * When complete, sets userrec::dns_done to true.
184 void StartDNSLookup();
186 /** The users nickname.
187 * An invalid nickname indicates an unregistered connection prior to the NICK command.
192 /** The users ident reply.
193 * Two characters are added to the user-defined limit to compensate for the tilde etc.
195 char ident[IDENTMAX+2];
197 /** The host displayed to non-opers (used for cloaking etc).
198 * This usually matches the value of userrec::host.
202 /** The users full name.
204 char fullname[MAXGECOS+1];
206 /** The user's mode list.
207 * This is NOT a null terminated string! In the 1.1 version of InspIRCd
208 * this is an array of values in a similar way to channel modes.
209 * A value of 1 in field (modeletter-65) indicates that the mode is
210 * set, for example, to work out if mode +s is set, we check the field
211 * userrec::modes['s'-65] != 0.
212 * The following RFC characters o, w, s, i have constants defined via an
213 * enum, such as UM_SERVERNOTICE and UM_OPETATOR.
217 /** What snomasks are set on this user.
218 * This functions the same as the above modes.
222 /** Channels this user is on, and the permissions they have there
226 /** The server the user is connected to.
230 /** The user's away message.
231 * If this string is empty, the user is not marked as away.
233 char awaymsg[MAXAWAY+1];
235 /** Number of lines the user can place into the buffer
236 * (up to the global NetBufferSize bytes) before they
237 * are disconnected for excess flood
241 /** Number of seconds this user is given to send USER/NICK
242 * If they do not send their details in this time limit they
243 * will be disconnected
245 unsigned int timeout;
247 /** The oper type they logged in as, if they are an oper.
248 * This is used to check permissions in operclasses, so that
249 * we can say 'yay' or 'nay' to any commands they issue.
250 * The value of this is the value of a valid 'type name=' tag.
254 /** True when DNS lookups are completed.
258 /** Number of seconds between PINGs for this user (set from <connect:allow> tag
260 unsigned int pingmax;
262 /** Password specified by the user when they registered.
263 * This is stored even if the <connect> block doesnt need a password, so that
264 * modules may check it.
268 /** User's receive queue.
269 * Lines from the IRCd awaiting processing are stored here.
270 * Upgraded april 2005, old system a bit hairy.
274 /** User's send queue.
275 * Lines waiting to be sent are stored here until their buffer is flushed.
289 /** Initialize the clients sockaddr
290 * @param protocol_family The protocol family of the IP address, AF_INET or AF_INET6
291 * @param ip A human-readable IP address for this user matching the protcol_family
292 * @param port The port number of this user or zero for a remote user
294 void SetSockAddr(int protocol_family, const char* ip, int port);
296 /** Get port number from sockaddr
297 * @return The port number of this user.
301 /** Get protocol family from sockaddr
302 * @return The protocol family of this user, either AF_INET or AF_INET6
304 int GetProtocolFamily();
306 /** Get IP string from sockaddr, using static internal buffer
307 * @return The IP string
309 const char* GetIPString();
311 /** Get IP string from sockaddr, using caller-specified buffer
312 * @param buf A buffer to use
313 * @return The IP string
315 const char* GetIPString(char* buf);
317 /* Write error string
319 std::string WriteError;
321 /** Maximum size this user's sendq can become
325 /** Maximum size this user's recvq can become
329 /** Default constructor
330 * @throw Nothing at present
332 userrec(InspIRCd* Instance);
334 /** Returns the full displayed host of the user
335 * This member function returns the hostname of the user as seen by other users
336 * on the server, in nick!ident&at;host form.
337 * @return The full masked host of the user
339 virtual char* GetFullHost();
341 /** Returns the full real host of the user
342 * This member function returns the hostname of the user as seen by other users
343 * on the server, in nick!ident&at;host form. If any form of hostname cloaking is in operation,
344 * e.g. through a module, then this method will ignore it and return the true hostname.
345 * @return The full real host of the user
347 virtual char* GetFullRealHost();
349 /** Create a displayable mode string for this users snomasks
350 * @return The notice mask character sequence
352 const char* FormatNoticeMasks();
354 /** Process a snomask modifier string, e.g. +abc-de
355 * @param sm A sequence of notice mask characters
356 * @return True if the notice masks were successfully applied
358 bool userrec::ProcessNoticeMasks(const char *sm);
360 /** Returns true if a notice mask is set
361 * @param sm A notice mask character to check
362 * @return True if the notice mask is set
364 bool IsNoticeMaskSet(unsigned char sm);
366 /** Changed a specific notice mask value
367 * @param sm The server notice mask to change
368 * @param value An on/off value for this mask
370 void SetNoticeMask(unsigned char sm, bool value);
372 /** Create a displayable mode string for this users umodes
373 * @param The mode string
375 const char* FormatModes();
377 /** Returns true if a specific mode is set
378 * @param m The user mode
379 * @return True if the mode is set
381 bool IsModeSet(unsigned char m);
383 /** Set a specific usermode to on or off
384 * @param m The user mode
385 * @param value On or off setting of the mode
387 void SetMode(unsigned char m, bool value);
389 /** Returns true if a user is invited to a channel.
390 * @param channel A channel name to look up
391 * @return True if the user is invited to the given channel
393 virtual bool IsInvited(irc::string &channel);
395 /** Adds a channel to a users invite list (invites them to a channel)
396 * @param channel A channel name to add
398 virtual void InviteTo(irc::string &channel);
400 /** Removes a channel from a users invite list.
401 * This member function is called on successfully joining an invite only channel
402 * to which the user has previously been invited, to clear the invitation.
403 * @param channel The channel to remove the invite to
405 virtual void RemoveInvite(irc::string &channel);
407 /** Returns true or false for if a user can execute a privilaged oper command.
408 * This is done by looking up their oper type from userrec::oper, then referencing
409 * this to their oper classes and checking the commands they can execute.
410 * @param command A command (should be all CAPS)
411 * @return True if this user can execute the command
413 bool HasPermission(const std::string &command);
415 /** Calls read() to read some data for this user using their fd.
416 * @param buffer The buffer to read into
417 * @param size The size of data to read
418 * @return The number of bytes read, or -1 if an error occured.
420 int ReadData(void* buffer, size_t size);
422 /** This method adds data to the read buffer of the user.
423 * The buffer can grow to any size within limits of the available memory,
424 * managed by the size of a std::string, however if any individual line in
425 * the buffer grows over 600 bytes in length (which is 88 chars over the
426 * RFC-specified limit per line) then the method will return false and the
427 * text will not be inserted.
428 * @param a The string to add to the users read buffer
429 * @return True if the string was successfully added to the read buffer
431 bool AddBuffer(const std::string &a);
433 /** This method returns true if the buffer contains at least one carriage return
434 * character (e.g. one complete line may be read)
435 * @return True if there is at least one complete line in the users buffer
437 bool BufferIsReady();
439 /** This function clears the entire buffer by setting it to an empty string.
443 /** This method returns the first available string at the tail end of the buffer
444 * and advances the tail end of the buffer past the string. This means it is
445 * a one way operation in a similar way to strtok(), and multiple calls return
446 * multiple lines if they are available. The results of this function if there
447 * are no lines to be read are unknown, always use BufferIsReady() to check if
448 * it is ok to read the buffer before calling GetBuffer().
449 * @return The string at the tail end of this users buffer
451 std::string GetBuffer();
453 /** Sets the write error for a connection. This is done because the actual disconnect
454 * of a client may occur at an inopportune time such as half way through /LIST output.
455 * The WriteErrors of clients are checked at a more ideal time (in the mainloop) and
456 * errored clients purged.
457 * @param error The error string to set.
459 void SetWriteError(const std::string &error);
461 /** Returns the write error which last occured on this connection or an empty string
463 * @return The error string which has occured for this user
465 const char* GetWriteError();
467 /** Adds to the user's write buffer.
468 * You may add any amount of text up to this users sendq value, if you exceed the
469 * sendq value, SetWriteError() will be called to set the users error string to
470 * "SendQ exceeded", and further buffer adds will be dropped.
471 * @param data The data to add to the write buffer
473 void AddWriteBuf(const std::string &data);
475 /** Flushes as much of the user's buffer to the file descriptor as possible.
476 * This function may not always flush the entire buffer, rather instead as much of it
477 * as it possibly can. If the send() call fails to send the entire buffer, the buffer
478 * position is advanced forwards and the rest of the data sent at the next call to
481 void FlushWriteBuf();
483 /** Returns the list of channels this user has been invited to but has not yet joined.
484 * @return A list of channels the user is invited to
486 InvitedList* GetInviteList();
488 /** Creates a wildcard host.
489 * Takes a buffer to use and fills the given buffer with the host in the format *!*@hostname
490 * @return The wildcarded hostname in *!*@host form
492 char* MakeWildHost();
495 * Takes a buffer to use and fills the given buffer with the host in the format nick!user@host
496 * @param Buffer to fill with host information
498 void MakeHost(char* nhost);
500 /** Shuts down and closes the user's socket
504 /** Disconnect a user gracefully
505 * @param user The user to remove
506 * @param r The quit reason
508 static void QuitUser(InspIRCd* Instance, userrec *user, const std::string &r);
510 /** Add the user to WHOWAS system
514 /** Oper up the user using the given opertype.
515 * This will also give the +o usermode.
516 * @param opertype The oper type to oper as
518 void Oper(const std::string &opertype);
520 /** Use this method to fully connect a user.
521 * This will send the message of the day, check G/K/E lines, etc.
522 * @param Goners If the user is disconnected by this method call, the
523 * value of 'this' will be pushed onto this CullList. This is used by
524 * the core to connect many users in rapid succession without invalidating
527 void FullConnect(CullList* Goners);
529 /** Change this users hash key to a new string.
530 * You should not call this function directly. It is used by the core
531 * to update the users hash entry on a nickchange.
532 * @param New new user_hash key
533 * @return Pointer to userrec in hash (usually 'this')
535 userrec* UpdateNickHash(const char* New);
537 /** Force a nickname change.
538 * If the nickname change fails (for example, because the nick in question
539 * already exists) this function will return false, and you must then either
540 * output an error message, or quit the user for nickname collision.
541 * @param newnick The nickname to change to
542 * @return True if the nickchange was successful.
544 bool ForceNickChange(const char* newnick);
546 /** Add a client to the system.
547 * This will create a new userrec, insert it into the user_hash,
548 * initialize it as not yet registered, and add it to the socket engine.
550 static void AddClient(InspIRCd* Instance, int socket, int port, bool iscached, insp_inaddr ip);
553 * This will clear the +o usermode and unset the user's oper type
557 /** Return the number of global clones of this user
559 long GlobalCloneCount();
561 /** Return the number of local clones of this user
563 long LocalCloneCount();
565 /** Write text to this user, appending CR/LF.
566 * @param text A std::string to send to the user
568 void Write(const std::string &text);
570 /** Write text to this user, appending CR/LF.
571 * @param text The format string for text to send to the user
572 * @param ... POD-type format arguments
574 void Write(const char *text, ...);
576 /** Write text to this user, appending CR/LF and prepending :server.name
577 * @param text A std::string to send to the user
579 void WriteServ(const std::string& text);
581 /** Write text to this user, appending CR/LF and prepending :server.name
582 * @param text The format string for text to send to the user
583 * @param ... POD-type format arguments
585 void WriteServ(const char* text, ...);
587 /** Write text to this user, appending CR/LF and prepending :nick!user@host of the user provided in the first parameter.
588 * @param user The user to prepend the :nick!user@host of
589 * @param text A std::string to send to the user
591 void WriteFrom(userrec *user, const std::string &text);
593 /** Write text to this user, appending CR/LF and prepending :nick!user@host of the user provided in the first parameter.
594 * @param user The user to prepend the :nick!user@host of
595 * @param text The format string for text to send to the user
596 * @param ... POD-type format arguments
598 void WriteFrom(userrec *user, const char* text, ...);
600 /** Write text to the user provided in the first parameter, appending CR/LF, and prepending THIS user's :nick!user@host.
601 * @param dest The user to route the message to
602 * @param text A std::string to send to the user
604 void WriteTo(userrec *dest, const std::string &data);
606 /** Write text to the user provided in the first parameter, appending CR/LF, and prepending THIS user's :nick!user@host.
607 * @param dest The user to route the message to
608 * @param text The format string for text to send to the user
609 * @param ... POD-type format arguments
611 void WriteTo(userrec *dest, const char *data, ...);
613 /** Write to all users that can see this user (including this user in the list), appending CR/LF
614 * @param text A std::string to send to the users
616 void WriteCommon(const std::string &text);
618 /** Write to all users that can see this user (including this user in the list), appending CR/LF
619 * @param text The format string for text to send to the users
620 * @param ... POD-type format arguments
622 void WriteCommon(const char* text, ...);
624 /** Write to all users that can see this user (not including this user in the list), appending CR/LF
625 * @param text The format string for text to send to the users
626 * @param ... POD-type format arguments
628 void WriteCommonExcept(const char* text, ...);
630 /** Write to all users that can see this user (not including this user in the list), appending CR/LF
631 * @param text A std::string to send to the users
633 void WriteCommonExcept(const std::string &text);
635 void WriteWallOps(const char* text, ...);
637 void WriteWallOps(const std::string &text);
639 bool SharesChannelWith(userrec *other);
641 bool ChangeDisplayedHost(const char* host);
643 bool ChangeName(const char* gecos);
647 void NoticeAll(char* text, ...);
649 std::string ChannelList(userrec* source);
651 void SplitChanList(userrec* dest, const std::string &cl);
653 void PurgeEmptyChannels();
655 ConnectClass& GetClass();
657 /** Default destructor
665 /** Holds whowas related functions and classes
670 /** Used to hold WHOWAS information
672 class WhoWasGroup : public classbase
694 /** Initialize this WhoQasFroup with a user
696 WhoWasGroup(userrec* user);
702 /** A group of users related by nickname
704 typedef std::deque<WhoWasGroup*> whowas_set;
706 /** Sets of users in the whowas system
708 typedef std::map<irc::string,whowas_set*> whowas_users;
710 /** Called every hour by the core to remove expired entries
712 void MaintainWhoWas(time_t TIME);
716 /* Configuration callbacks */
718 bool InitTypes(ServerConfig* conf, const char* tag);
719 bool InitClasses(ServerConfig* conf, const char* tag);
720 bool DoType(ServerConfig* conf, const char* tag, char** entries, void** values, int* types);
721 bool DoClass(ServerConfig* conf, const char* tag, char** entries, void** values, int* types);
722 bool DoneClassesAndTypes(ServerConfig* conf, const char* tag);