SHOW:
|
|
- or go back to the newest paste.
| 1 | using System; | |
| 2 | using System.Collections.Generic; | |
| 3 | using System.Linq; | |
| 4 | using System.Text; | |
| 5 | using System.Net.Sockets; | |
| 6 | using System.IO; | |
| 7 | ||
| 8 | namespace timebot1 {
| |
| 9 | class Program {
| |
| 10 | public static TcpClient socket; | |
| 11 | public static StreamReader reader; | |
| 12 | public static StreamWriter writer; | |
| 13 | ||
| 14 | static bool running = true; | |
| 15 | ||
| 16 | static DateTime dtLaunched = DateTime.Now; | |
| 17 | ||
| 18 | static string | |
| 19 | username = "timebot", | |
| 20 | host = "irc.freenode.net", | |
| 21 | desc = "timebot", | |
| 22 | password = "hunter2", | |
| 23 | admin = "timeshifter", | |
| 24 | command_char = "~"; | |
| 25 | ||
| 26 | static void Main() {
| |
| 27 | try {
| |
| 28 | socket = new TcpClient(host, 6667); | |
| 29 | socket.ReceiveBufferSize = 1024; | |
| 30 | Console.WriteLine("Connected");
| |
| 31 | NetworkStream stream = socket.GetStream(); | |
| 32 | reader = new StreamReader(stream); | |
| 33 | writer = new StreamWriter(stream); | |
| 34 | Send("USER " + username + " 8 * :" + desc);
| |
| 35 | Send("NICK " + username);
| |
| 36 | Send("PRIVMSG NickServ :identify " + password);
| |
| 37 | Send("JOIN #timebot");
| |
| 38 | ReadMessage(reader); | |
| 39 | reader.Close(); | |
| 40 | writer.Close(); | |
| 41 | stream.Close(); | |
| 42 | } | |
| 43 | catch (Exception ex) {
| |
| 44 | Console.WriteLine("Error: " + ex.Message);
| |
| 45 | } | |
| 46 | } | |
| 47 | ||
| 48 | //shorthand command for sending to a specific target | |
| 49 | static void SendMessage(string target, string data) {
| |
| 50 | Send("PRIVMSG " + target + " :" + data);
| |
| 51 | } | |
| 52 | ||
| 53 | //general "send to network" command | |
| 54 | static void Send(string data) {
| |
| 55 | try {
| |
| 56 | writer.WriteLine(data); | |
| 57 | Console.WriteLine(">>> " + data);
| |
| 58 | writer.Flush(); | |
| 59 | } | |
| 60 | catch (Exception ex) {
| |
| 61 | Console.WriteLine("Send error: " + ex.Message);
| |
| 62 | } | |
| 63 | } | |
| 64 | ||
| 65 | static void ReadMessage(StreamReader reader) {
| |
| 66 | try {
| |
| 67 | while (running) {
| |
| 68 | string data = reader.ReadLine(); | |
| 69 | Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + data);
| |
| 70 | string[] msgParts = data.Split(' ');
| |
| 71 | ||
| 72 | if (msgParts[0].ToLower() == "ping") { //ping request
| |
| 73 | Send("PONG " + msgParts[1]);
| |
| 74 | } | |
| 75 | else {
| |
| 76 | string | |
| 77 | user = msgParts[0].Split('!')[0].Substring(1),
| |
| 78 | cmd = msgParts[1].ToLower(), | |
| 79 | target = msgParts[2]; | |
| 80 | ||
| 81 | switch (cmd) {
| |
| 82 | case "join": | |
| 83 | //user joined target | |
| 84 | //if user == username, bot joined | |
| 85 | break; | |
| 86 | case "part": | |
| 87 | //user left target | |
| 88 | //if user == username, bot left | |
| 89 | break; | |
| 90 | case "nick": | |
| 91 | //user changed their nick | |
| 92 | break; | |
| 93 | case "quit": | |
| 94 | //user quit | |
| 95 | break; | |
| 96 | case "invite": | |
| 97 | //bot was invited to target | |
| 98 | break; | |
| 99 | case "notice": | |
| 100 | string msg = data.Split(":".ToCharArray(), 3)[2];
| |
| 101 | if (msg.Contains("You are now identified for ")) {
| |
| 102 | //bot is identified | |
| 103 | SendMessage(admin, "I am authenticated."); | |
| 104 | } | |
| 105 | break; | |
| 106 | case "privmsg": | |
| 107 | //channel or private message | |
| 108 | ParseInput(user, target, data.Split(":".ToCharArray(), 3)[2]);
| |
| 109 | break; | |
| 110 | } | |
| 111 | } | |
| 112 | } | |
| 113 | } | |
| 114 | catch (Exception ex) {
| |
| 115 | Console.WriteLine("Error: " + ex.Message);
| |
| 116 | System.Threading.Thread.Sleep(15000); | |
| 117 | ReadMessage(reader); | |
| 118 | } | |
| 119 | } | |
| 120 | ||
| 121 | static void ParseInput(string user, string target, string msg) {
| |
| 122 | if (msg.StartsWith(command_char)) { //bot command
| |
| 123 | string[] cmdParts = msg.Split(" ".ToCharArray(), 2);
| |
| 124 | ||
| 125 | switch (cmdParts[0].Substring(1)) {
| |
| 126 | case "uptime": | |
| 127 | TimeSpan ts = DateTime.Now - dtLaunched; | |
| 128 | SendMessage(target, string.Format("I have been running for {0}{1}{2}{3}",
| |
| 129 | ts.Days > 0 ? ts.Days.ToString() + "d " : "", | |
| 130 | ts.Hours > 0 ? ts.Hours.ToString() + "h " : "", | |
| 131 | ts.Minutes > 0 ? ts.Minutes.ToString() + "m " : "", | |
| 132 | ts.Seconds.ToString() + "s" | |
| 133 | )); | |
| 134 | break; | |
| 135 | case "hello": | |
| 136 | SendMessage(target, string.Format("Greetings, {0}!", user));
| |
| 137 | break; | |
| 138 | case "quit": | |
| 139 | if (user == admin) {
| |
| 140 | Send("QUIT");
| |
| 141 | running = false; | |
| 142 | } | |
| 143 | break; | |
| 144 | case "rolldie": | |
| 145 | if(cmdParts.Length<2) | |
| 146 | SendMessage(target, "Not enough parameters."); | |
| 147 | else{
| |
| 148 | Random r = new Random(); | |
| 149 | int result = r.Next(int.Parse(cmdParts[1]), int.Parse(cmdParts[2])); | |
| 150 | SendMessage(target, string.Format("You rolled {0}.", result);
| |
| 151 | } | |
| 152 | break; | |
| 153 | } | |
| 154 | } | |
| 155 | else { // any other message
| |
| 156 | ||
| 157 | } | |
| 158 | } | |
| 159 | ||
| 160 | } | |
| 161 | } |