summaryrefslogtreecommitdiff
path: root/Main.hs
blob: 60d522c49ceeb4b65509d935ea97fcaa70bfc414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module Main where

import System

import Cmd
import Conf
import Env
import IRC
import State
import Tools

main :: IO ()	
main = do
   let conf = makeConf
   databaseFile <- get "databaseFile" conf
   let state = stateLoad databaseFile
   state' <- state -- Extract State from the IO Monad 
   ircConnect (Env state' conf dispatch)

-- Shortcut
r :: IO ()
r = main

dispatch :: Dispatch
dispatch cmd sendMessage env@(Env state conf _) = 
   getLambda ("!" ++ cmd) state 
      where
         printHelp = sendMessage . concat . showL 
         getLambda x = let (Cmd _ _ c) = getCmd x in c
         getDescr x = let (Cmd _ b _) = getCmd x in b
         getCmd x = 
               let command = [ (Cmd a b c) | (Cmd a b c) <- commands, a == x ]
                in if length command == 0
                     then getCmd "!i" 
                     else head command

         commands = [      
            Cmd "!h" "Prints help" 
               (\_ -> printHelp commands ),

            Cmd "!i" "Prints infos" 
               (\_ -> sendMessage $ (envGet "name" env) 
                     ++ " " ++ (envGet "version" env) 
                     ++ " (try !h)" ),

            Cmd "!p" "Prints current state" 
               (\s -> sendMessage $ show s ),

            Cmd "!s" "Stores current state to file" 
               (\s -> do 
                     sendMessage "Storing current state"
                     stateSave (envGet "databaseFile" env) s ),

            Cmd "!q" "quits"
               (\s -> do
                     sendMessage "Good bye"
                     stateSave (envGet "databaseFile" env) s
                     exitWith ExitSuccess ) 
            ]