Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <socket>
- #define MAX_NICK_LEN 30
- #define MAX_CHANNEL_LEN 32
- #define MAX_BOTS 5
- #define COMMAND_PREFIX '!'
- forward onBotConnect(botid);
- forward onBotDisconnect(botid);
- forward onCTCPRequest(botid, user[], request_msg[], user_ident[]);
- forward onChannelMessage(botid, user[], channel[], message[], user_ident[]);
- forward onIRCCommand(botid, user[], channel[], command[], user_ident[]);
- forward onPrivateMessage(botid, user[], message[], user_ident[]);
- forward onChannelJoin(botid, user[], channel[], user_ident[]);
- forward onChannelPart(botid, user[], channel[], part_msg[], user_ident[]);
- forward onBotNotice(botid, source[], message[], user_ident);
- forward onUserQuit(botid, user[], leaving_msg[], user_ident[]);
- forward onNicknameChange(botid, user[], new_name[], user_ident[]);
- forward onChannelKick(botid, user[], victim[], channel[], reason[], user_ident[]);
- forward onBotInvite(botid, user[], channel[], user_ident[]);
- forward onRawCommand(botid, raw_code, raw_data[]);
- forward onChannelMode(botid, user[], channel[], mode[], option[], user_ident[]);
- forward onTopicChange(botid, user[], channel[], topic[], user_ident[]);
- enum botInfo {
- Socket:socket,
- sNickname[MAX_NICK_LEN],
- sUsername[MAX_NICK_LEN],
- sRealName[MAX_NICK_LEN*2],
- sServer[80],
- iPort,
- bool:bConnected,
- }
- new g_aIRCBot[MAX_BOTS][botInfo];
- new g_iBots = -1;
- stock ircbot_connect(server[], port, nickname[], username[], realname[])
- {
- new sData[256];
- g_iBots++;
- g_aIRCBot[g_iBots][socket] = socket_create(TCP);
- if(is_socket_valid(g_aIRCBot[g_iBots][socket])) {
- g_aIRCBot[g_iBots][bConnected] = false;
- format(g_aIRCBot[g_iBots][sServer], 80, server);
- g_aIRCBot[g_iBots][iPort] = port;
- format(g_aIRCBot[g_iBots][sNickname], MAX_NICK_LEN, nickname);
- format(g_aIRCBot[g_iBots][sUsername], MAX_NICK_LEN, username);
- format(g_aIRCBot[g_iBots][sRealName], MAX_NICK_LEN*2, realname);
- socket_connect(g_aIRCBot[g_iBots][socket], g_aIRCBot[g_iBots][sServer], g_aIRCBot[g_iBots][iPort]);
- format(sData, sizeof sData, "NICK %s\r\nUSER %s - - :%s\r\nMODE %s +B\r\n", g_aIRCBot[g_iBots][sNickname], g_aIRCBot[g_iBots][sUsername], g_aIRCBot[g_iBots][sRealName], g_aIRCBot[g_iBots][sNickname]);
- socket_send(g_aIRCBot[g_iBots][socket], sData);
- return g_iBots;
- }
- g_iBots--;
- return (-1);
- }
- stock ircbot_reconnect(botid)
- {
- new sData[80];
- if(is_socket_valid(g_aIRCBot[botid][socket]) && !g_aIRCBot[botid][bConnected]) {
- g_aIRCBot[botid][socket] = socket_create(TCP);
- socket_connect(g_aIRCBot[botid][socket], g_aIRCBot[botid][server], g_aIRCBot[botid][port]);
- format(sData, sizeof sData, "NICK %s\r\nUSER %s - - :%s\r\nMODE %s +B\r\n", g_aIRCBot[botid][sNickname], g_aIRCBot[botid][sUsername], g_aIRCBot[botid][sRealName], g_aIRCBot[botid][sNickname]);
- socket_send(g_aIRCBot[botid][socket], sData);
- return 1;
- }
- return 0;
- }
- stock ircbot_say(botid, receiver[], message[])
- {
- new sMessage[256];
- if(is_socket_valid(g_aIRCBot[botid][socket]) && g_aIRCBot[botid][bConnected]) {
- format(sMessage, sizeof sMessage, "PRIVMSG %s :%s\r\n", receiver, message);
- socket_send(g_aIRCBot[botid][socket], sMessage);
- return 1;
- }
- return 0;
- }
- // /me command
- stock ircbot_action(botid, receiver[], message[])
- {
- new sMessage[256];
- if(is_socket_valid(g_aIRCBot[botid][socket]) && g_aIRCBot[botid][bConnected]) {
- format(sMessage, sizeof sMessage, "PRIVMSG %s :\001ACTION %s\001\r\n", receiver, message);
- socket_send(g_aIRCBot[botid][socket], sMessage);
- return 1;
- }
- return 0;
- }
- stock ircbot_kick(botid, channel[], user[], reason[])
- {
- new sMessage[256];
- if(is_socket_valid(g_aIRCBot[botid][socket]) && g_aIRCBot[botid][bConnected]) {
- format(sMessage, sizeof sMessage, "KICK %s %s %s", channel, user, reason);
- socket_send(g_aIRCBot[botid][socket], sMessage);
- return 1;
- }
- return 0;
- }
- stock ircbot_notice(botid, receiver[], message[])
- {
- new sMessage[256];
- if(is_socket_valid(g_aIRCBot[botid][socket]) && g_aIRCBot[botid][bConnected]) {
- format(sMessage, sizeof sMessage, "NOTICE %s %s", receiver, message);
- socket_send(g_aIRCBot[botid][socket], sMessage);
- return 1;
- }
- return 0;
- }
- stock is_bot_connected(botid)
- {
- return g_aIRCBot[botid][bConnected];
- }
- stock ircbot_join(botid, channel[])
- {
- new sData[MAX_CHANNEL_LEN+9];
- if(is_socket_valid(g_aIRCBot[botid][socket]) && g_aIRCBot[botid][bConnected]) {
- format(sData, sizeof sData, "JOIN %s\r\n", channel);
- socket_send(g_aIRCBot[botid][socket], sData);
- return 1;
- }
- return 0;
- }
- stock ircbot_part(botid, channel[], message[])
- {
- new sData[256];
- if(is_socket_valid(g_aIRCBot[botid][socket]) && g_aIRCBot[botid][bConnected]) {
- format(sData, sizeof sData, "PART %s :%s\r\n", channel, msg);
- socket_send(g_aIRCBot[botid][socket], sData);
- return 1;
- }
- return 0;
- }
- stock ircbot_quit(botid, message[])
- {
- new sData[256];
- if(is_socket_valid(g_aIRCBot[botid][socket]) && g_aIRCBot[botid][bConnected]) {
- format(sData, sizeof sData, "QUIT :%s\r\n", message);
- socket_send(g_aIRCBot[botid][socket], sData);
- return 1;
- }
- return 0;
- }
- stock ircbot_raw(botid, raw_cmd[])
- {
- new sData[256];
- if(is_socket_valid(g_aIRCBot[botid][socket]) && g_aIRCBot[botid][bConnected]) {
- format(sData, sizeof sData, "%s\r\n", raw_cmd);
- socket_send(g_aIRCBot[botid][socket], sData);
- return 1;
- }
- return 0;
- }
- stock ircbot_setmode(botid, channel[], mode[])
- {
- new sData[256];
- if(is_socket_valid(g_aIRCBot[botid][socket]) && g_aIRCBot[botid][bConnected]) {
- format(sData, sizeof sData, "MODE %s %s\r\n", channel, mode);
- socket_send(g_aIRCBot[botid][socket], sData);
- return 1;
- }
- return 0;
- }
- public onSocketAnswer(Socket:id, data[], data_len)
- {
- for(new i;i < MAX_BOTS;i++) {
- if(g_aIRCBot[i][socket] == id) {
- // :server 376 nickname :End of /MOTD command.
- if(!g_aIRCBot[g_iBots][bConnected]) {
- if(strfind(data, ":End of /MOTD command.") != -1) {
- g_aIRCBot[g_iBots][bConnected] = true;
- // onBotConnect(botid);
- CallRemoteFunction("onBotConnect", "i", i);
- return 0;
- }
- }
- if(!g_aIRCBot[g_iBots][bConnected] || !Ping(id, data)) return 0;
- new strParts[5][128];
- split(data, strParts, 5, ' ');
- if(!isNumeric(strParts[1])) {
- new sUser[MAX_NICK_LEN], sIdent[60];
- substr(sIdent, strParts[0], 1);
- FilterNickname(strParts[0], sUser);
- // lets parse all incoming events
- if(!strcmp(strParts[1], "PRIVMSG")) {
- new sMessage[256];
- trim(array_slice(data, 3), sMessage);
- if(sMessage[0] == COMMAND_PREFIX && sMessage[1] != ' ')
- CallRemoteFunction("onIRCCommand", "issss", i, sUser, strParts[2], sMessage, sIdent);
- if(sMessage[0] == '\001') {
- strmid(sMessage, sMessage, 1, strlen(sMessage)-1);
- // onCTCPRequest(botid, user[], request_msg[], user_ident[]);
- CallRemoteFunction("onCTCPRequest", "isss", i, sUser, sMessage, sIdent);
- } else if(strParts[2][0] == '#') {
- // onChannelMessage(botid, user[], channel[], message[], user_ident[]);
- CallRemoteFunction("onChannelMessage", "issss", i, sUser, strParts[2], sMessage, sIdent);
- } else {
- // onPrivateMessage(botid, user[], message[], user_ident[]);
- CallRemoteFunction("onPrivateMessage", "isss", i, sUser, sMessage, sIdent);
- }
- } else if(!strcmp(strParts[1], "JOIN")) {
- new sChannel[MAX_CHANNEL_LEN];
- strmid(sChannel, strParts[2], 1, strlen(strParts[2])-1);
- trim(sChannel, sChannel);
- // onChannelJoin(botid, user[], channel[], user_ident[]);
- CallRemoteFunction("onChannelJoin", "isss", i, sUser, sChannel, sIdent);
- } else if(!strcmp(strParts[1], "PART")) {
- new sMessage[128];
- if(strlen(strParts[3]) > 0)
- trim(array_slice(data, 3), sMessage);
- else
- format(sMessage, sizeof sMessage, "None");
- // onChannelPart(botid, user[], channel[], part_msg[], user_ident[]);
- CallRemoteFunction("onChannelPart", "issss", i, sUser, strParts[2], sMessage, sIdent);
- } else if(!strcmp(strParts[1], "NOTICE")) {
- new sMessage[128];
- trim(array_slice(data, 3), sMessage);
- // onBotNotice(botid, source[], message[], user_ident);
- CallRemoteFunction("onBotNotice", "issss", i, strParts[2], sMessage, sIdent);
- } else if(!strcmp(strParts[1], "QUIT")) {
- new sMessage[128];
- trim(array_slice(data, 2), sMessage);
- // onUserQuit(botid, user[], leaving_msg[], user_ident[]);
- CallRemoteFunction("onUserQuit", "isss", i, sUser, sMessage, sIdent);
- } else if(!strcmp(strParts[1], "NICK")) {
- new sNew[128];
- substr(sNew, strParts[2], 1);
- // onNicknameChange(botid, user[], new_name[], user_ident[]);
- CallRemoteFunction("onNicknameChange", "isss", i, sUser, sNew, sIdent);
- } else if(!strcmp(strParts[1], "KICK")) {
- new sMessage[128];
- trim(array_slice(data, 4), sMessage);
- // onChannelKick(botid, user[], victim[], channel[], reason[], user_ident[]);
- CallRemoteFunction("onChannelKick", "isssss", i, sUser, strParts[3], strParts[2], sMessage, sIdent);
- } else if(!strcmp(strParts[1], "INVITE")) {
- new sChannel[MAX_CHANNEL_LEN];
- substr(sChannel, strParts[3], 1);
- // onBotInvite(botid, user[], channel[], user_ident[]);
- CallRemoteFunction("onBotInvite", "iss", i, sUser, sChannel, sIdent);
- } else if(!strcmp(strParts[1], "MODE")) {
- new sOption[256];
- if(strlen(strParts[4]) > 0)
- trim(array_slice(data, 4, 1), sOption);
- // onChannelMode(botid, user[], channel[], mode[], option[], user_ident[]);
- CallRemoteFunction("onChannelMode", "isssss", i, sUser, strParts[2], strParts[3], sOption, sIdent);
- } else if(!strcmp(strParts[1], "TOPIC")) {
- new sTopic[256];
- trim(array_slice(data, 3), sTopic);
- // onTopicChange(botid, user[], channel[], topic[], user_ident[]);
- CallRemoteFunction("onTopicChange", "issss", i, sUser, strParts[2], sTopic, sIdent);
- }
- } else {
- CallRemoteFunction("onRawCommand", "iis", i, strval(strParts[1]), array_slice(data, 2, 1));
- }
- }
- break;
- }
- return 1;
- }
- public onSocketClose(Socket:id)
- {
- for(new i;i < MAX_BOTS;i++) {
- if(g_aIRCBot[i][socket] == id) {
- if(g_aIRCBot[i][bConnected]) {
- CallRemoteFunction("onBotDisconnect", "i", i);
- g_aIRCBot[i][bConnected] = false;
- }
- }
- }
- return 1;
- }
- stock substr(dest[], source[], pos, max_len = sizeof(dest))
- {
- return strmid(dest, source, pos, strlen(source), max_len);
- }
- stock trim(string_data[], dest[])
- {
- for(new i, a = strlen(string_data);i < a;i++)
- {
- if(string_data[i] != '\r' && string_data[i] != '\n'/* string_data[i] != '\001'*/)
- dest[i] = string_data[i];
- }
- }
- stock FilterNickname(const ident[], dest[])
- {
- // :nick!user@hostname
- new pos = strfind(ident, "!");
- return strmid(dest, ident, 1, pos, MAX_NICK_LEN);
- }
- // php like behaviour
- stock array_slice(array[], offset, start_pos = 2)
- {
- new pCount = 0, final[1024];
- for(new i, a = strlen(array);i < a;i++)
- {
- if(array[i] == ' ') {
- pCount++;
- if(pCount == offset) {
- // skip :msg
- strmid(final, array, i+start_pos, a);
- break;
- }
- }
- }
- return final;
- }
- stock Ping(Socket:id, raw_data[])
- {
- // handle ping pong
- new idx, string[256];
- string = strtok(raw_data, idx);
- if(!strcmp(string, "PING")) {
- new pong[64];
- string = strtok(raw_data, idx);
- format(pong, sizeof pong, "PONG :%s\r\n", string);
- socket_send(id, pong);
- return 0;
- }
- return 1;
- }
- stock isNumeric(const string[])
- {
- new len = strlen(string);
- if(!len) return 0;
- for (new i = 0; i < len; i++) {
- if(string[i] < '0' || string[i] > '9') return 0;
- }
- return 1;
- }
- strtok(const string[], &index)
- {
- new length = strlen(string);
- while ((index < length) && (string[index] <= ' '))
- {
- index++;
- }
- new offset = index;
- new result[256];
- while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
- {
- result[index - offset] = string[index];
- index++;
- }
- result[index - offset] = EOS;
- return result;
- }
- // (c) to whoever made this function
- stock split(const strsrc[], strdest[][], limit, delimiter = '|')
- {
- new i, li, aNum, len, srclen = strlen(strsrc);
- while(i <= srclen && aNum < limit)
- {
- if (strsrc[i] == delimiter || i == srclen)
- {
- len = strmid(strdest[aNum], strsrc, li, i, 128);
- strdest[aNum][len] = 0;
- li = i + 1;
- aNum++;
- }
- i++;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement