| 1 | using System;
|
|---|
| 2 | using System.IO;
|
|---|
| 3 | using System.Collections;
|
|---|
| 4 | using System.Threading;
|
|---|
| 5 | using System.Text;
|
|---|
| 6 | using System.Net;
|
|---|
| 7 | using System.Net.Sockets;
|
|---|
| 8 |
|
|---|
| 9 | namespace IrcSend
|
|---|
| 10 | {
|
|---|
| 11 | public class IRCClient
|
|---|
| 12 | {
|
|---|
| 13 | private string mServer;
|
|---|
| 14 | private int mPort;
|
|---|
| 15 | private string mNick;
|
|---|
| 16 | private string mEncoding;
|
|---|
| 17 | private ArrayList mChannels;
|
|---|
| 18 | private StreamWriter mWriter;
|
|---|
| 19 | private StreamReader mReader;
|
|---|
| 20 | private NetworkStream mNetStream;
|
|---|
| 21 | private Thread mNetThread;
|
|---|
| 22 | private TcpClient mTCP;
|
|---|
| 23 | private bool mContinue;
|
|---|
| 24 | private IRCListener mListener;
|
|---|
| 25 |
|
|---|
| 26 | public IRCClient(IRCListener ln)
|
|---|
| 27 | {
|
|---|
| 28 | mChannels = new ArrayList();
|
|---|
| 29 | mListener = ln;
|
|---|
| 30 | mEncoding = "iso-2022-jp";
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | public void start()
|
|---|
| 34 | {
|
|---|
| 35 | mContinue = true;
|
|---|
| 36 | mTCP = new TcpClient(mServer, mPort);
|
|---|
| 37 | mNetStream = mTCP.GetStream();
|
|---|
| 38 |
|
|---|
| 39 | mReader = new StreamReader(mNetStream, Encoding.GetEncoding("iso-2022-jp"));
|
|---|
| 40 | mWriter = new StreamWriter(mNetStream, Encoding.GetEncoding("iso-2022-jp"));
|
|---|
| 41 |
|
|---|
| 42 | string user_cmd = "USER "+mNick+" gyuque * :"+mNick;
|
|---|
| 43 |
|
|---|
| 44 | mWriter.WriteLine(user_cmd);
|
|---|
| 45 | mWriter.WriteLine("NICK " + mNick);
|
|---|
| 46 |
|
|---|
| 47 | foreach(Object ch in mChannels)
|
|---|
| 48 | {
|
|---|
| 49 | string chname = ch.ToString();
|
|---|
| 50 | mWriter.WriteLine("JOIN " + chname);
|
|---|
| 51 | }
|
|---|
| 52 | mWriter.Flush();
|
|---|
| 53 |
|
|---|
| 54 | mNetThread = new Thread(new ThreadStart(run));
|
|---|
| 55 | mNetThread.Start();
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | public bool loadConfigFromFile(string fn)
|
|---|
| 59 | {
|
|---|
| 60 | StreamReader sr = new StreamReader(fn, System.Text.Encoding.GetEncoding("Shift_JIS"));
|
|---|
| 61 | string content = sr.ReadToEnd();
|
|---|
| 62 |
|
|---|
| 63 | JsonApi.Parser json = new JsonApi.Parser(content);
|
|---|
| 64 | Hashtable root = json.Parse();
|
|---|
| 65 |
|
|---|
| 66 | mServer = root["server"].ToString();
|
|---|
| 67 | mPort = Int32.Parse( root["port"].ToString() );
|
|---|
| 68 | mNick = root["nick"].ToString();
|
|---|
| 69 |
|
|---|
| 70 | Object ch = root["channels"];
|
|---|
| 71 | if (ch is ArrayList)
|
|---|
| 72 | {
|
|---|
| 73 | ArrayList channels = ch as ArrayList;
|
|---|
| 74 | foreach(Object c in channels)
|
|---|
| 75 | mChannels.Add(c.ToString());
|
|---|
| 76 | }
|
|---|
| 77 | else if (ch is String)
|
|---|
| 78 | {
|
|---|
| 79 | mChannels.Add(ch.ToString());
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | if (root["encoding"] != null)
|
|---|
| 83 | {
|
|---|
| 84 | mEncoding = root["encoding"].ToString();
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | return true;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | private static bool isJoinMsg(string s)
|
|---|
| 91 | {
|
|---|
| 92 | return (s.IndexOf("JOIN :") >= 0);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | private static bool isChannelMsg(string s)
|
|---|
| 96 | {
|
|---|
| 97 | char[] dlms = {' '};
|
|---|
| 98 | string[] fields = s.Split(dlms);
|
|---|
| 99 |
|
|---|
| 100 | if (fields.Length < 4)
|
|---|
| 101 | return false;
|
|---|
| 102 |
|
|---|
| 103 | if (fields[2].StartsWith("#") && fields[1].ToUpper().Equals("PRIVMSG"))
|
|---|
| 104 | return true;
|
|---|
| 105 |
|
|---|
| 106 | return false;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | private void handleChannelMsg(string s)
|
|---|
| 110 | {
|
|---|
| 111 | char[] dlms = {' '};
|
|---|
| 112 | string[] fields = s.Split(dlms);
|
|---|
| 113 | string channel = fields[2];
|
|---|
| 114 | string message = fields[3];
|
|---|
| 115 |
|
|---|
| 116 | if (fields.Length > 4)
|
|---|
| 117 | {
|
|---|
| 118 | for (int i = 4;i < fields.Length;i++)
|
|---|
| 119 | message += " "+fields[i];
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | string nick = s.Substring(1, s.IndexOf ("!") - 1);
|
|---|
| 123 | if (message.StartsWith(":"))
|
|---|
| 124 | message = message.Substring(1, message.Length-1);
|
|---|
| 125 |
|
|---|
| 126 | if (mListener != null)
|
|---|
| 127 | mListener.channelMessageArrived(channel, nick, message);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | private void handleJoinMsg(string s)
|
|---|
| 131 | {
|
|---|
| 132 | string nick = s.Substring(1, s.IndexOf ("!") - 1);
|
|---|
| 133 | string channel = s.Substring(s.IndexOf ("JOIN :") + 6);
|
|---|
| 134 |
|
|---|
| 135 | if (nick == mNick && mListener != null)
|
|---|
| 136 | mListener.afterIJoined(channel);
|
|---|
| 137 |
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | public void run()
|
|---|
| 141 | {
|
|---|
| 142 | string readLn;
|
|---|
| 143 | KeepAlive ka = new KeepAlive(mWriter, mServer, 15000);
|
|---|
| 144 | while(mContinue)
|
|---|
| 145 | {
|
|---|
| 146 | while ( (readLn = mReader.ReadLine () ) != null && mContinue)
|
|---|
| 147 | {
|
|---|
| 148 | Console.WriteLine(readLn);
|
|---|
| 149 | if (mListener != null)
|
|---|
| 150 | mListener.anyMessageArrived(readLn);
|
|---|
| 151 |
|
|---|
| 152 | if (isJoinMsg(readLn))
|
|---|
| 153 | handleJoinMsg(readLn);
|
|---|
| 154 | else if (isChannelMsg(readLn))
|
|---|
| 155 | handleChannelMsg(readLn);
|
|---|
| 156 |
|
|---|
| 157 | Thread.Sleep(100);
|
|---|
| 158 | }
|
|---|
| 159 | Thread.Sleep(1000);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | ka.stop();
|
|---|
| 163 | mReader.Close();
|
|---|
| 164 | mWriter.Close();
|
|---|
| 165 | mNetStream.Close();
|
|---|
| 166 |
|
|---|
| 167 | mTCP.Close();
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | public void privmsg(string chname, string msg)
|
|---|
| 171 | {
|
|---|
| 172 | lock(mWriter)
|
|---|
| 173 | {
|
|---|
| 174 | mWriter.WriteLine("PRIVMSG "+chname+" :"+msg);
|
|---|
| 175 | mWriter.Flush();
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | public void stop()
|
|---|
| 180 | {
|
|---|
| 181 | mContinue = false;
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 | class KeepAlive
|
|---|
| 187 | {
|
|---|
| 188 | static string PING = "PING :";
|
|---|
| 189 | private Thread th;
|
|---|
| 190 | private string mServerName;
|
|---|
| 191 | private int mInterval;
|
|---|
| 192 | private StreamWriter mOut;
|
|---|
| 193 | private bool mContinue;
|
|---|
| 194 |
|
|---|
| 195 | public KeepAlive(StreamWriter outs, string servername, int intv)
|
|---|
| 196 | {
|
|---|
| 197 | mServerName = servername;
|
|---|
| 198 | mOut = outs;
|
|---|
| 199 | mInterval = intv;
|
|---|
| 200 | mContinue = true;
|
|---|
| 201 | th = new Thread (new ThreadStart(run));
|
|---|
| 202 | th.Start();
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | public void stop()
|
|---|
| 206 | {
|
|---|
| 207 | mContinue = false;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | public void run()
|
|---|
| 211 | {
|
|---|
| 212 | while (mContinue)
|
|---|
| 213 | {
|
|---|
| 214 | lock(mOut)
|
|---|
| 215 | {
|
|---|
| 216 | mOut.WriteLine (PING + mServerName);
|
|---|
| 217 | mOut.Flush();
|
|---|
| 218 | }
|
|---|
| 219 | Thread.Sleep(mInterval);
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | }
|
|---|
| 224 | }
|
|---|