{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# IRC" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import socket\n", "import sys\n", "\n", "# Adapted from https://pythonspot.com/building-an-irc-bot/\n", "\n", "class IRC:\n", "\n", " irc = socket.socket()\n", " \n", " def __init__(self): \n", " self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n", "\n", " def send(self, chan, msg):\n", " self.irc.send(f\"PRIVMSG {chan} {msg}\\n\".encode())\n", "\n", " def connect(self, server, channel, botnick):\n", " #defines the socket\n", " print (\"connecting to:\"+server)\n", " self.irc.connect((server, 6667)) #connects to the server\n", " self.irc.send(f\"USER {botnick} {botnick} {botnick} :This is a fun bot!\\n\".encode()) #user authentication\n", " self.irc.send(f\"NICK {botnick}\\n\".encode()) \n", " self.irc.send(f\"JOIN {channel}\\n\".encode()) #join the chan\n", "\n", " def get_text(self):\n", " text=self.irc.recv(2040).decode() #receive the text\n", "\n", " if text.find('PING') != -1: \n", " self.irc.send('PONG {text.split()[1]}\\r\\n'.encode()) \n", "\n", " return text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://webchat.freenode.net/##xpub" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import random\n", "\n", "channel = \"##xpub\"\n", "server = \"irc.freenode.net\"\n", "nickname = \"xpubot\"\n", "\n", "irc = IRC()\n", "irc.connect(server, channel, nickname)\n", "\n", "\n", "while 1:\n", " text = irc.get_text()\n", " print (text)\n", " \n", " if \"PRIVMSG\" in text and channel in text and \"hello\" in text:\n", " irc.send(channel, \"Hello!\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }