Exemple #1
0
		void Client_TopicReceived (object sender, TopicInformation e) {
			var act = new Action (() => Text = string.Format ("JacoChat | Topic: {0}", e.Topic));
			if (e.Channel == "#main") {
				if (InvokeRequired)
					BeginInvoke (act);
				else
					act ();
			}
		}
		bool ReceiveOne () {
			if (Token.IsCancellationRequested)
				return false;
			if (Client.Client.Poll (0, SelectMode.SelectRead)) {
				var buf = new byte [1];
				if (Client.Client.Receive (buf, SocketFlags.Peek) == 0)
					return false;
			}
			var handled = true;
			var skip = false;
			string msg;
			try {
				msg = Reader.ReadLine ().Trim ();
			} catch (Exception) {
				return false;
			}
			var parts = msg.Split (' ');
			if (msg == "PING") {
				Send ("PONG");
				return true;
			}
			if (parts.Skip (1).FirstOrDefault () != default (string)) {
				var sender = parts.First ();
				var command = parts.Skip (1).First ();
				if (sender == "server") {
					skip = true;
					switch (command) {
					case "TOPIC":
						{
							var channel = parts.Skip (2).First ();
							var topic = msg.Substring (msg.IndexOf (':') + 1).Trim ();
							var topicInfo = new TopicInformation (channel, topic);
							if (TopicReceived != null)
								TopicReceived (this, topicInfo);
						}
						break;
					case "NAMES":
						{
							var channel = parts.Skip (2).First ();
							var names = msg.Substring (msg.IndexOf (':') + 1).Trim ().Split (' ');
							var nameInfo = new NameInformation (channel, names);
							if (NamesReceived != null)
								NamesReceived (this, nameInfo);
						}
						break;
					default:
						skip = false;
						handled = false;
						break;
					}
				}
				if (skip)
					return true;
				skip = true;
				switch (command) {
				case "PRIVMSG":
					if (MessageReceived != null)
						MessageReceived (this, new PrivateMessage (
							sender: sender,
							target: parts.Skip (2).First (),
							message: msg.Substring (msg.IndexOf (':') + 1).Trim ()
						));
					break;
				default:
					skip = false;
					handled = false;
					break;
				}
				if (skip)
					return true;
			}
			if (!handled && UnhandledResponseReceived != null)
				UnhandledResponseReceived (this, new Response (msg));
			return true;
		}