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 (6437B)


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