2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2006-2007, 2009 Dennis Friis <peavey@inspircd.org>
6 * Copyright (C) 2003-2008 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
8 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
9 * Copyright (C) 2006-2007 Oliver Lupton <oliverlupton@gmail.com>
10 * Copyright (C) 2003 randomdan <???@???>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "inspsocket.h"
40 /** Used to define a set of behavior bits for a module
43 VF_NONE = 0, // module is not special at all
44 VF_STATIC = 1, // module is static, cannot be /unloadmodule'd
45 VF_VENDOR = 2, // module is a vendor module (came in the original tarball, not 3rd party)
46 VF_COMMON = 4, // module needs to be common on all servers in a network to link
47 VF_OPTCOMMON = 8, // module should be common on all servers for unsurprising behavior
48 VF_CORE = 16 // module is a core command, can be assumed loaded on all servers
51 /** Used to represent an event type, for user, channel or server
53 enum TargetTypeFlags {
60 /** Used to represent wether a message was PRIVMSG or NOTICE
67 #define MOD_RES_ALLOW (ModResult(1))
68 #define MOD_RES_PASSTHRU (ModResult(0))
69 #define MOD_RES_DENY (ModResult(-1))
71 /** Used to represent an allow/deny module result.
72 * Not constructed as an enum because it reverses the value logic of some functions;
73 * the compiler will inline accesses to have the same efficiency as integer operations.
77 ModResult() : res(0) {}
78 explicit ModResult(int r) : res(r) {}
79 inline bool operator==(const ModResult& r) const
83 inline bool operator!=(const ModResult& r) const
87 inline bool operator!() const
91 inline bool check(bool def) const
93 return (res == 1 || (res == 0 && def));
96 * Merges two results, preferring ALLOW to DENY
98 inline ModResult operator+(const ModResult& r) const
100 if (res == r.res || r.res == 0)
104 // they are different, and neither is passthru
105 return MOD_RES_ALLOW;
109 /** InspIRCd major version.
110 * 1.2 -> 102; 2.1 -> 201; 2.12 -> 212
112 #define INSPIRCD_VERSION_MAJ 202
113 /** InspIRCd API version.
114 * If you change any API elements, increment this value. This counter should be
115 * reset whenever the major version is changed. Modules can use these two values
116 * and numerical comparisons in preprocessor macros if they wish to support
117 * multiple versions of InspIRCd in one file.
119 #define INSPIRCD_VERSION_API 1
122 * This #define allows us to call a method in all
123 * loaded modules in a readable simple way, e.g.:
124 * 'FOREACH_MOD(I_OnConnect,OnConnect(user));'
126 #define FOREACH_MOD(y,x) do { \
127 EventHandlerIter safei; \
128 for (EventHandlerIter _i = ServerInstance->Modules->EventHandlers[y].begin(); _i != ServerInstance->Modules->EventHandlers[y].end(); ) \
136 catch (CoreException& modexcept) \
138 ServerInstance->Logs->Log("MODULE",DEFAULT,"Exception caught: %s",modexcept.GetReason()); \
145 * Custom module result handling loop. This is a paired macro, and should only
146 * be used with while_each_hook.
148 * See src/channels.cpp for an example of use.
150 #define DO_EACH_HOOK(n,v,args) \
152 EventHandlerIter iter_ ## n = ServerInstance->Modules->EventHandlers[I_ ## n].begin(); \
153 while (iter_ ## n != ServerInstance->Modules->EventHandlers[I_ ## n].end()) \
155 Module* mod_ ## n = *iter_ ## n; \
159 v = (mod_ ## n)->n args;
161 #define WHILE_EACH_HOOK(n) \
163 catch (CoreException& except_ ## n) \
165 ServerInstance->Logs->Log("MODULE",DEFAULT,"Exception caught: %s", (except_ ## n).GetReason()); \
166 (void) mod_ ## n; /* catch mismatched pairs */ \
172 * Module result iterator
173 * Runs the given hook until some module returns a useful result.
175 * Example: ModResult result;
176 * FIRST_MOD_RESULT(OnUserPreNick, result, (user, newnick))
178 #define FIRST_MOD_RESULT(n,v,args) do { \
179 v = MOD_RES_PASSTHRU; \
180 DO_EACH_HOOK(n,v,args) \
182 if (v != MOD_RES_PASSTHRU) \
185 WHILE_EACH_HOOK(n); \
188 /** Holds a module's Version information.
189 * The members (set by the constructor only) indicate details as to the version number
190 * of a module. A class of type Version is returned by the GetVersion method of the Module class.
192 class CoreExport Version
195 /** Module description
197 const std::string description;
203 /** Server linking description string */
204 const std::string link_data;
206 /** Simple module version */
207 Version(const std::string &desc, int flags = VF_NONE);
209 /** Complex version information, including linking compatability data */
210 Version(const std::string &desc, int flags, const std::string& linkdata);
212 virtual ~Version() {}
215 /** The Request class is a unicast message directed at a given module.
216 * When this class is properly instantiated it may be sent to a module
217 * using the Send() method, which will call the given module's OnRequest
218 * method with this class as its parameter.
220 class CoreExport Request : public classbase
223 /** This should be a null-terminated string identifying the type of request,
224 * all modules should define this and use it to determine the nature of the
225 * request before they attempt to cast the Request in any way.
227 const char* const id;
228 /** This is a pointer to the sender of the message, which can be used to
229 * directly trigger events, or to create a reply.
232 /** The single destination of the Request
236 /** Create a new Request
237 * This is for the 'new' way of defining a subclass
238 * of Request and defining it in a common header,
239 * passing an object of your Request subclass through
240 * as a Request* and using the ID string to determine
241 * what to cast it back to and the other end.
243 Request(Module* src, Module* dst, const char* idstr);
244 /** Send the Request.
250 /** The Event class is a unicast message directed at all modules.
251 * When the class is properly instantiated it may be sent to all modules
252 * using the Send() method, which will trigger the OnEvent method in
253 * all modules passing the object as its parameter.
255 class CoreExport Event : public classbase
258 /** This is a pointer to the sender of the message, which can be used to
259 * directly trigger events, or to create a reply.
262 /** The event identifier.
263 * This is arbitary text which should be used to distinguish
264 * one type of event from another.
266 const std::string id;
268 /** Create a new Event
270 Event(Module* src, const std::string &eventid);
272 * The return result of an Event::Send() will always be NULL as
273 * no replies are expected.
278 class CoreExport DataProvider : public ServiceProvider
281 DataProvider(Module* Creator, const std::string& Name)
282 : ServiceProvider(Creator, Name, SERVICE_DATA) {}
285 class CoreExport dynamic_reference_base : public interfacebase
291 ServiceProvider* value;
294 dynamic_reference_base(Module* Creator, const std::string& Name);
295 ~dynamic_reference_base();
296 inline const std::string& GetProvider() { return name; }
297 void SetProvider(const std::string& newname);
299 operator bool() { return (value != NULL); }
300 static void reset_all();
303 inline void dynamic_reference_base::check()
306 throw ModuleException("Dynamic reference to '" + name + "' failed to resolve");
310 class dynamic_reference : public dynamic_reference_base
313 dynamic_reference(Module* Creator, const std::string& Name)
314 : dynamic_reference_base(Creator, Name) {}
316 inline T* operator->()
319 return static_cast<T*>(value);
329 class dynamic_reference_nocheck : public dynamic_reference_base
332 dynamic_reference_nocheck(Module* Creator, const std::string& Name)
333 : dynamic_reference_base(Creator, Name) {}
337 return static_cast<T*>(value);
346 class ModeReference : public dynamic_reference_nocheck<ModeHandler>
349 ModeReference(Module* mod, const std::string& modename)
350 : dynamic_reference_nocheck<ModeHandler>(mod, "mode/" + modename) {}
353 /** Priority types which can be used by Module::Prioritize()
355 enum Priority { PRIORITY_FIRST, PRIORITY_LAST, PRIORITY_BEFORE, PRIORITY_AFTER };
357 /** Implementation-specific flags which may be set in Module::Implements()
362 I_OnUserConnect, I_OnUserQuit, I_OnUserDisconnect, I_OnUserJoin, I_OnUserPart, I_OnRehash,
363 I_OnSendSnotice, I_OnUserPreJoin, I_OnUserPreKick, I_OnUserKick, I_OnOper, I_OnInfo, I_OnWhois,
364 I_OnUserPreInvite, I_OnUserInvite, I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserPreNick,
365 I_OnUserMessage, I_OnUserNotice, I_OnMode, I_OnGetServerDescription, I_OnSyncUser,
366 I_OnSyncChannel, I_OnDecodeMetaData, I_OnWallops, I_OnAcceptConnection, I_OnUserInit,
367 I_OnChangeHost, I_OnChangeName, I_OnAddLine, I_OnDelLine, I_OnExpireLine,
368 I_OnUserPostNick, I_OnPreMode, I_On005Numeric, I_OnKill, I_OnRemoteKill, I_OnLoadModule,
369 I_OnUnloadModule, I_OnBackgroundTimer, I_OnPreCommand, I_OnCheckReady, I_OnCheckInvite,
370 I_OnRawMode, I_OnCheckKey, I_OnCheckLimit, I_OnCheckBan, I_OnCheckChannelBan, I_OnExtBanCheck,
371 I_OnStats, I_OnChangeLocalUserHost, I_OnPreTopicChange,
372 I_OnPostTopicChange, I_OnEvent, I_OnGlobalOper, I_OnPostConnect,
373 I_OnChangeLocalUserGECOS, I_OnUserRegister, I_OnChannelPreDelete, I_OnChannelDelete,
374 I_OnPostOper, I_OnSyncNetwork, I_OnSetAway, I_OnPostCommand, I_OnPostJoin,
375 I_OnWhoisLine, I_OnBuildNeighborList, I_OnGarbageCollect, I_OnSetConnectClass,
376 I_OnText, I_OnPassCompare, I_OnRunTestSuite, I_OnNamesListItem, I_OnNumeric, I_OnHookIO,
377 I_OnPreRehash, I_OnModuleRehash, I_OnSendWhoLine, I_OnChangeIdent, I_OnSetUserIP,
381 /** Base class for all InspIRCd modules
382 * This class is the base class for InspIRCd modules. All modules must inherit from this class,
383 * its methods will be called when irc server events occur. class inherited from module must be
384 * instantiated by the ModuleFactory class (see relevent section) for the module to be initialised.
386 class CoreExport Module : public classbase, public usecountbase
389 /** File that this module was loaded from
391 std::string ModuleSourceFile;
392 /** Reference to the dlopen() value
394 DLLManager* ModuleDLLManager;
396 /** Default constructor.
397 * Creates a module class. Don't do any type of hook registration or checks
398 * for other modules here; do that in init().
403 * \exception ModuleException Throwing this class, or any class derived from ModuleException, causes loading of the module to abort.
405 virtual void init() {}
407 /** Clean up prior to destruction
408 * If you override, you must call this AFTER your module's cleanup
410 virtual CullResult cull();
412 /** Default destructor.
413 * destroys a module class
417 virtual void Prioritize()
421 /** Returns the version number of a Module.
422 * The method should return a Version object with its version information assigned via
425 virtual Version GetVersion() = 0;
427 /** Called when a user connects.
428 * The details of the connecting user are available to you in the parameter User *user
429 * @param user The user who is connecting
431 virtual void OnUserConnect(LocalUser* user);
433 /** Called when a user quits.
434 * The details of the exiting user are available to you in the parameter User *user
435 * This event is only called when the user is fully registered when they quit. To catch
436 * raw disconnections, use the OnUserDisconnect method.
437 * @param user The user who is quitting
438 * @param message The user's quit message (as seen by non-opers)
439 * @param oper_message The user's quit message (as seen by opers)
441 virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message);
443 /** Called whenever a user's socket is closed.
444 * The details of the exiting user are available to you in the parameter User *user
445 * This event is called for all users, registered or not, as a cleanup method for modules
446 * which might assign resources to user, such as dns lookups, objects and sockets.
447 * @param user The user who is disconnecting
449 virtual void OnUserDisconnect(LocalUser* user);
451 /** Called whenever a channel is about to be deleted
452 * @param chan The channel being deleted
453 * @return An integer specifying whether or not the channel may be deleted. 0 for yes, 1 for no.
455 virtual ModResult OnChannelPreDelete(Channel *chan);
457 /** Called whenever a channel is deleted, either by QUIT, KICK or PART.
458 * @param chan The channel being deleted
460 virtual void OnChannelDelete(Channel* chan);
462 /** Called when a user joins a channel.
463 * The details of the joining user are available to you in the parameter User *user,
464 * and the details of the channel they have joined is available in the variable Channel *channel
465 * @param memb The channel membership being created
466 * @param sync This is set to true if the JOIN is the result of a network sync and the remote user is being introduced
467 * to a channel due to the network sync.
468 * @param created This is true if the join created the channel
469 * @param except_list A list of users not to send to.
471 virtual void OnUserJoin(Membership* memb, bool sync, bool created, CUList& except_list);
473 /** Called after a user joins a channel
474 * Identical to OnUserJoin, but called immediately afterwards, when any linking module has
476 * @param memb The channel membership created
478 virtual void OnPostJoin(Membership* memb);
480 /** Called when a user parts a channel.
481 * The details of the leaving user are available to you in the parameter User *user,
482 * and the details of the channel they have left is available in the variable Channel *channel
483 * @param memb The channel membership being destroyed
484 * @param partmessage The part message, or an empty string (may be modified)
485 * @param except_list A list of users to not send to.
487 virtual void OnUserPart(Membership* memb, std::string &partmessage, CUList& except_list);
489 /** Called on rehash.
490 * This method is called prior to a /REHASH or when a SIGHUP is received from the operating
491 * system. This is called in all cases -- including when this server will not execute the
492 * rehash because it is directed at a remote server.
494 * @param user The user performing the rehash, if any. If this is server initiated, the value of
495 * this variable will be NULL.
496 * @param parameter The (optional) parameter given to REHASH from the user. Empty when server
499 virtual void OnPreRehash(User* user, const std::string ¶meter);
501 /** Called on rehash.
502 * This method is called when a user initiates a module-specific rehash. This can be used to do
503 * expensive operations (such as reloading SSL certificates) that are not executed on a normal
504 * rehash for efficiency. A rehash of this type does not reload the core configuration.
506 * @param user The user performing the rehash.
507 * @param parameter The parameter given to REHASH
509 virtual void OnModuleRehash(User* user, const std::string ¶meter);
511 /** Called on rehash.
512 * This method is called after a rehash has completed. You should use it to reload any module
513 * configuration from the main configuration file.
514 * @param user The user that performed the rehash, if it was initiated by a user and that user
515 * is still connected.
517 virtual void OnRehash(User* user);
519 /** Called whenever a snotice is about to be sent to a snomask.
520 * snomask and type may both be modified; the message may not.
521 * @param snomask The snomask the message is going to (e.g. 'A')
522 * @param type The textual description the snomask will go to (e.g. 'OPER')
523 * @param message The text message to be sent via snotice
524 * @return 1 to block the snotice from being sent entirely, 0 else.
526 virtual ModResult OnSendSnotice(char &snomask, std::string &type, const std::string &message);
528 /** Called whenever a user is about to join a channel, before any processing is done.
529 * Returning a value of 1 from this function stops the process immediately, causing no
530 * output to be sent to the user by the core. If you do this you must produce your own numerics,
531 * notices etc. This is useful for modules which may want to mimic +b, +k, +l etc. Returning -1 from
532 * this function forces the join to be allowed, bypassing restrictions such as banlists, invite, keys etc.
536 * If the user joins a NEW channel which does not exist yet, OnUserPreJoin will be called BEFORE the channel
537 * record is created. This will cause Channel* chan to be NULL. There is very little you can do in form of
538 * processing on the actual channel record at this point, however the channel NAME will still be passed in
539 * char* cname, so that you could for example implement a channel blacklist or whitelist, etc.
540 * @param user The user joining the channel
541 * @param chan If the channel is a new channel, this will be NULL, otherwise it will be a pointer to the channel being joined
542 * @param cname The channel name being joined. For new channels this is valid where chan is not.
543 * @param privs A string containing the users privilages when joining the channel. For new channels this will contain "o".
544 * You may alter this string to alter the user's modes on the channel.
545 * @param keygiven The key given to join the channel, or an empty string if none was provided
546 * @return 1 To prevent the join, 0 to allow it.
548 virtual ModResult OnUserPreJoin(User* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven);
550 /** Called whenever a user is about to be kicked.
551 * Returning a value of 1 from this function stops the process immediately, causing no
552 * output to be sent to the user by the core. If you do this you must produce your own numerics,
554 * @param source The user issuing the kick
555 * @param memb The channel membership of the user who is being kicked.
556 * @param reason The kick reason
557 * @return 1 to prevent the kick, 0 to continue normally, -1 to explicitly allow the kick regardless of normal operation
559 virtual ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason);
561 /** Called whenever a user is kicked.
562 * If this method is called, the kick is already underway and cannot be prevented, so
563 * to prevent a kick, please use Module::OnUserPreKick instead of this method.
564 * @param source The user issuing the kick
565 * @param memb The channel membership of the user who was kicked.
566 * @param reason The kick reason
567 * @param except_list A list of users to not send to.
569 virtual void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except_list);
571 /** Called whenever a user opers locally.
572 * The User will contain the oper mode 'o' as this function is called after any modifications
573 * are made to the user's structure by the core.
574 * @param user The user who is opering up
575 * @param opertype The opers type name
577 virtual void OnOper(User* user, const std::string &opertype);
579 /** Called after a user opers locally.
580 * This is identical to Module::OnOper(), except it is called after OnOper so that other modules
581 * can be gauranteed to already have processed the oper-up, for example m_spanningtree has sent
582 * out the OPERTYPE, etc.
583 * @param user The user who is opering up
584 * @param opername The name of the oper that the user is opering up to. Only valid locally. Empty string otherwise.
585 * @param opertype The opers type name
587 virtual void OnPostOper(User* user, const std::string &opername, const std::string &opertype);
589 /** Called whenever a user types /INFO.
590 * The User will contain the information of the user who typed the command. Modules may use this
591 * method to output their own credits in /INFO (which is the ircd's version of an about box).
592 * It is purposefully not possible to modify any info that has already been output, or halt the list.
593 * You must write a 371 numeric to the user, containing your info in the following format:
595 * <nick> :information here
597 * @param user The user issuing /INFO
599 virtual void OnInfo(User* user);
601 /** Called whenever a /WHOIS is performed on a local user.
602 * The source parameter contains the details of the user who issued the WHOIS command, and
603 * the dest parameter contains the information of the user they are whoising.
604 * @param source The user issuing the WHOIS command
605 * @param dest The user who is being WHOISed
607 virtual void OnWhois(User* source, User* dest);
609 /** Called whenever a user is about to invite another user into a channel, before any processing is done.
610 * Returning 1 from this function stops the process immediately, causing no
611 * output to be sent to the user by the core. If you do this you must produce your own numerics,
612 * notices etc. This is useful for modules which may want to filter invites to channels.
613 * @param source The user who is issuing the INVITE
614 * @param dest The user being invited
615 * @param channel The channel the user is being invited to
616 * @param timeout The time the invite will expire (0 == never)
617 * @return 1 to deny the invite, 0 to check whether or not the user has permission to invite, -1 to explicitly allow the invite
619 virtual ModResult OnUserPreInvite(User* source,User* dest,Channel* channel, time_t timeout);
621 /** Called after a user has been successfully invited to a channel.
622 * You cannot prevent the invite from occuring using this function, to do that,
623 * use OnUserPreInvite instead.
624 * @param source The user who is issuing the INVITE
625 * @param dest The user being invited
626 * @param channel The channel the user is being invited to
627 * @param timeout The time the invite will expire (0 == never)
629 virtual void OnUserInvite(User* source,User* dest,Channel* channel, time_t timeout);
631 /** Called whenever a user is about to PRIVMSG A user or a channel, before any processing is done.
632 * Returning any nonzero value from this function stops the process immediately, causing no
633 * output to be sent to the user by the core. If you do this you must produce your own numerics,
634 * notices etc. This is useful for modules which may want to filter or redirect messages.
635 * target_type can be one of TYPE_USER or TYPE_CHANNEL. If the target_type value is a user,
636 * you must cast dest to a User* otherwise you must cast it to a Channel*, this is the details
637 * of where the message is destined to be sent.
638 * @param user The user sending the message
639 * @param dest The target of the message (Channel* or User*)
640 * @param target_type The type of target (TYPE_USER or TYPE_CHANNEL)
641 * @param text Changeable text being sent by the user
642 * @param status The status being used, e.g. PRIVMSG @#chan has status== '@', 0 to send to everyone.
643 * @param exempt_list A list of users not to send to. For channel messages, this will usually contain just the sender.
644 * It will be ignored for private messages.
645 * @return 1 to deny the message, 0 to allow it
647 virtual ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text,char status, CUList &exempt_list);
649 /** Called whenever a user is about to NOTICE A user or a channel, before any processing is done.
650 * Returning any nonzero value from this function stops the process immediately, causing no
651 * output to be sent to the user by the core. If you do this you must produce your own numerics,
652 * notices etc. This is useful for modules which may want to filter or redirect messages.
653 * target_type can be one of TYPE_USER or TYPE_CHANNEL. If the target_type value is a user,
654 * you must cast dest to a User* otherwise you must cast it to a Channel*, this is the details
655 * of where the message is destined to be sent.
656 * You may alter the message text as you wish before relinquishing control to the next module
657 * in the chain, and if no other modules block the text this altered form of the text will be sent out
658 * to the user and possibly to other servers.
659 * @param user The user sending the message
660 * @param dest The target of the message (Channel* or User*)
661 * @param target_type The type of target (TYPE_USER or TYPE_CHANNEL)
662 * @param text Changeable text being sent by the user
663 * @param status The status being used, e.g. PRIVMSG @#chan has status== '@', 0 to send to everyone.
664 * @param exempt_list A list of users not to send to. For channel notices, this will usually contain just the sender.
665 * It will be ignored for private notices.
666 * @return 1 to deny the NOTICE, 0 to allow it
668 virtual ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text,char status, CUList &exempt_list);
670 /** Called when sending a message to all "neighbors" of a given user -
671 * that is, all users that share a common channel. This is used in
672 * commands such as NICK, QUIT, etc.
673 * @param source The source of the message
674 * @param include_c Channels to scan for users to include
675 * @param exceptions Map of user->bool that overrides the inclusion decision
677 * Set exceptions[user] = true to include, exceptions[user] = false to exclude
679 virtual void OnBuildNeighborList(User* source, UserChanList &include_c, std::map<User*,bool> &exceptions);
681 /** Called before any nickchange, local or remote. This can be used to implement Q-lines etc.
682 * Please note that although you can see remote nickchanges through this function, you should
683 * NOT make any changes to the User if the user is a remote user as this may cause a desnyc.
684 * check user->server before taking any action (including returning nonzero from the method).
685 * If your method returns nonzero, the nickchange is silently forbidden, and it is down to your
686 * module to generate some meaninful output.
687 * @param user The username changing their nick
688 * @param newnick Their new nickname
689 * @return 1 to deny the change, 0 to allow
691 virtual ModResult OnUserPreNick(User* user, const std::string &newnick);
693 /** Called after any PRIVMSG sent from a user.
694 * The dest variable contains a User* if target_type is TYPE_USER and a Channel*
695 * if target_type is TYPE_CHANNEL.
696 * @param user The user sending the message
697 * @param dest The target of the message
698 * @param target_type The type of target (TYPE_USER or TYPE_CHANNEL)
699 * @param text the text being sent by the user
700 * @param status The status being used, e.g. PRIVMSG @#chan has status== '@', 0 to send to everyone.
701 * @param exempt_list A list of users to not send to.
703 virtual void OnUserMessage(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list);
705 /** Called after any NOTICE sent from a user.
706 * The dest variable contains a User* if target_type is TYPE_USER and a Channel*
707 * if target_type is TYPE_CHANNEL.
708 * @param user The user sending the message
709 * @param dest The target of the message
710 * @param target_type The type of target (TYPE_USER or TYPE_CHANNEL)
711 * @param text the text being sent by the user
712 * @param status The status being used, e.g. NOTICE @#chan has status== '@', 0 to send to everyone.
713 * @param exempt_list A list of users to not send to.
715 virtual void OnUserNotice(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list);
717 /** Called immediately before any NOTICE or PRIVMSG sent from a user, local or remote.
718 * The dest variable contains a User* if target_type is TYPE_USER and a Channel*
719 * if target_type is TYPE_CHANNEL.
720 * The difference between this event and OnUserPreNotice/OnUserPreMessage is that delivery is gauranteed,
721 * the message has already been vetted. In the case of the other two methods, a later module may stop your
722 * message. This also differs from OnUserMessage which occurs AFTER the message has been sent.
723 * @param user The user sending the message
724 * @param dest The target of the message
725 * @param target_type The type of target (TYPE_USER or TYPE_CHANNEL)
726 * @param text the text being sent by the user
727 * @param status The status being used, e.g. NOTICE @#chan has status== '@', 0 to send to everyone.
728 * @param exempt_list A list of users not to send to. For channel messages, this will usually contain just the sender.
730 virtual void OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list);
732 /** Called after every MODE command sent from a user
733 * The dest variable contains a User* if target_type is TYPE_USER and a Channel*
734 * if target_type is TYPE_CHANNEL. The text variable contains the remainder of the
735 * mode string after the target, e.g. "+wsi" or "+ooo nick1 nick2 nick3".
736 * @param user The user sending the MODEs
737 * @param dest The target of the modes (User* or Channel*)
738 * @param target_type The type of target (TYPE_USER or TYPE_CHANNEL)
739 * @param text The actual modes and their parameters if any
740 * @param translate The translation types of the mode parameters
742 virtual void OnMode(User* user, void* dest, int target_type, const std::vector<std::string> &text, const std::vector<TranslateType> &translate);
744 /** Allows modules to alter or create server descriptions
745 * Whenever a module requires a server description, for example for display in
746 * WHOIS, this function is called in all modules. You may change or define the
747 * description given in std::string &description. If you do, this description
748 * will be shown in the WHOIS fields.
749 * @param servername The servername being searched for
750 * @param description Alterable server description for this server
752 virtual void OnGetServerDescription(const std::string &servername,std::string &description);
754 /** Allows modules to synchronize data which relates to users during a netburst.
755 * When this function is called, it will be called from the module which implements
756 * the linking protocol. This currently is m_spanningtree.so. A pointer to this module
757 * is given in Module* proto, so that you may call its methods such as ProtoSendMode
758 * (see below). This function will be called for every user visible on your side
759 * of the burst, allowing you to for example set modes, etc. Do not use this call to
760 * synchronize data which you have stored using class Extensible -- There is a specialist
761 * function OnSyncUserMetaData and OnSyncChannelMetaData for this!
762 * @param user The user being syncronized
763 * @param proto A pointer to the module handling network protocol
764 * @param opaque An opaque pointer set by the protocol module, should not be modified!
766 virtual void OnSyncUser(User* user, Module* proto, void* opaque);
768 /** Allows modules to synchronize data which relates to channels during a netburst.
769 * When this function is called, it will be called from the module which implements
770 * the linking protocol. This currently is m_spanningtree.so. A pointer to this module
771 * is given in Module* proto, so that you may call its methods such as ProtoSendMode
772 * (see below). This function will be called for every user visible on your side
773 * of the burst, allowing you to for example set modes, etc.
775 * @param chan The channel being syncronized
776 * @param proto A pointer to the module handling network protocol
777 * @param opaque An opaque pointer set by the protocol module, should not be modified!
779 virtual void OnSyncChannel(Channel* chan, Module* proto, void* opaque);
781 /* Allows modules to syncronize metadata not related to users or channels, over the network during a netburst.
782 * Whenever the linking module wants to send out data, but doesnt know what the data
783 * represents (e.g. it is Extensible metadata, added to a User or Channel by a module) then
784 * this method is called. You should use the ProtoSendMetaData function after you've
785 * correctly decided how the data should be represented, to send the metadata on its way if
786 * if it belongs to your module.
787 * @param proto A pointer to the module handling network protocol
788 * @param opaque An opaque pointer set by the protocol module, should not be modified!
789 * @param displayable If this value is true, the data is going to be displayed to a user,
790 * and not sent across the network. Use this to determine wether or not to show sensitive data.
792 virtual void OnSyncNetwork(Module* proto, void* opaque);
794 /** Allows module data, sent via ProtoSendMetaData, to be decoded again by a receiving module.
795 * Please see src/modules/m_swhois.cpp for a working example of how to use this method call.
796 * @param target The Channel* or User* that data should be added to
797 * @param extname The extension name which is being sent
798 * @param extdata The extension data, encoded at the other end by an identical module through OnSyncChannelMetaData or OnSyncUserMetaData
800 virtual void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata);
802 /** Implemented by modules which provide the ability to link servers.
803 * These modules will implement this method, which allows transparent sending of servermodes
804 * down the network link as a broadcast, without a module calling it having to know the format
805 * of the MODE command before the actual mode string.
807 * @param opaque An opaque pointer set by the protocol module, should not be modified!
808 * @param target_type The type of item to decode data for, TYPE_USER or TYPE_CHANNEL
809 * @param target The Channel* or User* that modes should be sent for
810 * @param modeline The modes and parameters to be sent
811 * @param translate The translation types of the mode parameters
813 virtual void ProtoSendMode(void* opaque, TargetTypeFlags target_type, void* target, const std::vector<std::string> &modeline, const std::vector<TranslateType> &translate);
815 /** Implemented by modules which provide the ability to link servers.
816 * These modules will implement this method, which allows metadata (extra data added to
817 * user and channel records using class Extensible, Extensible::Extend, etc) to be sent
818 * to other servers on a netburst and decoded at the other end by the same module on a
821 * More documentation to follow soon. Please see src/modules/m_swhois.cpp for example of
822 * how to use this function.
823 * @param opaque An opaque pointer set by the protocol module, should not be modified!
824 * @param target The Channel* or User* that metadata should be sent for
825 * @param extname The extension name to send metadata for
826 * @param extdata Encoded data for this extension name, which will be encoded at the oppsite end by an identical module using OnDecodeMetaData
828 virtual void ProtoSendMetaData(void* opaque, Extensible* target, const std::string &extname, const std::string &extdata);
830 /** Called after every WALLOPS command.
831 * @param user The user sending the WALLOPS
832 * @param text The content of the WALLOPS message
834 virtual void OnWallops(User* user, const std::string &text);
836 /** Called whenever a user's hostname is changed.
837 * This event triggers after the host has been set.
838 * @param user The user whos host is being changed
839 * @param newhost The new hostname being set
841 virtual void OnChangeHost(User* user, const std::string &newhost);
843 /** Called whenever a user's GECOS (realname) is changed.
844 * This event triggers after the name has been set.
845 * @param user The user who's GECOS is being changed
846 * @param gecos The new GECOS being set on the user
848 virtual void OnChangeName(User* user, const std::string &gecos);
850 /** Called whenever a user's IDENT is changed.
851 * This event triggers after the name has been set.
852 * @param user The user who's IDENT is being changed
853 * @param ident The new IDENT being set on the user
855 virtual void OnChangeIdent(User* user, const std::string &ident);
857 /** Called whenever an xline is added by a local user.
858 * This method is triggered after the line is added.
859 * @param source The sender of the line or NULL for local server
860 * @param line The xline being added
862 virtual void OnAddLine(User* source, XLine* line);
864 /** Called whenever an xline is deleted MANUALLY. See OnExpireLine for expiry.
865 * This method is triggered after the line is deleted.
866 * @param source The user removing the line or NULL for local server
867 * @param line the line being deleted
869 virtual void OnDelLine(User* source, XLine* line);
871 /** Called whenever an xline expires.
872 * This method is triggered after the line is deleted.
873 * @param line The line being deleted.
875 virtual void OnExpireLine(XLine *line);
877 /** Called before your module is unloaded to clean up Extensibles.
878 * This method is called once for every user and channel on the network,
879 * so that when your module unloads it may clear up any remaining data
880 * in the form of Extensibles added using Extensible::Extend().
881 * If the target_type variable is TYPE_USER, then void* item refers to
882 * a User*, otherwise it refers to a Channel*.
883 * @param target_type The type of item being cleaned
884 * @param item A pointer to the item's class
886 virtual void OnCleanup(int target_type, void* item);
888 /** Called after any nickchange, local or remote. This can be used to track users after nickchanges
889 * have been applied. Please note that although you can see remote nickchanges through this function, you should
890 * NOT make any changes to the User if the user is a remote user as this may cause a desnyc.
891 * check user->server before taking any action (including returning nonzero from the method).
892 * Because this method is called after the nickchange is taken place, no return values are possible
893 * to indicate forbidding of the nick change. Use OnUserPreNick for this.
894 * @param user The user changing their nick
895 * @param oldnick The old nickname of the user before the nickchange
897 virtual void OnUserPostNick(User* user, const std::string &oldnick);
899 /** Called before any mode change, to allow a single access check for
900 * a full mode change (use OnRawMode to check individual modes)
902 * Returning MOD_RES_ALLOW will skip prefix level checks, but can be overridden by
903 * OnRawMode for each individual mode
905 * @param source the user making the mode change
906 * @param dest the user destination of the umode change (NULL if a channel mode)
907 * @param channel the channel destination of the mode change
908 * @param parameters raw mode parameters; parameters[0] is the user/channel being changed
910 virtual ModResult OnPreMode(User* source, User* dest, Channel* channel, const std::vector<std::string>& parameters);
912 /** Called when a 005 numeric is about to be output.
913 * The module should modify the 005 numeric if needed to indicate its features.
914 * @param output The 005 map to be modified if neccessary.
916 virtual void On005Numeric(std::map<std::string, std::string>& tokens);
918 /** Called when a client is disconnected by KILL.
919 * If a client is killed by a server, e.g. a nickname collision or protocol error,
921 * Return 1 from this function to prevent the kill, and 0 from this function to allow
922 * it as normal. If you prevent the kill no output will be sent to the client, it is
923 * down to your module to generate this information.
924 * NOTE: It is NOT advisable to stop kills which originate from servers or remote users.
925 * If you do so youre risking race conditions, desyncs and worse!
926 * @param source The user sending the KILL
927 * @param dest The user being killed
928 * @param reason The kill reason
929 * @return 1 to prevent the kill, 0 to allow
931 virtual ModResult OnKill(User* source, User* dest, const std::string &reason);
933 /** Called when an oper wants to disconnect a remote user via KILL
934 * @param source The user sending the KILL
935 * @param dest The user being killed
936 * @param reason The kill reason
937 * @param operreason The oper kill reason
939 virtual void OnRemoteKill(User* source, User* dest, const std::string &reason, const std::string &operreason);
941 /** Called whenever a module is loaded.
942 * mod will contain a pointer to the module, and string will contain its name,
943 * for example m_widgets.so. This function is primary for dependency checking,
944 * your module may decide to enable some extra features if it sees that you have
945 * for example loaded "m_killwidgets.so" with "m_makewidgets.so". It is highly
946 * recommended that modules do *NOT* bail if they cannot satisfy dependencies,
947 * but instead operate under reduced functionality, unless the dependency is
948 * absolutely neccessary (e.g. a module that extends the features of another
950 * @param mod A pointer to the new module
952 virtual void OnLoadModule(Module* mod);
954 /** Called whenever a module is unloaded.
955 * mod will contain a pointer to the module, and string will contain its name,
956 * for example m_widgets.so. This function is primary for dependency checking,
957 * your module may decide to enable some extra features if it sees that you have
958 * for example loaded "m_killwidgets.so" with "m_makewidgets.so". It is highly
959 * recommended that modules do *NOT* bail if they cannot satisfy dependencies,
960 * but instead operate under reduced functionality, unless the dependency is
961 * absolutely neccessary (e.g. a module that extends the features of another
963 * @param mod Pointer to the module being unloaded (still valid)
965 virtual void OnUnloadModule(Module* mod);
967 /** Called once every five seconds for background processing.
968 * This timer can be used to control timed features. Its period is not accurate
969 * enough to be used as a clock, but it is gauranteed to be called at least once in
970 * any five second period, directly from the main loop of the server.
971 * @param curtime The current timer derived from time(2)
973 virtual void OnBackgroundTimer(time_t curtime);
975 /** Called whenever any command is about to be executed.
976 * This event occurs for all registered commands, wether they are registered in the core,
977 * or another module, and for invalid commands. Invalid commands may only be sent to this
978 * function when the value of validated is false. By returning 1 from this method you may prevent the
979 * command being executed. If you do this, no output is created by the core, and it is
980 * down to your module to produce any output neccessary.
981 * Note that unless you return 1, you should not destroy any structures (e.g. by using
982 * InspIRCd::QuitUser) otherwise when the command's handler function executes after your
983 * method returns, it will be passed an invalid pointer to the user object and crash!)
984 * @param command The command being executed
985 * @param parameters An array of array of characters containing the parameters for the command
986 * @param user the user issuing the command
987 * @param validated True if the command has passed all checks, e.g. it is recognised, has enough parameters, the user has permission to execute it, etc.
988 * You should only change the parameter list and command string if validated == false (e.g. before the command lookup occurs).
989 * @param original_line The entire original line as passed to the parser from the user
990 * @return 1 to block the command, 0 to allow
992 virtual ModResult OnPreCommand(std::string &command, std::vector<std::string>& parameters, LocalUser *user, bool validated, const std::string &original_line);
994 /** Called after any command has been executed.
995 * This event occurs for all registered commands, wether they are registered in the core,
996 * or another module, but it will not occur for invalid commands (e.g. ones which do not
997 * exist within the command table). The result code returned by the command handler is
999 * @param command The command being executed
1000 * @param parameters An array of array of characters containing the parameters for the command
1001 * @param user the user issuing the command
1002 * @param result The return code given by the command handler, one of CMD_SUCCESS or CMD_FAILURE
1003 * @param original_line The entire original line as passed to the parser from the user
1005 virtual void OnPostCommand(const std::string &command, const std::vector<std::string>& parameters, LocalUser *user, CmdResult result, const std::string &original_line);
1007 /** Called when a user is first connecting, prior to starting DNS lookups, checking initial
1008 * connect class, or accepting any commands.
1010 virtual void OnUserInit(LocalUser* user);
1012 /** Called to check if a user who is connecting can now be allowed to register
1013 * If any modules return false for this function, the user is held in the waiting
1014 * state until all modules return true. For example a module which implements ident
1015 * lookups will continue to return false for a user until their ident lookup is completed.
1016 * Note that the registration timeout for a user overrides these checks, if the registration
1017 * timeout is reached, the user is disconnected even if modules report that the user is
1018 * not ready to connect.
1019 * @param user The user to check
1020 * @return true to indicate readiness, false if otherwise
1022 virtual ModResult OnCheckReady(LocalUser* user);
1024 /** Called whenever a user is about to register their connection (e.g. before the user
1025 * is sent the MOTD etc). Modules can use this method if they are performing a function
1026 * which must be done before the actual connection is completed (e.g. ident lookups,
1027 * dnsbl lookups, etc).
1028 * Note that you should NOT delete the user record here by causing a disconnection!
1029 * Use OnUserConnect for that instead.
1030 * @param user The user registering
1031 * @return 1 to indicate user quit, 0 to continue
1033 virtual ModResult OnUserRegister(LocalUser* user);
1035 /** Called whenever a user joins a channel, to determine if invite checks should go ahead or not.
1036 * This method will always be called for each join, wether or not the channel is actually +i, and
1037 * determines the outcome of an if statement around the whole section of invite checking code.
1038 * return 1 to explicitly allow the join to go ahead or 0 to ignore the event.
1039 * @param user The user joining the channel
1040 * @param chan The channel being joined
1041 * @return 1 to explicitly allow the join, 0 to proceed as normal
1043 virtual ModResult OnCheckInvite(User* user, Channel* chan);
1045 /** Called whenever a mode character is processed.
1046 * Return 1 from this function to block the mode character from being processed entirely.
1047 * @param user The user who is sending the mode
1048 * @param chan The channel the mode is being sent to (or NULL if a usermode)
1049 * @param mode The mode character being set
1050 * @param param The parameter for the mode or an empty string
1051 * @param adding true of the mode is being added, false if it is being removed
1052 * @param pcnt The parameter count for the mode (0 or 1)
1053 * @return ACR_DENY to deny the mode, ACR_DEFAULT to do standard mode checking, and ACR_ALLOW
1054 * to skip all permission checking. Please note that for remote mode changes, your return value
1057 virtual ModResult OnRawMode(User* user, Channel* chan, const char mode, const std::string ¶m, bool adding, int pcnt);
1059 /** Called whenever a user joins a channel, to determine if key checks should go ahead or not.
1060 * This method will always be called for each join, wether or not the channel is actually +k, and
1061 * determines the outcome of an if statement around the whole section of key checking code.
1062 * if the user specified no key, the keygiven string will be a valid but empty value.
1063 * return 1 to explicitly allow the join to go ahead or 0 to ignore the event.
1064 * @param user The user joining the channel
1065 * @param chan The channel being joined
1066 * @param keygiven The key given on joining the channel.
1067 * @return 1 to explicitly allow the join, 0 to proceed as normal
1069 virtual ModResult OnCheckKey(User* user, Channel* chan, const std::string &keygiven);
1071 /** Called whenever a user joins a channel, to determine if channel limit checks should go ahead or not.
1072 * This method will always be called for each join, wether or not the channel is actually +l, and
1073 * determines the outcome of an if statement around the whole section of channel limit checking code.
1074 * return 1 to explicitly allow the join to go ahead or 0 to ignore the event.
1075 * @param user The user joining the channel
1076 * @param chan The channel being joined
1077 * @return 1 to explicitly allow the join, 0 to proceed as normal
1079 virtual ModResult OnCheckLimit(User* user, Channel* chan);
1082 * Checks for a user's ban from the channel
1083 * @param user The user to check
1084 * @param chan The channel to check in
1085 * @return MOD_RES_DENY to mark as banned, MOD_RES_ALLOW to skip the
1086 * ban check, or MOD_RES_PASSTHRU to check bans normally
1088 virtual ModResult OnCheckChannelBan(User* user, Channel* chan);
1091 * Checks for a user's match of a single ban
1092 * @param user The user to check for match
1093 * @param chan The channel on which the match is being checked
1094 * @param mask The mask being checked
1095 * @return MOD_RES_DENY to mark as banned, MOD_RES_ALLOW to skip the
1096 * ban check, or MOD_RES_PASSTHRU to check bans normally
1098 virtual ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask);
1100 /** Checks for a match on a given extban type
1101 * @return MOD_RES_DENY to mark as banned, MOD_RES_ALLOW to skip the
1102 * ban check, or MOD_RES_PASSTHRU to check bans normally
1104 virtual ModResult OnExtBanCheck(User* user, Channel* chan, char type);
1106 /** Called on all /STATS commands
1107 * This method is triggered for all /STATS use, including stats symbols handled by the core.
1108 * @param symbol the symbol provided to /STATS
1109 * @param user the user issuing the /STATS command
1110 * @param results A string_list to append results into. You should put all your results
1111 * into this string_list, rather than displaying them directly, so that your handler will
1112 * work when remote STATS queries are received.
1113 * @return 1 to block the /STATS from being processed by the core, 0 to allow it
1115 virtual ModResult OnStats(char symbol, User* user, string_list &results);
1117 /** Called whenever a change of a local users displayed host is attempted.
1118 * Return 1 to deny the host change, or 0 to allow it.
1119 * @param user The user whos host will be changed
1120 * @param newhost The new hostname
1121 * @return 1 to deny the host change, 0 to allow
1123 virtual ModResult OnChangeLocalUserHost(LocalUser* user, const std::string &newhost);
1125 /** Called whenever a change of a local users GECOS (fullname field) is attempted.
1126 * return 1 to deny the name change, or 0 to allow it.
1127 * @param user The user whos GECOS will be changed
1128 * @param newhost The new GECOS
1129 * @return 1 to deny the GECOS change, 0 to allow
1131 virtual ModResult OnChangeLocalUserGECOS(LocalUser* user, const std::string &newhost);
1133 /** Called before a topic is changed.
1134 * Return 1 to deny the topic change, 0 to check details on the change, -1 to let it through with no checks
1135 * As with other 'pre' events, you should only ever block a local event.
1136 * @param user The user changing the topic
1137 * @param chan The channels who's topic is being changed
1138 * @param topic The actual topic text
1139 * @return 1 to block the topic change, 0 to allow
1141 virtual ModResult OnPreTopicChange(User* user, Channel* chan, const std::string &topic);
1143 /** Called whenever a topic has been changed.
1144 * To block topic changes you must use OnPreTopicChange instead.
1145 * @param user The user changing the topic
1146 * @param chan The channels who's topic is being changed
1147 * @param topic The actual topic text
1149 virtual void OnPostTopicChange(User* user, Channel* chan, const std::string &topic);
1151 /** Called whenever an Event class is sent to all modules by another module.
1152 * You should *always* check the value of Event::id to determine the event type.
1153 * @param event The Event class being received
1155 virtual void OnEvent(Event& event);
1157 /** Called whenever a Request class is sent to your module by another module.
1158 * The value of Request::id should be used to determine the type of request.
1159 * @param request The Request class being received
1161 virtual void OnRequest(Request& request);
1163 /** Called whenever a password check is to be made. Replaces the old OldOperCompare API.
1164 * The password field (from the config file) is in 'password' and is to be compared against
1165 * 'input'. This method allows for encryption of passwords (oper, connect:allow, die/restart, etc).
1166 * You should return a nonzero value to override the normal comparison, or zero to pass it on.
1167 * @param ex The object that's causing the authentication (User* for \<oper> \<connect:allow> etc, Server* for \<link>).
1168 * @param password The password from the configuration file (the password="" value).
1169 * @param input The password entered by the user or whoever.
1170 * @param hashtype The hash value from the config
1171 * @return 0 to do nothing (pass on to next module/default), 1 == password is OK, -1 == password is not OK
1173 virtual ModResult OnPassCompare(Extensible* ex, const std::string &password, const std::string &input, const std::string& hashtype);
1175 /** Called whenever a user is given usermode +o, anywhere on the network.
1176 * You cannot override this and prevent it from happening as it is already happened and
1177 * such a task must be performed by another server. You can however bounce modes by sending
1178 * servermodes out to reverse mode changes.
1179 * @param user The user who is opering
1181 virtual void OnGlobalOper(User* user);
1183 /** Called after a user has fully connected and all modules have executed OnUserConnect
1184 * This event is informational only. You should not change any user information in this
1185 * event. To do so, use the OnUserConnect method to change the state of local users.
1186 * This is called for both local and remote users.
1187 * @param user The user who is connecting
1189 virtual void OnPostConnect(User* user);
1191 /** Called to install an I/O hook on an event handler
1192 * @param user The socket to possibly install the I/O hook on
1193 * @param via The port that the user connected on
1195 virtual void OnHookIO(StreamSocket* user, ListenSocket* via);
1197 /** Called when a port accepts a connection
1198 * Return MOD_RES_ACCEPT if you have used the file descriptor.
1199 * @param fd The file descriptor returned from accept()
1200 * @param sock The socket connection for the new user
1201 * @param client The client IP address and port
1202 * @param server The server IP address and port
1204 virtual ModResult OnAcceptConnection(int fd, ListenSocket* sock, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
1206 /** Called immediately after any connection is accepted. This is intended for raw socket
1207 * processing (e.g. modules which wrap the tcp connection within another library) and provides
1208 * no information relating to a user record as the connection has not been assigned yet.
1209 * There are no return values from this call as all modules get an opportunity if required to
1210 * process the connection.
1211 * @param sock The socket in question
1212 * @param client The client IP address and port
1213 * @param server The server IP address and port
1215 virtual void OnStreamSocketAccept(StreamSocket* sock, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
1218 * Called when a hooked stream has data to write, or when the socket
1219 * engine returns it as writable
1220 * @param sock The socket in question
1221 * @param sendq Data to send to the socket
1222 * @return 1 if the sendq has been completely emptied, 0 if there is
1223 * still data to send, and -1 if there was an error
1225 virtual int OnStreamSocketWrite(StreamSocket* sock, std::string& sendq);
1227 /** Called immediately before any socket is closed. When this event is called, shutdown()
1228 * has not yet been called on the socket.
1229 * @param sock The socket in question
1231 virtual void OnStreamSocketClose(StreamSocket* sock);
1233 /** Called immediately upon connection of an outbound BufferedSocket which has been hooked
1235 * @param sock The socket in question
1237 virtual void OnStreamSocketConnect(StreamSocket* sock);
1240 * Called when the stream socket has data to read
1241 * @param sock The socket that is ready
1242 * @param recvq The receive queue that new data should be appended to
1243 * @return 1 if new data has been read, 0 if no new data is ready (but the
1244 * socket is still connected), -1 if there was an error or close
1246 virtual int OnStreamSocketRead(StreamSocket* sock, std::string& recvq);
1248 /** Called whenever a user sets away or returns from being away.
1249 * The away message is available as a parameter, but should not be modified.
1250 * At this stage, it has already been copied into the user record.
1251 * If awaymsg is empty, the user is returning from away.
1252 * @param user The user setting away
1253 * @param awaymsg The away message of the user, or empty if returning from away
1254 * @return nonzero if the away message should be blocked - should ONLY be nonzero for LOCAL users (IS_LOCAL) (no output is returned by core)
1256 virtual ModResult OnSetAway(User* user, const std::string &awaymsg);
1258 /** Called whenever a line of WHOIS output is sent to a user.
1259 * You may change the numeric and the text of the output by changing
1260 * the values numeric and text, but you cannot change the user the
1261 * numeric is sent to. You may however change the user's User values.
1262 * @param user The user the numeric is being sent to
1263 * @param dest The user being WHOISed
1264 * @param numeric The numeric of the line being sent
1265 * @param text The text of the numeric, including any parameters
1266 * @return nonzero to drop the line completely so that the user does not
1267 * receive it, or zero to allow the line to be sent.
1269 virtual ModResult OnWhoisLine(User* user, User* dest, int &numeric, std::string &text);
1271 /** Called at intervals for modules to garbage-collect any hashes etc.
1272 * Certain data types such as hash_map 'leak' buckets, which must be
1273 * tidied up and freed by copying into a new item every so often. This
1274 * method is called when it is time to do that.
1276 virtual void OnGarbageCollect();
1278 /** Called when a user's connect class is being matched
1279 * @return MOD_RES_ALLOW to force the class to match, MOD_RES_DENY to forbid it, or
1280 * MOD_RES_PASSTHRU to allow normal matching (by host/port).
1282 virtual ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass);
1284 /** Add test suite hooks here. These are used for testing functionality of a module
1285 * via the --testsuite debugging parameter.
1287 virtual void OnRunTestSuite();
1289 /** Called for every item in a NAMES list, so that modules may reformat portions of it as they see fit.
1290 * For example NAMESX, channel mode +u and +I, and UHNAMES. If the nick is set to an empty string by any
1291 * module, then this will cause the nickname not to be displayed at all.
1293 virtual void OnNamesListItem(User* issuer, Membership* item, std::string &prefixes, std::string &nick);
1295 virtual ModResult OnNumeric(User* user, unsigned int numeric, const std::string &text);
1297 /** Called whenever a result from /WHO is about to be returned
1298 * @param source The user running the /WHO query
1299 * @param params The parameters to the /WHO query
1300 * @param user The user that this line of the query is about
1301 * @param line The raw line to send; modifiable, if empty no line will be returned.
1303 virtual void OnSendWhoLine(User* source, const std::vector<std::string>& params, User* user, std::string& line);
1305 /** Called whenever a local user's IP is set for the first time, or when a local user's IP changes due to
1306 * a module like m_cgiirc changing it.
1307 * @param user The user whose IP is being set
1309 virtual void OnSetUserIP(LocalUser* user);
1312 /** Caches a text file into memory and can be used to retrieve lines from it.
1313 * This class contains methods for read-only manipulation of a text file in memory.
1314 * Either use the constructor type with one parameter to load a file into memory
1315 * at construction, or use the LoadFile method to load a file.
1317 class CoreExport FileReader : public classbase
1319 /** The file contents
1321 std::vector<std::string> fc;
1323 /** Content size in bytes
1325 unsigned long contentsize;
1327 /** Calculate content size in bytes
1332 /** Default constructor.
1333 * This method does not load any file into memory, you must use the LoadFile method
1334 * after constructing the class this way.
1338 /** Secondary constructor.
1339 * This method initialises the class with a file loaded into it ready for GetLine and
1340 * and other methods to be called. If the file could not be loaded, FileReader::FileSize
1343 FileReader(const std::string &filename);
1345 /** Default destructor.
1346 * This deletes the memory allocated to the file.
1350 /** Used to load a file.
1351 * This method loads a file into the class ready for GetLine and
1352 * and other methods to be called. If the file could not be loaded, FileReader::FileSize
1355 void LoadFile(const std::string &filename);
1357 /** Returns the whole content of the file as std::string
1359 std::string Contents();
1361 /** Returns the entire size of the file as std::string
1363 unsigned long ContentSize();
1365 /** Returns true if the file exists
1366 * This function will return false if the file could not be opened.
1370 /** Retrieve one line from the file.
1371 * This method retrieves one line from the text file. If an empty non-NULL string is returned,
1372 * the index was out of bounds, or the line had no data on it.
1374 std::string GetLine(int x);
1376 /** Returns the size of the file in lines.
1377 * This method returns the number of lines in the read file. If it is 0, no lines have been
1378 * read into memory, either because the file is empty or it does not exist, or cannot be
1379 * opened due to permission problems.
1384 /** A list of modules
1386 typedef std::vector<Module*> IntModuleList;
1388 /** An event handler iterator
1390 typedef IntModuleList::iterator EventHandlerIter;
1392 /** ModuleManager takes care of all things module-related
1395 class CoreExport ModuleManager
1398 /** Holds a string describing the last module error to occur
1400 std::string LastModuleError;
1402 /** Total number of modules loaded into the ircd
1406 /** List of loaded modules and shared object/dll handles
1407 * keyed by module name
1409 std::map<std::string, Module*> Modules;
1415 } prioritizationState;
1417 /** Internal unload module hook */
1418 bool CanUnload(Module*);
1421 /** Event handler hooks.
1422 * This needs to be public to be used by FOREACH_MOD and friends.
1424 IntModuleList EventHandlers[I_END];
1426 /** List of data services keyed by name */
1427 std::multimap<std::string, ServiceProvider*> DataProviders;
1429 /** Simple, bog-standard, boring constructor.
1437 /** Change the priority of one event in a module.
1438 * Each module event has a list of modules which are attached to that event type.
1439 * If you wish to be called before or after other specific modules, you may use this
1440 * method (usually within void Module::Prioritize()) to set your events priority.
1441 * You may use this call in other methods too, however, this is not supported behaviour
1443 * @param mod The module to change the priority of
1444 * @param i The event to change the priority of
1445 * @param s The state you wish to use for this event. Use one of
1446 * PRIO_FIRST to set the event to be first called, PRIO_LAST to
1447 * set it to be the last called, or PRIO_BEFORE and PRIORITY_AFTER
1448 * to set it to be before or after one or more other modules.
1449 * @param which If PRIO_BEFORE or PRIORITY_AFTER is set in parameter 's',
1450 * then this contains a the module that your module must be placed before
1453 bool SetPriority(Module* mod, Implementation i, Priority s, Module* which = NULL);
1455 /** Backwards compat interface */
1456 inline bool SetPriority(Module* mod, Implementation i, Priority s, Module** dptr)
1458 return SetPriority(mod, i, s, *dptr);
1461 /** Change the priority of all events in a module.
1462 * @param mod The module to set the priority of
1463 * @param s The priority of all events in the module.
1464 * Note that with this method, it is not possible to effectively use
1465 * PRIO_BEFORE or PRIORITY_AFTER, you should use the more fine tuned
1466 * SetPriority method for this, where you may specify other modules to
1467 * be prioritized against.
1469 bool SetPriority(Module* mod, Priority s);
1471 /** Attach an event to a module.
1472 * You may later detatch the event with ModuleManager::Detach().
1473 * If your module is unloaded, all events are automatically detatched.
1474 * @param i Event type to attach
1475 * @param mod Module to attach event to
1476 * @return True if the event was attached
1478 bool Attach(Implementation i, Module* mod);
1480 /** Detatch an event from a module.
1481 * This is not required when your module unloads, as the core will
1482 * automatically detatch your module from all events it is attached to.
1483 * @param i Event type to detach
1484 * @param mod Module to detach event from
1485 * @return True if the event was detached
1487 bool Detach(Implementation i, Module* mod);
1489 /** Attach an array of events to a module
1490 * @param i Event types (array) to attach
1491 * @param mod Module to attach events to
1492 * @param sz The size of the implementation array
1494 void Attach(Implementation* i, Module* mod, size_t sz);
1496 /** Detach all events from a module (used on unload)
1497 * @param mod Module to detach from
1499 void DetachAll(Module* mod);
1501 /** Returns text describing the last module error
1502 * @return The last error message to occur
1504 std::string& LastError();
1506 /** Load a given module file
1507 * @param filename The file to load
1508 * @param defer Defer module init (loading many modules)
1509 * @return True if the module was found and loaded
1511 bool Load(const std::string& filename, bool defer = false);
1513 /** Unload a given module file. Note that the module will not be
1514 * completely gone until the cull list has finished processing.
1516 * @return true on success; if false, LastError will give a reason
1518 bool Unload(Module* module);
1520 /** Run an asynchronous reload of the given module. When the reload is
1521 * complete, the callback will be run with true if the reload succeeded
1522 * and false if it did not.
1524 void Reload(Module* module, HandlerBase1<void, bool>* callback);
1526 /** Called by the InspIRCd constructor to load all modules from the config file.
1530 void DoSafeUnload(Module*);
1532 /** Get the total number of currently loaded modules
1533 * @return The number of loaded modules
1537 return this->ModCount;
1540 /** Find a module by name, and return a Module* to it.
1541 * This is preferred over iterating the module lists yourself.
1542 * @param name The module name to look up
1543 * @return A pointer to the module, or NULL if the module cannot be found
1545 Module* Find(const std::string &name);
1547 /** Register a service provided by a module */
1548 void AddService(ServiceProvider&);
1550 /** Unregister a service provided by a module */
1551 void DelService(ServiceProvider&);
1553 inline void AddServices(ServiceProvider** list, int count)
1555 for(int i=0; i < count; i++)
1556 AddService(*list[i]);
1559 /** Find a service by name.
1560 * If multiple modules provide a given service, the first one loaded will be chosen.
1562 ServiceProvider* FindService(ServiceType Type, const std::string& name);
1564 template<typename T> inline T* FindDataService(const std::string& name)
1566 return static_cast<T*>(FindService(SERVICE_DATA, name));
1569 /** Return a list of all modules matching the given filter
1570 * @param filter This int is a bitmask of flags set in Module::Flags,
1571 * such as VF_VENDOR or VF_STATIC. If you wish to receive a list of
1572 * all modules with no filtering, set this to 0.
1573 * @return The list of module names
1575 const std::vector<std::string> GetAllModuleNames(int filter);
1578 /** Do not mess with these functions unless you know the C preprocessor
1579 * well enough to explain why they are needed. The order is important.
1581 #define MODULE_INIT_STR MODULE_INIT_STR_FN_2(MODULE_INIT_SYM)
1582 #define MODULE_INIT_STR_FN_2(x) MODULE_INIT_STR_FN_1(x)
1583 #define MODULE_INIT_STR_FN_1(x) #x
1584 #define MODULE_INIT_SYM MODULE_INIT_SYM_FN_2(INSPIRCD_VERSION_MAJ, INSPIRCD_VERSION_API)
1585 #define MODULE_INIT_SYM_FN_2(x,y) MODULE_INIT_SYM_FN_1(x,y)
1586 #define MODULE_INIT_SYM_FN_1(x,y) inspircd_module_ ## x ## _ ## y
1590 struct AllCommandList {
1591 typedef Command* (*fn)(Module*);
1592 AllCommandList(fn cmd);
1594 #define COMMAND_INIT(x) static Command* MK_ ## x(Module* m) { return new x(m); } \
1595 static const AllCommandList PREP_ ## x(&MK_ ## x);
1597 struct AllModuleList {
1598 typedef Module* (*fn)();
1601 AllModuleList(fn mod, const std::string& Name);
1604 #define MODULE_INIT(x) static Module* MK_ ## x() { return new x; } \
1605 static const AllModuleList PREP_ ## x(&MK_ ## x, MODNAMESTR);
1607 #define MODNAMESTR MODNAMESTR_FN_2(MODNAME)
1608 #define MODNAMESTR_FN_2(x) MODNAMESTR_FN_1(x)
1609 #define MODNAMESTR_FN_1(x) #x
1613 /** This definition is used as shorthand for the various classes
1614 * and functions needed to make a module loadable by the OS.
1615 * It defines the class factory and external init_module function.
1619 #define MODULE_INIT(y) \
1620 extern "C" DllExport Module * MODULE_INIT_SYM() \
1624 BOOLEAN WINAPI DllMain(HINSTANCE hDllHandle, DWORD nReason, LPVOID Reserved) \
1626 switch ( nReason ) \
1628 case DLL_PROCESS_ATTACH: \
1629 case DLL_PROCESS_DETACH: \
1637 #define MODULE_INIT(y) \
1638 extern "C" DllExport Module * MODULE_INIT_SYM() \
1642 extern "C" const char inspircd_src_version[] = VERSION " r" REVISION;
1645 #define COMMAND_INIT(c) MODULE_INIT(CommandModule<c>)