Nim wrapper over the Toxcore library
import toxcore
from std/os import sleep
const
bootstrapHost = "tox.neuland.technology"
bootstrapKey = "15E9C309CFCB79FDDF0EBA057DABB49FE15F3803B1BFF06536AE2E5BA5E4690E".toPublicKey
proc setupCallbacks(bot: Tox) =
var echoCount = 0
# bind a value for callback closure magic
bot.onFriendRequest do (pk: PublicKey; msg: string):
discard bot.addFriendNoRequest(pk)
bot.onFriendMessage do (f: Friend; msg: string; kind: TOX_MESSAGE_TYPE):
discard bot.send(f, msg, kind)
inc echoCount
bot.statusMessage = $echoCount & " echos served "
proc newEchoBot(name: string): Tox =
result = newTox()
result.name = name
result.setupCallbacks()
result.bootstrap(bootstrapHost, bootstrapKey)
echo result.name, " echos messages to ", result.address
echo result.name, " joins DHT with port ", result.udpPort, " address ", result.dhtId
let
a = newEchoBot "alice"
b = newEchoBot "bob"
while true:
iterate a
iterate b
sleep min(a.iterationInterval, b.iterationInterval)