home *** CD-ROM | disk | FTP | other *** search
/ CD Shareware Magazine 1999 April / CD_Shareware_Magazine_31.iso / Free / Prg / irc-code.exe / IRCPRE2.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-10-21  |  11.7 KB  |  362 lines

  1. VERSION 4.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "IRC Preface Example Client - Revised"
  4.    ClientHeight    =   4672
  5.    ClientLeft      =   1552
  6.    ClientTop       =   1664
  7.    ClientWidth     =   6880
  8.    Height          =   5312
  9.    Left            =   1488
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   292
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   430
  14.    Top             =   1088
  15.    Width           =   7008
  16.    Begin VB.ListBox NameList 
  17.       Height          =   3808
  18.       Left            =   5296
  19.       Sorted          =   -1  'True
  20.       TabIndex        =   3
  21.       Top             =   384
  22.       Width           =   1488
  23.    End
  24.    Begin VB.TextBox Topic 
  25.       Height          =   304
  26.       Left            =   64
  27.       TabIndex        =   2
  28.       Top             =   64
  29.       Width           =   6720
  30.    End
  31.    Begin VB.TextBox Outgoing 
  32.       Height          =   300
  33.       Left            =   64
  34.       TabIndex        =   1
  35.       Top             =   3888
  36.       Width           =   5232
  37.    End
  38.    Begin VB.TextBox Incoming 
  39.       Height          =   3504
  40.       Left            =   64
  41.       MultiLine       =   -1  'True
  42.       ScrollBars      =   2  'Vertical
  43.       TabIndex        =   0
  44.       Top             =   384
  45.       Width           =   5232
  46.    End
  47.    Begin WINSOCKLib.TCP TCP1 
  48.       Left            =   6400
  49.       Top             =   4224
  50.       _ExtentX        =   709
  51.       _ExtentY        =   709
  52.       RemoteHost      =   ""
  53.       RemotePort      =   0
  54.       LocalPort       =   0
  55.    End
  56.    Begin VB.Menu mnuFile 
  57.       Caption         =   "&File"
  58.       Begin VB.Menu FileConnect 
  59.          Caption         =   "&Connect"
  60.       End
  61.       Begin VB.Menu FileSetup 
  62.          Caption         =   "&Setup"
  63.       End
  64.       Begin VB.Menu dash 
  65.          Caption         =   "-"
  66.       End
  67.       Begin VB.Menu FileExit 
  68.          Caption         =   "E&xit"
  69.       End
  70.    End
  71.    Begin VB.Menu mnuHelp 
  72.       Caption         =   "&Help"
  73.       Begin VB.Menu HelpAbout 
  74.          Caption         =   "&About"
  75.       End
  76.    End
  77. Attribute VB_Name = "Form1"
  78. Attribute VB_Creatable = False
  79. Attribute VB_Exposed = False
  80. ' This example and included document are
  81. ' Copyright (C) 1996 by Dann Daggett II
  82. ' Please read the document that comes with this
  83. ' program.
  84. Dim CRLF As String ' Cairrage return/Line feed
  85. Dim OldText As String ' Holds any text still
  86.                       ' needing processed
  87. Dim channel As String ' Holds the channel name
  88. Dim CMode ' CurrentMode of client
  89.           ' 0 is logged in
  90.           ' 1 is joining channel
  91.           ' 2 is in channel
  92. Sub AddText(textmsg As String)
  93.   ' Add the data in textmsg to the Incoming
  94.   ' text box and force the text down
  95.   Incoming.Text = Incoming.Text & textmsg & CRLF
  96. End Sub
  97. Sub SendData(textmsg As String)
  98.   ' Send the data in textmsg to the server, and
  99.   ' add a CRLF
  100.   TCP1.SendData textmsg & CRLF
  101. End Sub
  102. Private Sub FileConnect_Click()
  103.   If FileConnect.Caption = "&Connect" Then
  104.     ' Set the RemoteHost to the IRC Server Host
  105.     TCP1.RemoteHost = Server
  106.     ' Set the Port to connect to
  107.     TCP1.RemotePort = Port
  108.     ' Connect
  109.     TCP1.Connect
  110.     ' Clear textbox, topic and listbox
  111.     Incoming.Text = ""
  112.     NameList.Clear
  113.     Topic.Text = ""
  114.     AddText "*** Attempting to connect to " & Server & "..."
  115.     FileConnect.Caption = "&Disconnect"
  116.   Else
  117.     FileConnect.Caption = "&Connect"
  118.     AddText "*** Disconnected"
  119.     ' Close the socket
  120.     TCP1.Close
  121.   End If
  122. End Sub
  123. Private Sub FileExit_Click()
  124.   ' Close the program
  125.   Unload Me
  126. End Sub
  127. Private Sub FileSetup_Click()
  128.   ' Show the setup form
  129.   setup.Show 1
  130. End Sub
  131. Private Sub Form_Activate()
  132.   ' Scroll the textbox down again
  133.   Incoming_Change
  134. End Sub
  135. Private Sub Form_Load()
  136.   ' Set CRLF to be Cairrage Return + Line Feed,
  137.   ' ALL IRC messages end with this
  138.   CRLF = Chr$(13) & Chr$(10)
  139.   ' Set the current mode to 0
  140.   CMode = 0
  141.   'Set the default values
  142.   Server = "irc.neosoft.com"
  143.   Port = 6667
  144.   Nickname = "IRCPre2"
  145. End Sub
  146. Private Sub HelpAbout_Click()
  147.   about.Show 1
  148. End Sub
  149. Private Sub Incoming_Change()
  150. ' We want this box to scroll down automatically.
  151.   Incoming.SelStart = Len(Incoming.Text)
  152. ' What this does is says, make the start of my
  153. ' selected text the end of the entire text,
  154. ' which effectively scrolls down the textbox,
  155. ' but does not select anything. The len()
  156. ' command returns the length of characters of
  157. ' the text, in a number.
  158. End Sub
  159. Private Sub Incoming_GotFocus()
  160. ' We don't want the client to be able to edit
  161. ' the Incoming textbox.
  162.   Outgoing.SetFocus
  163. ' This make it so the user cannot click inside
  164. ' the Incoming text box, but can still scroll it.
  165. ' It does this by giving another object the
  166. ' focus.
  167. End Sub
  168. Private Sub Outgoing_KeyPress(KeyAscii As Integer)
  169.   Dim msg As String
  170.   ' Exit unless its a return, then process
  171.   If KeyAscii <> 13 Then Exit Sub
  172.   KeyAscii = 0 ' Stop that stupid beep!
  173.   msg = Outgoing.Text
  174.   If Left$(msg, 1) <> "/" Then
  175.     ' they want to send a msg, send it if we're
  176.     ' in a channel
  177.     If NameList.ListCount > 0 Then
  178.       SendData "PRIVMSG " & channel & " :" & msg
  179.       AddText "> " & msg
  180.     End If
  181.   Else
  182.     Outgoing.Text = Mid$(Outgoing.Text, 2)
  183.     msg = Mid$(Outgoing.Text, InStr(Outgoing.Text, " ") + 1)
  184.     Select Case UCase$(Left$(Outgoing.Text, InStr(Outgoing.Text, " ") - 1)) ' see what kind of action to do
  185.       Case "JOIN"
  186.         SendData "JOIN " & msg: CMode = 1 ' join the channel, set the mode
  187.         channel = msg
  188.       Case "ME"
  189.         ' if we're in a channel, then do an action
  190.         If NameList.ListCount > 0 Then SendData "PRIVMSG " & channel & " :" & Chr$(1) & "ACTION " & msg & Chr$(1)
  191.         AddText "* " & Nickname & " " & msg
  192.       Case "MSG"
  193.         ' send a priv msg
  194.         SendData "PRIVMSG " & Left$(msg, InStr(msg, " ") - 1) & " :" & Mid$(msg, InStr(msg, " ") + 1)
  195.         AddText "=->" & Left$(msg, InStr(msg, " ") - 1) & "<-= " & Mid$(msg, InStr(msg, " ") + 1)
  196.     End Select
  197.   End If
  198.   ' clear the textbox
  199.   Outgoing.Text = ""
  200. End Sub
  201. Private Sub TCP1_Close()
  202.   FileConnect.Caption = "&Connect"
  203.   AddText "*** Disconnected"
  204.   ' Close the socket
  205.   TCP1.Close
  206. End Sub
  207. Private Sub TCP1_Connect()
  208.   ' Physical connect
  209.   AddText "*** Connection established."
  210.   AddText "*** Sending login information..."
  211.   ' Send the server my nickname
  212.   SendData "NICK " & Nickname
  213.   ' Send the server the user information
  214.   SendData "USER email " & TCP1.LocalIP & " " & Server & " :username"
  215. End Sub
  216. Private Sub TCP1_DataArrival(ByVal bytesTotal As Long)
  217.   Dim inData As String
  218.   Dim sline As String
  219.   Dim msg As String
  220.   Dim msg2 As String
  221.   Dim x
  222.   ' Get the incoming data into a string
  223.   TCP1.GetData inData, vbString
  224.   ' Add any unprocessed text on first
  225.   inData = OldText & inData
  226.   ' Some IRC servers are only using a Cairrage
  227.   ' Retrun, or a LineFeed, instead of both, so
  228.   ' we need to be prepared for that
  229.   x = 0
  230.   If Right$(inData, 2) = CRLF Then x = 1
  231.   If Right$(inData, 1) = Chr$(10) Then x = 1
  232.   If Right$(inData, 1) = Chr$(13) Then x = 1
  233.   If x = 1 Then
  234.     OldText = "" ' its a full send, process
  235.   Else
  236.     OldText = inData: Exit Sub ' incomplete send
  237.                                ' save and exit
  238.   End If
  239. again:
  240.   GoSub parsemsg ' get next msg fragment
  241.   If Left$(sline, 6) = "PING :" Then ' we need to pong to stay alive
  242.     AddText "PING? PONG!"
  243.     SendData "PONG " & Server
  244.     GoTo again ' get next msg
  245.   End If
  246.   If Left$(sline, 5) = "ERROR" Then ' some error
  247.     AddText "*** ERROR " & Mid$(sline, InStr(sline, "("))
  248.   End If
  249.   If Left$(sline, Len(Nickname) + 1) = ":" & Nickname Then
  250.     ' a command for the client only
  251.     sline = Mid$(sline, InStr(sline, " ") + 1)
  252.     Select Case Left$(sline, InStr(sline, " ") - 1)
  253.       Case "MODE"
  254.         AddText "*** Your mode is now " & Mid$(sline, InStr(sline, ":") + 1)
  255.     End Select
  256.   End If
  257.   If Mid$(sline, InStr(sline, " ") + 1, 7) = "PRIVMSG" Then
  258.     'someone /msged us
  259.     msg = Mid$(sline, InStr(sline, " ") + 9)
  260.     If LCase$(Left$(msg, InStr(msg, " ") - 1)) = LCase$(Nickname) Then ' private msg
  261.       ' add so its: --nick-- msg here
  262.       AddText "--" & Mid$(sline, 2, InStr(sline, "!") - 2) & "-- " & Mid$(msg, InStr(msg, ":") + 1)
  263.     End If
  264.   End If
  265.   Select Case CMode
  266.     Case 0 ' not in channel
  267.       If Mid$(sline, InStr(1, sline, " ") + 1, 3) = "001" Then
  268.         Server = Mid$(sline, 2, InStr(sline, " ") - 2)
  269.       End If
  270.       If Left$(sline, Len(Server) + 1) = ":" & Server Then
  271.         ' its a server msg, add the important part
  272.         sline = Mid$(sline, InStr(2, sline, ":") + 1)
  273.         ':washington.dc.us.undernet.org 001 Das2 :Welcome to the Internet Relay Network Das2
  274.         AddText sline
  275.       End If
  276.     Case 1 ' joining channel
  277.       If Left$(sline, Len(Server) + 1) = ":" & Server Then
  278.         msg = Mid$(sline, InStr(sline, " ") + 1)
  279.         Select Case Left$(msg, InStr(msg, " ") - 1)
  280.           Case "332" ' Topic
  281.             Topic.Text = Mid$(msg, InStr(msg, ":") + 1)
  282.           Case "353" ' Name list
  283.             msg = Mid$(msg, InStr(msg, ":") + 1)
  284.             Do Until msg = "" ' break apart names and add them seperatly
  285.               x = InStr(msg, " ")
  286.               If x <> 0 Then
  287.                 NameList.AddItem Left$(msg, x - 1)
  288.                 msg = Mid$(msg, x + 1)
  289.               Else
  290.                 NameList.AddItem msg
  291.                 msg = ""
  292.               End If
  293.             Loop
  294.           Case "366" ' End of Name List
  295.             CMode = 2 ' change mode to joined channel
  296.         End Select
  297.       Else
  298.         ' someone joined the channel, us!
  299.         If Left$(sline, InStr(sline, " ") - 1) = "JOIN" Then
  300.           AddText "*** " & Nickname & " has joined " & channel
  301.         End If
  302.       End If
  303.     Case 2 ' in a channel
  304.       If Mid$(sline, InStr(sline, " ") + 1, 7) = "PRIVMSG" Then
  305.         msg = Mid$(sline, InStr(sline, " ") + 9)
  306.         If LCase$(Left$(msg, InStr(msg, " ") - 1)) = LCase$(Nickname) Then ' private msg
  307.           AddText "--" & Mid$(sline, 2, InStr(sline, "!") - 2) & "-- " & Mid$(msg, InStr(msg, ":") + 1)
  308.         Else ' channel msg
  309.           If Left$(Mid$(msg, InStr(msg, ":") + 1), 1) = Chr$(1) Then ' action
  310.             msg2 = Mid$(msg, InStr(msg, ":") + 9)
  311.             AddText "* " & Mid$(sline, 2, InStr(sline, "!") - 2) & " " & Left$(msg2, Len(msg2) - 1)
  312.           Else ' msg
  313.             AddText "<" & Mid$(sline, 2, InStr(sline, "!") - 2) & "> " & Mid$(msg, InStr(msg, ":") + 1)
  314.           End If
  315.         End If
  316.       Else
  317.         ' command not yet supported, just display it
  318.         AddText sline
  319.       End If
  320.   End Select
  321.   ' Did I say "Good programming practice?"
  322.   ' Sometimes its easier to do this
  323.   GoTo again
  324. Exit Sub
  325. parsemsg:
  326.   ' irc may send more than one msg at a time,
  327.   ' so parse them first
  328.   If inData = "" Then Exit Sub
  329.   x = InStr(inData, CRLF) ' find the break
  330.   If x <> 0 Then
  331.     sline = Left$(inData, x - 1)
  332.     ' strip off the text
  333.     If Len(inData) > x + 2 Then
  334.       inData = Mid$(inData, x + 2)
  335.     Else
  336.       inData = ""
  337.     End If
  338.   Else
  339.     x = InStr(inData, Chr$(13)) ' find the break
  340.     If x = 0 Then
  341.       x = InStr(inData, Chr$(10)) ' find the break
  342.     End If
  343.     If x <> 0 Then
  344.       sline = Left$(inData, x - 1)
  345.     Else
  346.       sline = inData
  347.     End If
  348.     ' strip off the text
  349.     If Len(inData) > x + 1 Then
  350.       inData = Mid$(inData, x + 1)
  351.     Else
  352.       inData = ""
  353.     End If
  354.   End If
  355. Return
  356. End Sub
  357. Private Sub Topic_GotFocus()
  358.   ' We don't want the client to be able to edit
  359.   ' the topic
  360.   Outgoing.SetFocus
  361. End Sub
  362.