From bec1d30c93e1e7124d06eca674c8e717a05493e8 Mon Sep 17 00:00:00 2001 From: Araq Date: Tue, 14 Sep 2010 03:11:05 -0700 Subject: [PATCH] Migrated from tutorial-sockets v2 --- Tutorial:-Sockets.textile | 70 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/Tutorial:-Sockets.textile b/Tutorial:-Sockets.textile index e69de29..79833e4 100644 --- a/Tutorial:-Sockets.textile +++ b/Tutorial:-Sockets.textile @@ -0,0 +1,70 @@ +The first thing you need to do is import the sockets module
+import sockets
+Then you need to create and initialize a TSocket object
+var s: TSocket = socket()
+After that you can use s.connect("addr", TPort(80)) to connect to a server
+Once you connect to a server, you can send and receive messages to/from the server by using s.send("Message") to send, and s.recv() to receive. +== Examples == +=== IRC Bot === +
import sockets, strutils
+
+when not defined(strutils.toStringSep):
+    proc toStringSep(x: int64): string = 
+        var s = $x
+        result = ""
+        var j = 0
+        for i in countdown(len(s)-1, 0): 
+            inc(j)
+            if j == 4: 
+                result = ',' & result 
+                j = 0
+            result = s[i] & result
+
+proc toMiB(size: int64): int64 =
+    return size div 1024 div 1024
+
+proc formatSize(size: int64): string =
+    var gigabytes: int64 = size div 1024 div 1024 div 1024
+    var megabytes: int64 = size.toMiB()
+    var kilobytes: int64 = size div 1024
+    if gigabytes != 0:
+        return toStringSep(gigabytes) & "GB"
+    elif megabytes != 0:
+        return toStringSep(megabytes) & "MB"
+    elif kilobytes != 0:
+        return toStringSep(kilobytes) & "KB"
+    else:
+        return toStringSep(size) & "B"
+
+var s = socket()
+
+s.connect("irc.freenode.net", TPort(6667))
+
+s.send("NICK NimrodBot\c\L")
+s.send("USER NimrodBot * 0 :IRC Bot programmed in Nimrod!\c\L")
+
+while True:
+  var data: string = ""
+  discard s.recvLine(data)
+  echo(data)
+  if data.split()[0] == "PING":
+    s.send("PONG " & data.split()[1] & "\c\L")
+  if data.split()[1] == "001":
+    s.send("JOIN #nimrod\c\L")
+  if data.split()[1] == "PRIVMSG":
+    if data.split()[3] == ":!about":
+      var msg: string = "NimrodBot 0.1 compiled on " & CompileDate & " " & CompileTime
+      msg = msg & " running on " & hostOS
+      s.send("PRIVMSG " & data.split()[2] & " :" & msg & "\c\L")
+
+    elif data.split()[3] == ":!mem":
+      var msg: string = "Occupied memory: " & formatSize(int64(getOccupiedMem()))
+      s.send("PRIVMSG " & data.split()[2] & " :" & msg & "\c\L")
+
+    elif data.split()[3] == ":!freemem":
+      var msg: string = "Free memory: " & formatSize(int64(getFreeMem()))
+      s.send("PRIVMSG " & data.split()[2] & " :" & msg & "\c\L")
+
+    elif data.split()[3] == ":!totalmem":
+      var msg: string = "Total memory: " & formatSize(int64(getTotalMem()))
+      s.send("PRIVMSG " & data.split()[2] & " :" & msg & "\c\L")