import std/[json, jsonutils, os, sequtils, strutils, tables] import ../cirrus type PoolItem = tuple name: string desc: string JavaPool = seq[PoolItem] var javaPool: JavaPool poolFile* = ircConfigDir & "javapool.json" # Save pool to pool config file. proc savePool(file = poolFile, pool = javaPool): void = try: writeFile(unixToNativePath(file), pretty(toJson(pool))) except IOError: debug("savePool", "error: could not write to file " & file) # Load pool config file. proc loadPool(file = poolFile): JavaPool = # If config file doesn't exist, create one if not fileExists(unixToNativePath(file)): savePool(file, @[]) try: var nodes = parseFile(unixToNativePath(file)) conf = to(nodes, JavaPool) return conf except IOError: debug("loadPool", "error: could not read JSON file " & file) except JsonParsingError: debug("loadPool", "error: invalid JSON in file " & file) # Given an item name, return its description and its index if the item is in # the pool, or empty string and -1 otherwise. proc lookupPoolItem(name: string, pool = javaPool): (string, int) = var count = 0 for i in pool: if name == i.name: return (desc: i.desc, index: count) count += 1 return (desc: "", index: -1) # Handlers # ---------------------------------------------------------------------------- # Show a list of pool items. proc hdlPoolList*(st: ServerState, msg: Message): void = var reply: string if javaPool.len() == 0: reply = "There are no items in the pool yet." else: reply = "There are " & intToStr(javaPool.len()) & " items in the pool: " & join(map(javaPool, proc(i: PoolItem): string = i.name), ", ") message(st.sock, msg.replyTo, reply) # Show an item's description if it exists in the pool, or list the pool items # if no item name was given in the message. proc hdlPoolLook*(st: ServerState, msg: Message): void = if msg.codeParams.len() > 0: var reply: string for p in msg.codeParams: if lookupPoolItem(p)[1] != -1: reply = join(["The ", p, " is ", lookupPoolItem(p)[0], "."]) else: reply = "There is no " & p & " in the pool." message(st.sock, msg.replyTo, reply) else: hdlPoolList(st, msg) # Add an item to the pool. proc hdlPoolAdd*(st: ServerState, msg: Message): void = var reply: string if msg.codeParams.len() >= 2: var desc = join(msg.codeParams[1..(msg.codeParams.len() - 1)], " ") add(javaPool, (name: msg.codeParams[0], desc: desc)) savePool() reply = "Item " & msg.codeParams[0] & " added to the pool." message(st.sock, msg.replyTo, reply) else: reply = "Usage: " & st.codePrefix & msg.code & " [name] [description]" message(st.sock, msg.replyTo, reply) # Remove an item from the pool. proc hdlPoolRemove*(st: ServerState, msg: Message): void = var reply: string if msg.codeParams.len() >= 1: for p in msg.codeParams: if lookupPoolItem(p)[1] != -1: delete(javaPool, lookupPoolItem(p)[1]) reply = "Item " & p & " removed from the pool." message(st.sock, msg.replyTo, reply) else: reply = "There is no " & p & " in the pool." message(st.sock, msg.replyTo, reply) savePool() else: reply = "Usage: " & st.codePrefix & msg.code & " [name]" message(st.sock, msg.replyTo, reply) # Init # ---------------------------------------------------------------------------- when defined(nimHasUsed): {.used.} javaPool = loadPool() handlers["hdlPoolList"] = hdlPoolList handlers["hdlPoolLook"] = hdlPoolLook handlers["hdlPoolAdd"] = hdlPoolAdd handlers["hdlPoolRemove"] = hdlPoolRemove