-
Notifications
You must be signed in to change notification settings - Fork 0
/
JenkemBot.scala
338 lines (306 loc) · 12.9 KB
/
JenkemBot.scala
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* #%L
* JenkemBot.scala - Jenkem - Tok - 2012
* %%
* Copyright (C) 2012 - 2013 Lukas Steiger <[email protected]>
* %%
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar.
* See http://www.wtfpl.net/ for more details.
* #L%
*/
package jenkem.bot
import java.io.IOException
import java.lang.InterruptedException
import org.jibble.pircbot.IrcException
import org.jibble.pircbot.NickAlreadyInUseException
import org.jibble.pircbot.PircBot
import jenkem.engine.Method
import jenkem.engine.Engine
import jenkem.engine.Pal
import jenkem.engine.color.Power
import jenkem.engine.color.Scheme
import jenkem.util.AwtImageUtil
import jenkem.util.GoogleUtil
import jenkem.util.UrlOptionizer
import jenkem.ui.ImagePreparer
import jenkem.util.InitUtil
import jenkem.engine.Proportion
import jenkem.bot.status.Connected
import jenkem.bot.status.Disconnected
import jenkem.bot.status.NotSending
import jenkem.bot.status.BotStatus
class JenkemBot extends PircBot {
def init: Unit = {
super.setEncoding("UTF-8")
super.setLogin(IrcSettings.login)
super.setVersion(IrcSettings.version)
super.setMessageDelay(IrcSettings.defaultDelayMs)
super.setAutoNickChange(false)
}
init
object Command extends Enumeration {
type Command = Value
val QUIT, GTFO, STOP, STFU, HELP, CONFIG, SET, RESET = Value
}
object ConfigItem extends Enumeration {
type ConfigItem = Value
val DELAY, WIDTH, MODE, SCHEME, CHARSET, CHARS, ASCII, ANSI, POWER, PROPORTION, FULLBLOCK = Value
}
object IntExtractor {
def unapply(s: String): Option[Int] = try { Some(s.toInt) }
catch { case _: java.lang.NumberFormatException => None }
}
val settings = new ConversionSettings
var lastChan = ""
override def onMessage(channel: String, sender: String, login: String, hostname: String, message: String): Unit = {
evaluateCommand(channel, splitAtSpace(message))
}
private def evaluateCommand(channel: String, message: Array[String]): Unit = {
if (message.head.equalsIgnoreCase("Jenkem") ||
message.head.equalsIgnoreCase(getLogin) ||
message.head.equalsIgnoreCase(getNick)) {
if (message.tail.isEmpty) { convertAndPlay(channel, "") }
else {
try {
val item = if (message.length > 2) { Some(message(2)) } else { None }
val value = if (message.length > 3) { Some(message(3)) } else { None }
executeCommand(channel, message.tail.head.toUpperCase, item, value)
} catch {
case nsee: NoSuchElementException => convertAndPlay(channel, message.tail.mkString("+"))
}
}
}
}
private def executeCommand(channel: String, command: String, item: Option[String], value: Option[String]): Unit = {
Command.withName(command) match {
case Command.GTFO | Command.QUIT => disconnect
case Command.STFU | Command.STOP => Sender.makeStop
case Command.HELP => showHelp(channel)
case Command.CONFIG => showConfig(channel)
case Command.SET => changeConfig(channel, item, value)
case Command.RESET => reset(channel)
}
}
private def changeConfig(channel: String, item: Option[String], value: Option[String]): Unit = {
item match {
case Some(i) =>
value match {
case Some(v) => applyNewConfigValue(channel, i, v)
case None => sendMessage(channel, "New value needed for " + i)
}
case None => sendMessage(channel, "Set command requires an item to change and a new value.")
}
}
private def applyNewConfigValue(sender: String, item: String, value: String): Unit = {
try {
ConfigItem.withName(item.toUpperCase) match {
case ConfigItem.DELAY => setMessageDelay(sender, value)
case ConfigItem.WIDTH => setWidth(sender, value)
case ConfigItem.MODE => setMethod(sender, value)
case ConfigItem.SCHEME => setScheme(sender, value)
case ConfigItem.CHARSET => setCharset(sender, value)
case ConfigItem.CHARS | ConfigItem.ASCII | ConfigItem.ANSI => setChars(sender, value)
case ConfigItem.POWER => setPower(sender, value)
case ConfigItem.PROPORTION => setProportion(sender, value)
case ConfigItem.FULLBLOCK => setFullBlock(sender, value)
}
} catch {
case nse: NoSuchElementException => sendMessage(sender, "Config item unknown: " + item)
}
}
override def onPrivateMessage(sender: String, login: String, hostname: String, message: String): Unit = {
val m = splitAtSpace(message)
try {
Command.withName(m.head.toUpperCase) match {
case Command.HELP => showHelp(sender)
case Command.CONFIG => showConfig(sender)
}
} catch {
case nsee: NoSuchElementException => sendMessage(sender, "Command unknown: " + message)
}
}
override def onConnect: Unit = {
Sender.botStatus = new BotStatus.Value(Connected, NotSending, getServer, lastChan, getNick, getDelay)
}
override def onDisconnect: Unit = {
Sender.botStatus = new BotStatus.Value(Disconnected, NotSending, getServer, lastChan, getNick, getDelay)
}
/**
* Shows the help-text.
* @param target channel name or name of the receiver.
*/
private def showHelp(target: String): Unit = {
sendMessage(target, "Play image from url: JENKEM [url]")
sendMessage(target, "Let jenkem search for an image to play: JENKEM [search term]")
sendMessage(target, "Change config: JENKEM [ConfigItem] [Value]")
sendMessage(target, " ConfigItems are: MODE, DELAY, WIDTH, SCHEME, CHARSET, CHARS, POWER, PROPORTION")
sendMessage(target, "Show config: JENKEM CONFIG")
sendMessage(target, "Reset config: JENKEM RESET")
}
/**
* Shows the configuration.
* @param target channel name or name of the receiver.
*/
private def showConfig(target: String): Unit = {
sendMessage(target, "Mode: " + settings.getMethod
+ ", Delay (ms): " + getMessageDelay
+ ", Width (chars): " + settings.width
+ ", Power: " + settings.power
+ ", Scheme: " + settings.schemeName
+ ", Charset: " + settings.chars
+ ", Proportion: " + settings.proportion)
}
def getDelay: Int = super.getMessageDelay.toInt
private def setMessageDelay(target: String, value: String): Unit = {
val min = 100
val max = 3000
val between = " between " + min + " and " + max + "."
value match {
case IntExtractor(v) if min to max contains v =>
setMessageDelay(v)
sendMessage(target, ConfigItem.DELAY + " set to " + v)
Sender.botStatus = new BotStatus.Value(Connected, NotSending, getServer, lastChan, getNick, getDelay)
case IntExtractor(v) => sendMessage(target, ConfigItem.DELAY + " must be" + between)
case _ => sendMessage(target, ConfigItem.DELAY + " must be a numeric value" + between)
}
}
private def reset(target: String): Unit = {
settings.reset
sendMessage(target, "Conversion settings have been resetted.")
}
private def setWidth(target: String, value: String): Unit = {
val min = 16
val max = 80
val between = ConfigItem.WIDTH + " must be between " + min + " and " + max + "."
value match {
case IntExtractor(v) if min to max contains v =>
settings.width = v
reportConfigChange(target, ConfigItem.WIDTH, v.toString)
case IntExtractor(v) => sendMessage(target, between)
case _ => sendMessage(target, between)
}
}
private def setMethod(target: String, value: String): Unit = {
Method.valueOf(value) match {
case Some(method) =>
settings.method = method
reportConfigChange(target, ConfigItem.MODE, value)
case None => sendMessage(target, "Mode must be one of: " + makeString(Method.values))
}
}
private def setScheme(target: String, value: String): Unit = {
Scheme.valueOf(value) match {
case Some(scheme) =>
settings.colorMap = Scheme.createColorMap(scheme)
settings.setSchemeName(scheme.name)
reportConfigChange(target, ConfigItem.SCHEME, value)
case None => sendMessage(target, "Scheme must be one of: " + makeString(Scheme.values))
}
}
private def setCharset(target: String, value: String): Unit = {
Pal.valueOf(value) match {
case Some(scheme) =>
settings.chars = scheme.chars
reportConfigChange(target, ConfigItem.CHARSET, scheme.chars)
case None =>
sendMessage(target, "Charset must be one of: " + makeString(Pal.values))
}
}
private def setChars(target: String, value: String): Unit = {
settings.chars = " " + value.replaceAll("[0-9],", "")
sendMessage(target, "Chars set to \"" + settings.chars + "\".")
}
private def setPower(target: String, value: String): Unit = {
Power.valueOf(value) match {
case Some(power) =>
settings.power = power
reportConfigChange(target, ConfigItem.POWER, power.name)
case None =>
sendMessage(target, "Power must be one of " + makeString(Power.values))
}
}
private def setProportion(target: String, value: String): Unit = {
Proportion.valueOf(value) match {
case Some(proportion) =>
settings.proportion = proportion
reportConfigChange(target, ConfigItem.PROPORTION, proportion.toString)
case None =>
sendMessage(target, "Proportion must be one of " + makeString(Proportion.values))
}
}
private def setFullBlock(target: String, value: String): Unit = {
try {
settings.fullBlock = value.toBoolean
reportConfigChange(target, ConfigItem.FULLBLOCK, value.toBoolean.toString)
} catch {
case iae: IllegalArgumentException => sendMessage(target, "FullBlock must be true or false.")
}
}
private def reportConfigChange(target: String, item: ConfigItem.Value, value: String): Unit = {
sendMessage(target, item + " set to " + value)
}
private def convertAndPlay(channel: String, urlOrTerm: String): Unit = {
UrlOptionizer.extract(urlOrTerm) match {
case Some(u) => //is url
Sender.playImage(this, generate(urlOrTerm, settings))
case None => //is term
GoogleUtil.getUrlForTerm(urlOrTerm) match {
case Some(imageUrl) =>
sendMessage(channel, imageUrl)
Sender.playImage(this, generate(imageUrl, settings))
case None => sendMessage(channel, "Fail: Cannot find image for \"" + urlOrTerm + "\"")
}
}
if (settings.schemeName.equals(Scheme.Bwg.name)) { settings.reset }
}
private def splitAtSpace(message: String): Array[String] = message.split(" ")
private def makeString(list: List[Any]): String = list.mkString(", ")
/**
* Connects the jenkem bot to IRC and joins the selected channel.
*/
def connectAndJoin(network: String, port: Int, channel: String, nick: String): String = {
settings.reset
def handleConnectionException(message: String, network: String, channel: String, nick: String): String = {
Sender.botStatus = new BotStatus.Value(Disconnected, NotSending, network, channel, nick, getDelay)
message
}
try {
super.setName(nick)
lastChan = channel
connect(network, port)
joinChannel(channel)
"Joining " + channel + "..."
} catch {
case nie: NickAlreadyInUseException => handleConnectionException("Fail: Nick is already in use.", network, channel, nick)
case ioe: IOException => handleConnectionException("Fail: IOException: " + ioe, network, channel, nick)
case ie: IrcException => handleConnectionException("Fail: IrcException: " + ie, network, channel, nick)
}
}
private def generate(url: String, cs: ConversionSettings): List[String] = {
val invert = false
val originalImage = AwtImageUtil.bufferImage(url, "white", invert)
val originalWidth = originalImage.getWidth
val originalHeight = originalImage.getHeight
val (width, height) = Proportion.getWidthAndHeight(cs.proportion, cs.method, cs.width, originalWidth, originalHeight)
val scaled = AwtImageUtil.getScaled(originalImage, width, height, cs.kick, InitUtil.DEFAULT_IMAGE_BRIGHTNESS, 0)
val imageRgb = AwtImageUtil.getImageRgb(scaled)
val lastIndex = height
val params = cs.getParams(imageRgb)
def generate0(index: Int): List[String] = {
if (index + 2 > lastIndex) { Nil }
else { Engine.generateLine(params, index) :: generate0(index + 2) }
}
val hasColor = params.method.hasColor
val notPwntari = !params.method.equals(Method.Pwntari)
val colorString = if (hasColor) { ", Scheme: " + cs.schemeName } else { "" }
val powerString = if (notPwntari) { ", Power: " + params.power } else { "" }
val charsString = if (notPwntari) { ", Chars: " + params.characters } else { "" }
val widthDivisor = if (!params.method.equals(Method.Pwntari)) { 2 } else { 1 }
val message = List("Mode: " + params.method + colorString + powerString
+ charsString + ", Width: " + (width / widthDivisor).intValue.toString)
message ::: generate0(0)
}
}