Guest User

IRC CODE

a guest
Feb 14th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 9.60 KB | None | 0 0
  1. #include <YSI\y_hooks>
  2.  
  3.  
  4. static
  5.     irc_Active,
  6.     irc_Serv[32],
  7.     irc_Port,
  8.     irc_ChatChan[32],
  9.     irc_ChatPass[32],
  10.     irc_ChatPrefix,
  11.     irc_StaffChan[32],
  12.     irc_StaffPass[32],
  13.     irc_StaffPrefix,
  14.     irc_BotNick[32],
  15.     irc_BotName[32],
  16.     irc_BotUser[32],
  17.     irc_BotMail[64],
  18.     irc_BotPass[32];
  19.  
  20.  
  21. static
  22.     irc_Bot,
  23.     irc_Group;
  24.  
  25.  
  26. hook OnScriptInit()
  27. {
  28.     print("\n[OnScriptInit] Initialising 'IRC'...");
  29.  
  30.     GetSettingInt       ("irc/use-irc",     1,                  irc_Active);
  31.     GetSettingString    ("irc/serv",        "irc.tl",           irc_Serv);
  32.     GetSettingInt       ("irc/port",        6667,               irc_Port);
  33.     GetSettingString    ("irc/chat-chan",   "#ScavengeChat",    irc_ChatChan);
  34.     GetSettingString    ("irc/chat-pass",   "",         irc_ChatPass);
  35.     GetSettingInt       ("irc/chat-prefix", '.',                irc_ChatPrefix);
  36.     GetSettingString    ("irc/staff-chan""#ScavengeAdmin",   irc_StaffChan);
  37.     GetSettingString    ("irc/staff-pass""",         irc_StaffPass);
  38.     GetSettingInt       ("irc/staff-prefix",'.',                irc_StaffPrefix);
  39.     GetSettingString    ("irc/bot-name",    "SS-Bsdadot",           irc_BotName);
  40.     GetSettingString    ("irc/bot-nick",    "SS-Bsadadot",          irc_BotNick);
  41.     GetSettingString    ("irc/bot-pass",    "122chsadadangeme",         irc_BotPass);
  42.     GetSettingString    ("irc/bot-mail",    "ra*****@.in",          irc_BotMail);
  43.     GetSettingString    ("irc/bot-user",    "SS-Bot",           irc_BotUser);
  44.  
  45.     if(!irc_Active)
  46.         return 1;
  47.  
  48.     if(!strcmp(irc_BotPass, "changeme"))
  49.     {
  50.         print("ERROR: Please set the IRC bot password in settings.json");
  51.         for(;;){}
  52.     }
  53.  
  54.     if(!strcmp(irc_BotMail, "[email protected]"))
  55.     {
  56.         print("ERROR: Please set the IRC bot email in settings.json");
  57.         for(;;){}
  58.     }
  59.  
  60.     irc_Bot = IRC_Connect(irc_Serv, irc_Port, irc_BotNick, irc_BotName, irc_BotUser);
  61.     IRC_SetIntData(irc_Bot, E_IRC_CONNECT_DELAY, 20);
  62.  
  63.     irc_Group = IRC_CreateGroup();
  64.  
  65.     return 1;
  66. }
  67.  
  68. hook OnScriptExit()
  69. {
  70.     print("\n[OnScriptExit] Shutting down 'IRC'...");
  71.  
  72.     if(!irc_Active)
  73.         return 1;
  74.  
  75.     IRC_Quit(irc_Bot, "Gamemode Closing/Restarting");
  76.  
  77.     IRC_DestroyGroup(irc_Group);
  78.  
  79.     return 1;
  80. }
  81.  
  82.  
  83. hook OnPlayerConnect(playerid)
  84. {
  85.     if(!irc_Active)
  86.         return 1;
  87.  
  88.     // If the server is booting up after a restart, don't send connects.
  89.     if(gServerInitialising)
  90.         return 1;
  91.  
  92.     new
  93.         joinMsg[128],
  94.         name[MAX_PLAYER_NAME + 1];
  95.  
  96.     GetPlayerName(playerid, name, sizeof(name));
  97.     strins(name, ".", 1);
  98.  
  99.     format(joinMsg, sizeof(joinMsg), " >>> [%02d][%s] has joined the server.", playerid, name);
  100.     IRC_GroupSay(irc_Group, irc_ChatChan, joinMsg);
  101.  
  102.     return 1;
  103. }
  104.  
  105. hook OnPlayerDisconnect(playerid, reason)
  106. {
  107.     if(!irc_Active)
  108.         return 1;
  109.  
  110.     // If the server is restarting, don't send disconnects.
  111.     if(gServerRestarting)
  112.         return 1;
  113.  
  114.     new
  115.         leaveMsg[128],
  116.         name[MAX_PLAYER_NAME + 1],
  117.         reasonMsg[8];
  118.  
  119.     switch(reason)
  120.     {
  121.         case 0: reasonMsg = "Timeout";
  122.         case 1: reasonMsg = "Leaving";
  123.         case 2: reasonMsg = "Kicked";
  124.     }
  125.  
  126.     GetPlayerName(playerid, name, sizeof(name));
  127.     strins(name, ".", 1);
  128.  
  129.     format(leaveMsg, sizeof(leaveMsg), " <<< [%02d][%s] has left the server. (%s)", playerid, name, reasonMsg);
  130.     IRC_GroupSay(irc_Group, irc_ChatChan, leaveMsg);
  131.  
  132.     return 1;
  133. }
  134.  
  135. public OnPlayerSendChat(playerid, text[], Float:frequency)
  136. {
  137.     if(irc_Active)
  138.         _IRC_HandleServerChat(playerid, text, frequency);
  139.  
  140.     #if defined irc_OnPlayerSendChat
  141.         return irc_OnPlayerSendChat(playerid, text, frequency);
  142.     #else
  143.         return 0;
  144.     #endif
  145. }
  146. #if defined _ALS_OnPlayerSendChat
  147.     #undef OnPlayerSendChat
  148. #else
  149.     #define _ALS_OnPlayerSendChat
  150. #endif
  151.  
  152. #define OnPlayerSendChat irc_OnPlayerSendChat
  153. #if defined irc_OnPlayerSendChat
  154.     forward irc_OnPlayerSendChat(playerid, text[], Float:frequency);
  155. #endif
  156.  
  157. _IRC_HandleServerChat(playerid, text[], Float:frequency)
  158. {
  159.     if(frequency == 1.0)
  160.     {
  161.         if(irc_ChatPrefix != 0)
  162.         {
  163.             if(text[0] != irc_ChatPrefix)
  164.                 return 0;
  165.  
  166.             strdel(text, 0, 1);
  167.         }
  168.  
  169.         new
  170.             name[MAX_PLAYER_NAME + 1],
  171.             message[7 + MAX_PLAYER_NAME + 128];
  172.  
  173.         GetPlayerName(playerid, name, sizeof(name));
  174.         strins(name, ".", strlen(name) / 2); // Prevents repeated nickalerts.
  175.  
  176.         format(message, sizeof(message), "[%02d][%s]: %s", playerid, name, text);
  177.         IRC_GroupSay(irc_Group, irc_ChatChan, message);    
  178.     }
  179.  
  180.     if(frequency == 3.0)
  181.     {
  182.         if(irc_StaffPrefix != 0)
  183.         {
  184.             if(text[0] != irc_StaffPrefix)
  185.                 return 0;
  186.  
  187.             strdel(text, 0, 1);
  188.         }
  189.  
  190.         new
  191.             name[MAX_PLAYER_NAME + 1],
  192.             message[7 + MAX_PLAYER_NAME + 128];
  193.  
  194.         GetPlayerName(playerid, name, sizeof(name));
  195.         strins(name, ".", strlen(name) / 2); // Prevents repeated nickalerts.
  196.  
  197.         format(message, sizeof(message), "[%02d][%s]: %s", playerid, name, text);
  198.         IRC_GroupSay(irc_Group, irc_StaffChan, message);       
  199.     }
  200.  
  201.     return 1;
  202. }
  203.  
  204. public IRC_OnUserSay(botid, recipient[], user[], host[], message[])
  205. {
  206.     if(irc_Active)
  207.         _IRC_HandleChannelChat(recipient, user, message);
  208.  
  209.     #if defined irc_IRC_OnUserSay
  210.         return irc_IRC_OnUserSay(botid, recipient[], user[], host[], message[]);
  211.     #else
  212.         return 1;
  213.     #endif
  214. }
  215. #if defined _ALS_IRC_OnUserSay
  216.     #undef IRC_OnUserSay
  217. #else
  218.     #define _ALS_IRC_OnUserSay
  219. #endif
  220.  
  221. #define IRC_OnUserSay irc_IRC_OnUserSay
  222. #if defined irc_IRC_OnUserSay
  223.     forward irc_IRC_OnUserSay(botid, recipient[], user[], host[], message[]);
  224. #endif
  225.  
  226. _IRC_HandleChannelChat(recipient[], user[], message[])
  227. {
  228.     if(!strcmp(recipient, irc_ChatChan, true))
  229.     {
  230.         if(irc_ChatPrefix != 0)
  231.         {
  232.             if(message[0] != irc_ChatPrefix)
  233.                 return 0;
  234.  
  235.             strdel(message, 0, 1);
  236.         }
  237.  
  238.         logf("[CHAT] [IRC-C] [%s]: %s", user, message);
  239.  
  240.         new
  241.             line1[256],
  242.             line2[128];
  243.  
  244.         format(line1, 256, "[IRC] "C_AQUA"%s"C_WHITE": %s", user, TagScan(message));
  245.  
  246.         TruncateChatMessage(line1, line2);
  247.  
  248.         foreach(new i : Player)
  249.         {
  250.             if(GetPlayerBitFlag(i, GlobalQuiet))
  251.                 continue;
  252.  
  253.             SendClientMessage(i, WHITE, line1);
  254.  
  255.             if(!isnull(line2))
  256.                 SendClientMessage(i, WHITE, line2);
  257.         }
  258.     }
  259.  
  260.     if(!strcmp(recipient, irc_StaffChan, true))
  261.     {
  262.         if(irc_StaffPrefix != 0)
  263.         {
  264.             if(message[0] != irc_StaffPrefix)
  265.                 return 0;
  266.  
  267.             strdel(message, 0, 1);
  268.         }
  269.  
  270.         logf("[CHAT] [IRC-S] [%s]: %s", user, message);
  271.  
  272.         new
  273.             line1[256],
  274.             line2[128];
  275.  
  276.         format(line1, 256, "[STAFF IRC] "C_AQUA"%s"C_WHITE": %s", user, TagScan(message));
  277.  
  278.         TruncateChatMessage(line1, line2);
  279.  
  280.         foreach(new i : Player)
  281.         {
  282.             if(GetPlayerAdminLevel(i) == 0)
  283.                 continue;
  284.  
  285.             SendClientMessage(i, WHITE, line1);
  286.  
  287.             if(!isnull(line2))
  288.                 SendClientMessage(i, WHITE, line2);
  289.         }
  290.     }
  291.  
  292.     return 1;
  293. }
  294.  
  295.  
  296. public IRC_OnConnect(botid, ip[], port)
  297. {
  298.     if(!irc_Active)
  299.         return 1;
  300.  
  301.     printf("[IRC] IRC_OnConnect: Bot ID %d connected to %s:%d", botid, ip, port);
  302.  
  303.     IRC_SendRaw(botid, sprintf("ns identify %s", irc_BotPass));
  304.  
  305.     IRC_JoinChannel(botid, irc_ChatChan, irc_ChatPass);
  306.     IRC_JoinChannel(botid, irc_StaffChan, irc_StaffPass);
  307.  
  308.     IRC_AddToGroup(irc_Group, botid);
  309.  
  310.     return 1;
  311. }
  312.  
  313. public IRC_OnDisconnect(botid, ip[], port, reason[])
  314. {
  315.     printf("[IRC] IRC_OnDisconnect: Bot ID %d disconnected from %s:%d (%s)", botid, ip, port, reason);
  316.     // Remove the bot from the group
  317.     IRC_RemoveFromGroup(irc_Group, botid);
  318.     return 1;
  319. }
  320.  
  321. public IRC_OnJoinChannel(botid, channel[])
  322. {
  323.     // printf("[IRC] IRC_OnJoinChannel: Bot ID %d joined channel %s", botid, channel);
  324.     return 1;
  325. }
  326.  
  327. public IRC_OnLeaveChannel(botid, channel[], message[])
  328. {
  329.     // printf("[IRC] IRC_OnLeaveChannel: Bot ID %d left channel %s (%s)", botid, channel, message);
  330.     return 1;
  331. }
  332.  
  333.  
  334.  
  335. public IRC_OnKickedFromChannel(botid, channel[], oppeduser[], oppedhost[], message[])
  336. {
  337.     printf("[IRC] IRC_OnKickedFromChannel: Bot ID %d kicked by %s (%s) from channel %s (%s)", botid, oppeduser, oppedhost, channel, message);
  338.     IRC_JoinChannel(botid, channel);
  339.     return 1;
  340. }
  341.  
  342. public IRC_OnUserDisconnect(botid, user[], host[], message[])
  343. {
  344.     printf("[IRC] IRC_OnUserDisconnect (Bot ID %d): User %s (%s) disconnected (%s)", botid, user, host, message);
  345.     return 1;
  346. }
  347.  
  348. public IRC_OnUserJoinChannel(botid, channel[], user[], host[])
  349. {
  350.     printf("[IRC] IRC_OnUserJoinChannel (Bot ID %d): User %s (%s) joined channel %s", botid, user, host, channel);
  351.     return 1;
  352. }
  353.  
  354. public IRC_OnUserLeaveChannel(botid, channel[], user[], host[], message[])
  355. {
  356.     printf("[IRC] IRC_OnUserLeaveChannel (Bot ID %d): User %s (%s) left channel %s (%s)", botid, user, host, channel, message);
  357.     return 1;
  358. }
  359.  
  360. public IRC_OnUserKickedFromChannel(botid, channel[], kickeduser[], oppeduser[], oppedhost[], message[])
  361. {
  362.     printf("[IRC] IRC_OnUserKickedFromChannel (Bot ID %d): User %s kicked by %s (%s) from channel %s (%s)", botid, kickeduser, oppeduser, oppedhost, channel, message);
  363. }
  364.  
  365. public IRC_OnUserNickChange(botid, oldnick[], newnick[], host[])
  366. {
  367.     printf("[IRC] IRC_OnUserNickChange (Bot ID %d): User %s (%s) changed his/her nick to %s", botid, oldnick, host, newnick);
  368.     return 1;
  369. }
  370.  
  371. public IRC_OnUserSetChannelMode(botid, channel[], user[], host[], mode[])
  372. {
  373.     printf("[IRC] IRC_OnUserSetChannelMode (Bot ID %d): User %s (%s) on %s set mode: %s", botid, user, host, channel, mode);
  374.     return 1;
  375. }
  376.  
  377. public IRC_OnUserSetChannelTopic(botid, channel[], user[], host[], topic[])
  378. {
  379.     printf("[IRC] IRC_OnUserSetChannelTopic (Bot ID %d): User %s (%s) on %s set topic: %s", botid, user, host, channel, topic);
  380.     return 1;
  381. }
  382.  
  383.  
  384. stock SendIrcChatMessage(name[], text[])
  385. {
  386.     new
  387.         message[7 + MAX_PLAYER_NAME + 128];
  388.  
  389.     strins(name, ".", strlen(name) / 2, MAX_PLAYER_NAME);
  390.  
  391.     format(message, sizeof(message), "[-][%s]: %s", name, text);
  392.  
  393.     IRC_GroupSay(irc_Group, irc_ChatChan, message);
  394.  
  395.     return 1;
  396. }
  397.  
  398. stock SendIrcStaffMessage(name[], text[])
  399. {
  400.     new
  401.         message[7 + MAX_PLAYER_NAME + 128];
  402.  
  403.     strins(name, ".", strlen(name) / 2, MAX_PLAYER_NAME);
  404.  
  405.     format(message, sizeof(message), "[-][%s]: %s", name, text);
  406.  
  407.     IRC_GroupSay(irc_Group, irc_StaffChan, message);
  408.  
  409.     return 1;
  410. }
Add Comment
Please, Sign In to add comment