LucasRed

VCMP IRC Script - Sockets

Nov 6th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.82 KB | None | 0 0
  1. local BotNick       =   [ "BotNick0", "BotNick1", "BotNick2" ];     // The BotNames
  2.  
  3. const NickServ      =   "";                     // The NickServ Password
  4.  
  5. const server        =   "91.121.134.5";                 // Server Ip
  6. const port      =    6667;                      // Server port
  7.  
  8. const chan      =   "#rulk";                    // Home channel
  9. const pass      =   "";                     // Channel password
  10.  
  11. const PREFIX        =   ".";                        // The command prefix.
  12.  
  13.  
  14. local socket        =   {};                     // A empty table to put our socket instances in.
  15.  
  16. local count         =   0;                      // A counter to count recieved data.
  17.  
  18. local prev      =   null;                       // A var to hold the BotName that last sent data to IRC.
  19.  
  20.  
  21. function LoadBot()
  22. {
  23.     // A var to count.
  24.     local t = 0;
  25.  
  26.     // Iterate our BotNames.
  27.     while ( t < BotNick.len() )
  28.     {
  29.         // Open a new socket and send the data to be parsed.
  30.         socket[ t ] <- NewSocket( "connect" );
  31.  
  32.         // Connect our socket to the server.
  33.         socket[ t ].Connect( server, port );
  34.  
  35.         // The function to call, once connected.
  36.         socket[ t ].SetNewConnFunc( "Login" );
  37.  
  38.         // Increase our var.
  39.         t++;
  40.     }
  41. }
  42.  
  43. function connect( data )
  44. {
  45.     // Parse the data
  46.     local  
  47.         raw         =   split( data, "\r\n" ),
  48.         event       =   raw.len() > 1 ? split( raw[ 1 ], " " ) : "",
  49.         EventID     =   event.len() > 1 ? event[ 1 ] : "";
  50.  
  51.     // Find the Ping event.
  52.     local
  53.         FindPing    =   split( raw[ 0 ], " " ),
  54.         ping        =   FindPing[ 0 ];
  55.  
  56.    
  57.     // Find the channel event ( I've done this separately for de-bugging purposes )
  58.     local
  59.         FindChan    =   split( raw[ 0 ], " " ),
  60.         channel     =   FindChan.len() > 2 ? FindChan[ 2 ] : "";
  61.  
  62.  
  63.     // Ok, Now let's get those irc commands ( Join, part, Kick...etc etc )
  64.     local
  65.         FindIrcCommand  =   split( raw[ 0 ], " " ),
  66.         IrcCommand  =   FindIrcCommand.len() > 1 ? FindIrcCommand[ 1 ] : "";
  67.  
  68.     // Reply to ping's ( 'PONG' + irc server )
  69.     if ( ping == "PING" )
  70.     {
  71.         // A var to count.
  72.         local t = 0;
  73.  
  74.         // Iterate our BotNames.
  75.         while ( t < BotNick.len() )
  76.         {
  77.             socket[ t ].Send( "PONG " + FindPing[ 1 ] + "\n" );
  78.  
  79.             // Increase var.
  80.             t++;
  81.         }
  82.     }
  83.  
  84.     // We got the welcome message, so We're connected.
  85.     if ( EventID == "001" )
  86.     {
  87.         // A var to count.
  88.         local t = 0;
  89.  
  90.         // Iterante the BotNames
  91.         while ( t < BotNick.len() )
  92.         {
  93.             // Join our HomeChannel
  94.             socket[ t ].Send( "JOIN " + chan + " " + pass + "\n" );
  95.  
  96.             // Login to NickServ.
  97.             socket[ t ].Send( "PRIVMSG NickServ IDENTIFY " + NickServ + "\n" );
  98.  
  99.             // Increase var.
  100.             t++;
  101.         }
  102.     }
  103.  
  104.     // This is returned at the end of a NAMES request, after all visible names are returned.
  105.     else if ( EventID == "366" )
  106.     {
  107.         // A dynamic array to store each user's class instance in.
  108.         NickList        <-  [];
  109.  
  110.         // Now, Lets parse the names query.
  111.         local
  112.             FindUsers   =   split( raw[ 0 ], ":" ),
  113.             users       =   split( FindUsers[ 1 ], " " );
  114.  
  115.         // Iterate our parsed list of users.
  116.         for ( local i = 0; i < users.len(); i++ )
  117.         {
  118.             // Establish the level symbol.
  119.             local
  120.                 Level   =   users[ i ].slice( 0, 1 );
  121.  
  122.             if ( Level == "~" ) NickList.push( UserLevels( users[ i ].slice( 1, users[ i ].len() ), "Owner", 5 ) );
  123.             else if ( Level == "&" ) NickList.push( UserLevels( users[ i ].slice( 1, users[ i ].len() ), "SOP", 4 ) );
  124.             else if ( Level == "@" ) NickList.push( UserLevels( users[ i ].slice( 1, users[ i ].len() ), "AOP", 3 ) );
  125.             else if ( Level == "%" ) NickList.push( UserLevels( users[ i ].slice( 1, users[ i ].len() ), "HOP", 2 ) );
  126.             else if ( Level == "+" ) NickList.push( UserLevels( users[ i ].slice( 1, users[ i ].len() ), "VOP", 1 ) );
  127.             else NickList.push( UserLevels( users[ i ], "User", 0 ) );
  128.         }
  129.     }
  130.     if ( IrcCommand == "KICK" )
  131.     {
  132.         // A var to count.
  133.         local t = 0;
  134.  
  135.         // Iterante the BotNames
  136.         while ( t < BotNick.len() )
  137.         {
  138.             // Re-join our channel when kicked
  139.             socket[ t ].Send( "JOIN " + chan + " " + pass + "\n" );
  140.  
  141.             // Increase var.
  142.             t++;
  143.         }
  144.     }
  145.     else if ( ( IrcCommand == "MODE" ) || ( IrcCommand == "NICK" ) || ( IrcCommand == "JOIN" ) || ( IrcCommand == "PART" ) || ( IrcCommand == "QUIT" ) )
  146.     {
  147.         // Send a NAMES request.
  148.         socket[ 0 ].Send( "NAMES :" + chan + "\n" );
  149.     }
  150.    
  151.     // Somethings been said in our HomeChannel.
  152.     if ( channel == chan )
  153.     {
  154.         // Find the text that was typed.
  155.         local
  156.             FindText    =   split( raw[ 0 ], ":" ),
  157.             text        =   FindText.len() > 1 ? FindText[ 1 ] : "",
  158.             prefix      =   text != "" ? text.slice( 0, 1 ) : "";
  159.  
  160.         // Find the command that was typed.
  161.         local
  162.             Findcmd     =   text != "" ? split( text, " " ) : "",
  163.             cmd     =   Findcmd != "" ? Findcmd[ 0 ].slice( 1, Findcmd[ 0 ].len() ) : "";
  164.  
  165.  
  166.         // Find the text that was typed after the command.
  167.         local
  168.             NewText     =   text != "" ? strip( text.slice( cmd.len() + 1, text.len() ) ) : "";
  169.  
  170.  
  171.         // Find the person that typed the text.
  172.         local
  173.             FindNick    =   split( raw[ 0 ], "!" ),
  174.             Nick        =   FindNick[ 0 ].slice( 1, FindNick[ 0 ].len() );
  175.  
  176.         // Ensure a command was typed with our prefix.
  177.         if ( prefix  == PREFIX )
  178.         {
  179.             // Depending how many bot's we have connected, depends how many times we recieve the data.
  180.             // So we need to only forward the last recieved data to the command handler.
  181.  
  182.             // We do this using the 'Modulus Operator', This checks that our 'data recieved counter' devides into
  183.             // the total number of bot's we have connected, ensuring that we only forward the recieved data once.
  184.  
  185.             if ( count % BotNick.len() == 0 )
  186.             {
  187.                 // Forward to our command handler.
  188.                 ProcessCommands( Nick, cmd, NewText );
  189.             }
  190.  
  191.             // Increase our 'recieved data' counter.
  192.             count++;
  193.         }
  194.     }
  195. }
  196.  
  197. function Login()
  198. {
  199.     // Our counter.
  200.     local t = 0;
  201.    
  202.     // Iterate the BotNames
  203.     while ( t < BotNick.len() )
  204.     {
  205.         // Set the bots name and real name
  206.         socket[ t ].Send( "USER " + BotNick[ t ] + " 0 * : " + BotNick[ t ] + " \n" );
  207.  
  208.         // Set the nick that the bot will use on the irc server
  209.         socket[ t ].Send( "NICK " + BotNick[ t ] + "\n" );
  210.    
  211.         // Increase our counter.
  212.         t++;
  213.     }
  214. }
  215.  
  216. // Send a message to be echoed.
  217. function SendToIRC( text )
  218. {
  219.     // Establish the index of the next bot.
  220.     local
  221.         BotID       =   FindNexBotId();
  222.  
  223.     // Send the data to IRC.
  224.     socket[ BotID ].Send( "PRIVMSG " + chan + " " + text + "\n" );
  225. }
  226.  
  227. // Establish the index of the next bot.
  228. function FindNexBotId()
  229. {
  230.     // If the index exists.
  231.     try{
  232.         local
  233.             next    =   BotNick.find( prev ) + 1;   // Find the next array index.
  234.             prev    =   BotNick[ next ];        // Put it's value in our var.
  235.         return  next;                       // Return the index.
  236.     } catch ( e ) { prev = BotNick[ 0 ]; return 0; }        // Return index zero on error.
  237. }
  238.  
  239. // A empty class to store Channel Users and their levels.
  240. class UserLevels
  241. {
  242.     // The Constructor.
  243.     constructor( ... ) {
  244.  
  245.         user    =   vargv[ 0 ];
  246.         level   =   vargv[ 1 ];
  247.         ilevel  =   vargv[ 2 ];
  248.     }
  249.  
  250.     // Reset Property values
  251.     user    =   null;
  252.     level   =   null;
  253.     ilevel  =   null;
  254. }
  255.  
  256. // Find the specified user's level.
  257. function FindLevel( user, ilevel )
  258. {
  259.     // Iterate our NickList array.
  260.     foreach ( obj in NickList )
  261.     {
  262.         // Ensure a class instance was found.
  263.         if ( obj != null )
  264.         {
  265.             // Add the object properties to a tmp variable, so we can access squirrel's VM built in manipulation functions for that data type.
  266.             local tmp = obj.user;
  267.  
  268.             // Perform a string comparison, if we get a match, return the 'level' property.
  269.             if ( tmp.find( user ) != null )
  270.             {
  271.                 // Return the numerical level if 'true'
  272.                 if ( ilevel ) return obj.ilevel;
  273.        
  274.                 // Otherwise return the Non-Numerical level.
  275.                 else return obj.level;
  276.             }
  277.         }
  278.     }
  279. }
  280.  
  281.  
  282. // The commands.
  283. function ProcessCommands( nick, cmd, text )
  284. {
  285.     if ( cmd == "commands" )
  286.     {
  287.         SendToIRC( ">> Current commands: " + "say, players" );
  288.     }
  289.     else if ( cmd == "level" )
  290.     {
  291.         // Change parameter two, to 'true' for numerical level.
  292.         SendToIRC( FindLevel( nick, false ) );
  293.     }
  294.     else if ( cmd == "say" )
  295.     {
  296.         // Check text is not a empty string.
  297.         if ( text == "" ) SendToIRC( "Error: Missing paremeter - text" );
  298.         else
  299.         {
  300.             // Send the message in-game
  301.             Message( "[ irc " + FindLevel( nick, false ) + " ] " + nick + ":" +  " " + text );
  302.            
  303.             // Echo it back, so we know whats happening.
  304.             SendToIRC( "[ irc " + FindLevel( nick, false ) + " ] " + nick + ":" +  " " + text );
  305.         }
  306.     }
  307.     else if ( cmd == "players" )
  308.     {
  309.         // A tempory var to collect our players in
  310.         local   tmp     =   "",
  311.             count   =   0;
  312.  
  313.         // Iterate all players 
  314.         for( local i=0; i < 50; ++i )
  315.         {
  316.             // Grab it's instance in a variable.
  317.             local plr = FindPlayer( i );
  318.  
  319.             // If a instance has been found
  320.             if ( plr )
  321.             {
  322.                 // Add it to our temp string.
  323.                 tmp = tmp + " " + plr.Name + "[" + plr.ID + "]" + ",",
  324.  
  325.                 // Increase our counter
  326.                 count++;
  327.             }
  328.         }
  329.        
  330.         // Output if tmp is not a empty string still.
  331.         tmp != "" ? SendToIRC( "Online Players: " + strip( tmp.slice(0, tmp.len() - 1) ) ) : "";
  332.  
  333.         // Output our player count.
  334.         SendToIRC( "Total players: " + count );
  335.     }
  336.  
  337.     // Anything else is a unknown command.
  338.     else SendToIRC( "I don't know that command!" );
  339. }
Advertisement
Add Comment
Please, Sign In to add comment