VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "IRCConnection"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Public Server As String
Public RealServer As String
Public Port As Long
Public Caption As String
Public State As Integer
Public Nick As String
Public StartCommands As String
Public Realname As String
Public ListID As Integer
Public ServerPass As String
Public NickPass As String
Public UserID As String
Public JoinMode As Integer
Public LogedIn As Boolean
Public CTCKey As String
Public CTCAktiv As Boolean
Public IgnoreList As String
Public ComBuffer As String
Public BufferFrame As ChatFrame
Public Silent As Boolean
Public Temp As Boolean
Public CodePage As Long
Public UTF8 As Boolean
Public UTF8dyn As Integer
Public ConnID As Integer
Public SockProxyServer As String
Public SockProxyPort As Long
Public SockProxyType As Long
Public UseSSL As Integer
Public ChannelCount As Long

Private SendBuffer() As Byte
Private SendBufferPos As Long
Private SendBufferState As Integer
Private ctcAk As Boolean
Private ConTimeOut As Long
Private Buffer(10240) As Byte
Private BufferWrite As Long
Private BufferRead As Long
Private RefList As Integer
Private TimePoint As Long
Private ReConn As Integer
Private LastRealNick As String
Private ChanListRef As Single
Private RListMode As Integer
Private DataWaitMode As Byte
Private RoomPass As String
Private ProxyState As Integer
Private ServerIP As String
Private CtcpSkipCount As Integer
Private CtcpSkipPoint As Long
Private Debuging As Boolean
Private TextComs As String
  

Private cvChanneltypes As String
Private cvPrefix As String
Private cvPrefixSign As String

Private WithEvents wins As CSocket
Attribute wins.VB_VarHelpID = -1
Private ResIP As URLtoIP

Public Sub Connect()
  Dim I As Integer
  Dim I2 As Integer
  
  SockProxyPort = SockProxyPortGlob
  SockProxyServer = SockProxyServerGlob
  SockProxyType = SockProxyTypeGlob
  
  If State > 1 Or ntScript_Call("Serv_Connect", CStr(ConnID)) = False Then Exit Sub
  State = 3
  
  If CodePage = 0 Then CodePage = BasicCodepage
    
  If Len(Trim(Nick)) < 1 Then Nick = "User" + CStr(Int(Rnd * 10000))
  For I = 1 To ChatFrames.Count
    If ChatFrames(I).IrcConn Is Me Then
      If ChatFrames(I).FrameType = cfRoom Or ChatFrames(I).FrameType = cfPrivate Then
        If SockProxyPort > 0 Then
          ChatFrames(I).PrintText "~ Sockproxy v" & SockProxyType / 10 & ": " + SockProxyServer + ":" + CStr(Abs(SockProxyPort)), CoVal.ClientMSG, , True
        End If
        ChatFrames(I).PrintText "~ " + TeVal.ConStart, CoVal.ClientMSG, , True
      ElseIf ChatFrames(I).FrameType = cfStatus Then
        ChatFrames(I).Caption = Me.Caption
        If SockProxyPort > 0 Then
          ChatFrames(I).PrintText "~ Sockproxy v" & SockProxyType / 10 & ": " + SockProxyServer + ":" + CStr(Abs(SockProxyPort)), CoVal.ClientMSG, , True
        End If
        ChatFrames(I).PrintText "~ " + TeVal.ConStart, CoVal.ClientMSG, , True
        If SSLIsAvailable Then
          wins.UseSSL = UseSSL
        Else
          wins.UseSSL = 0
        End If
        wins.ServerFrameID = ChatFrames(I).FrameID
      End If
      If ChatFrames(I) Is FrontCFrame Then
        Form1.pStateIntRefresh
        DrawTitel
      End If
      ChatFrames(I).UserListRef = 0
    End If
  Next
  ConnList(ListID).State = 1
  
  RefreshList
  Form1.ColumRedraw
  If LCase(Left(Server, 7)) = "http://" Then
    Set ResIP = New URLtoIP
    ResIP.ConvertToIP Me, Server
  Else
    RealServer = Server
    FinishConnection
  End If
End Sub

Public Sub FinishConnection()
  If SockProxyType > 4 And SockProxyPort > 0 Then
    DnsResEvent ""
  Else
    DnsResolve Me
  End If
End Sub

Public Sub DnsResEvent(IP As String)
  If State = 3 Then
    Set ResIP = Nothing
    ServerIP = IP
    If SockProxyPort > 0 Then
      wins.Connect SockProxyServer, SockProxyPort
      ProxyState = 1
    Else
      wins.Connect IP, Port
      ProxyState = 0
    End If
    wins.LocalRooterIP = 0
    TimePoint = AltTimer + 10
    StartTimer Me, 500, 1
  End If
End Sub

Public Function GetSeocket() As CSocket
  Set GetSeocket = wins
End Function

Private Sub Class_Initialize()
  Dim ChFrame As New ChatFrame
  Dim I As Integer
  
  ConnID = 1
  Do
    For I = 1 To IRCconns.Count
      If IRCconns(I).ConnID = ConnID Then Exit For
    Next
    If I > IRCconns.Count Then Exit Do
    ConnID = ConnID + 1
  Loop
  
  TextComs = " PRIVMSG NOTICE TOPIC 332 QUIT "
  BufferWrite = 0
  BufferRead = 0
  ReDim UserDList(1 To 32)
  ChFrame.Caption = "Open..."
  ChFrame.Inst cfStatus, 0
  Set ChFrame.IrcConn = Me
  ChatFrames.Add ChFrame
  SetFrontFrame ChFrame
  SetPrSets
  Set wins = New CSocket
  
  ReDim SendBuffer(1024)
End Sub

Private Sub SetPrSets()
  cvChanneltypes = "#!&~+"
  cvPrefix = "qaohv"
  cvPrefixSign = "*!@%+"
End Sub

Public Function ChatNoSign(ChaterName As String) As String
  Dim I As Long
  
  For I = 1 To Len(ChaterName)
    If InStr(1, cvPrefixSign, Mid(ChaterName, I, 1)) = 0 And InStr(1, cvChanneltypes, Mid(ChaterName, I, 1)) = 0 Then Exit For
  Next
  ChatNoSign = Mid(ChaterName, I)
End Function

Public Function RemChanSign(ChanName As String) As String
  Dim I As Long
  
  For I = 1 To Len(ChanName)
    If InStr(1, cvChanneltypes, Mid(ChanName, I, 1)) = 0 Then Exit For
  Next
  RemChanSign = Mid(ChanName, I)
End Function

Public Sub TimerEvent(TimerID As Integer)
  Dim ChatFrs As ChatFrame
  Dim I As Integer
  If TimerID = 1 Then
    If (State <> 3 And State > 1) Or AltTimeDiff(TimePoint) > 0 Then
      For Each ChatFrs In ChatFrames
        If ChatFrs.IrcConn Is Me Then
          If ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfPrivate Or ChatFrs.FrameType = cfStatus Then
            ChatFrs.PrintText "  " + TeVal.ConErr, CoVal.ErrorMSG, , False
          End If
        End If
      Next
      CloseConnection
      ReConn = RetryTime
      StartTimer Me, 1000, 3
    Else
      StartTimer Me, 500, 1
    End If
  ElseIf TimerID = 2 Then
    DataWaitMode = 0
    WinS_DataArrival
  ElseIf TimerID = 3 Then
    If RetryTime > 0 And State < 2 Then
      If ReConn < 1 Then
        Connect
      End If
      If ReConn > -4 Then
        ReConn = ReConn - 1
        StartTimer Me, 1000, 3
      End If
    End If
  ElseIf TimerID = 4 Then
    If State = 4 Then
      If ConTimeOut > IRCTimeOut Then
        SendLine "ping " + CStr(Int(Timer))
      Else
        ConTimeOut = ConTimeOut + 20
      End If
      StartTimer Me, 20000, 4
    End If
  End If
End Sub

Private Sub Class_Terminate()
  If State > 1 Then CloseConnection
  RemRegedEvent "", ConnID, ""
  KillTimerByID 1, Me
  KillTimerByID 2, Me
  KillTimerByID 3, Me
  KillTimerByID 4, Me
  If Not ResIP Is Nothing Then ResIP.CanceldRes
End Sub

Private Sub wins_Closed()
  CloseConnection
  If ReConn > -5 Then
    StartTimer Me, 3000, 3
    ReConn = RetryTime
  Else
    StartTimer Me, 1000, 3
    ReConn = 0
  End If
End Sub

Public Sub CloseConnection()
  Dim ChatFrs As ChatFrame
  Dim I As Integer
  Dim I2 As Integer
  Dim DAT As String
  
  KillTimerByID 1, Me
  KillTimerByID 4, Me
  If Not ResIP Is Nothing Then ResIP.CanceldRes
  If wins.State > 1 Then
    If Len(QuitMsg) Then
      SendLine "Quit :" + QuitMsg
    Else
      SendLine "Quit"
    End If
    wins.Disconnect
  End If
  I = -1
  Do
    I2 = I
    I = InStr(I + 2, StartCommands, vbNewLine)
    If I = 0 Then I = Len(StartCommands) + 1
    If Mid(StartCommands, I2 + 2, 1) <> "$" And I - I2 > 2 Then
      DAT = DAT + Mid(StartCommands, I2 + 2, I - I2 - 2) + vbNewLine
    End If
  Loop Until I = Len(StartCommands) + 1
  If DAT = vbNewLine Then DAT = ""
  StartCommands = DAT
  DAT = ""
  For Each ChatFrs In ChatFrames
    If ChatFrs.IrcConn Is Me Then
      Select Case ChatFrs.FrameType
        Case cfRoom
          If RemaindRooms = True Then
            DAT = DAT + "$join " + Replace(ChatFrs.Caption + GetRoomPass(ChatFrs.Caption), "$", "$($)") + vbNewLine
          End If
          ChatFrs.PrintText "~ " + TeVal.ConClose, CoVal.ClientMSG, , True
          ChatFrs.UListF.Clear
          If ChatFrs Is FrontCFrame Then DrawTitel
        Case cfRoomList
          If RemaindList = True Then
            DAT = DAT + "$list" + vbNewLine
          End If
        Case cfStatus, cfPrivate
          ChatFrs.PrintText "~ " + TeVal.ConClose, CoVal.ClientMSG, , True
        Case cfDebug
          If Debuging Then
            ChatFrs.PrintText "- " + CStr(Time), CoVal.TimeStamp, True, True
            ChatFrs.PrintText " Connection closed", CoVal.ClientMSG, , False
          End If
      End Select
    End If
  Next
  StartCommands = DAT + StartCommands
  If LogedIn = True Then
    ConnList(ListID).Commands = StartCommands
  Else
    StartCommands = ConnList(ListID).Commands
  End If
  LogedIn = False
  If IsNumeric(Mid(Nick, Len(LastRealNick) + 1)) Then
    If LastRealNick = Left(Nick, Len(LastRealNick)) Then Nick = LastRealNick
  End If
  ConnList(ListID).Nick = Nick
  ConnList(ListID).Pass = NickPass
  ConnList(ListID).UserID = UserID
  ConnList(ListID).Name = Realname
  ConnList(ListID).State = 3
  ConnList(ListID).CTCKey = CTCKey
  ConnList(ListID).CTCOn = Abs(CInt(CTCAktiv))
  ConnList(ListID).IgnoreList = IgnoreList
  ConnList(ListID).Silent = CInt(Silent)
  ConnList(ListID).LastUsed = Now
  ConnList(ListID).CodePage = CodePage
  ConnList(ListID).UTF8dyn = UTF8dyn
  ConnList(ListID).UseSSL = UseSSL
    
  If Not AppUnloading Then RefreshList
  State = 1
  SendBufferState = 0
  SendBufferPos = 0

  
  SetPrSets
  
  If FrontCFrame.IrcConn Is Me And Not AppUnloading Then Form1.pStateIntRefresh
  If Not AppUnloading Then Form1.ColumRedraw
  DisconnectMsg
  SetFList "", Server, 0, 0
  ntScript_Call "Serv_Closed", CStr(ConnID)
End Sub

Public Sub Quit()
  Dim I As Integer
  KillTimerByID 2, Me
  KillTimerByID 3, Me
  CloseConnection
  For I = ChatFrames.Count To 1 Step -1
    If ChatFrames(I).IrcConn Is Me Then
      ChatFrames(I).QuitFrame
    End If
  Next
  For I = IRCconns.Count To 1 Step -1
    If IRCconns(I) Is Me Then
      IRCconns.Remove I
    End If
  Next
  ConnList(ListID).State = 0
  If ConnList(ListID).Temp Then
    If MsgBox(TeVal.ConRemove, vbYesNo + vbQuestion, ConnList(ListID).Caption) = vbNo Then
      Form1.RemoveServer ListID
    Else
      ConnList(ListID).Temp = 0
    End If
  End If
  RefreshList
End Sub

Private Sub AddRoomPass(Room As String, Passw As String)
  If InStr(1, RoomPass, vbTab + Room + " " + Passw + vbTab, vbTextCompare) > 0 Or Len(Trim(Passw)) = 0 Then Exit Sub
  If Len(RoomPass) = 0 Then RoomPass = vbTab
  RoomPass = vbTab + Room + " " + Passw + RoomPass
  If Len(RoomPass) > 255 Then RoomPass = vbTab + Right(RoomPass, 255)
End Sub

Private Function GetRoomPass(Room As String) As String
  Dim I As Integer
  Dim I2 As Integer
  I = InStr(1, RoomPass, vbTab + Room + " ", vbTextCompare)
  If I = 0 Then
    GetRoomPass = ""
  Else
    I = InStr(I + 1, RoomPass, " ")
    I2 = InStr(I + 1, RoomPass, vbTab)
    GetRoomPass = " " + Mid(RoomPass, I + 1, I2 - I - 1)
  End If
End Function

Public Function SendComm(Data As String, ChatFrs As ChatFrame, Optional scIndex As Integer = -1) As Boolean
  Dim sDAT(0 To 9) As String
  Dim I As Long
  Dim I2 As Integer
  Dim Iind As Integer
  Dim OutStr As String
  Dim OutStr2 As String
  Dim DAT As String
  Dim LIndex As Integer
  Dim Sendable As Boolean
  Dim KeepText As Boolean
  
  Do
    If scIndex > -2 Then
      OutStr = CharReplace(Data, ChatFrs, wins, scIndex, LIndex)
    Else
      OutStr = Data
      LIndex = -1
    End If
    
    If Len(OutStr) Then
      I = 0
      Iind = 0
      Do
        I2 = I
        I = InStr(I + 1, OutStr, " ")
        If I = 0 Or Iind > 8 Then Exit Do
        sDAT(Iind) = Mid(OutStr, I2 + 1, I - I2 - 1)
        Iind = Iind + 1
      Loop
      sDAT(Iind) = Mid(OutStr, I2 + 1)
      Select Case LCase(sDAT(0))
        Case "me"
          If ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfPrivate Then
            OutStr2 = "privmsg " + ChatFrs.Caption + " :" + Chr(1) + "ACTION " + Mid(OutStr, Len(sDAT(0)) + 2) + Chr(1)
            DrawSLine ChatFrs, 0, "* " + Nick + " " + Mid(OutStr, Len(sDAT(0)) + 2), CoVal.NormalText
            WriteLog Format(Now, "dd.mm.yyyy hh:mm:ss") + " * " + Me.Nick + " " + Mid(OutStr, Len(sDAT(0)) + 2), ChatFrs
          End If
        Case "msg"
          OutStr2 = "PRIVMSG " + sDAT(1) + " :" + Mid(OutStr, Len(sDAT(0)) + Len(sDAT(1)) + 3)
          If ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfStatus Or ChatFrs.FrameType = cfPrivate Then
            DrawSLine ChatFrs, 0, "<PRIVATE:" + Nick + ">", CoVal.Notice, Mid(OutStr, Len(sDAT(0)) + Len(sDAT(1)) + 3)
          End If
        Case "quit", "away"
          OutStr2 = UCase(sDAT(0)) + " :" + Mid(OutStr, Len(sDAT(0)) + 2)
        Case "notice"
          OutStr2 = "notice " + sDAT(1) + " :" + Mid(OutStr, Len(sDAT(0)) + Len(sDAT(1)) + 3)
          If ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfStatus Or ChatFrs.FrameType = cfPrivate Then
            DrawSLine ChatFrs, 0, "- " + Nick + ": " + Mid(OutStr, Len(sDAT(0)) + Len(sDAT(1)) + 3), CoVal.Notice
          End If
        Case "topic"
          If Len(sDAT(1)) = 0 Then DAT = ChatFrs.Caption Else DAT = sDAT(1)
          If Len(sDAT(2)) = 0 Then
            OutStr2 = "topic " + DAT
          Else
            OutStr2 = "topic " + DAT + " :" + GetAP(sDAT, 2, Iind)
          End If
        Case "query"
          If InStr(1, cvChanneltypes, Left(sDAT(1), 1)) > 0 Then
            OutStr2 = "join " + Mid(OutStr, Len(sDAT(0)) + 2)
          Else
            ChatFrs.PrivateFrame sDAT(1)
            If sDAT(2) <> "" Then OutStr2 = "PRIVMSG " + sDAT(1) + " :" + GetAP(sDAT, 2, Iind)
          End If
        Case "squery"
          OutStr2 = "SQUERY " + sDAT(1) + " :" + GetAP(sDAT, 2, Iind)
        Case "bann"
          If ChatFrs.FrameType = cfRoom Then
            SendLine CReplace("/// " + sDAT(1) + " " + sDAT(1), "mode $(room) +b *!*@$(useradr)", ChatFrs, wins)
          End If
        Case "debann"
          If ChatFrs.FrameType = cfRoom Then
            If InStr(1, sDAT(1), "*") Or InStr(1, sDAT(1), "@") Then
              SendLine "mode " + ChatFrs.Caption + " -b " + sDAT(1)
            Else
              SendLine CReplace("/// " + sDAT(1) + " " + sDAT(1), "mode $(room) -b *!*@$(useradr)", ChatFrs, wins)
            End If
          End If
        Case "kick"
          OutStr2 = "KICK " + sDAT(1) + " " + sDAT(2) + " :" + GetAP(sDAT, 3, Iind)
        Case "kickandbann"
          If ChatFrs.FrameType = cfRoom Then
            OutStr2 = "kick " + sDAT(2) + " " + Trim(sDAT(1) + " " + GetAP(sDAT, 4, Iind))
            SendLine "mode " + sDAT(2) + " +b " + sDAT(3)
          End If
        Case "kickbann"
          If ChatFrs.FrameType = cfRoom Then
            OutStr2 = "/// " + sDAT(1) + " " + ChatFrs.Caption + " " + Mid(OutStr, Len(sDAT(0)) + 2)
            SendLine CReplace(OutStr2, "kickandbann $1 $2 *!*@$(useradr:3) $>3", ChatFrs, wins)
            OutStr2 = ""
          End If
        Case "dcc"
          If sDAT(1) <> "" Then
            If sDAT(2) <> "" Then
              OutStr2 = Mid(OutStr, Len(sDAT(0) + sDAT(1) + sDAT(2)) + 4)
              If UCase(sDAT(1)) = "SEND" Then
                On Error Resume Next
                If Dir(OutStr2) = "" Then
                  On Error GoTo 0
                  DrawTLine ChatFrs, "- " + TeVal.dccFileNotFound, CoVal.ErrorMSG
                Else
                  On Error GoTo 0
                  OpenDCC Nick, sDAT(2), OutStr2, sDAT(1), Me, 0, ChatFrs
                End If
              Else
                OpenDCC Nick, sDAT(2), OutStr2, sDAT(1), Me, 0, ChatFrs
              End If
            Else
              OpenDCC Nick, sDAT(1), "", "CHAT", Me, 0
            End If
            OutStr2 = ""
          End If
        Case "ctcp"
          OutStr2 = "PRIVMSG " + sDAT(1) + " :" + Chr(1) + UCase(sDAT(2))
          If Trim(sDAT(3)) = "" Then
            If LCase(sDAT(2)) = "ping" Then OutStr2 = OutStr2 + " " + Format(Fix(Timer * 1000), "#")
            OutStr2 = OutStr2 + Chr(1)
          Else
            OutStr2 = OutStr2 + " " + Mid(OutStr, Len(sDAT(0)) + Len(sDAT(1)) + Len(sDAT(2)) + 4) + Chr(1)
          End If
          If ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfStatus Or ChatFrs.FrameType = cfPrivate Then
            DrawSLine ChatFrs, 0, "<CTCP:" + Nick + ">", CoVal.Notice, Mid(OutStr, Len(sDAT(0)) + Len(sDAT(1)) + 3)
          End If
        Case "join"
          If InStr(1, cvChanneltypes, Left(sDAT(1), 1)) > 0 Then
            DAT = sDAT(1)
          Else
            DAT = "#" + sDAT(1)
          End If
          If Len(sDAT(2)) > 0 Then
            OutStr2 = "join " + DAT + " " + sDAT(2)
            AddRoomPass DAT, sDAT(2)
          Else
            OutStr2 = "join " + DAT + GetRoomPass(DAT)
          End If
        Case "rejoin"
          If ChatFrs.FrameType = cfRoom Then
            If ChatFrs.InChannel Then SendLine "part " + ChatFrs.Caption
            OutStr2 = "join " + ChatFrs.Caption + GetRoomPass(ChatFrs.Caption)
          End If
        Case "part"
          If ChatFrs.FrameType = cfRoom And Len(sDAT(1)) = 0 Then
            OutStr2 = "part " + ChatFrs.Caption
          Else
            OutStr2 = "part " + sDAT(1)
          End If
        Case "reg"
          OutStr2 = "PRIVMSG NickServ :REGISTER " + sDAT(1)
          If sDAT(2) <> "" Then OutStr2 = OutStr2 + " " + sDAT(2)
          NickPass = Trim(sDAT(1))
        Case "ident"
          If Trim(sDAT(1)) <> "" Then NickPass = Trim(sDAT(1))
          OutStr2 = "PRIVMSG NickServ :IDENTIFY " + NickPass
        Case "regchan"
          OutStr2 = "PRIVMSG ChanServ :REGISTER " + Mid(OutStr, Len(sDAT(0)) + 2)
        Case "connect"
          If State < 2 Then Connect
        Case "disconnect"
          If State > 1 Then CloseConnection
        Case "amsg"
          For I = 1 To ChatFrames.Count
            If ChatFrames(I).FrameType = cfRoom And ChatFrames(I).IrcConn Is ChatFrs.IrcConn Then
              ChatFrames(I).SendM Mid(OutStr, Len(sDAT(0)) + 2)
            End If
          Next
        Case "ame"
          For I = 1 To ChatFrames.Count
            If ChatFrames(I).FrameType = cfRoom And ChatFrames(I).IrcConn Is ChatFrs.IrcConn Then
              SendLine "privmsg " + ChatFrames(I).Caption + " :" + Chr(1) + "ACTION " + Mid(OutStr, Len(sDAT(0)) + 2) + Chr(1)
              DrawSLine ChatFrames(I), 0, "* " + Nick + " " + Mid(OutStr, Len(sDAT(0)) + 2), CoVal.NormalText
              WriteLog Format(Now, "dd.mm.yyyy hh:mm:ss") + " * " + Me.Nick + " " + Mid(OutStr, Len(sDAT(0)) + 2), ChatFrames(I)
            End If
          Next
        Case "watch"
          ShowFriedsList
          WatchNewUser sDAT(1), Me.Server
        Case "coding"
          CTCAktiv = CTCAktiv = False
          DrawTLine ChatFrs, "- " + TeVal.fFunction + " " + IIf(CTCAktiv, TeVal.fOn, TeVal.fOff), CoVal.ClientMSG
          Form1.pStateIntRefresh
        Case "setcode"
          CTCKey = sDAT(1)
        Case "ignore"
          If Len(sDAT(1)) Then
            If Len(IgnoreList) = 0 Then IgnoreList = " "
            UnIgnore IgnoreList, sDAT(1)
            IgnoreList = IgnoreList + sDAT(1) + "@" + Format(Now, "dd.mm.yyyy") + " "
            DrawTLine ChatFrs, "- " + TeVal.Ignore, CoVal.ClientMSG, sDAT(1)
          Else
            DrawTLine ChatFrs, "- " + Trim(IgnoreList), CoVal.ClientMSG
          End If
        Case "unignore"
          If UnIgnore(IgnoreList, sDAT(1)) Then
            DrawTLine ChatFrs, "- " + TeVal.UnIgnore, CoVal.ClientMSG, sDAT(1)
          End If
        Case "setquitmsg"
          If Len(sDAT(1)) > 0 Then QuitMsg = Mid(OutStr, Len(sDAT(0)) + 2) Else QuitMsg = ""
          DrawSLine ChatFrs, 2, "--> " + SetInVals(TeVal.Quit, Nick, QuitMsg), CoVal.ClientMSG
        Case "setfinger"
          If Len(sDAT(1)) > 0 Then FingerData = Mid(OutStr, Len(sDAT(0)) + 2) Else FingerData = ""
          DrawSLine ChatFrs, 2, "--> Finger = " + FingerData, CoVal.ClientMSG
        Case "chanproperties"
          If ChatFrs.FrameType = cfRoom Then
            If Form1.Visible Then
              Set SellCFrame = ChatFrs
              Form8.Show 1
            End If
          End If
        Case "debug"
          StartDebunging
        Case "utf8", "utf-8"
          If Val(sDAT(1)) < 2 Then
            UTF8dyn = Val(sDAT(1))
            DrawSLine ChatFrs, 2, "- " + TeVal.fFunction + " " + IIf(UTF8dyn = 1, TeVal.fOn, TeVal.fOff), CoVal.ClientMSG
          End If
        Case "chartset", "charset", "codepage"
          If Val(sDAT(1)) < 65000 Then
            'If GetCharSetByCP(Val(sDAT(1))) Then
              CodePage = Val(sDAT(1))
            'Else
              'Codepage = BasicCodepage
            'End If
            DrawSLine ChatFrs, 2, "- " + TeVal.butOK, CoVal.ClientMSG
          End If
        Case "quote"
          OutStr2 = Mid(OutStr, Len(sDAT(0)) + 2)
        Case "?"
          SetInCommand Mid(OutStr, Len(sDAT(0)) + 2)
          KeepText = True
        Case Else
          If CCommands(ChatFrs, sDAT, OutStr, OutStr2) = False Then
            If Left(OutStr, 1) = "/" Then
              OutStr2 = Mid(OutStr, 2)
            Else
              OutStr2 = OutStr
            End If
          End If
      End Select
      '########################## Send #############################
      If Not KeepText Then
        If OutStr2 = "" Then
          Sendable = True
        Else
          If State = 4 Then
            SendLine OutStr2
            Sendable = True
          End If
        End If
      End If
    End If
    LIndex = LIndex + 1
  Loop While LIndex
  SendComm = Sendable
End Function

Private Sub SetInCommand(Text As String)
  Dim I As Long
  Dim I2 As Long
  
  For I = 0 To UBound(ComIndex)
    If CStr(Val(Left(ComIndex(I), 1))) = Left(ComIndex(I), 1) And Len(ComIndex(I)) > 2 Then
      I2 = Val(Left(ComIndex(I), 1))
      If FrontCFrame.ConnTyp = -1 And I2 = 3 Then I2 = -1
      If FrontCFrame.ConnTyp = 0 And (I2 = 0 Or I2 = 2 Or I2 = 3) Then I2 = -1
      
      If I2 = -1 Then
        If AreStringsIncl(LCase(Text), LCase(ComIndex(I))) Then
          I2 = InStr(ComIndex(I), " ")
          If I2 = 0 Then I2 = Len(ComIndex(I)) + 1
          Form1.Text1.Text = Mid(ComIndex(I), 2, I2 - 2) + " "
          Form1.Text1.SelStart = Len(Form1.Text1.Text)
        End If
      End If
    End If
  Next
End Sub

Public Sub StartDebunging(Optional StopDebug As Boolean)
  Dim CFrame As ChatFrame
  Dim I As Integer
  
  If Debuging Or StopDebug Then
    For Each CFrame In ChatFrames
      If CFrame.FrameType = cfDebug And CFrame.IrcConn Is Me Then
        CFrame.PrintText "- Stop Debuging", CoVal.ClientMSG, True, True
        Exit For
      End If
    Next
    Debuging = False
  Else
    For I = 1 To ChatFrames.Count
      If ChatFrames(I).FrameType = cfDebug And ChatFrames(I).IrcConn Is Me Then Exit For
    Next
    If I > ChatFrames.Count Then
      Set CFrame = NewFrame(Me, "Debug", cfDebug, True)
    Else
      Set CFrame = ChatFrames(I)
    End If
    CFrame.PrintText " ", 0, True, True
    CFrame.PrintText "- Start Debuging", CoVal.ClientMSG, True, True
    CFrame.PrintText "- Server: ", CoVal.NormalVon, True, True
    CFrame.PrintText Server + " (" + ServerIP + ":" + CStr(Port) + ")", CoVal.NormalText, True, False
    CFrame.PrintText "- Client: ", CoVal.FlusterVon, True, True
    CFrame.PrintText GetLocalHostName + ":" + CStr(GetLocalPort(wins.SocketHandle)), CoVal.NormalText, True, False
    CFrame.TextF.SetDivLine CoVal.ClientMSG
    CFrame.PrintText " ", 0, True, True
    Debuging = True
  End If
End Sub

Public Sub SendLine(DataIn As String, Optional ManuSend As Boolean)
  Dim I As Long
  Dim Data As String
  
  If State = 4 Then
    If Len(DataIn) Then
      
      If UTF8 Then
        AddStringToSendBuff DataIn, CP_UTF8
      Else
        AddStringToSendBuff DataIn, BasicCodepage
      End If

      AddBrToSendBuff
      SendFromBuff
    End If
  End If
End Sub

Public Function GetCharSet(IsMsg As Boolean) As Long
  If IsMsg Then
    If UTF8dyn Then
      GetCharSet = CP_UTF8
    Else
      GetCharSet = InputCodepage
    End If
  Else
    If UTF8 Then
      GetCharSet = CP_UTF8
    Else
      GetCharSet = BasicCodepage
    End If
  End If
End Function

Public Sub AddStringToSendBuff(Data As String, CharSet As Long)
  Dim CharCount As Long
  
  CharCount = EncodeToByteArr(Data, SendBuffer, SendBufferPos, CharSet)
  SendBufferPos = SendBufferPos + CharCount
End Sub

Public Sub AddBrToSendBuff()
  If SendBufferPos > UCase(SendBuffer) Then
    ReDim Preserve SendBuffer(SendBufferPos + 32)
  End If
  SendBuffer(SendBufferPos) = 10
  SendBufferPos = SendBufferPos + 1
End Sub

Public Sub SendFromBuff()
  Dim CharCount As Long
  Dim I As Long
  Dim I2 As Long
  Dim I3 As Long
  Dim LastBuffPos As Long
  
  If SendBufferState = 1 Then
    For I2 = 0 To SendBufferPos - 1
      If SendBuffer(I2) = 10 Then
        I = wins.SendDataBin(VarPtr(SendBuffer(LastBuffPos)), I2 - LastBuffPos + 1)
        If I = 0 Then
          SendBufferState = 0
          LastBuffPos = I2 + 1
          Exit For
        Else
          If Debuging Then
            For I3 = 1 To ChatFrames.Count
              If ChatFrames(I3).FrameType = cfDebug And ChatFrames(I3).IrcConn Is Me Then
                ChatFrames(I3).PrintText "> " + CStr(Time), CoVal.TimeStamp, True, True
                ChatFrames(I3).PrintText " " + ConvForDebug(SendBuffer, LastBuffPos, I2 - LastBuffPos), CoVal.FlusterVon, True, False
                Exit For
              End If
            Next
          End If
          SendedBytes = SendedBytes + (I2 + 1 - LastBuffPos)
          LastBuffPos = I2 + 1
        End If
      End If
    Next
    For I2 = LastBuffPos To SendBufferPos - 1
      SendBuffer(I2 - LastBuffPos) = SendBuffer(I2)
    Next
    SendBufferPos = SendBufferPos - LastBuffPos

    If (FrontCFrame.FrameType = cfServerList Or FrontCFrame.FrameType = cfScript Or FrontCFrame.FrameType = cfDCC) And _
    Form1.Visible = True And Form1.WindowState <> 1 Then Form1.pStateRefresh
  End If
End Sub

Private Function GetAP(pArray() As String, StrStart As Integer, StrEnd As Integer) As String
  Dim I As Integer
  Dim DAT  As String
  
  For I = StrStart To StrEnd
    If I > UBound(pArray) Then Exit For
    If Len(DAT) Then
      DAT = DAT + " " + pArray(I)
    Else
      DAT = pArray(I)
    End If
  Next
  GetAP = DAT
End Function

Public Sub PraseCommandFromByteArray(Data() As Byte, FirstByte As Long, BuffLen As Long)
  Dim I As Integer
  Dim I2 As Long
  Dim PIndex As Integer
  Dim PData(32) As String
  Dim ChatFrs As ChatFrame
  Dim mVon As String
  Dim ComName As String

  If Debuging Then
    For Each ChatFrs In ChatFrames
      If ChatFrs.IrcConn Is Me And ChatFrs.FrameType = cfDebug Then
        ChatFrs.PrintText "< " + CStr(Time), CoVal.TimeStamp, True, True
        ChatFrs.PrintText " " + ConvForDebug(Data, FirstByte, BuffLen), CoVal.NormalVon, True, False
      End If
    Next
  End If
  I2 = FirstByte - 1
  For I = FirstByte To FirstByte + BuffLen
    If Data(I) = 32 Or I = FirstByte + BuffLen Then
      If I > I2 + 1 Then
        If Data(I2 + 1) = 58 And I2 > FirstByte Then '58 = Asc(":")
          If InStr(1, TextComs, UCase(PData(1))) Then
            PData(PIndex) = ConvMsgToUnicode(Data(), I2 + 2, BuffLen + FirstByte - I2 - 2, CodePage)
          Else
            PData(PIndex) = ConvToUnicode(Data(), I2 + 2, BuffLen + FirstByte - I2 - 2, BasicCodepage, UTF8)
          End If
          Exit For
        Else
          PData(PIndex) = ConvToUnicode(Data(), I2 + 1, I - I2 - 1, BasicCodepage, UTF8)
        End If
        PIndex = PIndex + 1
        If PIndex = UBound(PData) Then Exit For
      End If
      I2 = I
    End If
  Next
  
  If Left(PData(0), 1) = ":" Then
    I = InStr(1, PData(0), "!")
    If I = 0 Then I = Len(PData(0)) + 1
    mVon = Mid(PData(0), 2, I - 2)
  End If
  ComName = UCase(PData(1))
  
  If Len(mVon) Then
    If PhraseRegedEvent(ComName, mVon, ConnID, PData(PIndex), DecodeByteArr(Data(), FirstByte, BuffLen, BasicCodepage)) = False Then Exit Sub
  Else
    If PhraseRegedEvent(PData(0), "", ConnID, PData(PIndex), DecodeByteArr(Data(), FirstByte, BuffLen, BasicCodepage)) = False Then Exit Sub
  End If
  
  PraseCommand PData, PIndex
End Sub

Public Sub PraseCommandFromString(Data As String)
  Dim I As Integer
  Dim I2 As Long
  Dim PIndex As Integer
  Dim PData(32) As String
  Dim ChatFrs As ChatFrame
  Dim FirstByte As Integer
  
  I2 = 0
  For I = 1 To Len(Data) + 1
    If Mid(Data, I, 1) = " " Or I = Len(Data) + 1 Then
      If I > I2 + 1 Then
        If Mid(Data, I2 + 1, 1) = ":" And I2 > 1 Then '58 = Asc(":")
          PData(PIndex) = Mid(Data, I2 + 2, Len(Data) - I2 - 1)
          Exit For
        Else
          PData(PIndex) = Mid(Data, I2 + 1, I - I2 - 1)
        End If
        PIndex = PIndex + 1
      End If
      I2 = I
    End If
  Next
  
  PraseCommand PData, PIndex
End Sub

Private Sub PraseCommand(ArgList() As String, LastCom As Integer)
  Dim ChatFrs As ChatFrame
  Dim DAT As String
  Dim DAT2 As String
  Dim I As Integer
  Dim I2 As Long
  Dim I3 As Integer
  Dim PIndex As Integer
  Dim Joined As Integer
  Dim PrivMSG As Integer
  Dim ListRooms As Integer
  Dim sNotice As Integer
  Dim sDCC As Integer
  Dim mVon As String
  Dim DecodedText As String
  Dim vSign As Integer
  Dim ComName As String
  Dim SignCounter As Integer
  
  If Left(ArgList(0), 1) = ":" Then
    I = InStr(1, ArgList(0), "!")
    If I = 0 Then I = Len(ArgList(0)) + 1
    mVon = Mid(ArgList(0), 2, I - 2)
  End If
  ComName = UCase(ArgList(1))
      
  For Each ChatFrs In ChatFrames
    If ChatFrs.IrcConn Is Me Then
      If Len(ArgList(1)) < 4 Then
        Select Case Val(ArgList(1))
          Case 301, 221, 302, 307, 311, 312, 313, 317, 318, 319, 321, 322, 323, 324, 329, 330, 332, 333, 340, 353, 366, 378, 600, 601, 604, 605, 367, 368
            'Do Nothing Not(1, 2, 375, 372, 401, 280, 291, 412, 292, 421, 311, 319, 312, 318, 403)
          Case 1 To 299, 375, 376
            If ChatFrs.FrameType = cfStatus Then
              If LastCom = 4 Then
                ChatFrs.PrintText "- " + ArgList(4) + ": ", CoVal.ServerMSG, True
                ChatFrs.PrintText ArgList(3), CoVal.NormalVon, True, False
              Else
                ChatFrs.PrintText "- " + GetAP(ArgList, 3, LastCom), CoVal.ServerMSG, True
              End If
            End If
          Case 372
            If ChatFrs.FrameType = cfStatus Then ChatFrs.PrintText ArgList(3), CoVal.NormalText, True
          Case Is > 0
            If ChatFrs.FrameType = cfStatus Or _
              ((ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfPrivate) And ChatFrs Is FrontCFrame) Then
              If LastCom > 3 Then DAT = " (" + GetAP(ArgList, 3, LastCom - 1) + ")"
              If Val(ArgList(1)) > 399 And Val(ArgList(1)) < 500 Then
                DrawTLine ChatFrs, "- " + ArgList(LastCom) + DAT, CoVal.ErrorMSG
              Else
                DrawTLine ChatFrs, "- " + ArgList(LastCom) + DAT, CoVal.ServerMSG
              End If
            End If
        End Select
      End If
      
      If ChatFrs.FrameType = cfStatus And ArgList(0) = "ERROR" Then
        DrawTLine ChatFrs, "- " + GetAP(ArgList, 1, LastCom), CoVal.ErrorMSG
      End If
            
      If ChatFrs.FrameType = cfStatus And ArgList(0) = "MODE" Then
        DrawTLine ChatFrs, "- " + GetAP(ArgList, 1, LastCom), CoVal.ServerMSG
      End If
      
      'Server Values
      If ComName = "005" Then
        If ChatFrs.FrameType = cfStatus Then
          For I = 3 To LastCom
            If Len(ArgList(I)) Then
              I2 = InStr(1, ArgList(I), "=")
              If I2 = 0 Then I2 = Len(ArgList(I)) + 1
              Select Case UCase(Left(ArgList(I), I2 - 1))
                Case "CHANTYPES"
                  cvChanneltypes = Mid(ArgList(I), I2 + 1)
                Case "PREFIX"
                  I3 = InStr(I2 + 1, ArgList(I), ")")
                  If I3 - I2 > 2 Then
                    cvPrefix = Mid(ArgList(I), I2 + 2, I3 - I2 - 2)
                    cvPrefixSign = Mid(ArgList(I), I3 + 1)
                  End If
                Case "NETWORK"
                  If Len(ArgList(I)) > I2 + 1 And LCase(Mid(ArgList(I), I2 + 1)) <> LCase(Caption) _
                  And "Server:" + Server = Left(Caption, Len(Server) + 7) Then
                    Caption = CheckConns(Mid(ArgList(I), I2 + 1))
                    ConnList(ListID).Caption = Caption
                    For I3 = 1 To ChatFrames.Count
                      If ChatFrames(I3).IrcConn Is Me And ChatFrames(I3).FrameType = cfStatus Then
                        ChatFrames(I3).Caption = Caption
                        Exit For
                      End If
                    Next
                    RefreshList
                    Form1.ColumRedraw False
                  End If
              End Select
            End If
          Next
        End If
      End If
      
      'Get channel count
      If ComName = "254" Then
        If ChatFrs.FrameType = cfStatus Then
          ChannelCount = Val(ArgList(3))
        End If
      End If
      
      'Check Mode
      If ComName = "221" Then
        If ChatFrs.FrameType = cfStatus Or ((ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfPrivate) And ChatFrs Is FrontCFrame) Then
          DrawTLine ChatFrs, "- " + TeVal.CUserRank, CoVal.ServerMSG, ArgList(2), ArgList(3)
        End If
      End If

      'Whois 311 Hostmask
      If ComName = "311" Then
        If ChatFrs.FrameType = cfStatus Or ((ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfPrivate) And ChatFrs Is FrontCFrame) Then
          DrawTLine ChatFrs, "- " + TeVal.WhoisList, CoVal.ServerMSG, ArgList(3)
          ChatFrs.TextF.SetShiftIn True
          DrawTLine ChatFrs, "- " + TeVal.WhoisRealName + ": ", CoVal.ServerMSG
          ChatFrs.PrintText ArgList(7), CoVal.NormalText, True, False
          DrawTLine ChatFrs, "- " + TeVal.WhoisHostMask + ": ", CoVal.ServerMSG
          ChatFrs.PrintText ArgList(3) + "!" + ArgList(4) + "@" + ArgList(5) + " " + ArgList(6), CoVal.NormalText, True, False
        End If
      End If
      
      'Whois 378 IP
      If ComName = "378" Then
        If ChatFrs.FrameType = cfStatus Or ((ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfPrivate) And ChatFrs Is FrontCFrame) Then
          DrawTLine ChatFrs, "- " + ArgList(4), CoVal.ServerMSG
        End If
      End If
            
      'Whois  307 is a registered nick
      If ComName = "307" Then
        If ChatFrs.FrameType = cfStatus Or ((ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfPrivate) And ChatFrs Is FrontCFrame) Then
          DrawTLine ChatFrs, "- " + TeVal.WhoisReged, CoVal.ServerMSG, ArgList(3)
        End If
      End If
      
      'Whois  330 is authed as
      If ComName = "330" Then
        If ChatFrs.FrameType = cfStatus Or ((ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfPrivate) And ChatFrs Is FrontCFrame) Then
          DrawTLine ChatFrs, "- " + TeVal.WhoisRegedAs, CoVal.ServerMSG, ArgList(3), ArgList(4)
        End If
      End If
            
      'Whois 319 CHannels
      If ComName = "319" Then
        If ChatFrs.FrameType = cfStatus Or ((ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfPrivate) And ChatFrs Is FrontCFrame) Then
          DrawTLine ChatFrs, "- " + TeVal.WhoisIn + ": ", CoVal.ServerMSG, ArgList(3)
          ChatFrs.PrintText ArgList(4), CoVal.NormalText, True, False
        End If
      End If
      
      'Whois 312 Server
      If ComName = "312" Then
        If ChatFrs.FrameType = cfStatus Or ((ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfPrivate) And ChatFrs Is FrontCFrame) Then
          DrawTLine ChatFrs, "- " + TeVal.WhoisServer + ": ", CoVal.ServerMSG
          ChatFrs.PrintText ArgList(4) + " (" + ArgList(5) + ")", CoVal.NormalText, True, False
        End If
      End If
      
      'Whois 313 IsService
      If ComName = "313" Then
        If ChatFrs.FrameType = cfStatus Or ((ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfPrivate) And ChatFrs Is FrontCFrame) Then
          DrawTLine ChatFrs, "- " + ArgList(3) + " " + ArgList(4), CoVal.ServerMSG
        End If
      End If
      
      'Whois 317 Date/Time Idle
      If ComName = "317" Then
        If ChatFrs.FrameType = cfStatus Or ((ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfPrivate) And ChatFrs Is FrontCFrame) Then
          If Val(ArgList(4)) < MAX_LNG And Val(ArgList(4)) > 0 Then
            DrawTLine ChatFrs, "- " + TeVal.WhoisIdle + ": ", CoVal.ServerMSG
            ChatFrs.PrintText FormatSec(Val(ArgList(4))), CoVal.NormalText, True, False
          End If
          If Val(ArgList(5)) < MAX_LNG And Val(ArgList(5)) > 0 Then
            DrawTLine ChatFrs, "- " + TeVal.WhoisSO + ": ", CoVal.ServerMSG
            ChatFrs.PrintText DateAdd("s", Val(ArgList(5)) + GMTDiff, START_DATE), CoVal.NormalText, True, False
          End If
        End If
      End If
            
      'Whois 318 End
      If ComName = "318" Then
        If ChatFrs.FrameType = cfStatus Or ((ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfPrivate) And ChatFrs Is FrontCFrame) Then
          DrawTLine ChatFrs, "- " + ArgList(4), CoVal.ServerMSG
          ChatFrs.TextF.SetShiftIn False
        End If
      End If
      
      'Mode
      If ComName = "MODE" Then
        If ChatFrs.FrameType = cfStatus And UCase(ArgList(2)) = UCase(Nick) Then
          If ntScript_Call("Serv_Mode", mVon, ArgList(3), GetAP(ArgList, 4, LastCom), CStr(ConnID)) = True Then
            DrawTLine ChatFrs, "- " + TeVal.UserRank, CoVal.ServerMSG, mVon, GetAP(ArgList, 3, LastCom)
          End If
        End If
        If ChatFrs.FrameType = cfRoom And UCase(ArgList(2)) = UCase(ChatFrs.Caption) Then
          I = 0
          If ntScript_Call("Chan_Mode", mVon, ArgList(3), GetAP(ArgList, 4, LastCom), CStr(ChatFrs.FrameID), CStr(ConnID)) = True Then
            Select Case ArgList(3)
              Case "-x", "+x", "-y", "+y", "-y+x", "-x+y"
              Case Else
                DrawTLine ChatFrs, "- " + TeVal.UserRank, CoVal.ServerMSG, mVon, GetAP(ArgList, 3, LastCom)
            End Select
          End If
          If Len(ArgList(3)) > 1 Then
            PIndex = 4
            SignCounter = 0
            Do
              I3 = 0
              vSign = 0
              For I2 = 1 To Len(ArgList(3))
                If Mid(ArgList(3), I2, 1) = "+" Then
                  vSign = 0
                ElseIf Mid(ArgList(3), I2, 1) = "-" Then
                  vSign = 1
                Else
                  I3 = I3 + 1
                  If I3 > SignCounter Then
                    SignCounter = SignCounter + 1
                    Exit For
                  End If
                End If
              Next
              If I2 > Len(ArgList(3)) Then Exit Do
              
              I = InStr(1, cvPrefix, Mid(ArgList(3), I2, 1))
              If I = 0 Then
                If Mid(ArgList(3), I2, 1) = "b" Then
                  If ChatFrs.AddRemBann(ArgList(PIndex), True) = CBool(vSign) Then ChatFrs.AddRemBann ArgList(PIndex), False, mVon
                  If ChatFrs.ChanValForm Then Form8.LoadBannList
                  PIndex = PIndex + 1
                End If
              Else
                For I2 = 0 To ChatFrs.UListF.ListCount - 1
                  If LCase(ChatNoSign(ChatFrs.UListF.List(I2))) = LCase(ArgList(PIndex)) Then Exit For
                Next
                If I2 < ChatFrs.UListF.ListCount Then
                  DAT = ChatFrs.UListF.List(I2)
                  If vSign Then
                    I3 = InStr(1, DAT, Mid(cvPrefixSign, I, 1))
                    If I3 Then
                      DAT = Left(DAT, I3 - 1) + Mid(DAT, I3 + 1)
                    End If
                  Else
                    I3 = InStr(1, DAT, Mid(cvPrefixSign, I, 1))
                    If I3 = 0 Then
                      For I3 = 1 To Len(DAT)
                        If GetCharValue(Mid(DAT, I3, 1), cvPrefixSign) > I Then Exit For
                      Next
                      DAT = Left(DAT, I3 - 1) + Mid(cvPrefixSign, I, 1) + Mid(DAT, I3)
                    End If
                  End If
                  
                  DAT2 = ChatFrs.UListF.List(ChatFrs.UListF.ListIndex)
                  'ChatFrs.UListF.RemoveByText ChatFrs.UListF.List(I2)
                  ChatFrs.UListF.Remove I2
                  'If ChatFrs Is FrontCFrame Then
                  '  ChatFrs.UListF.Add DAT, , False
                  'Else
                    ChatFrs.UListF.Add DAT, , False
                  'End If
                  ChatFrs.UListF.GotoItem ChatNoSign(DAT2)
                  If ChatFrs Is FrontCFrame Then ChatFrs.UListF.Refresh
                End If
                PIndex = PIndex + 1
              End If
            Loop
          End If
        End If
      End If
            
      'Server Notice
      If UCase(ArgList(0)) = "NOTICE" Then
        If ChatFrs.FrameType = cfStatus Then
          DrawTLine ChatFrs, ArgList(2), CoVal.ServerMSG
        End If
      End If
      
      'Change nick at start
      If ComName = "433" Then
        If ChatFrs.FrameType = cfStatus And LogedIn = False Then
          LastRealNick = Nick
          If IsNumeric(Right(Nick, 1)) Then
            Nick = Left(Nick, Len(Nick) - 1) + CStr(Val(Right(Nick, 1)) + 1)
          Else
            Nick = Nick + "2"
          End If
          If ntScript_Call("Serv_NickChange", LastRealNick, Nick, CStr(ConnID)) Then
            SendLine "NICK " + Nick
          Else
            Nick = LastRealNick
          End If
          Form1.pStateIntRefresh
        End If
      End If
      
      'Show topic (at join)
      If ComName = "332" Then
        If ChatFrs.FrameType = cfRoom And UCase(ChatFrs.Caption) = UCase(ArgList(3)) Then
          If Len(Trim(ArgList(4))) Then
            ChatFrs.PrintText "- Topic: " + ArgList(4), CoVal.ServerMSG, True
            ChatFrs.ChanTopic = ArgList(4)
          End If
        End If
      End If
      
      'Show Room info
      If ComName = "333" Then
        If ChatFrs.FrameType = cfRoom And UCase(ChatFrs.Caption) = UCase(ArgList(3)) Then
          If Val(ArgList(5)) < MAX_LNG And Val(ArgList(5)) > 0 Then
            On Error Resume Next
            DrawTLine ChatFrs, "- " + TeVal.TopicSeter, CoVal.ServerMSG, ArgList(4), _
            Format(DateAdd("s", Val(ArgList(5)) + GMTDiff, START_DATE), "dd.mm.yyyy"), _
            Format(DateAdd("s", Val(ArgList(5)) + GMTDiff, START_DATE), "hh:nn:ss")
            If Err > 0 Then ChatFrs.PrintText "- " + Err.Description + " 333 (Fehler bitte umgehend an Nettalk@ntalk.de melden!", CoVal.ErrorMSG
            On Error GoTo 0
          End If
        End If
      End If
            
      'Show/Set topic
      If ComName = "TOPIC" Then
        If ChatFrs.FrameType = cfRoom And UCase(ChatFrs.Caption) = UCase(ArgList(2)) Then
          DrawTLine ChatFrs, "- " + TeVal.TopicSet + ": ", CoVal.ServerMSG, mVon
          ChatFrs.PrintText ArgList(3), CoVal.ServerMSG, True, False
          ntScript_Call "Chan_TopicChange", ChatFrs.ChanTopic, ArgList(3), ChatFrs.Caption, CStr(ChatFrs.FrameID), CStr(ConnID)
          ChatFrs.ChanTopic = ArgList(3)
        End If
      End If
      
      'Join
      If ComName = "JOIN" Then
        If UCase(ChatFrs.Caption) = UCase(ArgList(2)) And ChatFrs.FrameType = cfRoom Then
          If ChatFrs.UListF.ListCount = 0 Then
            If Nick <> mVon Then
              Nick = mVon
              Form1.pStateIntRefresh
            End If
            If ChatFrs.Caption <> ArgList(2) Then
              ChatFrs.Caption = ArgList(2)
              Form1.ColumRedraw
            End If
            DrawTLine ChatFrs, "- " + TeVal.RoomEnter, CoVal.ServerMSG, ArgList(2)
            ChatFrs.NoBeepTime = AltTimer
            SendLine "mode " + ArgList(2) + " b"
            ChatFrs.BannList = ""
            ChatFrs.BannListState = 0
            ChatFrs.ChanValForm = False
            ChatFrs.InChannel = True
            ntScript_Call "Chan_Join", mVon, ChatFrs.Caption, CStr(ChatFrs.FrameID), CStr(ConnID)
          Else
            For I = 0 To ChatFrs.UListF.ListCount - 1
              If UCase(ChatNoSign(ChatFrs.UListF.List(I))) = UCase(mVon) Then Exit For
            Next
            If I > ChatFrs.UListF.ListCount - 1 Then
              If ntScript_Call("Chan_Join", mVon, ChatFrs.Caption, CStr(ChatFrs.FrameID), CStr(ConnID)) Then
                DrawTLine ChatFrs, "- " + TeVal.Join, CoVal.ServerMSG, mVon
              End If
              If ChatFrs Is FrontCFrame Then ChatFrs.UListF.Add mVon: DrawTitel Else ChatFrs.UListF.Add mVon, , False
            End If
          End If
          Joined = 1
        Else
          If Joined = 0 Then Joined = 2
        End If
      End If
    
      'PART
      If ComName = "PART" Then
        If UCase(ChatFrs.Caption) = UCase(ArgList(2)) And ChatFrs.FrameType = cfRoom Then
          If ntScript_Call("Chan_Part", mVon, ChatFrs.Caption, CStr(ChatFrs.FrameID), CStr(ConnID)) Then
            DrawTLine ChatFrs, "- " + TeVal.Leave, CoVal.ServerMSG, mVon
          End If
          If mVon = Nick Then
            ChatFrs.UListF.Clear
            ChatFrs.InChannel = False
          Else
            ChatFrs.UListF.RemoveByText mVon
          End If
          If ChatFrs Is FrontCFrame Then DrawTitel
        End If
      End If
      
      'KICK
      If ComName = "KICK" Then
        If UCase(ChatFrs.Caption) = UCase(ArgList(2)) And ChatFrs.FrameType = cfRoom Then
          If UCase(ArgList(3)) = UCase(Nick) Then
            If ArgList(4) = mVon Or Len(ArgList(4)) = 0 Then DAT = "-" Else DAT = ArgList(4)
            DrawTLine ChatFrs, "- " + TeVal.Kick, CoVal.ServerMSG, mVon, DAT
            ChatFrs.UListF.Clear
            If ChatFrs Is FrontCFrame Then DrawTitel
            If AutoReJoin = True Then SendLine "JOIN " + ArgList(2) + GetRoomPass(ArgList(2))
            ChatFrs.InChannel = False
          Else
            If ArgList(4) = mVon Or Len(ArgList(4)) = 0 Then DAT = "-" Else DAT = ArgList(4)
            DrawTLine ChatFrs, "- " + TeVal.Kick2, CoVal.ServerMSG, ArgList(3), mVon, DAT
            ChatFrs.UListF.RemoveByText ArgList(3)
            If ChatFrs Is FrontCFrame Then DrawTitel
          End If
          ntScript_Call "Chan_Kick", ArgList(3), mVon, DAT, ChatFrs.Caption, CStr(ChatFrs.FrameID)
        End If
      End If
      
      'QUIT
      If ComName = "QUIT" Then
        If ChatFrs.FrameType = cfPrivate Then
          If UCase(ChatFrs.Caption) = UCase(mVon) Then
            DrawTLine ChatFrs, "- " + TeVal.Quit, CoVal.ServerMSG, mVon, ArgList(2)
          End If
        ElseIf ChatFrs.FrameType = cfRoom Then
          For I = 0 To ChatFrs.UListF.ListCount - 1
            If UCase(ChatNoSign(ChatFrs.UListF.List(I))) = UCase(mVon) Then
              ChatFrs.UListF.RemoveByText mVon
              If ChatFrs Is FrontCFrame Then DrawTitel
              If ntScript_Call("Chan_Quit", mVon, ChatFrs.Caption, ArgList(2), CStr(ChatFrs.FrameID), CStr(ConnID)) Then
                DrawTLine ChatFrs, "- " + TeVal.Quit, CoVal.ServerMSG, mVon, ArgList(2)
              End If
              Exit For
            End If
          Next
        ElseIf ChatFrs.FrameType = cfStatus Then
          ntScript_Call "Serv_Quit", mVon, ArgList(2), CStr(ConnID)
        End If
      End If
    
      'PRIVMSG
      If ComName = "PRIVMSG" Then
        If IsIgnored(IgnoreList, mVon) Then Exit For
        If Left(ArgList(3), 1) = Chr(1) And Left(ArgList(3), 8) <> Chr(1) + "ACTION " And Len(ArgList(3)) > 1 Then
          I = InStr(1, ArgList(3), " ")
          If I = 0 Then I = Len(ArgList(3))
          If Len(ArgList(3)) - I > 0 Then
            I2 = Len(ArgList(3)) - I - 1
          Else
            I2 = 0
          End If
          If ChatFrs.FrameType = cfStatus And LCase(Mid(ArgList(3), 2, I - 2)) <> "dcc" Then
            If Not ntScript_Call("Serv_CTCP", LCase(Mid(ArgList(3), 2, I - 2)), Mid(ArgList(3), I + 1, I2), mVon, CStr(Me.ConnID)) Then Exit Sub
          End If
          If AltTimeDiff(CtcpSkipPoint) > 5 Or CtcpSkipCount < 4 Then
            If ChatFrs.FrameType = cfStatus Then
              If AltTimeDiff(CtcpSkipPoint) > 5 Then CtcpSkipCount = 0
              CtcpSkipCount = CtcpSkipCount + 1
            End If
            Select Case LCase(Mid(ArgList(3), 2, I - 2))
              Case "dcc"
                If FrontCFrame Is ChatFrs And (ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfStatus Or ChatFrs.FrameType = cfPrivate) Then
                  DccConnRequest mVon, ArgList(3), ChatFrs, Nick
                  sDCC = 1
                Else
                  If sDCC = 0 Then sDCC = 2
                End If
              Case "version"
                If ChatFrs.FrameType = cfStatus Then
                  SendLine "NOTICE " + mVon + " :" + Chr(1) + "VERSION " + App.Title + "  " & App.Major & "." & App.Minor & "." & App.Revision & "  " + App.LegalCopyright + " (www.ntalk.de)" + Chr(1)
                  DrawTLine ChatFrs, "- " + mVon + " CTCP-Version", CoVal.ServerMSG
                End If
              Case "time"
                If ChatFrs.FrameType = cfStatus Then
                  SendLine "NOTICE " + mVon + " :" + Chr(1) + "TIME " + DateFormatEn(Now) + Chr(1)
                  DrawTLine ChatFrs, "- " + mVon + " CTCP-Time", CoVal.ServerMSG
                End If
              Case "finger"
                If ChatFrs.FrameType = cfStatus Then
                  SendLine "NOTICE " + mVon + " :" + Chr(1) + "FINGER " + FingerData + Chr(1)
                  DrawTLine ChatFrs, "- " + mVon + " CTCP-Finger", CoVal.ServerMSG
                End If
              Case "ping"
                If ChatFrs.FrameType = cfStatus Then
                  If Len(ArgList(3)) < 32 Then
                    If I + 1 > Len(ArgList(3)) Then I = Len(ArgList(3)) - 1
                    SendLine "NOTICE " + mVon + " :" + Chr(1) + "Ping " + Mid(ArgList(3), I + 1, Len(ArgList(3)) - I - 1) + Chr(1)
                  End If
                  DrawTLine ChatFrs, "- " + mVon + " CTCP-Ping/Pong", CoVal.ServerMSG
                End If
              Case Else
                If ChatFrs.FrameType = cfStatus Then
                  DrawTLine ChatFrs, "- " + mVon + " CTCP: " + ArgList(3), CoVal.ServerMSG
                End If
            End Select
          ElseIf ChatFrs.FrameType = cfStatus Then
            DrawTLine ChatFrs, "- CTCP request skiped (" + mVon + ": CTCP-" + ArgList(3) + ")", CoVal.ErrorMSG
          End If
          CtcpSkipPoint = AltTimer
        Else
          If (UCase(ChatFrs.Caption) = UCase(ArgList(2)) And ChatFrs.FrameType = cfRoom) _
          Or (UCase(ChatFrs.Caption) = UCase(mVon) And ChatFrs.FrameType = cfPrivate And UCase(ArgList(2)) = UCase(Nick)) Then
            If Left(ArgList(3), 8) = Chr(1) + "ACTION " Then  'Me-MSG
              DecodedText = DecodeText(Mid(ArgList(3), 9, Len(ArgList(3)) - 9), ctcAk, CTCKey)
              If Not ntScript_Call("Serv_Action", DecodedText, mVon, CStr(ChatFrs.FrameID), CStr(Me.ConnID)) Then Exit Sub
              If ChatFrs.FrameType = cfPrivate Then SetAl ChatFrs, 4, mVon, DecodedText, True
              If ChatFrs.FrameType = cfRoom Then
                SetAlPriv ChatFrs, mVon, DecodedText, Nick, True
                NewTextAdded ChatFrs
              End If
              DrawSLine ChatFrs, 0, "* ", CoVal.NormalText, mVon, 5, , " " + DecodedText, ctcAk
            Else
              DecodedText = DecodeText(ArgList(3), ctcAk, CTCKey)
              If ChatNoSign(ArgList(2)) = ArgList(2) Then
                If Not ntScript_Call("Serv_Query", DecodedText, mVon, CStr(ChatFrs.FrameID), CStr(Me.ConnID)) Then Exit Sub
              Else
                If Not ntScript_Call("Chan_Msg", DecodedText, mVon, ArgList(2), CStr(ChatFrs.FrameID), CStr(Me.ConnID)) Then Exit Sub
              End If
              If ChatFrs.FrameType = cfPrivate Then SetAl ChatFrs, 4, mVon, DecodedText
              If ChatFrs.FrameType = cfRoom Then
                SetAlPriv ChatFrs, mVon, DecodedText, Nick
                NewTextAdded ChatFrs
              End If
              DrawSLine ChatFrs, 0, "<" + mVon + ">", CoVal.NormalVon, DecodedText, 3, , , ctcAk
            End If
            PrivMSG = 1
          Else
            If PrivMSG = 0 Or PrivMSG = 3 And ChatNoSign(ArgList(2)) = ArgList(2) Then PrivMSG = 2
          End If
        End If
      End If
      
      'NOTICE
      If ComName = "NOTICE" And Left(ArgList(3), 10) <> "DCC Chat (" And Left(ArgList(3), 9) <> "DCC Send " Then
        If IsIgnored(IgnoreList, mVon) Then Exit For
        DecodedText = DecodeText(ArgList(3), ctcAk, CTCKey)
        If RemChanSign(ArgList(2)) = ArgList(2) Or ArgList(2) = "*" Then
          If sNotice = 0 Then
            If ntScript_Call("Serv_Notice", DecodedText, mVon, ArgList(2), CStr(Me.ConnID)) = False Then sNotice = 1: Exit For
          End If
          
          If sNotice <> 1 And FrontCFrame Is ChatFrs Then
            I = InStr(1, ArgList(0), "@")
            If ChatFrs.FrameType = cfRoom And I Then
              For I = 0 To ChatFrs.UListF.ListCount - 1
                If UCase(ChatNoSign(ChatFrs.UListF.List(I))) = UCase(mVon) Then
                  ChatFrs.LastWisperer = mVon
                  If Left(DecodedText, 1) = Chr(1) Then
                    I2 = InStr(1, DecodedText, " ")
                    If I2 < Len(DecodedText) And I2 > 2 Then
                      If LCase(Mid(ArgList(3), 2, 5)) = "ping " Then
                        DrawSLine ChatFrs, 0, "<CTCP-Pong:" + mVon + ">", CoVal.ServerMSG, CStr(Fix(Timer * 1000) - Val(Mid(ArgList(3), 7, Len(ArgList(3)) - 2))) + " ms", 3, mVon, , ctcAk
                      Else
                        DrawSLine ChatFrs, 0, "<CTCP-" + Mid(DecodedText, 2, I2 - 2) + ":" + mVon + ">", CoVal.ServerMSG, Mid(DecodedText, I2 + 1, Len(DecodedText) - I2 - 1), 3, mVon, , ctcAk
                      End If
                    End If
                  Else
                    If Nick = ArgList(2) Then
                      DrawSLine ChatFrs, 0, "<" + mVon + " " + TeVal.Wispers + ">", CoVal.FlusterVon, DecodedText, 3, , , ctcAk
                    Else
                      DrawSLine ChatFrs, 0, "<" + mVon + " " + TeVal.WispersTo + " " + ArgList(2) + ">", CoVal.FlusterVon, DecodedText, 3, , , ctcAk
                    End If
                  End If
                  If AltTimeDiff(ChatFrs.NoBeepTime + 10) > 0 Then
                    SetAl ChatFrs, 4, mVon, DecodedText
                  End If
                  sNotice = 1
                  Exit For
                End If
              Next
            End If
          End If
          If sNotice = 0 Then sNotice = 2
        Else
          If LCase(ArgList(2)) = LCase(ChatFrs.Caption) And ChatFrs.FrameType = cfRoom Then
            SetAlPriv ChatFrs, mVon, DecodedText, Nick
            DrawSLine ChatFrs, 0, "- ", CoVal.FlusterVon, mVon, 3, , ": " + DecodedText, ctcAk
          End If
        End If
      End If
      
      'NICK
      If ComName = "NICK" Then
        If ChatFrs.FrameType = cfPrivate And UCase(ChatFrs.Caption) = UCase(mVon) Then
          DrawTLine ChatFrs, "- " + TeVal.Nick, CoVal.ServerMSG, mVon, ArgList(2)
          ChatFrs.Caption = ArgList(2)
          Form1.ColumRedraw True
        End If
        If ChatFrs.FrameType = cfStatus Then
          If UCase(mVon) = UCase(Nick) Or mVon = ArgList(2) Then
            Nick = ArgList(2)
            LastUsedNick = Nick
            DrawTLine ChatFrs, "- " + TeVal.Nick, CoVal.ServerMSG, mVon, ArgList(2)
            Form1.pStateIntRefresh
          End If
          ntScript_Call "Serv_NickChange", LastRealNick, Nick, CStr(ConnID)
        End If
        If ChatFrs.FrameType = cfRoom Then
          I = 0
          For I = 0 To ChatFrs.UListF.ListCount - 1
            If UCase(ChatNoSign(ChatFrs.UListF.List(I))) = UCase(mVon) Then
              If ntScript_Call("Chan_NickChange", ChatFrs.UListF.List(I), ArgList(2), CStr(ChatFrs.FrameID), CStr(ConnID)) Then
                DrawTLine ChatFrs, "- " + TeVal.Nick, CoVal.ServerMSG, mVon, ArgList(2)
              End If
              If ChatFrs.UListF.List(I) = ChatNoSign(ChatFrs.UListF.List(I)) Then DAT = "" Else DAT = Left(ChatFrs.UListF.List(I), 1)
              ChatFrs.UListF.RemoveByText ChatFrs.UListF.List(I)
              If ChatFrs Is FrontCFrame Then
                ChatFrs.UListF.Add DAT + ArgList(2)
              Else
                ChatFrs.UListF.Add DAT + ArgList(2), , False
              End If
              Exit For
            End If
          Next
        End If
      End If
      
      'StartCommands
      If ComName = "001" Then
        If ChatFrs.FrameType = cfStatus Then
          If Len(NickPass) Then
            SendLine "PRIVMSG NickServ :IDENTIFY " + NickPass
          End If
          I = -1
          Do
            I2 = I
            I = InStr(I + 2, StartCommands, vbNewLine)
            If I = 0 Then I = Len(StartCommands) + 1
            If Mid(StartCommands, I2 + 2, 1) = "/" Or Mid(StartCommands, I2 + 2, 1) = "$" Then I2 = I2 + 1
            If I - I2 > 2 Then SendComm CReplace("", Mid(StartCommands, I2 + 2, I - I2 - 2), ChatFrs, wins), ChatFrs
          Loop Until I = Len(StartCommands) + 1
          If JoinMode <> 0 Then SendLine "MODE " + Nick + " +i"
          RequestWUState Server
          LogedIn = True
          Form1.ColumRedraw
          If wins.LocalRooterIP = 0 Then
            I = InStr(1, ArgList(3), "@")
            If I > 0 Then
              wins.LocalRooterIP = ConvIPtoDouble(GetAscIp(GetHostByNameAlias(Mid(ArgList(3), I + 1))), True)
            End If
            If wins.LocalRooterIP = 0 And UseSecIP = True Then
              SendLine "USERIP " + Nick
            End If
          End If
          ntScript_Call "Serv_Ready", CStr(ConnID)
        End If
      End If
      
      'GetRouterIP / USERIP
      If ComName = "340" Then
        If ChatFrs.FrameType = cfStatus Then
          If wins.LocalRooterIP = 0 Then
            I = InStr(1, ArgList(3), "@")
            If I > 0 Then
              wins.LocalRooterIP = ConvIPtoDouble(GetAscIp(GetHostByNameAlias(Mid(ArgList(3), I + 1))), True)
            End If
          End If
          ChatFrs.PrintText "- " + ArgList(3), CoVal.ServerMSG, True
        ElseIf (ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfPrivate) And ChatFrs Is FrontCFrame Then
          ChatFrs.PrintText "- " + ArgList(3), CoVal.ServerMSG, True
        End If
      End If
            
      'UserListe
      If ComName = "353" Then
        If UCase(ChatFrs.Caption) = UCase(ArgList(4)) And ChatFrs.FrameType = cfRoom Then
          If ChatFrs.UserListRef = 0 Then
            ChatFrs.UListF.Clear
            ChatFrs.UserListRef = AltTimer
          End If
          I = 0
          Do
            I2 = I
            I = InStr(I + 1, ArgList(5), " ")
            If I = 0 Then I = Len(ArgList(5)) + 1
            ChatFrs.UListF.Add Mid(ArgList(5), I2 + 1, I - I2 - 1), , False
            If AltTimeDiff(ChatFrs.UserListRef) > 0.4 And ChatFrs Is FrontCFrame Then
              ChatFrs.UListF.Refresh
              ChatFrs.UserListRef = AltTimer
              DrawTitel
            End If
          Loop Until I > Len(ArgList(5)) - 1
        End If
      End If
      
      'End of Userliste
      If ComName = "366" Then
        If ChatFrs Is FrontCFrame And ChatFrs.FrameType = cfRoom Then
          ChatFrs.UListF.Refresh: DrawTitel
        End If
        ChatFrs.UserListRef = 0
      End If
      
      'StartRoomListe
      If ComName = "321" Then
        If ChatFrs.FrameType = cfRoomList Then
          ChatFrs.UListF.Clear
          ChanListRef = 0
          RListMode = 1
          ListRooms = 1
          ChatFrs.Caption = TeVal.lsRooms + "..."
          Form1.ColumRedraw
        Else
          If ListRooms = 0 Then ListRooms = 2
        End If
      End If
      
      'RoomListe
      If ComName = "322" Then
        If ChatFrs.FrameType = cfRoomList Then
          If RListMode = 0 Then
            ChatFrs.UListF.Clear
            RListMode = 1
            ChatFrs.Caption = TeVal.lsRooms + "..."
            Form1.ColumRedraw
          End If
          DAT = ""
          If Left(ArgList(3), 1) <> "*" Then
            DAT = DAT + ArgList(3) + Chr(0)
            I2 = I
            DAT = DAT + Space(5 - Len(ArgList(4))) + ArgList(4) + Chr(0)
            DAT = DAT + ArgList(5) + Chr(0)
            ChatFrs.UListF.Add DAT, , False, , False
            If AltTimeDiff(ChanListRef) > 0.4 And ChatFrs Is FrontCFrame Then
              ChatFrs.UListF.Refresh
              ChanListRef = AltTimer
              If ChatFrs Is FrontCFrame Then DrawTitel
            End If
            ListRooms = 1
          End If
        ElseIf RListMode = 0 Then
          If ListRooms = 0 Then ListRooms = 2
        End If
      End If
    
      'End of RoomListe
      If ComName = "323" Then
        If ChatFrs.FrameType = cfRoomList Then
          ChatFrs.Caption = TeVal.lsRooms
          If ChatFrs Is FrontCFrame Then ChatFrs.UListF.Refresh: DrawTitel
          Form1.ColumRedraw
        ElseIf ChatFrs.FrameType = cfStatus Then
          RListMode = 0
          'DrawTLine ChatFrs, "- " + ArgList(3), CoVal.ServerMSG
        End If
      End If
    
      'Ping
      If ArgList(0) = "PING" Then
        If ChatFrs.FrameType = cfStatus Then
          If ntScript_Call("Serv_Ping", CStr(ConnID), ArgList(1)) Then
            SendLine "PONG :" + ArgList(1)
            DrawTLine ChatFrs, "- Ping/Pong", CoVal.NormalVon
          End If
        End If
      End If
      
      'Pong
      If ArgList(0) = "PONG" Then
        If ChatFrs.FrameType = cfStatus Then
          DrawTLine ChatFrs, "- Pong", CoVal.NormalVon
        End If
      End If
      
      'Friend comes Online
      If ComName = "604" Or ArgList(1) = "600" Then
        If ChatFrs.FrameType = cfStatus Then
          If I > 0 Then
            SetFList ArgList(3), Server, 1, 0
            If ComName = "600" Then
              If ntScript_Call("Serv_Watch", ArgList(3), "1", CStr(ConnID)) Then
                SetAl ChatFrs, 4, ArgList(3), SetInVals(TeVal.FrindOnline, ArgList(3))
                DrawTLine ChatFrs, "- " + TeVal.FrindOnline, CoVal.ClientMSG, ArgList(3)
              End If
            Else
              ntScript_Call "Serv_Watch", ArgList(3), "3", CStr(ConnID)
            End If
          End If
        End If
      End If
    
      'Friend comes Offline
      If ComName = "605" Or ArgList(1) = "601" Then
        If ChatFrs.FrameType = cfStatus Then
          If I > 0 Then
            SetFList ArgList(3), Server, 2, 0
            If ArgList(1) = "601" Then
              ntScript_Call "Serv_Watch", ArgList(3), "0", CStr(ConnID)
            Else
              ntScript_Call "Serv_Watch", ArgList(3), "2", CStr(ConnID)
            End If
          End If
        End If
      End If
      
      'UserInfo
      If ComName = "302" Then
        If ChatFrs.FrameType = cfStatus Then
          If SendCommFromBuffer(Trim(ArgList(3))) = False Then
            DrawTLine ChatFrs, "- " + GetAP(ArgList, 3, LastCom), CoVal.ServerMSG
          End If
        End If
      End If
      
      'BannListEntry
      If ComName = "367" Then
        If ChatFrs.FrameType = cfRoom And ChatFrs.Caption = ArgList(3) Then
          If ChatFrs.BannListState = 2 Then
            DrawTLine ChatFrs, "- " + GetAP(ArgList, 3, LastCom), CoVal.ServerMSG
          Else
            ChatFrs.AddRemBann ArgList(4), False, ArgList(5), ArgList(6)
            ChatFrs.BannListState = 1
          End If
        End If
      End If
      
      'End of BannList
      If ComName = "368" Then
        If ChatFrs.FrameType = cfRoom And ChatFrs.Caption = ArgList(3) Then
          If ChatFrs.BannListState = 2 Then
            DrawTLine ChatFrs, "- " + GetAP(ArgList, 3, LastCom), CoVal.ServerMSG
          Else
            ChatFrs.BannListState = 2
          End If
        End If
      End If
    
      'ModeReques (Owner)
      If ComName = "324" Then
        If (ChatFrs.FrameType = cfRoom And ChatFrs.Caption = ArgList(3)) Or ChatFrs.FrameType = cfStatus Then
          If ChatFrs.ModeRequesState Then
            If Not SellCFrame Is Nothing Then
              If SellCFrame.ChanValForm Then Form8.GetData ArgList(4), ArgList(5)
            End If
          Else
            DrawTLine ChatFrs, "- " + GetAP(ArgList, 3, LastCom), CoVal.ServerMSG
          End If
        End If
      End If
      
      'ModeReques
      If ComName = "329" Then
        If (ChatFrs.FrameType = cfRoom And ChatFrs.Caption = ArgList(3)) Or ChatFrs.FrameType = cfStatus Then
          If ChatFrs.ModeRequesState Then
            ChatFrs.ModeRequesState = False
          Else
            If Val(ArgList(4)) < MAX_LNG And Val(ArgList(4)) > 0 Then
              DrawTLine ChatFrs, "- " + CStr(DateAdd("s", Val(ArgList(4)) + GMTDiff, START_DATE)), CoVal.ServerMSG
            End If
          End If
        End If
      End If
    
      'AwayMsg
      If ComName = "301" Then
        If ChatFrs.FrameType = cfStatus Or ((ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfPrivate) And ChatFrs Is FrontCFrame) Then
          If ChatFrs.AwayMarker = False Then
            ChatFrs.AwayMarker = True
            DrawTLine ChatFrs, "- " + TeVal.Away, CoVal.ServerMSG, ArgList(3), ArgList(4)
          End If
        End If
      End If
      
      'Invited
      If LCase(ArgList(1)) = "invite" Then
        If ChatFrs.FrameType = cfStatus Then ntScript_Call "Serv_Invited", ArgList(3), mVon, CStr(ConnID)
        If ChatFrs.FrameType = cfStatus Or ((ChatFrs.FrameType = cfRoom Or ChatFrs.FrameType = cfPrivate) And ChatFrs Is FrontCFrame) Then
          DrawTLine ChatFrs, "- " + TeVal.Invite, CoVal.ServerMSG, mVon, ArgList(3)
        End If
      End If
    End If
  Next
  If Joined = 2 Then
    If Nick <> mVon Then
      Nick = mVon
      Form1.pStateIntRefresh
    End If
    Set ChatFrs = NewFrame(Me, ArgList(2), cfRoom)
    ChatFrs.UListF.cvPrefix = cvPrefix
    ChatFrs.UListF.cvPrefixSign = cvPrefixSign
    DrawTLine ChatFrs, "- " + TeVal.RoomEnter, CoVal.ClientMSG, ArgList(2)
    ChatFrs.NoBeepTime = AltTimer
    SendLine "mode " + ArgList(2) + " b"
    ChatFrs.BannList = ""
    ChatFrs.BannListState = 0
    ChatFrs.ModeRequesState = False
    ChatFrs.InChannel = True
    If InStr(1, LastUsedChans, "|" + ChatFrs.Caption + "|") = 0 Then
      If Len(LastUsedChans) = 0 Then LastUsedChans = LastUsedChans + "|"
      LastUsedChans = Right(LastUsedChans, 256) + ChatFrs.Caption + "|"
    End If
    ntScript_Call "Chan_Join", mVon, ChatFrs.Caption, CStr(ChatFrs.FrameID), CStr(ConnID)
  End If
  If ListRooms = 2 Then
    Set ChatFrs = NewFrame(Me, TeVal.lsRooms + "...", cfRoomList)
    ChatFrs.UListF.SetColum 0, TeVal.ls2Name, 120
    ChatFrs.UListF.AddColum TeVal.ls2User, 50
    ChatFrs.UListF.AddColum TeVal.ls2Topic, , True
    RListMode = 1
  End If
  If PrivMSG = 2 Then
    Set ChatFrs = NewFrame(Me, mVon, cfPrivate, False)
    If Left(ArgList(3), 8) = Chr(1) + "ACTION " Then
      DecodedText = DecodeText(Mid(ArgList(3), 9, Len(ArgList(3)) - 9), ctcAk, CTCKey)
      If Not ntScript_Call("Serv_Action", DecodedText, mVon, CStr(ChatFrs.FrameID), CStr(Me.ConnID)) Then Exit Sub
      DrawSLine ChatFrs, 0, "* ", CoVal.NormalText, mVon, 5, , " " + DecodedText, ctcAk
      SetAl ChatFrs, 4, mVon, DecodedText, True
    Else
      DecodedText = DecodeText(ArgList(3), ctcAk, CTCKey)
      If Not ntScript_Call("Serv_Query", DecodedText, mVon, CStr(ChatFrs.FrameID), CStr(Me.ConnID)) Then Exit Sub
      DrawSLine ChatFrs, 0, "<" + mVon + ">", CoVal.NormalVon, DecodedText, 3, , , ctcAk
      SetAl ChatFrs, 4, mVon, DecodedText
    End If
  End If
  If sNotice = 2 Then
    DecodedText = DecodeText(ArgList(3), ctcAk, CTCKey)
    sNotice = 1
    For Each ChatFrs In ChatFrames
      If ChatFrs.IrcConn Is Me And ChatFrs.FrameType = cfRoom Then
        I = 0
        For I = 0 To ChatFrs.UListF.ListCount - 1
          If UCase(ChatNoSign(ChatFrs.UListF.List(I))) = UCase(mVon) Then
            If Nick = ArgList(2) Then
              DrawSLine ChatFrs, 0, "<" + mVon + " " + TeVal.Wispers + ">", CoVal.FlusterVon, DecodedText, 3, , , ctcAk
            Else
              DrawSLine ChatFrs, 0, "<" + mVon + " " + TeVal.WispersTo + " " + ArgList(2) + ">", CoVal.FlusterVon, DecodedText, 3, , , ctcAk
            End If
            If AltTimeDiff(ChatFrs.NoBeepTime) > 10 Then
              SetAl ChatFrs, 4, mVon, DecodedText
            End If
            sNotice = 2
            Exit For
          End If
        Next
        If sNotice = 2 Then Exit For
      End If
    Next
    If sNotice = 1 Then
      For Each ChatFrs In ChatFrames
        If ChatFrs.IrcConn Is Me And (ChatFrs.FrameType = cfStatus Or _
        (FrontCFrame Is ChatFrs And ChatFrs.FrameType = cfPrivate) Or _
        (FrontCFrame Is ChatFrs And ChatFrs.FrameType = cfRoom And AltTimeDiff(ChatFrs.NoBeepTime) > 10)) Then
          DrawSLine ChatFrs, 2, "- ", CoVal.Notice, mVon, 5, , ": " + DecodedText, ctcAk
          'SetAl ChatFrs, 4, mVon, Right(DecodedText, Len(DecodedText) - 1) '<--- wegen Willkommens msg
        End If
      Next
    End If
  End If
  If sDCC = 2 Then
    sDCC = 1
    For Each ChatFrs In ChatFrames
      If ChatFrs.IrcConn Is Me And ChatFrs.FrameType = cfRoom Then
        I = 0
        For I = 0 To ChatFrs.UListF.ListCount - 1
          If UCase(ChatNoSign(ChatFrs.UListF.List(I))) = UCase(mVon) Then
            DccConnRequest mVon, ArgList(3), ChatFrs, Nick
            sDCC = 2
            Exit For
          End If
        Next
        If sDCC = 2 Then Exit For
      End If
    Next
    If sDCC = 1 Then
      For Each ChatFrs In ChatFrames
        If ChatFrs.IrcConn Is Me And ChatFrs.FrameType = cfStatus Then
          DccConnRequest mVon, ArgList(3), ChatFrs, Nick
          Exit For
        End If
      Next
    End If
  End If
End Sub

Private Function SendCommFromBuffer(Data As String) As Boolean
  Dim I As Integer
  Dim I2 As Integer
  Dim I3 As Integer
  Dim uAdr As String
  Dim uNick As String
  Dim uID As String
  Dim Okval As Boolean
  
  I = InStr(1, Data, "=")
  If I > 0 Then uNick = LCase(Left(Data, I - 1))
  If Len(Data) > I + 2 Then
    I2 = InStr(I + 2, Data, "@")
    If I2 > 0 Then
      uAdr = Mid(Data, I2 + 1)
      uID = ChatNoSign(Mid(Data, I + 1, I2 - I - 1))
    End If
  End If
  
  I = 0
  Do
    I = InStr(I + 1, ComBuffer, "$(")
    If I = 0 Then
      If Okval Then
        SendComm ComBuffer, BufferFrame, -2
        Set BufferFrame = Nothing
        SendCommFromBuffer = True
      End If
      Exit Do
    End If
    I2 = InStr(I + 1, ComBuffer, ":")
    I3 = InStr(I2 + 1, ComBuffer, ")")
    If I2 = 0 Or I3 = 0 Then Exit Do
    If LCase(Mid(ComBuffer, I2 + 1, I3 - I2 - 1)) = uNick Then
      Select Case Mid(ComBuffer, I + 2, I2 - I - 2)
        Case "UserAdr"
          ComBuffer = Left(ComBuffer, I - 1) + uAdr + Mid(ComBuffer, I3 + 1)
        Case "UserID"
          ComBuffer = Left(ComBuffer, I - 1) + uID + Mid(ComBuffer, I3 + 1)
      End Select
      Okval = True
    End If
  Loop
End Function

Private Sub wins_Connected()
  Dim I As Integer
  
  If State <> 3 Then Exit Sub
  If SockProxyPort > 0 Then SendProxyLogin wins, RealServer, ServerIP, Port, SockProxyType
  
  State = 4
  For I = 1 To ChatFrames.Count
    If ChatFrames(I).IrcConn Is Me Then
      Select Case ChatFrames(I).FrameType
        Case cfRoom, cfPrivate, cfStatus
          ChatFrames(I).PrintText "  " + TeVal.ConOK, CoVal.ClientMSG, , False
          ChatFrames(I).NoBeepTime = AltTimer
        Case cfDebug
          If Debuging Then
            ChatFrames(I).PrintText "- " + CStr(Time), CoVal.TimeStamp, True, True
            ChatFrames(I).PrintText " Connected", CoVal.ClientMSG, True, False
          End If
      End Select
      If ChatFrames(I) Is FrontCFrame Then
        Form1.pStateIntRefresh
        DrawTitel
      End If
    End If
  Next
  KillTimerByID 1, Me
  If Len(ServerPass) Then SendLine "PASS " + ServerPass
  SendLine "NICK " + Nick
  
  If Len(Trim(Realname)) = 0 Then Realname = Nick
  If Len(Trim(UserID)) = 0 Then UserID = MakeUserID(Nick)
  
  SendLine "USER " + UserID + " """ + GetLocalHostName _
  + """ """ + RealServer + """ :" + Realname
  
  ConnList(ListID).State = 2
  RefreshList
  ConTimeOut = 0
  StartTimer Me, 20000, 4
  Form1.IsNowActiv
  ntScript_Call "Serv_Connected", CStr(ConnID)
End Sub

Private Sub WinS_DataArrival()
  Dim iStart As Long
  Dim iLen As Long
  Dim I As Long

  ConTimeOut = 0
  
  If DataWaitMode = 0 Then
    iStart = wins.ReciveBin(VarPtr(Buffer(BufferWrite)), UBound(Buffer) - BufferWrite - 1)
    If iStart = 0 Then
      DataWaitMode = 0
    Else
      DataWaitMode = 1
      RecevedBytes = RecevedBytes + iStart
      BufferWrite = BufferWrite + iStart
      
      If ProxyState = 1 Then
        BufferRead = BufferRead + 8
        ProxyState = 2
      End If
      iStart = 0
      
      For I = 0 To BufferWrite - 1
        If Buffer(I) = 10 Then
          iStart = I
          If I > BufferRead Then
            If Buffer(I - 1) = 13 Then iStart = iStart - 1
          End If
          PraseCommandFromByteArray Buffer, BufferRead, iStart - BufferRead
          BufferRead = I + 1
        End If
      Next
      If BufferWrite > BufferRead Then
        MemCopy Buffer(0), VarPtr(Buffer(BufferRead)), BufferWrite - BufferRead
      End If
      BufferWrite = BufferWrite - BufferRead
      BufferRead = 0
      
      StartTimer Me, 1, 2
      If FrontCFrame.FrameType = cfServerList And Form1.Visible = True Then Form1.pStateRefresh
    End If
  End If
  If FrontCFrame.TCHange And FrontCFrame.IrcConn Is Me Then
    FrontCFrame.TextF.Refresh
    FrontCFrame.TCHange = False
  End If
End Sub

Private Sub wins_Error(Error As Long, Description As String)
  Dim I As Integer
  For I = 1 To ChatFrames.Count
    If ChatFrames(I).IrcConn Is Me Then
      If ChatFrames(I).FrameType = cfRoom Or ChatFrames(I).FrameType = cfPrivate Or ChatFrames(I).FrameType = cfStatus Then
        If State = 3 Then
          ChatFrames(I).PrintText "  " + TeVal.ConErr, CoVal.ErrorMSG, , False
        End If
      End If
    End If
  Next
  If State > 1 Then
    CloseConnection
    ReConn = RetryTime
    StartTimer Me, 1000, 3
  End If
End Sub

Private Sub wins_SendComplete()
  SendBufferState = 1
  SendFromBuff
End Sub
