root/lang/csharp/IrcSendOnly/IRCClient.cs

Revision 936, 5.3 kB (checked in by gyuque, 15 months ago)

lang/csharp/IrcSendOnly

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