SHARE
TWEET

Untitled

a guest May 19th, 2017 138 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define IRC_VERSION "0.1"
  2.  
  3. #define IRC_CMD_MAXLEN 64 // The maximum length of a command
  4. #define IRC_NICK_MAXLEN 64 // The maximum length of a nickname
  5. #define IRC_CHANNEL_MAXLEN 64 // The maximum length of a channel  name
  6. #define IRC_MAXLEN 512 // The maximum length of a line from the server. IRC RFC says maximum line length is 512, there again it also says a nicknames max length is 9. This might not be right.
  7.  
  8. #define IRC_CMDFLAG_NOPREFIX 1<<0 // Allow command to be triggered even without a prefix. For example instead of typing "SourceIRC: command args" you could just type "command args"
  9.  
  10. public SharedPlugin:__pl_sourceirc =
  11. {
  12.     name = "sourceirc",
  13.     file = "sourceirc.smx",
  14. #if defined REQUIRE_PLUGIN
  15.     required = 1,
  16. #else
  17.     required = 0,
  18. #endif
  19. };
  20.  
  21. public __pl_sourceirc_SetNTVOptional()
  22. {
  23.     MarkNativeAsOptional("IRC_RegCmd");
  24.     MarkNativeAsOptional("IRC_RegAdminCmd");
  25.     MarkNativeAsOptional("IRC_Send");
  26.     MarkNativeAsOptional("IRC_ReplyToCommand");
  27.     MarkNativeAsOptional("IRC_GetCmdArgString");
  28.     MarkNativeAsOptional("IRC_GetCmdArg");
  29.     MarkNativeAsOptional("IRC_GetEventArg");
  30.     MarkNativeAsOptional("IRC_CleanUp");
  31.     MarkNativeAsOptional("IRC_GetHostMask");
  32.     MarkNativeAsOptional("IRC_HookEvent");
  33.     MarkNativeAsOptional("IRC_GetNick");
  34.     MarkNativeAsOptional("IRC_ChannelHasFlag");
  35.     MarkNativeAsOptional("IRC_GetUserFlagBits");
  36.     MarkNativeAsOptional("IRC_GetAdminFlag");
  37.     MarkNativeAsOptional("IRC_GetTeamColor");
  38. }
  39.  
  40. /**
  41.  * Called when an IRC command is invoked.
  42.  *
  43.  * @param nick          The nickname of the user who invoked hte command
  44.  * @param args          Number of arguments that were in the argument string.
  45.  * @return              An Action value.  Not handling the command
  46.  *                      means that SourceIRC will report it as "Unknown Command."
  47.  */
  48. functag IRCCmd Action:public(const String:nick[], args);
  49.  
  50. /**
  51.  * Called when an IRC event is fired.
  52.  *
  53.  * @param prefix        This is the hostmask for IRC events that are triggered by a user, otherwise blank.
  54.  * @param args          Number of arguments that are in the argument string.
  55.  * @return              Only Plugin_Stop has any effect here, as this is a post hook.
  56.  */
  57.  
  58. functag IRCEvent Action:public(const String:prefix[], args);
  59.  
  60. /**
  61.  * Registers a command people can use in IRC
  62.  *
  63.  * @param cmd           String containing command to register
  64.  * @param callback      A Function to use as a callback for when the command is invoked
  65.  * @param description   Optional description to use for help.
  66.  * @param flags         Optional command flags.
  67.  * @noreturn
  68.  */
  69.  
  70. native IRC_RegCmd(const String:cmd[], IRCCmd:callback, const String:description[]="", flags=0);
  71.  
  72. /**
  73.  * Creates an IRC command as an administrative command.
  74.  * When this command is invoked, the access rights of the user are
  75.  * automatically checked before allowing it to continue.
  76.  *
  77.  * @param cmd           String containing command to register.
  78.  * @param callback      A function to use as a callback for when the command is invoked.
  79.  * @param adminflags    Administrative flags (bitstring) to use for permissions.
  80.  * @param description   Optional description to use for help.
  81.  * @param flags         Optional command flags.
  82.  * @noreturn
  83.  */
  84.  
  85. native IRC_RegAdminCmd(const String:cmd[], IRCCmd:callback, adminflags, const String:description[]="", flags=0);
  86.  
  87. /**
  88.  * Sends a packet to the IRC Server. \r\n is automatically appended for you.
  89.  *
  90.  * @param format        Formatting rules.
  91.  * @param ...           Variable number of format parameters.
  92.  * @noreturn
  93.  * @error               String contains \r or \n.
  94.  */
  95.  
  96. native IRC_Send(const String:format[], any:...);
  97.  
  98. /**
  99.  * Replys to a message (Using a notice)
  100.  *
  101.  * @param nick          Nickname of the user to message
  102.  * @param format        Formatting rules.
  103.  * @param ...           Variable number of format parameters.
  104.  * @noreturn
  105.  */
  106.  
  107. native IRC_ReplyToCommand(const String:nick[], const String:format[], any:...);
  108.  
  109. /**
  110.  * Retrieves the entire command argument string in one lump from the current
  111.  * IRC command.
  112.  *
  113.  * @param buffer        Buffer to use for storing the string.
  114.  * @param maxlength     Maximum length of the buffer.
  115.  * @return              Length of string written to buffer.
  116.  */
  117.  
  118. native IRC_GetCmdArgString(String:buffer[], maxlength);
  119.  
  120. /**
  121.  * Retrieves a command argument given its index, from the current IRC command.
  122.  * @note Argument indexes start at 1; 0 retrieves the command name.
  123.  *
  124.  * @param argnum        Argument number to retrieve.
  125.  * @param buffer        Buffer to use for storing the string.
  126.  * @param maxlength     Maximum length of the buffer.
  127.  * @return              Length of string written to buffer.
  128.  */
  129.  
  130. native IRC_GetCmdArg(argnum, String:buffer[], maxlength);
  131.  
  132.  
  133. /**
  134.  * Gets an event argument, note that this is different to SourceMods GetEvent*
  135.  * function, and works more like GetCmdArg.
  136.  *
  137.  * @param argnum        Argument number to retrieve.
  138.  * @param buffer            Buffer to use for storing the string.
  139.  * @param maxlength     Maximum length of the buffer.
  140.  * @return              Length of string written to buffer.
  141.  */
  142.  
  143. native IRC_GetEventArg(argnum, String:buffer[], maxlength);
  144.  
  145. /**
  146.  * Sends a message to all channels with the given flag.
  147.  *
  148.  * @param flag          The flag channels must have to recieve this message.
  149.  * @param format        Formatting rules.
  150.  * @param ...           Variable number of format parameters.
  151.  * @noreturn
  152.  */
  153.  
  154. native IRC_MsgFlaggedChannels(const String:flag[], const String:format[], any:...);
  155.  
  156. /**
  157.  * If your plugin calls any of the IRC_Reg* functions
  158.  * you must call this function upon unload otherwise you will cause errors
  159.  * in the core.
  160.  *
  161.  * @noreturn
  162.  */
  163.  
  164. native IRC_CleanUp();
  165.  
  166. /**
  167.  * Gets the hostmask (eg Nick!ident@address.com) for the user executing
  168.  * a command, only valid inside a IRCCmd callback.
  169.  *
  170.  * @param hostmask      String to store the hostmask in
  171.  * @param maxlength     Maximum length of the buffer.
  172.  * @noreturn
  173.  */
  174.  
  175. native IRC_GetHostMask(String:hostmask[], maxlength);
  176.  
  177. /**
  178.  * Creates a hook for when an IRC event is fired.
  179.  *
  180.  * @param name          Name of event.
  181.  * @param callback      An IRCEvent function pointer.
  182.  * @noreturn
  183.  */
  184.  
  185. native IRC_HookEvent(const String:event[], IRCEvent:callback);
  186.  
  187. /**
  188.  * Gets the bots current nickname
  189.  *
  190.  * @param mynick        String to store the bots name in.
  191.  * @param maxlength     Maximum length of the buffer.
  192.  * @noreturn
  193.  */
  194.  
  195. native IRC_GetNick(String:mynick[], maxlength);
  196.  
  197. /**
  198.  * Checks if a channel has a flag.
  199.  *
  200.  * @param channel       Channel to check.
  201.  * @param flag          Flag to check.
  202.  * @return              True if channel has flag, otherwise false.
  203.  */
  204.  
  205. native IRC_ChannelHasFlag(const String:channel[], const String:flag[]);
  206.  
  207. /**
  208.  * Returns users access flags. If the client is not an admin,
  209.  * the result is always 0.
  210.  *
  211.  * @param hostmask      hostmask of the user to check.
  212.  * @return              Flags
  213.  */
  214.  
  215. native IRC_GetUserFlagBits(const String:hostmask[]);
  216.  
  217. /**
  218.  * Returns whether or not a flag is enabled on an admin.
  219.  *
  220.  * @param hostmask      hostmask of the user to check.
  221.  * @param flag          Admin flag to use.
  222.  * @return              True if enabled, false otherwise.
  223.  */
  224.  
  225. native bool:IRC_GetAdminFlag(const String:hostmask[], AdminFlag:flag);
  226.  
  227. /**
  228.  * Gets the IRC color number for a team as specified in sourceirc.cfg
  229.  *
  230.  * @param team          Team index
  231.  * @return              Color code if available, otherwise -1.
  232.  */
  233.  
  234. native IRC_GetTeamColor(team);
  235.  
  236. forward IRC_Connected();
  237.  
  238. forward IRC_RetrieveUserFlagBits(const String:fulladdress[], &flagbits);
  239.  
  240.  
  241.  
  242.  
  243.  
  244. /**
  245.  * Performs a standard IRC Like wildcard match, useful for hostmasks.
  246.  *
  247.  * @param str           String to check
  248.  * @param wildcard      Wildcard to check against string
  249.  * @return              true if match, false otherwise.
  250.  */
  251.  
  252. stock bool:IsWildCardMatch(const String:str[], const String:wildcard[]) {
  253.     new wildpos = 0;
  254.     for (new a = 0; a <= strlen(str); a++) {       
  255.         if (wildcard[wildpos] == '*') {
  256.             if (wildpos == strlen(wildcard))
  257.                 return true;
  258.             if (CharToLower(str[a]) == CharToLower(wildcard[wildpos+1]))
  259.                 wildpos += 2;
  260.         }
  261.         else if (wildcard[wildpos] == '?') {
  262.             wildpos++;
  263.         }
  264.         else if (CharToLower(str[a]) == CharToLower(wildcard[wildpos])) {
  265.             wildpos++;
  266.         }
  267.         else {
  268.             return false;
  269.         }
  270.     }
  271.     if (wildpos == strlen(wildcard))
  272.         return false;
  273.     return true;
  274. }
  275.  
  276. /**
  277.  * Breaks a string into pieces and stores each piece into an adt_array of buffers.
  278.  *
  279.  * @param text              The string to split.
  280.  * @param split             The string to use as a split delimiter.
  281.  * @param adt_array         An adt_array of string buffers.
  282.  * @param maxlength         Maximum length of each string buffer.
  283.  * @return                  Number of strings retrieved.
  284.  */
  285.  
  286. stock ExplodeString_Array(const String:source[], const String:split[], Handle:adt_array, maxlength) {
  287.     ClearArray(adt_array);
  288.     decl String:arg[maxlength];
  289.     new strpos = 0;
  290.     for (new i = 0; i <= strlen(source); i++) {
  291.         if (!strncmp(source[i], split, strlen(split))) {
  292.             arg[strpos] = '\x00';
  293.             PushArrayString(adt_array, arg);
  294.  
  295.             strpos = 0;
  296.             i += strlen(split);
  297.         }
  298.         if (strpos < maxlength)
  299.             arg[strpos] = source[i];
  300.         strpos++;
  301.     }
  302.     arg[strpos] = '\x00';
  303.     PushArrayString(adt_array, arg);
  304.     return GetArraySize(adt_array);
  305. }
  306.  
  307. /**
  308.  * Extracts a nickname from a hostmask.
  309.  *
  310.  * @param hostmask          Hostmask to get the nickname from.
  311.  * @param nick              String to store the nickname in.
  312.  * @param maxlength         Maximum length of the nickname.
  313.  * @noreturn
  314.  */
  315.  
  316. stock IRC_GetNickFromHostMask(const String:hostmask[], String:nick[], maxlength) {
  317.     for (new i = 0; i <= maxlength; i++) {
  318.         if (hostmask[i] == '!') {
  319.             nick[i] = '\x00';
  320.             break;
  321.         }
  322.         nick[i] = hostmask[i];
  323.     }
  324. }
  325.  
  326. /**
  327.  * Strips IRC Color codes from a string
  328.  *
  329.  * @param str               String to strip
  330.  * @param maxlength         maximum length of str
  331.  * @noreturn
  332.  */
  333.  
  334. stock IRC_Strip(String:str[], maxlength) {
  335.     for (new i = 0; i <= strlen(str); i++) {
  336.         //  Underline           Reverse             Color codes off     Bold
  337.         if (str[i] == '\x1F' || str[i] == '\x16' || str[i] == '\x0f' || str[i] == '\x02')
  338.             RemoveChar(str, maxlength, i);
  339.         // Strip color codes
  340.         if (str[i] == '\x03') {
  341.             RemoveChar(str, maxlength, i);
  342.             new ignorelast = false;
  343.  
  344.             if (str[i] > 47 && str[i] < 58) {
  345.                 RemoveChar(str, maxlength, i);
  346.                 if ((str[i] > 47 && str[i] < 58) || str[i] == ',') {
  347.                     if (str[i] == ',')
  348.                         ignorelast = true;
  349.                     RemoveChar(str, maxlength, i);
  350.                     if ((str[i] > 47 && str[i] < 58) || str[i] == ',') {
  351.                         RemoveChar(str, maxlength, i);
  352.                         if (str[i] > 47 && str[i] < 58) {
  353.                             RemoveChar(str, maxlength, i);
  354.                             if (str[i] > 47 && str[i] < 58 && !ignorelast) {
  355.                                 RemoveChar(str, maxlength, i);
  356.                             }
  357.                         }
  358.                     }
  359.                 }
  360.             }
  361.             i--;
  362.         }
  363.     }
  364. }
  365.  
  366. /**
  367.  * Removes a character from a string.
  368.  *
  369.  * @param str               String to strip.
  370.  * @param maxlength         maximum length of str.
  371.  * @param c                 character index to remove.
  372.  * @noreturn
  373.  */
  374.  
  375. stock RemoveChar(String:str[], maxlen, c) {
  376.     for (new i = c; i < maxlen-1; i++) {
  377.         str[i] = str[i+1];
  378.     }
  379.     str[maxlen-1] = '\0';
  380. }
  381.  
  382.  
  383. /**
  384.  * Strips Game Color codes from a string
  385.  *
  386.  * @param str               String to strip
  387.  * @param maxlength         maximum length of str
  388.  * @noreturn
  389.  */
  390.  
  391. stock IRC_StripGame(String:str[], maxlen) {
  392.     for (new i = 0; i <= strlen(str); i++) {
  393.         //  Default             Team/LightGreen     Green               Olive
  394.         if (str[i] == '\x01' || str[i] == '\x03' || str[i] == '\x04' || str[i] == '\x05')
  395.             RemoveChar(str, maxlen, i);
  396.     }
  397. }
RAW Paste Data
Top