Advertisement
Ham62

Untitled

Apr 6th, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type ServerStruct
  2.   hSock as socket
  3.   Address as string     'Address of server
  4.   Port as integer       'Port of server
  5.   Nick as String        'Nickname for bot
  6.   ChanList as String    'List of channels to join
  7.   NSPass as String      'NickServ Password
  8.   OperLogin as String   'Oper login info
  9.   vhostLogin as String  'Login info for your bot's vhost
  10.   BotOPs as String      'Operators of the bot
  11.   WantQuit as uByte     'Do we want the server to disconnect us?
  12.   sInBuff             as string   'the actual variable holding the buffer
  13.   iInBuffPos          as integer  'how much is on the buffer?
  14.   union
  15.     iInBuffFlags        as ubyte  'all below... combined
  16.     type
  17.       bInBuffDiscard :1 as ubyte  'we are discarding all until EOL is found?
  18.       bInBuffDoCheck :1 as ubyte  'there's leftover data to check?
  19.     end type
  20.   end union
  21. end type
  22.  
  23. Function GetData (pServ as ServerStruct, Byref sOut as String) as Integer
  24.   with pServ
  25.     var iBufSz = len(.sInBuff)
  26.     if iBufSz = 0 then 'Init (clear state)
  27.       iBufSz = 8192: .sInBuff = space(iBufSz)
  28.       .iInBuffPos = 0: .iInBuffFlags = 0
  29.     end if        
  30.     for iTwice as integer = 0 to 1
  31.       if .bInBuffDiscard then .iInBuffPos=0 'keep discarding
  32.       'get more data if none in the buffer
  33.       if .iInBuffPos<=0 or .bInBuffDoCheck=0 then
  34.         if hSelect(.hSock) = 0 then sOut="": return 0
  35.         var pBuff = strptr(.sInBuff)+.iInBuffPos 'start from here
  36.         var iResu = hReceive( .hSock , pBuff , iBufSz-.iInBuffPos )
  37.         if iResu <= 0 then sOut="":return iResu-1 'error/done
  38.         .iInBuffPos += iResu
  39.       end if
  40.       'check for \n in the buffer
  41.       var iPosi = instr(.sInBuff,chr(10)),iAfter=iPosi
  42.       if iPosi>1 andalso .sInBuff[iPosi-2] = 13 then iPosi -= 1 '\r\n or \n?
  43.       if iPosi then 'found end of line!
  44.         if .bInBuffDiscard then 'discarding the entire line
  45.           sOut="":.bInBuffDiscard=0
  46.         else 'returning the line...
  47.           sOut=left$( .sInBuff , iPosi-1 )
  48.         end if
  49.         .iInBuffPos -= iAfter 'how many are left on the buffer?
  50.         if .iInBuffPos > 0 then 'There's more on buffer
  51.           'copy to begin of the buffer (using string functions would mess buffer size)
  52.           memmove(strptr(.sInBuff),strptr(.sInBuff)+iAfter,.iInBuffPos)
  53.           .bInBuffDoCheck = 1 'more to check
  54.         else
  55.           .bInBuffDoCheck = 0 'nothing to check later
  56.         end if
  57.         return len(sOut)
  58.       else 'not found end of line
  59.         'filled all of the buffer? yes? then discard until EOL
  60.         if .iInBuffPos = iBufSz then .bInBuffDiscard = 1
  61.         'if it wasnt checking for extra then just
  62.         if .bInBuffDoCheck=0 then sOut="": return 0
  63.         'otherwise try again! (now will hReceive)
  64.         .bInBuffDoCheck = 0
  65.       end if      
  66.     next iTwice
  67.     'should never reach here!
  68.   end with
  69. end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement