1 /**
2  * $Id$
3  * $Revision$
4  * $Author$
5  * $Date$
6  *
7  * SmartIrc4net - the IRC library for .NET/C# <http://smartirc4net.sf.net>
8  * This is a simple test client for the library.
9  *
10  * Copyright (c) 2003-2004 Mirco Bauer <meebey@meebey.net> <http://www.meebey.net>
11  *
12  * Full LGPL License: <http://www.gnu.org/licenses/lgpl.txt>
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  */
28 
29 using System;
30 using System.Threading;
31 using System.Collections;
32 using System.Collections.Generic;
33 
34 using Meebey.SmartIrc4net;
35 
36 // This is an VERY basic example how your IRC application could be written
37 // its mainly for showing how to use the API, this program just connects sends
38 // a few message to a channel and waits for commands on the console
39 // (raw RFC commands though! it's later explained).
40 // There are also a few commands the IRC bot/client allows via private message.
41 public class Test
42 {
43     // make an instance of the high-level API
44     public static IrcClient irc = new IrcClient();
45 
46     // this method we will use to analyse queries (also known as private messages)
OnQueryMessage(object sender, IrcEventArgs e)47     public static void OnQueryMessage(object sender, IrcEventArgs e)
48     {
49         switch (e.Data.MessageArray[0]) {
50             // debug stuff
51             case "dump_channel":
52                 string requested_channel = e.Data.MessageArray[1];
53                 // getting the channel (via channel sync feature)
54                 Channel channel = irc.GetChannel(requested_channel);
55 
56                 // here we send messages
57                 irc.SendMessage(SendType.Message, e.Data.Nick, "<channel '"+requested_channel+"'>");
58 
59                 irc.SendMessage(SendType.Message, e.Data.Nick, "Name: '"+channel.Name+"'");
60                 irc.SendMessage(SendType.Message, e.Data.Nick, "Topic: '"+channel.Topic+"'");
61                 irc.SendMessage(SendType.Message, e.Data.Nick, "Mode: '"+channel.Mode+"'");
62                 irc.SendMessage(SendType.Message, e.Data.Nick, "Key: '"+channel.Key+"'");
63                 irc.SendMessage(SendType.Message, e.Data.Nick, "UserLimit: '"+channel.UserLimit+"'");
64 
65                 // here we go through all users of the channel and show their
66                 // hashtable key and nickname
67                 string nickname_list = "";
68                 nickname_list += "Users: ";
69                 foreach (DictionaryEntry de in channel.Users) {
70                     string      key         = (string)de.Key;
71                     ChannelUser channeluser = (ChannelUser)de.Value;
72                     nickname_list += "(";
73                     if (channeluser.IsOp) {
74                         nickname_list += "@";
75                     }
76                     if (channeluser.IsVoice) {
77                         nickname_list += "+";
78                     }
79                     nickname_list += ")"+key+" => "+channeluser.Nick+", ";
80                 }
81                 irc.SendMessage(SendType.Message, e.Data.Nick, nickname_list);
82 
83                 irc.SendMessage(SendType.Message, e.Data.Nick, "</channel>");
84             break;
85             case "gc":
86                 GC.Collect();
87             break;
88             // typical commands
89             case "join":
90                 irc.RfcJoin(e.Data.MessageArray[1]);
91             break;
92             case "part":
93                 irc.RfcPart(e.Data.MessageArray[1]);
94             break;
95             case "die":
96                 Exit();
97             break;
98         }
99     }
100 
101     // this method handles when we receive "ERROR" from the IRC server
OnError(object sender, ErrorEventArgs e)102     public static void OnError(object sender, ErrorEventArgs e)
103     {
104         System.Console.WriteLine("Error: "+e.ErrorMessage);
105         Exit();
106     }
107 
108     // this method will get all IRC messages
OnRawMessage(object sender, IrcEventArgs e)109     public static void OnRawMessage(object sender, IrcEventArgs e)
110     {
111         System.Console.WriteLine("Received: "+e.Data.RawMessage);
112     }
113 
Main(string[] args)114     public static void Main(string[] args)
115     {
116         Thread.CurrentThread.Name = "Main";
117 
118         // UTF-8 test
119         irc.Encoding = System.Text.Encoding.UTF8;
120 
121         // wait time between messages, we can set this lower on own irc servers
122         irc.SendDelay = 200;
123 
124         // we use channel sync, means we can use irc.GetChannel() and so on
125         irc.ActiveChannelSyncing = true;
126 
127         // here we connect the events of the API to our written methods
128         // most have own event handler types, because they ship different data
129         irc.OnQueryMessage += new IrcEventHandler(OnQueryMessage);
130         irc.OnError += new ErrorEventHandler(OnError);
131         irc.OnRawMessage += new IrcEventHandler(OnRawMessage);
132 
133         string[] serverlist;
134         // the server we want to connect to, could be also a simple string
135         serverlist = new string[] {"irc.freenode.org"};
136         int port = 6667;
137         string channel = "#smartirc-test";
138         try {
139             // here we try to connect to the server and exceptions get handled
140             irc.Connect(serverlist, port);
141         } catch (ConnectionException e) {
142             // something went wrong, the reason will be shown
143             System.Console.WriteLine("couldn't connect! Reason: "+e.Message);
144             Exit();
145         }
146 
147         try {
148             // here we logon and register our nickname and so on
149             irc.Login("SmartIRC", "SmartIrc4net Test Bot");
150             // join the channel
151             irc.RfcJoin(channel);
152 
153             for (int i = 0; i < 3; i++) {
154                 // here we send just 3 different types of messages, 3 times for
155                 // testing the delay and flood protection (messagebuffer work)
156                 irc.SendMessage(SendType.Message, channel, "test message ("+i.ToString()+")");
157                 irc.SendMessage(SendType.Action, channel, "thinks this is cool ("+i.ToString()+")");
158                 irc.SendMessage(SendType.Notice, channel, "SmartIrc4net rocks ("+i.ToString()+")");
159             }
160 
161             // spawn a new thread to read the stdin of the console, this we use
162             // for reading IRC commands from the keyboard while the IRC connection
163             // stays in its own thread
164             new Thread(new ThreadStart(ReadCommands)).Start();
165 
166             // here we tell the IRC API to go into a receive mode, all events
167             // will be triggered by _this_ thread (main thread in this case)
168             // Listen() blocks by default, you can also use ListenOnce() if you
169             // need that does one IRC operation and then returns, so you need then
170             // an own loop
171             irc.Listen();
172 
173             // when Listen() returns our IRC session is over, to be sure we call
174             // disconnect manually
175             irc.Disconnect();
176         } catch (ConnectionException) {
177             // this exception is handled because Disconnect() can throw a not
178             // connected exception
179             Exit();
180         } catch (Exception e) {
181             // this should not happen by just in case we handle it nicely
182             System.Console.WriteLine("Error occurred! Message: "+e.Message);
183             System.Console.WriteLine("Exception: "+e.StackTrace);
184             Exit();
185         }
186     }
187 
ReadCommands()188     public static void ReadCommands()
189     {
190         // here we read the commands from the stdin and send it to the IRC API
191         // WARNING, it uses WriteLine() means you need to enter RFC commands
192         // like "JOIN #test" and then "PRIVMSG #test :hello to you"
193         while (true) {
194             string cmd = System.Console.ReadLine();
195             if (cmd.StartsWith("/list")) {
196                 int pos = cmd.IndexOf(" ");
197                 string channel = null;
198                 if (pos != -1) {
199                     channel = cmd.Substring(pos + 1);
200                 }
201 
202                 IList<ChannelInfo> channelInfos = irc.GetChannelList(channel);
203                 Console.WriteLine("channel count: {0}", channelInfos.Count);
204                 foreach (ChannelInfo channelInfo in channelInfos) {
205                     Console.WriteLine("channel: {0} user count: {1} topic: {2}",
206                                       channelInfo.Channel,
207                                       channelInfo.UserCount,
208                                       channelInfo.Topic);
209                 }
210             } else {
211                 irc.WriteLine(cmd);
212             }
213         }
214     }
215 
Exit()216     public static void Exit()
217     {
218         // we are done, lets exit...
219         System.Console.WriteLine("Exiting...");
220         System.Environment.Exit(0);
221     }
222 }
223