tillegaltyperecursion.nim 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. discard """
  2. cmd: "nim $target --threads:on $options $file"
  3. errormsg: "illegal recursion in type 'TIRC'"
  4. line: 16
  5. """
  6. import events
  7. import sockets
  8. import strutils
  9. import os
  10. type
  11. TMessageReceivedEventArgs = object of EventArgs
  12. Nick*: string
  13. Message*: string
  14. TIRC = object
  15. EventEmitter: EventEmitter
  16. MessageReceivedHandler*: EventHandler
  17. Socket: Socket
  18. Thread: Thread[TIRC]
  19. proc initIRC*(): TIRC =
  20. result.Socket = socket()
  21. result.EventEmitter = initEventEmitter()
  22. result.MessageReceivedHandler = initEventHandler("MessageReceived")
  23. proc IsConnected*(irc: var TIRC): bool =
  24. return running(irc.Thread)
  25. proc sendRaw*(irc: var TIRC, message: string) =
  26. irc.Socket.send(message & "\r\L")
  27. proc handleData(irc: TIRC) {.thread.} =
  28. var connected = False
  29. while connected:
  30. var tup = @[irc.Socket]
  31. var o = select(tup, 200)
  32. echo($o)
  33. echo($len(tup))
  34. if len(tup) == 1:
  35. #Connected
  36. connected = True
  37. #Parse data here
  38. else:
  39. #Disconnected
  40. connected = False
  41. return
  42. proc Connect*(irc: var TIRC, nick: string, host: string, port: int = 6667) =
  43. connect(irc.Socket, host, TPort(port), TDomain.AF_INET)
  44. send(irc.Socket,"USER " & nick & " " & nick & " " & nick & " " & nick & "\r\L")
  45. send(irc.Socket,"NICK " & nick & "\r\L")
  46. var thread: Thread[TIRC]
  47. createThread(thread, handleData, irc)
  48. irc.Thread = thread
  49. when isMainModule:
  50. var irc = initIRC()
  51. irc.Connect("AmryBot[Nim]","irc.freenode.net",6667)
  52. irc.sendRaw("JOIN #nim")
  53. os.Sleep(4000)