PageRenderTime 16ms CodeModel.GetById 12ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/sprink.ooc

http://github.com/tsion/sprink
Unknown | 99 lines | 89 code | 10 blank | 0 comment | 0 complexity | 6800a2bff6df5e4a6863675a8a359da0 MD5 | raw file
 1use spry, curl, xml
 2import spry/[IRC, Message, Prefix]
 3import curl/[Curl, Highlevel]
 4import xml/[Xml, XPath]
 5import structs/[List, ArrayList], text/StringTokenizer
 6
 7main: func (args: ArrayList<String>) {
 8    if(args size != 4) {
 9        "usage: sprink <nick> <server> <port>" println()
10        return -1
11    }
12
13    bot := IRC new(args[1], "sprink", "a spry little IRC bot", args[2], args[3] toInt())
14
15    bot on("send", |irc, msg|
16        ">> " print()
17        msg toString() println()
18    )
19
20    bot on("all", |irc, msg|
21        msg toString() println()
22    )
23
24    bot on("001", |irc, msg|
25        irc join("#programming,#offtopic,#bots,#minecraft")
26    )
27
28    bot on("PRIVMSG", |irc, msg|
29        target := msg params[0]
30        if(target startsWith?('#'))
31            irc runCallback("channel message", msg)
32        else
33            irc runCallback("private message", msg)
34    )
35
36    bot on("channel message", |irc, msg|
37        channel := msg params[0]
38        if(msg params[1] startsWith?(irc nickname)) {
39            words := msg params[1] split(' ', 3)
40            first := words[1]
41            match first {
42                case "!channels" =>
43                    buf := Buffer new()
44                    for(c in irc channels)
45                        buf append(c) .append(' ')
46                    irc privmsg(channel, msg prefix nick + ": " + buf toString())
47                case "!ping" =>
48                    irc privmsg(channel, msg prefix nick + ": pong")
49                case "!join" =>
50                    chan := words[2]
51                    if(irc channels contains?(chan)) {
52                        irc privmsg(channel, msg prefix nick + ": I'm already in " + chan + ".")
53                    } else {
54                        irc join(chan)
55                        irc privmsg(channel, msg prefix nick + ": Consider it done.")
56                    }
57                case "!part" =>
58                    chan := words[2]
59                    if(irc channels contains?(chan)) {
60                        irc part(chan)
61                        irc privmsg(channel, msg prefix nick + ": Consider it done.")
62                    } else {
63                        irc privmsg(channel, msg prefix nick + ": I'm not in " + chan + ".")
64                    }
65                case =>
66                    code := msg params[1] split(' ', 2)[1]
67                    result := frinkEval(code)
68                    if(result) {
69                        if(result size > 450)
70                            result = result[0..444] + " (...)"
71                        irc privmsg(channel, msg prefix nick + ": " + result)
72                    } else {
73                        irc privmsg(channel, msg prefix nick + ": No results found on " + frinkUrl(code))
74                    }
75            }
76        }
77    )
78
79    bot run()
80}
81
82frinkUrl: func (code: String) -> String {
83    "http://futureboy.us/fsp/frink.fsp?fromVal=" + Curl escape(code)
84}
85
86frinkEval: func (code: String) -> String {
87    code = "B := byte; b := bit;" + code
88    url := frinkUrl(code)
89    request := HTTPRequest new(url) .perform()
90    doc := HtmlDoc new(request getString(), "frink.fsp")
91    resultList := doc evalXPath("//a[@name=\"results\"]")
92    if(resultList size > 0 && resultList[0]@ children && resultList[0]@ children@ content) {
93        resultNode := resultList[0]
94        result := resultNode@ children@ content toString()
95        result
96    } else {
97        null
98    }
99}