fb:porticula NoPaste
test_irc.bas
Uploader: | ThePuppetMaster |
Datum/Zeit: | 28.08.2011 05:03:52 |
Hinweis: Dieser Quelltext ist Bestandteil des Projekts IRC.bi, zu dem es auf FreeBASIC-Portal.de eine Projektseite gibt.
Warnung: Es steht bereits eine neuere Version des Quelltexts zur Verfügung. Die hier vorliegende alte Version könnte Fehler enthalten, die in der neuen Version vielleicht ausgebessert wurden.
'-----------------------------------------------------------------------------------------------------------------
'Include the IRC Protocol and Managment (IRC.bi need TSNE_V3 whats included in irc.bi)
#include once "irc.bi"
'-----------------------------------------------------------------------------------------------------------------
Dim T_IRCID as UInteger 'Store the ID for the individual IRC comunication Port
'Multiple IRC connections can be establish at the same time.
'Each connection has a own ID
'-----------------------------------------------------------------------------------------------------------------
Dim T_RV as IRC_Return_Enum 'This Variable stores the Return code from each function call's
'-----------------------------------------------------------------------------------------------------------------
'This example demonstrates the using of Callbacks in IRC.bi
'We build a lot of Callbacks, for each function that IRC.bi supports.
'Generaly: If a callback not registered by IRC.bi then no Callback will called.
'That means. If u don't want a PING Callback (as example), so u doesnt create and register it.
'That saves system memory and CPU usage.
'-----------------------------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------
'This Callback calls IRC.bi on After a successfully connection establishmend. (Before te IRC_Connect() Function returns)
Sub IRC_Connected(ByVal V_IRCID as UInteger)
Print "[" & Str(V_IRCID) & "] Connected."
End Sub
'-----------------------------------------------------------------------------------------------------------------
'Is the Connection was terminated, IRC.bi will notify us by calling this Function.
Sub IRC_Disconnected(ByVal V_IRCID as UInteger)
Print "[" & Str(V_IRCID) & "] Disconnected."
End Sub
'-----------------------------------------------------------------------------------------------------------------
'This Callback calls IRC.bi on each PING message from the Server.
Sub IRC_PING(ByVal V_IRCID as UInteger, ByVal V_Message as String)
Print "[" & Str(V_IRCID) & "] PING with message: '" & V_Message & "'."
End Sub
'-----------------------------------------------------------------------------------------------------------------
'Any server (NOT ALL!!!) support a PONG by sending a PING to it. This Callback retrives the PONG answer from the Server.
Sub IRC_PONG(ByVal V_IRCID as UInteger, ByVal V_Message as String)
Print "[" & Str(V_IRCID) & "] PONG with message: '" & V_Message & "'."
End Sub
'-----------------------------------------------------------------------------------------------------------------
'After a connecting and successfully registration onto a IRC Server, the Server reports the Client his Informations
'Such as Servername, -version and his supportet User and Channel modes.
Sub IRC_ServerInfo(ByVal V_IRCID as UInteger, ByVal V_Servername as String, ByVal V_ServerVersion as String, ByVal V_SupportetUserModes as String, ByVal V_SupportetChannelModes as String)
Print "[" & Str(V_IRCID) & "] Server informations: Name: '" & V_Servername & "' Version: '" & V_ServerVersion & "' UserModes: '" & V_SupportetUserModes & "' ChannelModes: '" & V_SupportetChannelModes & "'."
End Sub
'-----------------------------------------------------------------------------------------------------------------
'MOTD (Message of the Day) recived!
'Messages including Color's, bold, underline, ... was converted into a BBCode like form!
Sub IRC_MOTD(ByVal V_IRCID as UInteger, ByVal V_MOTD_Message as String)
Print "[" & Str(V_IRCID) & "] MOTD begin!"
Print V_MOTD_Message
Print "[" & Str(V_IRCID) & "] MOTD end!"
End Sub
'-----------------------------------------------------------------------------------------------------------------
'Notice Message received!
'Messages including Color's, bold, underline, ... was converted into a BBCode like form!
Sub IRC_Notice(ByVal V_IRCID as UInteger, ByVal V_MessageType as IRC_MessageType_Enum, ByVal V_FromChannel as String, ByVal V_FromNick as String, V_FromNickHost as String, V_Message as String)
Dim T as String = "[" & Str(V_IRCID) & "] NOTICE message: ["
Select Case V_MessageType
Case IRCMT_Global : T += "Global "
Case IRCMT_Channel : T += "Channel"
Case IRCMT_Private : T += "Private"
End Select
Print T & "] FromNickHost: '" & V_FromNickHost & "' FromChannel: '" & V_FromChannel & "' FromNick: '" & V_FromNick & "' Message: '" & V_Message & "'."
End Sub
'-----------------------------------------------------------------------------------------------------------------
'Regular Message received! This can be a Global Server Message, a message to a channel or a privat message
'Messages including Color's, bold, underline, ... was converted into a BBCode like form!
Sub IRC_Message(ByVal V_IRCID as UInteger, ByVal V_MessageType as IRC_MessageType_Enum, ByVal V_FromChannel as String, ByVal V_FromNick as String, V_FromNickHost as String, V_Message as String)
Dim T as String = "[" & Str(V_IRCID) & "] REGULAR message: ["
Select Case V_MessageType
Case IRCMT_Global : T += "Global "
Case IRCMT_Channel : T += "Channel"
Case IRCMT_Private : T += "Private"
End Select
Print T & "] FromNickHost: '" & V_FromNickHost & "' FromChannel: '" & V_FromChannel & "' FromNick: '" & V_FromNick & "' Message: '" & V_Message & "'."
End Sub
'-----------------------------------------------------------------------------------------------------------------
'The ModeUser Callback notify the application about a User Mode Change like a +i (invisible) or so one.
'!!! User modes are NOT ChannelUser Modes !!!
'User modes are modes what channel independent! This can be +i for Invisible for any user's on the server. or +a for Away state and so one.
'Channel dependent modes (like +o for Channel Operator) was called by ModeUserChannel!
Sub IRC_ModeUser(ByVal V_IRCID as UInteger, ByVal V_MyClient as UByte, ByVal V_ModeSet as UByte, ByVal V_Mode as IRC_UserModeType_Enum, ByVal V_SetFromNick as String, ByVal V_SetToNick as String)
Dim T as String = "[" & Str(V_IRCID) & "] USER-MODE Change: Set from Nick: '" & V_SetFromNick &"' Set to Nick: '" & V_SetToNick & "'"
If V_MyClient = 1 Then
T += " [OWN NICK] "
Else: T += " "
End If
If V_ModeSet = 0 Then
T += "Reset Mode: '"
Else: T += "Set Mode: '"
End if
Select Case V_Mode
Case IRCUM_Away : T += "Is Away"
Case IRCUM_LocalOperator : T += "Is a Local Operator"
Case IRCUM_ServerNotice : T += "Can receive Server Notices"
Case IRCUM_Invisible : T += "Is Invisible"
Case IRCUM_Wallops : T += "Can receive Wallops"
Case IRCUM_RegisteredNick : T += "Using registered nick"
Case IRCUM_KillMessages : T += "Can receive KILL messages"
Case IRCUM_HiddenHostname : T += "Using a hidden hostname"
'U cannot sure the following modes are exist on each IRC-Server's!!!
'special UnrealIRCd Usermodes
Case IRCUM_ServerAdmin : T += "Is a Server Administrator"
Case IRCUM_ServiceAdmin : T += "Is a Service Administrator"
Case IRCUM_BOT : T += "Is a BOT"
Case IRCUM_CoAdmin : T += "Is a Co-Administrator"
Case IRCUM_BadWordFilter : T += "Using BAD-WORD filter"
Case IRCUM_SendReadGlobLocOPs : T += "Can send and read Global and Local OPs"
Case IRCUM_HideIRCop : T += "Hide his IRCop Flag"
Case IRCUM_HelpOp : T += "Is a Help Operator"
Case IRCUM_NetworkAdmin : T += "Is a Network Administrator"
Case IRCUM_GlobalOperator : T += "Is a Global Operator"
Case IRCUM_HideChannelIn : T += "Hides the channel are joinded in"
Case IRCUM_OnlyQlineKick : T += "Only Q-Lined Server can KICK him"
Case IRCUM_OnlyRegUserPrivNoteReci : T += "Only registered Users can send Private and Notice to him"
Case IRCUM_ServiceDeamon : T += "Is a Service Deamon"
Case IRCUM_ListenServerNotices : T += "Can listen to Server Notices"
Case IRCUM_NoCTCPs : T += "Can't receive CTCP's"
Case IRCUM_ImUseVHost : T += "Using a virtual Hostname"
Case IRCUM_WebTVUser : T += "Is a WebTV User"
Case IRCUM_InfectedDCCNotice : T += "Was prevent for Infected DCC Notices"
Case IRCUM_WhoisOnMeNotice : T += "Was notify on Whois requests on him"
Case IRCUM_SSLClient : T += "Using a SSL-Connection"
End Select
Print T & "'."
End Sub
'-----------------------------------------------------------------------------------------------------------------
'The ModeUserChannel Callback notify the application about a User Mode Change inside of a Channel like a +v (voice grand) or so one.
'!!! ChannelUser modes are NOT User Modes !!!
'ChannelUser modes are modes what channel dependent! This can be +v for Voice grand for a channel or +o for Channel Operator state and so one.
'Channel independent modes (like +i for Invisible) was called by ModeUser!
Sub IRC_ModeUserChannel(ByVal V_IRCID as UInteger, ByVal V_MyClient as UByte, ByVal V_ModeSet as UByte, ByVal V_Mode as IRC_ChannelUserModeType_Enum, ByVal V_SetFromNick as String, ByVal V_SetToNick as String, ByVal V_SetOnChannel as String)
Dim T as String = "[" & Str(V_IRCID) & "] USERChannel-MODE Change: Set on Channel: '" & V_SetOnChannel & "' Set From Nick: '" & V_SetFromNick &"' Set to Nick: '" & V_SetToNick & "'"
If V_MyClient = 1 Then
T += " [OWN NICK] "
Else: T += " "
End If
If V_ModeSet = 0 Then
T += "Reset Mode: '"
Else: T += "Set Mode: '"
End if
Select Case V_Mode
Case IRCCUMT_ChannelAdmin : T += "Channel Admin"
Case IRCCUMT_ChannelFounder : T += "Channel Founder"
Case IRCCUMT_ChannelOperator : T += "Channel Operator"
Case IRCCUMT_ChannelHalfOperator : T += "Channel HalfOperator"
Case IRCCUMT_ChannelVoice : T += "Channel Voice"
End Select
Print T & "'."
End Sub
'-----------------------------------------------------------------------------------------------------------------
'If u join a channel, or forced joining a channel on the IRC server, this callback will notify u about this action.
Sub IRC_Joined(ByVal V_IRCID as UInteger, ByVal V_MyClient as UByte, ByVal V_Nick as String, ByVal V_NickHost as String, ByVal V_Channel as String)
If V_MyClient = 1 Then
Print "[" & Str(V_IRCID) & "] I joined channel: '" & V_Channel & "'."
Else: Print "[" & Str(V_IRCID) & "] Joined Channel: '" & V_Channel & "' NickHost: '" & V_NickHost & "' Nick: '" & V_Nick & "."
End if
End Sub
'-----------------------------------------------------------------------------------------------------------------
'At first, we create a new IRC Profile. This profile hold the username, nickname , realname, and many other options and configurations.
'R_IRCID will filled by the function, if the profile was successfully created. This ID we need later for all other irc commands.
'V_Username is the IRC Username we want to use. IT IS NOT THE NICKNAME WHAT ALL OTHER PEOPLE CAN SEE AND TALK TO HIM!!!
'V_Realname is the Real Name of the User. It can and must not given. BUT, if it not given, it MUST '*'!!!
'The username can include all letters (a-z, A-Z), numerics (0-9), : and the space char
T_RV = IRC_CreateProfil(T_IRCID, "TestUser", "My name")
If T_RV <> IRCRE_NoError Then 'If a error occured ...
Print "ERROR: [Create] " & IRC_GetReturnDesc(T_RV) '... print the readable version of the errorcode ...
End '... and exit the application
End If
'-----------------------------------------------------------------------------------------------------------------
'We add a Nickname, we want to use, in this Profile. We can add Nicknames how much we want. No Limit!
'V_IRCID Is the IRC Profile ID what we have created.
'V_Nickname is the Nickname we want to add
'The IRC.bi will use the first Nickname. But, if this name is in use, the second was probe, and so one.
'Will all Nicknames are in use, IRC.bi reports an error while establisch the communication.
'The nickname can include all letters (a-z, A-Z), numerics (0-9), a lot of special chars ([, ], \, `, _, ^, [, |, }) and the - sign (-)
'The First Char of the nickname MUST be a letter or a spezial char!
T_RV = IRC_AddNickname(T_IRCID, "MyNick"): If T_RV <> IRCRE_NoError Then Print "ERROR: [Nick-1] " & IRC_GetReturnDesc(T_RV): End
T_RV = IRC_AddNickname(T_IRCID, "MyNick1"): If T_RV <> IRCRE_NoError Then Print "ERROR: [Nick-2] " & IRC_GetReturnDesc(T_RV): End
T_RV = IRC_AddNickname(T_IRCID, "MyNick2"): If T_RV <> IRCRE_NoError Then Print "ERROR: [Nick-3] " & IRC_GetReturnDesc(T_RV): End
'-----------------------------------------------------------------------------------------------------------------
'At last, we register the Callbacks
'If a Callback is not registered, the Callback will not fired if it raised!
'It's NOT a MUST to register any callback!
T_RV = IRC_SetCallback(T_IRCID, IRCC_Connected, @IRC_Connected): If T_RV <> IRCRE_NoError Then Print "ERROR: [Callback] " & IRC_GetReturnDesc(T_RV): End
T_RV = IRC_SetCallback(T_IRCID, IRCC_Disconnected, @IRC_Disconnected): If T_RV <> IRCRE_NoError Then Print "ERROR: [Callback] " & IRC_GetReturnDesc(T_RV): End
T_RV = IRC_SetCallback(T_IRCID, IRCC_PING, @IRC_PING): If T_RV <> IRCRE_NoError Then Print "ERROR: [Callback] " & IRC_GetReturnDesc(T_RV): End
T_RV = IRC_SetCallback(T_IRCID, IRCC_PONG, @IRC_PONG): If T_RV <> IRCRE_NoError Then Print "ERROR: [Callback] " & IRC_GetReturnDesc(T_RV): End
T_RV = IRC_SetCallback(T_IRCID, IRCC_ServerInfo, @IRC_ServerInfo): If T_RV <> IRCRE_NoError Then Print "ERROR: [Callback] " & IRC_GetReturnDesc(T_RV): End
T_RV = IRC_SetCallback(T_IRCID, IRCC_MOTD, @IRC_MOTD): If T_RV <> IRCRE_NoError Then Print "ERROR: [Callback] " & IRC_GetReturnDesc(T_RV): End
T_RV = IRC_SetCallback(T_IRCID, IRCC_Notice, @IRC_Notice): If T_RV <> IRCRE_NoError Then Print "ERROR: [Callback] " & IRC_GetReturnDesc(T_RV): End
T_RV = IRC_SetCallback(T_IRCID, IRCC_Message, @IRC_Message): If T_RV <> IRCRE_NoError Then Print "ERROR: [Callback] " & IRC_GetReturnDesc(T_RV): End
T_RV = IRC_SetCallback(T_IRCID, IRCC_ModeUser, @IRC_ModeUser): If T_RV <> IRCRE_NoError Then Print "ERROR: [Callback] " & IRC_GetReturnDesc(T_RV): End
T_RV = IRC_SetCallback(T_IRCID, IRCC_ModeUserChannel, @IRC_ModeUserChannel): If T_RV <> IRCRE_NoError Then Print "ERROR: [Callback] " & IRC_GetReturnDesc(T_RV): End
T_RV = IRC_SetCallback(T_IRCID, IRCC_Joined, @IRC_Joined): If T_RV <> IRCRE_NoError Then Print "ERROR: [Callback] " & IRC_GetReturnDesc(T_RV): End
'-----------------------------------------------------------------------------------------------------------------
'We can set a lot of options.
'This Option activate the automatic PONG answer what will send if a server sends a PING Request.
'The PING Callback will fired too, if the Callback is registered!
T_RV = IRC_SetOption(T_IRCID, IRCO_AutoPong, 1): If T_RV <> IRCRE_NoError Then Print "ERROR: [AutoPong] " & IRC_GetReturnDesc(T_RV): End
'Autoping activates a automatical PING sending to the server to preserve the current conection.
T_RV = IRC_SetOption(T_IRCID, IRCO_AutoPing, 1): If T_RV <> IRCRE_NoError Then Print "ERROR: [AutoPing] " & IRC_GetReturnDesc(T_RV): End
'NoCutMessages stops intelligent cutting of 'too long' messages while sending Messages and notices
'T_RV = IRC_SetOption(T_IRCID, IRCO_NoCutMessages, 1): If T_RV <> IRCRE_NoError Then Print "ERROR: [AutoPing] " & IRC_GetReturnDesc(T_RV): End
'NoBBCode Stops all BBCode translations (See sending messages or notices)
T_RV = IRC_SetOption(T_IRCID, IRCO_NoBBCode, 1): If T_RV <> IRCRE_NoError Then Print "ERROR: [AutoPing] " & IRC_GetReturnDesc(T_RV): End
'-----------------------------------------------------------------------------------------------------------------
'Now we can connect to the server by using the IRC_Connect command.
'V_IRCID Is the IRC Profile ID what we have created.
'V_Host are the Hostname or IP-Adress of the IRC Server
'V_Port are by default 6667 (Standard IRC TCP-Port
'V_Timeout by default 60 seconds and are the standard for TCP Connections
'The timeout results in the connecting timeout and, after a successfully connecting, in the try and respons time from the first
'connecting sequenzes to the IRC server. So ... If we use a timeout of 10 seconds, and the server connection is established after 9 seconds
'we have 10 seconds more for the communication process.
'By Default, a value of 60 seconds are absolutly OK!
Print "Connecting to server..."
T_RV = IRC_Connect(T_IRCID, "mln.ath.cx")
'T_RV = IRC_Connect(T_IRCID, "chat.eu.freenode.net")
If T_RV <> IRCRE_NoError Then Print "ERROR: [Connect] " & IRC_GetReturnDesc(T_RV): End
Print "Connected!"
'-----------------------------------------------------------------------------------------------------------------
'If a Connection was successfully established, we can JOIN channels.
T_RV = IRC_Join(T_IRCID, "#ircbi"): If T_RV <> IRCRE_NoError Then Print "ERROR: [Join] " & IRC_GetReturnDesc(T_RV)
'-----------------------------------------------------------------------------------------------------------------
'Now we send a message into a Channel
'The Message len can so long as u want! But, if it longer then 500 Bytes, then IRC.bi will cut it into peaces and send it.
'IRC.bi will try to cut the Messages intelligent. This means, that a Cut (if posibly) NOT places inside a word.
'A Cut will be made on each space char or after a Dot or so one. The Results are Line based cutting.
'U Can Stop this handling by set the 'IRCO_NoCutMessages' Option befor transmitting messages, or set it temporaly
'by set a '1' at the 4. parameter on IRC_SendMessages() or ....Notice() Function.
'Still note, that the IRC.bi will cutting the messages too, but without intelligent function!
'U can use BBCode syntax to use Highlighte btw. underline, colorized, bold, ... Text.
'BUT note! Text style modification's must accept the Server (This can filtered out by the Server, but regular was it not!) ...
'AND must recognized by the target client btw. client's! Else, no style changes will afect on the other side.
'BBCodes what understand IRC.bi are:
'Most clients supports the folloing Styles
'[b] <text> [/b] = Bold
'[u] <text> [/u] = Underlined
'[r] <text> [/r] = Reverse btw. Invers (Swap Backcolor and Forecolor)
'This styles are supportet by realy small group of client's
'[i] <text> [/i] = Italic
'[o] <text> [/o] = Overstrike
'[p] <text> [/p] = Proportional Font (Monospace Chars)
'[g] <text> [/g] = Greek Font
'[size=n] <text> [/size] = Change the Fontsize to n. N can be: -1 -> -4 / +1 -> +4 or a numeric value between 1 and 71.
' A numeric value of 0 will reset the Fontsize to the regular configuration of the Target-Client size (Default).
'[color=Fore] <text> <[/color] = Methode 1 (Worst). Change the Forecolor. PLEASE do NOT use this BBCode-Style. Its the worst way to change color!
'[color=Fore,Back] <text> <[/color] = Methode 2 (OK). Change the Forecolor and Backcolor.
' A Color must be a value between 1 and 15.
'A BBCode encodet Text looks like: Hello! [i]This is Ilalic.[/i] [b]And this is Bold. [i]And this is Italic and Bold too[/i][/b]
'If u don't want using BBCode Translation, u can switch it of by using 'IRCO_NoBBCode' in the Option's.
'This will stop's ALL translations by sending and in received messages!
'Else, u can stop translating temporaly by set '1' in the Function parameter 'V_NoBBCode'
'The Messages can contain CRLF. IRC.bi will automaticly cut and transmit the Lines. Linesbreaks can be LF or CRLF!
'Regular message with intelligent cutting if longer then 500 Bytes
T_RV = IRC_SendMessage(T_IRCID, "#test", "Hello! This is a short message!"): If T_RV <> IRCRE_NoError Then Print "ERROR: [Message] " & IRC_GetReturnDesc(T_RV)
'The same, but with temporaly 'NoCutMessages' Option
T_RV = IRC_SendMessage(T_IRCID, "#test", "Hello! This is a short message!", 1): If T_RV <> IRCRE_NoError Then Print "ERROR: [Message] " & IRC_GetReturnDesc(T_RV)
'The same, but with temporaly 'NoCutMessages' and 'NoBBCode' Option
T_RV = IRC_SendMessage(T_IRCID, "#test", "Hello! [b]BBCode BOLD[/b]", 1, 1): If T_RV <> IRCRE_NoError Then Print "ERROR: [Message] " & IRC_GetReturnDesc(T_RV)
'A message to us
T_RV = IRC_SendMessage(T_IRCID, IRC_GetMyNick(T_IRCID), "Hello! This is a short message to myself!"): If T_RV <> IRCRE_NoError Then Print "ERROR: [Message] " & IRC_GetReturnDesc(T_RV)
'If we are a OPER, or have Local / Global Operator or Admin states and the right Flag, we can Send messeges to the Server
'Messages to the Server need a '$' Char at the first, that signs that the target is a Servername.
T_RV = IRC_SendMessage(T_IRCID, "$" & IRC_GetMyServer(T_IRCID), "Hello! This is a short message to all!"): If T_RV <> IRCRE_NoError Then Print "ERROR: [Message] " & IRC_GetReturnDesc(T_RV)
'The same with using Notice Messages
T_RV = IRC_SendNotice(T_IRCID, "#test", "Hello! This is a short message!"): If T_RV <> IRCRE_NoError Then Print "ERROR: [Message] " & IRC_GetReturnDesc(T_RV)
T_RV = IRC_SendNotice(T_IRCID, IRC_GetMyNick(T_IRCID), "Hello! This is a short message to myself!"): If T_RV <> IRCRE_NoError Then Print "ERROR: [Message] " & IRC_GetReturnDesc(T_RV)
T_RV = IRC_SendNotice(T_IRCID, "$" & IRC_GetMyServer(T_IRCID), "Hello! This is a short message to all!"): If T_RV <> IRCRE_NoError Then Print "ERROR: [Message] " & IRC_GetReturnDesc(T_RV)
'-----------------------------------------------------------------------------------------------------------------
'Main Loop
Do Until InKey() = Chr(27)
Sleep 10, 1
Loop
'-----------------------------------------------------------------------------------------------------------------
'Regular a IRC_Disconect() must be called befor END the application.
'But IRC.bi will automaticly disconnect and terminate all connections befor the application close.
T_RV = IRC_Disconnect(T_IRCID)
If T_RV <> IRCRE_NoError Then Print "ERROR: [Message] " & IRC_GetReturnDesc(T_RV)
'-----------------------------------------------------------------------------------------------------------------
End