blob: 24d7506b79ec070b8b62789a22ef2c919e7e01e7 (
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
-- IRC art (spam)bot
import Data.Bits
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as S.Char8
import qualified Data.ByteString.Internal as S.Internal
import Data.List
import Data.Word
import System.Directory
import System.Environment
import System.Exit
import System.IO
import Text.Printf
--import Debug.Trace
type BitMap = [Word8]
type MircString = String
type PpmBmp = [Word8]
type IrcChannel = String
data Image = Image {
imgBmp :: BitMap,
imgPitch :: Int,
imgHeight :: Int
}
ctrlC = [toEnum 03] :: String
ctrlO = [toEnum 15] :: String
ctrlV = [toEnum 22] :: String
-- https://www.mirc.com/colors.html
w24ToMirc :: BitMap -> MircString
w24ToMirc word
-- White
| (word!!0 > 210) && (word!!1 > 210) && (word!!2 > 210)
= "0"
-- Black
| (word!!0 == 0) && (word!!1 == 0) && (word!!2 == 0)
= "1"
-- Light red
| (word!!0 > 127) && (word!!1 == 0) && (word!!2 == 0)
= "4"
-- Light green
| (word!!0 == 0) && (word!!1 > 147) && (word!!2 == 0)
= "9"
-- Light blue
| (word!!0 == 0) && (word!!1 == 0) && (word!!2 > 127)
= "12"
-- Red (aka brown)
| (word!!0 <= 127) && (word!!1 == 0) && (word!!2 == 0)
= "5"
-- Green
| (word!!0 == 0) && (word!!1 <= 147) && (word!!2 == 0)
= "3"
-- Blue
| (word!!0 == 0) && (word!!1 == 0) && (word!!2 <= 127)
= "2"
-- Yellow
| (word!!0 > 252) && (word!!1 > 127) && (word!!2 == 0)
= "8"
-- Orange
| (word!!0 <= 252) && (word!!1 <= 127) && (word!!2 == 0)
= "7"
-- Pink
| (word!!0 > 156) && (word!!1 == 0) && (word!!2 > 156)
= "13"
-- Purple
| (word!!0 <= 156) && (word!!1 == 0) && (word!!2 <= 156)
= "6"
-- Light cyan
| (word!!0 == 0) && (word!!1 > 147) && (word!!2 > 147)
= "11"
-- Cyan
| (word!!0 == 0) && (word!!1 <= 147) && (word!!2 <= 147)
= "10"
-- Light grey
| (word!!0 > 127) && (word!!1 > 127) && (word!!2 > 127)
= "15"
-- Grey
| otherwise = "14"
lineToMirc :: BitMap -> MircString
lineToMirc [] = []
lineToMirc line = ctrlC ++ w24ToMirc (take 3 line) ++ ctrlV ++ " " ++ ctrlO
++ lineToMirc (drop 3 line)
bmpToMirc :: Int -> Int -> BitMap -> MircString
bmpToMirc _ 0 _ = []
bmpToMirc pitch height bmp =lineToMirc (take pitch bmp) ++ "\n"
++ bmpToMirc pitch (height-1) (drop pitch bmp)
w8Split :: Word8 -> [Word8] -> [[Word8]]
w8Split c = map S.unpack . S.split c . S.pack
w8ToString :: [Word8] -> String
w8ToString = S.Char8.unpack . S.pack
ppmToImage :: PpmBmp -> Image
ppmToImage ppm = Image
(intercalate [S.Internal.c2w '\n'] $ drop 3
(w8Split (S.Internal.c2w '\n') ppm))
((read (w8ToString
((w8Split (S.Internal.c2w ' ') ((w8Split
(S.Internal.c2w '\n') ppm) !! 1)) !! 0)))*3)
(read ( w8ToString
((w8Split (S.Internal.c2w ' ') ((w8Split
(S.Internal.c2w '\n') ppm) !! 1)) !! 1)))
ircConnect :: String -> MircString
ircConnect credentials = printf "NICK miaowe\r\n\
\USER nya 0 * :mew mew\r\n\
\PRIVMSG NickServ :IDENTIFY %s\r\n" credentials
waitForMessage :: (String -> Bool) -> IO String
waitForMessage p = getLine >>= \l -> if p l
then return l
else
(if (words l)!!0 == "PING"
then
putStr
("PONG " ++ ((words l)!!1) ++ "\r\n")
>> hFlush stdout
else return ())
>> waitForMessage p
waitForCloak :: IO String
waitForCloak = waitForMessage (\l -> (words l)!!1 == "396")
waitForCommand :: IO String
waitForCommand = waitForMessage (\l -> if ((length (words l)) >= 4)
then ((words l)!!3 == ":miaowe:")
else False)
joinChannel :: String -> IO ()
joinChannel c = putStr (printf "JOIN %s\r\n" c)
sendToChannel :: IrcChannel -> MircString -> IO ()
sendToChannel c s = (putStr $ concat $ map (\l -> printf "PRIVMSG %s :%s\r\n" c l)
(lines s)) >> hFlush stdout
sendFile :: IrcChannel -> FilePath -> IO ()
sendFile channel path = S.readFile path
>>= sendToChannel channel .
(\img ->bmpToMirc
(imgPitch img)
(imgHeight img)
(imgBmp img)) .
ppmToImage .
S.unpack .
S.Char8.pack .
intercalate "\n" .
filter (\l->not (head l == '#')) .
lines .
S.Char8.unpack
trySendFile :: IrcChannel -> FilePath -> IO ()
trySendFile channel path = (doesFileExist (path)
>>= (\b ->
if b
then
sendFile channel (path)
else
sendToChannel channel "No such file."
>> hFlush stdout))
processCommand :: IrcChannel -> MircString -> IO ()
processCommand channel command | (words command)!!4 == "Quit" = exitSuccess
| otherwise = (if isInfixOf "/" ((words command)!!4)
then
sendToChannel channel
"Only PWD please."
else
trySendFile channel
((words command)!!4))
mainLoop :: IrcChannel -> IO ()
mainLoop channel = waitForCommand
>>= \line ->
processCommand channel line
>> mainLoop channel
main :: IO ()
main =
openFile "conf" ReadMode
>>= hGetLine
>>= putStr . ircConnect
>> hFlush stdout
>> waitForCloak
>> hFlush stdout
>> getArgs
>>= \args ->
joinChannel (args!!0)
>> hFlush stdout
>> mainLoop (args!!0)
|