11import irc, times, terminal, random, strutils, strtabs, os
2+ import parsetoml
23from rawsockets import Port
34
5+ var cfg: File
6+ if not cfg.open (" settings.toml" ):
7+ echo " ERROR: settings.toml file not found!"
8+ discard readChar (stdin)
9+ quit 1
10+
11+ type
12+ ChatColors = object
13+ chat, error, join, part, mods: ForegroundColor
14+ highlight, subs: BackgroundColor
15+
416const
517 CHAT_URL = " irc.chat.twitch.tv"
618 CHAT_PORT = Port (6667 )
719
20+ let
21+ config = parsetoml.parseFile (" settings.toml" )
22+
823var
924 shouldLog = false
1025 linecounter = 0
26+
1127# check if a string contains some text that can be found in a set
1228# there's probably a cleaner/smarter way to do this, will update at some point
1329proc hasTextInSet (str: string , s: seq ): bool =
@@ -21,6 +37,30 @@ proc hasTextInSet(str: string, s: seq): bool =
2137 continue
2238 return bHighlight
2339
40+ proc getFgColor (str: string ): ForegroundColor =
41+ case str:
42+ of " white" : return fgWhite
43+ of " black" : return fgBlack
44+ of " red" : return fgRed
45+ of " green" : return fgGreen
46+ of " yellow" : return fgYellow
47+ of " blue" : return fgBlue
48+ of " magenta" : return fgMagenta
49+ of " cyan" : return fgCyan
50+ else : discard
51+
52+ proc getBgColor (str: string ): BackgroundColor =
53+ case str:
54+ of " white" : return bgWhite
55+ of " black" : return bgBlack
56+ of " red" : return bgRed
57+ of " green" : return bgGreen
58+ of " yellow" : return bgYellow
59+ of " blue" : return bgBlue
60+ of " magenta" : return bgMagenta
61+ of " cyan" : return bgCyan
62+ else : discard
63+
2464# add a hash to the channel name if there isn't one there already
2565proc addHash (s: string ): string =
2666 if s.startsWith ('#' ):
@@ -51,46 +91,56 @@ proc logToFile(f: File, s: string) =
5191 if shouldLog:
5292 f.writeLine (s)
5393 linecounter += 1
54- if linecounter >= 10 :
94+ if linecounter >= config. getInt ( " log_save_lines " ) :
5595 f.flushFile ()
5696 linecounter = 0
5797
58- var chans = newSeq [string ](0 )
59- var highlights = newSeq [string ](0 )
60- if paramCount () == 0 :
98+ let chatColor: ref ChatColors = new (ChatColors )
99+
100+ chatColor.chat = getFgColor (config.getString (" colors.chat" ))
101+ chatColor.error = getFgColor (config.getString (" colors.error" ))
102+ chatColor.join = getFgColor (config.getString (" colors.join" ))
103+ chatColor.part = getFgColor (config.getString (" colors.part" ))
104+ chatColor.mods = getFgColor (config.getString (" colors.modstatus" ))
105+
106+ chatColor.highlight = getBgColor (config.getString (" colors.highlight" ))
107+ chatColor.subs = getBgColor (config.getString (" colors.subnotice" ))
108+
109+ var
110+ chans = newSeq [string ](0 )
111+ highlights = newSeq [string ](0 )
112+
113+ if config.getBool (" general.use_config_channels" ) == false :
61114 echo (" Type the usernames of the channels to join, seperated by spaces: " )
62115 var channelin: string = readLine (stdin)
63116 for chan in splitWhitespace (channelin):
64117 chans.add (addHash (chan.toLowerAscii ()))
118+ else :
119+ for chan in config.getStringArray (" general.channels" ):
120+ chans.add (addHash (chan.toLowerAscii ()))
65121
66- elif paramCount () >= 1 :
67- for param in commandLineParams ():
68- chans.add (addHash (param.toLowerAscii ()))
69-
70- echo (" Type the words you wish to highlight, seperated by spaces (or just press enter for none): " )
71- var highlightin: string = readLine (stdin)
72- if not highlightin.isNilOrWhitespace ():
73- for hl in splitWhitespace (highlightin):
74- highlights.add (hl)
122+ for highlight in config.getStringArray (" general.highlights" ):
123+ if not highlight.isNilOrWhitespace ():
124+ highlights.add (highlight)
75125
76- echo (" Do you want to log the chat to a file? (y = yes, just press enter for no)" )
77- var loggingin: string = readLine (stdin)
78- if loggingin.contains (" y" ) or loggingin.contains (" yes" ):
126+ if config.getBool (" general.logging" ) == true :
79127 shouldLog = true
128+ else :
129+ shouldLog = false
80130
81131var username = randomTwitchUser ()
82132var t = irc.newIrc (CHAT_URL , CHAT_PORT , username, username,
83133 joinChans = chans)
84- var curtime: string
85134
135+ var curtime: string
86136t.connect ()
87137# this gives us things such as userlist, joins, parts and mod status:
88138t.send (" CAP REQ :twitch.tv/membership twitch.tv/commands twitch.tv/tags" , false )
89139
90140var chatline = " "
91- var filename = getDateStr ()& " _twitchchat_log.txt "
92- var f: File
141+ var f: File
93142if shouldLog:
143+ var filename = getDateStr ()& " _twitchchat_log.txt "
94144 if f.open (filename):
95145 discard f.open (filename, fmAppend)
96146 else :
@@ -105,18 +155,18 @@ while true:
105155 case event.typ
106156 of EvConnected :
107157 chatline = " $1 - [INFO] Connected to server" % [curtime]
108- styledWriteLine (stdout, fgWhite , chatline)
158+ styledWriteLine (stdout, chatColor.chat , chatline)
109159 f.logToFile (chatline)
110160
111161 of EvDisconnected :
112162 chatline = " $1 - [ERR] Disconnected, reconnecting..." % [curtime]
113- styledWriteLine (stdout, fgRed , chatline)
163+ styledWriteLine (stdout, chatColor.error , chatline)
114164 f.logToFile (chatline)
115165 t.reconnect ()
116166
117167 of EvTimeout :
118168 chatline = " $1 - [ERR] Timeout, reconnecting..." % [curtime]
119- styledWriteLine (stdout, fgRed , chatline)
169+ styledWriteLine (stdout, chatColor.error , chatline)
120170 f.logToFile (chatline)
121171 t.reconnect ()
122172
@@ -127,64 +177,64 @@ while true:
127177 username = username.addTwitchBadges (event).strip ()
128178 if event.user == " twitchnotify" :
129179 chatline = " $1 $2 - [SUB] $3" % [curtime, event.origin, event.params[1 ]]
130- styledWriteLine (stdout, fgWhite, bgMagenta , styleBright, chatline)
180+ styledWriteLine (stdout, chatColor.chat, chatColor.subs , styleBright, chatline)
131181 f.logToFile (chatline)
132182 elif event.params[1 ].hasTextInSet (highlights):
133183 chatline = " $1 $2 - [MSG] $3: $4" % [curtime, event.origin, username, event.params[1 ]]
134- styledWriteLine (stdout, fgWhite, bgRed , styleBright, chatline)
184+ styledWriteLine (stdout, chatColor.chat, chatColor.highlight , styleBright, chatline)
135185 f.logToFile (chatline)
136186 else :
137187 chatline = " $1 $2 - [MSG] $3: $4" % [curtime, event.origin, username, event.params[1 ]]
138- styledWriteLine (stdout, fgWhite , chatline)
188+ styledWriteLine (stdout, chatColor.chat , chatline)
139189 f.logToFile (chatline)
140190
141191 of MJoin :
142192 chatline = " $1 $2 - [JOIN] $3" % [curtime, event.origin, event.nick]
143- styledWriteLine (stdout, fgGreen , chatline)
193+ styledWriteLine (stdout, chatColor.join , chatline)
144194 f.logToFile (chatline)
145195
146196 of MPart :
147197 chatline = " $1 $2 - [PART] $3" % [curtime, event.origin, event.nick]
148- styledWriteLine (stdout, fgRed , chatline)
198+ styledWriteLine (stdout, chatColor.part , chatline)
149199 f.logToFile (chatline)
150200
151201 of MMode :
152202 if event.params[1 ] == " +o" :
153203 chatline = " $1 $2 - [+MOD] $3" % [curtime, event.origin, event.params[2 ]]
154- styledWriteLine (stdout, fgCyan , chatline)
204+ styledWriteLine (stdout, chatColor.mods , chatline)
155205 f.logToFile (chatline)
156206 elif event.params[1 ] == " -o" :
157207 chatline = " $1 $2 - [-MOD] $3" % [curtime, event.origin, event.params[2 ]]
158- styledWriteLine (stdout, fgCyan , chatline)
208+ styledWriteLine (stdout, chatColor.mods , chatline)
159209 f.logToFile (chatline)
160210
161211 of MUnknown :
162212 if event.raw.contains (" CLEARCHAT " ):
163213 chatline = " $1 $2 - [INFO] $3 was timed out for $4 seconds" % [curtime, event.origin, event.params[1 ], event.tags[" ban-duration" ]]
164- styledWriteLine (stdout, fgWhite , chatline)
214+ styledWriteLine (stdout, chatColor.chat , chatline)
165215 f.logToFile (chatline)
166216 elif event.raw.contains (" USERNOTICE " ):
167217 var submsgs = event.tags[" system-msg" ]
168218 var submsg = " "
169219 if submsgs.contains (" ." ) and event.params.len () <= 1 :
170220 submsg = submsgs.split (" ." )[1 ].replace (r " \s " , " " ).strip ()
171221 chatline = " $1 $2 - [SUB] $3" % [curtime, event.origin, submsg]
172- styledWriteLine (stdout, fgWhite, bgMagenta , styleBright, chatline)
222+ styledWriteLine (stdout, chatColor.chat, chatColor.subs , styleBright, chatline)
173223 f.logToFile (chatline)
174224 elif submsgs.contains (" ." ) and event.params.len () >= 2 :
175225 submsg = submsgs.split (" ." )[1 ].replace (r " \s " , " " ).strip ()
176226 chatline = " $1 $2 - [SUB] $3 - $4" % [curtime, event.origin, submsg, event.params[1 ]]
177- styledWriteLine (stdout, fgWhite, bgMagenta , styleBright, chatline)
227+ styledWriteLine (stdout, chatColor.chat, chatColor.subs , styleBright, chatline)
178228 f.logToFile (chatline)
179229 elif event.params.len () <= 1 :
180230 submsg = submsgs.replace (r " \s " , " " ).strip ()
181231 chatline = " $1 $2 - [SUB] $3" % [curtime, event.origin, submsg]
182- styledWriteLine (stdout, fgWhite, bgMagenta , styleBright, chatline)
232+ styledWriteLine (stdout, chatColor.chat, chatColor.subs , styleBright, chatline)
183233 f.logToFile (chatline)
184234 else :
185235 submsg = submsgs.replace (r " \s " , " " ).strip ()
186236 chatline = " $1 $2 - [SUB] $3 - $4" % [curtime, event.origin, submsg, event.params[1 ]]
187- styledWriteLine (stdout, fgWhite, bgMagenta , styleBright, chatline)
237+ styledWriteLine (stdout, chatColor.chat, chatColor.subs , styleBright, chatline)
188238 f.logToFile (chatline)
189239 else :
190240 discard
0 commit comments