From 17aad66b8f6b5ef2af784443d97e2775e5743b9d Mon Sep 17 00:00:00 2001 From: darkf Date: Sun, 27 Oct 2013 01:40:09 -0700 Subject: [PATCH] add factoid list and definition --- irc.lamb | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/irc.lamb b/irc.lamb index 7768982..049331c 100644 --- a/irc.lamb +++ b/irc.lamb @@ -1,5 +1,27 @@ -- First we'll define some helper functions +map(f, []) -> []. +map(f, x::xs) -> f(x) :: map(f, xs). + +fst((x, _)) -> x. + +-- association list + +-- insert a pair into a map +map_insert(assoc, key, value) -> (key, value) :: assoc. + +-- lookup by key +map_lookup([], _) -> ("nothing"). +map_lookup((k,v)::xs, key) -> + if k == key then ("just", v) + else map_lookup(xs, key). + +-- remove a key from a map +map_remove([], key) -> []. +map_remove((k,v)::xs, key) -> + if k == key then xs + else (k,v) :: map_remove(xs, key). + -- irc stuff -- Splits a string by spaces, or until it encounters a :, whereby the following is considered one element. @@ -25,8 +47,37 @@ ircnick'("!"::xs, acc) -> acc. ircnick'(x::xs, acc) -> ircnick'(xs, acc + x). ircnick(str) -> ircnick'(str, ""). +-- state stuff +initialState = ([]). + +-- state getters +getFactoids(state) -> do + (factoids) = state; + factoids +end. + +-- state setters +setFactoids(state, factoids) -> do + (factoids) +end. + +-- event handling + say(chan, msg) -> fputstr(sock, "PRIVMSG " + chan + " :" + msg + "\r\n"). +handleMessage(s, nick, chan, "$factoids") -> do + factoids = map(fst, getFactoids(s)); + say(chan, nick + ": " + repr(factoids)); + s +end. + +handleMessage(s, nick, chan, "$defact "::line) -> do + (k,v) = takeUntilSpace(line); + factoids = getFactoids(s); + say(chan, nick + ": defined " + k); + setFactoids(s, map_insert(factoids, k, v)) +end. + handleMessage(s, nick, chan, "$ping") -> do say(chan, nick + ": pong"); s end. handleMessage(s, "darkf", chan, "$quit") -> do say(chan, "bye!"); @@ -84,13 +135,11 @@ end. handleLine(s, line) -> do -- non-sourced message command::args = splitirc(line); - handleCommand(s, source, command, args) + handleCommand(s, "", command, args) end. -- now for our actual program! -initialState = []. - -- build our socket and connect to the server sock = sockopen("127.0.0.1", 6667). @@ -112,6 +161,6 @@ mainloop(state) -> false. putstrln("beginning mainloop"). -loop(mainloop, []). +loop(mainloop, initialState). fclose(sock). putstrln("done"). \ No newline at end of file