logo

drewdevault.com

[mirror] blog and personal website of Drew DeVault git clone https://hacktivis.me/git/mirror/drewdevault.com.git

The-worlds-dumbest-IRC-bot.md (6423B)


  1. ---
  2. title: The world's stupidest IRC bot
  3. date: 2021-03-29
  4. ---
  5. I'm an [IRC](https://en.wikipedia.org/wiki/Internet_Relay_Chat) power user,
  6. having been hanging out in 200+ channels on 10+ networks 24/7 for the past 10
  7. years or so. Because IRC is
  8. [standardized](https://tools.ietf.org/html/rfc2812) and simple, a common
  9. pastime for IRC enthusiasts is the creation of bots. In one of the social
  10. channels I hang out in, we've spent the past 6 years gradually building the
  11. world's stupidest IRC bot: wormy.
  12. For a start, wormy is highly schizophrenic. Though it presents itself as a
  13. single bot, it is in fact a
  14. [bouncer](https://en.wikipedia.org/wiki/BNC_(software)) which combines the
  15. connections of 7 independent bots. At one point, this number was higher —
  16. as many as 11 — but some bots were consolidated.
  17. ```
  18. <@sircmpwn> .bots
  19. <wormy> Serving text/html since 2017, yours truly ["ps"] For a list of commands, try `.help`
  20. <wormy> minus' parcel tracking bot r10.b563abc (built on 2020-06-06T12:02:13Z, https://git.sr.ht/~minus/parcel-tracking-bot)
  21. <wormy> minus' dice bot r16.498a0b8 (built on 2020-02-04T20:16:14Z, https://git.sr.ht/~minus/dice-irc-bot)
  22. <wormy> Featuring arbitrary code execution by design and buffer overflows by mistake, jsbot checking in
  23. <wormy> Radiobot coming to you live from The Internet, taking listener requests at 1-800-GUD-SONGS
  24. <wormy> urlbot: live streaming moe directly to your eyeballs
  25. <wormy> o/ SirCmpwn made me so he wouldn't forget shit so much
  26. ```
  27. These bots provide a variety of features for channel members, such as checking
  28. tracking numbers for parcels out for delivery, requesting songs for our private
  29. internet radio, reading out the mimetypes and titles of URLs mentioned in the
  30. channel, or feeding queries into Wolfram Alpha.
  31. ```
  32. <wormy> Now playing: 8369492 小さき者への贖罪の為のソナタ by ALI PROJECT from 禁書 (4m42s FLAC)
  33. <wormy> Now playing: 1045361 アキノサクラ by Wakana from magic moment (5m0s FLAC) #live ♥ minus
  34. <wormy> Now playing: d0b1cb3 Forevermore by F from Cafe de Touhou 3 (4m9s FLAC) ♥ hummer12007
  35. <wormy> Now playing: 0911e90 Moeru San Shimai by Iwasaki Taku from Tengen Toppa Gurren Lagann Original Soundtrack - CD01 (3m3s FLAC)
  36. <wormy> Now playing: ac1a17e rebellion anthem by Yousei teikoku from rebellion anthem (5m15s MP3) ♥ minus
  37. <wormy> Now playing: a5ab39a Desirable Dream by GET IN THE RING from Aki-秋- (4m38s FLAC) ♥ minus
  38. ```
  39. Things really took off with the introduction of a truly stupid bot last year:
  40. [jsbot](https://git.sr.ht/~sircmpwn/jsbot). This bot adds a `.js` command which
  41. executes arbitrary JavaScript (using Fabrice Bellard's
  42. [quickjs](https://bellard.org/quickjs/)) expressions, and sending their
  43. stringified result to the channel.
  44. ```
  45. <@sircmpwn> .js Array(16).join("wat" - 1) + " Batman!"
  46. <wormy> => NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN Batman!
  47. ```
  48. We soon realized, however, that what we had effectively created was a persistent
  49. JavaScript environment which was connected to IRC. This has made it possible to
  50. write even more IRC bots in the least practical manner imaginable: by writing
  51. JavaScript statements, one line at a time, into IRC messages, and hoping it
  52. works.
  53. This has not been an entirely smart move.
  54. ![I say "I hate C++ templates", and the bot responds by writing "yeah, fuck C++
  55. templates!" with "C++ templates" displayed in rainbow colors. MartijnBraam
  56. follows up by asking "number of c++ programmers launched into the sun", which
  57. wormy claims is 367880 people as of 2009.](https://redacted.moe/f/52eb4eba.png)
  58. One "feature", inspired by [Bryan
  59. Cantrill](https://www.youtube.com/watch?v=30jNsCVLpAE), records every time the
  60. word "fuck" is used in the channel. Then, whenever anyone says "wtf", the bot
  61. helpfully offers up an example of the usage of the word "fuck" by printing one
  62. of the recorded messages. Here's how it was made:
  63. ```
  64. <sircmpwn> .js let wtf = [];
  65. <wormy> => undefined
  66. <sircmpwn> .js on(/fuck/, msg => wtf.push(msg.text))
  67. <wormy> => 25
  68. <sircmpwn> .js on(/^what the fuck$/, msg => msg.reply(wtf[Math.floor(Math.random() * wtf.length)]))
  69. <wormy> => 26
  70. ```
  71. Here's one which records whenever someone says "foo++" or "foo\-\-" and keeps
  72. track of scores:
  73. ```
  74. .js on(/^([a-zA-Z0-9_]+)(\+\+|--)$/, (msg, thing, op) => { if (typeof scores[thing] === "undefined") scores[thing] = 0; scores[thing] += op === "++" ? 1 : -1; msg.reply(`${thing}: ${scores[thing]}`) });
  75. .js on(/\.score (.*)/, (msg, item) => msg.reply(scores[item]));
  76. .js let worst = () => Object.entries(scores).sort((a, b) => a[1] - b[1]).slice(0, 5).map(s => `${s[0]}: ${s[1]}`).join(", ");
  77. .js let best = () => Object.entries(scores).sort((a, b) => b[1] - a[1]).slice(0, 5).map(s => `${s[0]}: ${s[1]}`).join(", ");
  78. .js on(/^.worst$/, msg => msg.reply(worst()));
  79. .js on(/^.best$/, msg => msg.reply(best()));
  80. ```
  81. Other "features" written in horrible one-liners include SI unit conversions,
  82. rewriting undesirable URLs (e.g. m.wikipedia.org => en.wikipedia.org), answering
  83. "wormy you piece of shit" with "¯\\\_(ツ)\_/¯", and giving the obvious response to
  84. "make me a sandwich".
  85. Eventually it occurred to us that we had two dozen stupid IRC bots storing not
  86. only their state, but their code, in a single long-lived process on some server.
  87. For a while, the answer to this was adding "don't reboot this server kthx" to
  88. the MotD, but eventually we did some magic nonsense to make certain variables
  89. persistent:
  90. ```js
  91. let persistent = {};
  92. function writePersistent() {
  93. let fd = std.open("persist.json", "w");
  94. fd.puts(JSON.stringify(persistent));
  95. fd.close();
  96. }
  97. let persist_handler = {
  98. set: (obj, prop, val) => {
  99. obj[prop] = val;
  100. writePersistent();
  101. },
  102. };
  103. let p = std.loadFile("persist.json");
  104. if (p !== null) {
  105. persistent = JSON.parse(p);
  106. Object.keys(persistent).map(key => {
  107. let proxy = new Proxy(persistent[key], persist_handler);
  108. persistent[key] = proxy;
  109. exports[key] = proxy;
  110. });
  111. }
  112. exports.persist = (name, obj) => {
  113. let proxy = new Proxy(obj, persist_handler);
  114. persistent[name] = proxy;
  115. writePersistent();
  116. return proxy;
  117. };
  118. ```
  119. Anyway, there's no moral to this story. We just have a silly IRC bot and I
  120. thought I'd share that with you. If you want a stupid IRC bot for your own
  121. channel, [jsbot](https://git.sr.ht/~sircmpwn/jsbot) is available on sourcehut. I
  122. highly disrecommend it and disavow any responsibility for the consequences.