~ehmry/nim-toxcore

Do not write to stdout
Make send(…) discardable
Export some max-length constants

refs

master
browse  log 
nim-toxcore-0.5.0
browse  .tar.gz 

clone

read-only
https://git.sr.ht/~ehmry/nim-toxcore
read/write
git@git.sr.ht:~ehmry/nim-toxcore

You can also use your local clone with git send-email.

Nim wrapper over the Toxcore library

import toxcore

from std/os import sleep

const
  bootstrapHost = "85.143.221.42"
  bootstrapKey = "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43".toPublicKey

type Bot = ref object
  tox: Tox

proc setupCallbacks(bot: Bot) =
  var echoCount = 0
    # bind a value for callback closure magic

  bot.tox.onFriendRequest do (pk: PublicKey; msg: string):
    discard bot.tox.addFriendNoRequest(pk)

  bot.tox.onFriendMessage do (f: Friend; msg: string; kind: MessageType):
    discard bot.tox.send(f, msg, kind)
    inc echoCount
    bot.tox.statusMessage = $echoCount & " echos served "

proc newEchoBot(name: string): Bot =
  result = Bot(tox: initTox())
  result.tox.name = name
  result.setupCallbacks()
  result.tox.bootstrap(bootstrapHost, bootstrapKey)
  echo result.tox.name, " echos messages to ", result.tox.address

let
  a = newEchoBot "alice"
  b = newEchoBot "bob"
while true:
  iterate a.tox
  iterate b.tox
  sleep min(a.tox.iterationInterval, b.tox.iterationInterval)