]> jfr.im git - z_archive/kelsier.git/blame - Bot.cs
Adding auth
[z_archive/kelsier.git] / Bot.cs
CommitLineData
7ed73705
JR
1// Kelsier project - Bot state and parser code (Bot.cs)
2// Written by the Jobbig codeteam. <http://jobbig.eu/code/>
3//
4// Copyright 2013 John Runyon.
5//
6// This file is part of the Kelsier project.
7//
8// Kelsier is free software: you can redistribute it and/or modify
9// it under the terms of the GNU Affero General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12//
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU Affero General Public License for more details.
17//
18// You should have received a copy of the GNU Affero General Public License
19// along with this program. If not, see <http://www.gnu.org/licenses/>.
20//
21
22using System;
12e0e204
JR
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using MySql.Data.MySqlClient;
27using System.Net;
28using System.Net.Sockets;
29
e5993b94
JR
30namespace Kelsier.Common {
31 public class Bot {
12e0e204
JR
32 public int id { get; private set; }
33
34 public string nick { get; private set; }
35 public string ident { get; private set; }
36 public string realname { get; private set; }
37 public string localip { get; private set; }
38 public string server { get; private set; }
39 public int serverport { get; private set; }
40
41 private IPEndPoint local;
42
43 private Socket s;
44 public bool online { get; private set; }
45
46 private Logger log;
47
48
49 public Bot(int id) {
50 this.id = id;
51
e5993b94 52 this.log = new Logger(id.ToString("0000"), Info.log);
12e0e204 53
e5993b94 54 MySqlDataReader rdr = Info.db.queryReader("SELECT nick, ident, realname, bindip, server, serverport FROM bots WHERE id = @id", new object[] { "@id", id });
12e0e204
JR
55 rdr.Read();
56
57 this.nick = rdr.GetString("nick");
58 this.ident = rdr.GetString("ident");
59 this.realname = rdr.GetString("realname");
60
61 if (rdr.IsDBNull(rdr.GetOrdinal("bindip"))) {
62 this.localip = null;
63 this.local = new IPEndPoint(IPAddress.Any, 0);
64 } else {
65 this.localip = rdr.GetString("bindip");
66 this.local = new IPEndPoint(IPAddress.Parse(this.localip), 0);
67 }
12e0e204
JR
68 this.server = rdr.GetString("server");
69 this.serverport = rdr.GetInt32("serverport");
36e6a9fe
JR
70
71 rdr.Close();
72
73 // TODO move this part into a 001 hook.
74 MySqlDataReader rdr = Info.db.queryReader("SELECT authname, authpass FROM m_id_quakenet WHERE botid = @id", new object[] { "@id", id });
75 rdr.Read();
76 this.authname = rdr.GetString("authname");
77 this.authpass = rdr.GetString("authpass");
78 rdr.Close();
12e0e204
JR
79 }
80
81 public string connect() {
82 if (online) throw new InvalidStateException();
83
84 s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
85 s.Bind(local);
86 s.Connect(server, serverport);
36e6a9fe
JR
87
88 register();
12e0e204
JR
89
90 Stator rcvstate = new Stator();
91 s.BeginReceive(rcvstate.buffer, 0, 1, SocketFlags.None, new AsyncCallback(dataIn), rcvstate);
92
93 return "TODO";
94 }
95
96 public void dataIn(IAsyncResult ar) {
97 Stator rcvstate = (Stator)ar.AsyncState;
98 s.EndReceive(ar);
99
100 if (rcvstate.buffer[0] == '\n') {
36e6a9fe
JR
101 string linein = rcvstate.buffersb.ToString();
102 processData(linein);
12e0e204 103 rcvstate = new Stator();
7ed73705 104 } else if (rcvstate.buffer[0] != '\r') {
12e0e204
JR
105 rcvstate.buffersb.Append(Encoding.ASCII.GetString(rcvstate.buffer, 0, 1));
106 }
107 s.BeginReceive(rcvstate.buffer, 0, 1, SocketFlags.None, new AsyncCallback(dataIn), rcvstate);
108 }
109 private void send(string data, params object[] args) {
7ed73705 110 log.info(">>> " + data);
12e0e204
JR
111 s.Send(Encoding.ASCII.GetBytes(data + "\n"));
112 }
113
114 private void processData(string data) {
7ed73705
JR
115 log.info("<<< " + data);
116
36e6a9fe
JR
117 string[] parts;
118 string source = null;
119 if (data.StartsWith(":")) {
120 parts = data.Split((char[])null, 2, StringSplitOptions.RemoveEmptyEntries);
121 source = parts[0].Substring(1);
122 data = parts[1];
123 }
124
125 parts = data.Split((char[])null, 2, StringSplitOptions.RemoveEmptyEntries);
126 if (parts[0] == "PRIVMSG") {
127 processMsg(source, parts[1]);
128 } else if (parts[0] == "376") {
129 send("AUTH "+this.authname+" "+this.authpass); // TODO 001 hook
130 send("MODE "+this.nick+" +x-w");
131 send("JOIN #jobbig");
132 } else if (parts[0] == "PING") {
133 send("PONG "+parts[1]);
134 }
135 }
136 // processMsg("DimeCadmium!dime@jobbig.eu", "#mustis :hi");
137 private void processMsg(string source, string data) {
138 string[] dataparts = data.Split((char[])null, 2, StringSplitOptions.RemoveEmptyEntries);
139
140 string nick = (source.Split(new char[] { '!' }))[0];
141 string target = dataparts[0];
142 string message = dataparts[1];
143 if (message.StartsWith(":"))
144 message = message.Substring(1);
145 string[] msgparts = message.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
146
147 // TODO check msgparts[0] first char for trigger
148 string cmdstr = msgparts[0].Substring(1);
149 string[] args;
150 bool chanmsg;
151 Channel chan;
152 if (target.StartsWith("#")) {
153 chanmsg = true;
154 args = new string[msgparts.Length - 1];
155 Array.Copy(msgparts, 1, args, 0, msgparts.Length - 1);
156 chan = new Channel(target);
157 } else {
158 chanmsg = false;
159 args = new string[msgparts.Length - 2];
160 Array.Copy(msgparts, 2, args, 0, msgparts.Length - 2);
161 chan = new Channel(msgparts[1]);
162 }
163
164 User user = new User(nick);
165
166 Command cmd = new Command(this, cmdstr, args, user, chan, chanmsg);
12e0e204
JR
167 }
168
169 private void register() {
170 send(String.Format("NICK {0}", nick));
171 send(String.Format("USER {0} * * :{1}", ident, realname));
172 online = true;
173 }
174 }
175
e5993b94 176 public class InvalidStateException : Exception { }
12e0e204 177
e5993b94 178 public class Stator {
12e0e204
JR
179 public byte[] buffer = new byte[1];
180 public StringBuilder buffersb = new StringBuilder();
181 }
182}