31
Off Topic / Christmas about to terminated
« on: 2016-Dec-24 »
Have you seen Santa Connor?
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
// --------------------------------- //
// Project: IRC
// Start: Sunday, October 30, 2016
// IDE Version: 14.401
// SETCURRENTDIR("Media") // go to media files
IRC_connect("irc.freenode.org", "Chatbot", "glbasic")
IRC_send("I'm a bot ask me something")
WHILE TRUE
// LOCAL msg$ = STDIN$()
LOCAL what$, sender$, text$
IF IRC_recv(what$, sender$, text$)
STDOUT "From: "+sender$+": what:" + what$+" text:"+text$+"\n"
ENDIF
IF what$="NOTICE"
IRC_send(sender$ + ", ask me anything but " +text$)
ENDIF
WEND
GLOBAL gIRC_sock%=-1
GLOBAL gIRC_nick$
GLOBAL gIRC_channel$
GLOBAL gIRC_server$
FUNCTION IRC_connect%: server$, nick$, channel$
SOCK_INIT()
gIRC_sock% = SOCK_TCPCONNECT(server$, 6667)
IF gIRC_sock%=-1
STDOUT "Error - connect\n"
STDOUT NETGETLASTERROR$()+"\n"
ENDIF
SOCK_SETBLOCKING gIRC_sock%, TRUE
LOCAL what$, sender$, text$
gIRC_nick$ = nick$
gIRC_channel$ = channel$
gIRC_server$ = server$
// SOCK_TCPSEND(gIRC_sock%, "PASS *\r\n")
SOCK_TCPSEND(gIRC_sock%, "NICK " + nick$+"\r\n")
IRC_recv(what$, sender$, text$)
SLEEP 2000
IRC_recv(what$, sender$, text$)
SOCK_TCPSEND(gIRC_sock%, "USER " + nick$+" 8 * :"+nick$+" v1.0 (glbasic.com)\r\n")
IRC_recv(what$, sender$, text$)
STDOUT "**** JOIN *****\n"
IRC_recv(what$, sender$, text$)
// SOCK_TCPSEND(gIRC_sock%, "QUIT\r\n")
// SOCK_TCPSEND(gIRC_sock%, "PRIVMSG NickServ register password test@gmail.com\r\n")
SOCK_TCPSEND(gIRC_sock%, "JOIN #"+channel$+"\r\n")
SOCK_SETBLOCKING gIRC_sock%, FALSE
STDOUT "**** reading *****\n"
ENDFUNCTION
FUNCTION IRC_send%: text$
SOCK_TCPSEND(gIRC_sock%, "PRIVMSG #"+gIRC_channel$+" :"+text$+"\r\n")
ENDFUNCTION
FUNCTION IRC_parse%: BYREF msg$, BYREF what$, BYREF sender$, BYREF text$
LOCAL cmd$ = LEFT$(msg$, 4)
IF cmd$ = "PING"
STDOUT "pong:"+MID$(msg$, 5)+"\n"
SOCK_TCPSEND(gIRC_sock%, "PONG "+MID$(msg$, 5) + "\r\n")
ENDIF
// message
// :KungPhoo!~kungphoo@p548E4C59.dip0.t-ipconnect.de PRIVMSG #glbasic :test
// private message
// :KungPhoo!~kungphoo@p548E4C59.dip0.t-ipconnect.de NOTICE Chatbot :xxx
// KunhPhoo joined
// :KungPhoo!~kungphoo@p548E4C59.dip0.t-ipconnect.de JOIN #glbasic
// KungPhoo left
// :KungPhoo!~kungphoo@p548E4C59.dip0.t-ipconnect.de PART #glbasic
// Chatbot quit
// :Chatbot!~Chatbot@p548E4C59.dip0.t-ipconnect.de QUIT :Ping timeout: 268 seconds
// KungPhoo kicked Chatbot
// :KungPhoo5!~kungphoo@p548E4C59.dip0.t-ipconnect.de KICK #MinionsDomain Chatbot :KungPhoo5
// Excess flooding:
// ERROR :Closing Link: Chatbot by dreamhack.se.quakenet.org (Excess Flood)
IF LEN(msg$)
LOCAL tok$[]
SPLITSTR(msg$, tok$[], " ")
IF LEN(tok$[]) > 2
LOCAL who$[]
SPLITSTR(tok$[0], who$[], ":!")
IF LEN(who$[]) THEN sender$ = who$[0]
LOCAL pos% = INSTR(msg$, ":", 1) + 1
// Message
IF tok$[1] = "PRIVMSG"
what$ = "MSG"
text$ = MID$(msg$, pos)
RETURN TRUE
ENDIF
// whisper
IF tok$[1] = "NOTICE"
what$ = "NOTICE"
text$ = MID$(msg$, pos)
IF sender$ = gIRC_server$
STDOUT "Msg from server: "+msg$+"\n"
RETURN FALSE // skip them
ENDIF
RETURN TRUE
ENDIF
IF tok$[1] = "JOIN"
what$ = "JOIN"
text$ = MID$(msg$, pos)
RETURN TRUE
ENDIF
IF tok$[1] = "KICK" AND LEN(tok$[])>3 AND tok$[3]=gIRC_nick$
STDOUT "*** got kicked ***\n"
tok$[1]="QUIT"
sender$ = gIRC_nick$
ENDIF
IF tok$[1] = "PART" OR tok$[1] = "QUIT"
what$ = "QUIT"
IF sender$ = gIRC_nick$
STDOUT "*** RECONNECT ***\n"
SOCK_TCPSEND(gIRC_sock%, "JOIN #"+gIRC_channel$+"\r\n")
ENDIF
RETURN TRUE
ENDIF
ENDIF
ENDIF
ENDFUNCTION
FUNCTION IRC_recv%: BYREF what$, BYREF sender$, BYREF text$
what$=""
sender$=""
text$=""
IF gIRC_sock% =-1 THEN RETURN FALSE
STATIC msg$
msg$=""
SOCK_RECV(gIRC_sock%, msg$, 8*1024)
IF NOT LEN(msg$) THEN RETURN FALSE
LOCAL rv% = FALSE
LOCAL tok$[]
SPLITSTR(msg$, tok$[], "\r\n")
FOREACH t$ IN tok$[]
IF IRC_parse(t$, what$, sender$, text$)
rv%=TRUE
STDOUT t$+"\n"
ENDIF
NEXT
RETURN rv%
ENDFUNCTION
LOCAL pp% = WildcardTextCompare("abcdefg", "*d*g*") // returns "3" -> where the "d" is
// case sensitive!
IMPORT int WildcardTextCompare(const char*, const char*)
INLINE
// #NEED find first non* character match
//---------------------------------------------------------------//
// This function compares text strings, one of which can have wildcards ('*').
// http://www.drdobbs.com/architecture-and-design/matching-wildcards-an-algorithm/210200888
// GF: improved version
// returns 0..x -> position of first non wildcard character
// -1 -> no match
//---------------------------------------------------------------//
int WildcardTextCompare(
const char* pTameText, // A string without wildcards
const char* pWildText // A (potentially) corresponding string with wildcards
)
{
const char* pOrigTame=pTameText;
const char* pFirstMatch=NULL;
int bMatch = TRUE;
const char* pAfterLastWild = NULL; // The location after the last '*', if we’ve encountered one
const char* pAfterLastTame = NULL; // The location in the tame string, from which we started after last wildcard
char t, w;
// Walk the text strings one character at a time.
for(;;)
{
t = *pTameText;
w = *pWildText;
// How do you match a unique text string?
if (!t)
{
// Easy: unique up on it!
if (!w)
{
break; // "x" matches "x"
}
else if (w == '*')
{
pWildText++;
continue; // "x*" matches "x" or "xy"
}
else if (pAfterLastTame)
{
if (!(*pAfterLastTame))
{
bMatch = FALSE;
break;
}
pTameText = pAfterLastTame++;
pWildText = pAfterLastWild;
continue;
}
bMatch = FALSE;
break; // "x" doesn't match "xy"
}
else
{
if(!pFirstMatch && w==t)
pFirstMatch = pTameText;
// How do you match a tame text string?
if (t != w)
{
// The tame way: unique up on it!
if (w == '*')
{
pAfterLastWild = ++pWildText;
pAfterLastTame = pTameText;
w = *pWildText;
if (!w )
{
break; // "*" matches "x"
}
continue; // "*y" matches "xy"
}
else if (pAfterLastWild)
{
if (pAfterLastWild != pWildText)
{
pWildText = pAfterLastWild;
w = *pWildText;
if (t == w)
{
pWildText++;
}
}
pTameText++;
continue; // "*sip*" matches "mississippi"
}
else
{
bMatch = FALSE;
break; // "x" doesn't match "y"
}
}
}
pTameText++;
pWildText++;
}
if(bMatch)
{
if(pFirstMatch)
return (int)(pFirstMatch-pOrigTame); // return position of first match
return 0; // match at first character
}
return -1; // no match
}
ENDINLINE