Beispiel #1
0
		private void session_OnAnySend(object sender, XmlProtocolElement element)
		{
			logForm.Log("SENT: "+element.ToString());
		}
Beispiel #2
0
		private void receivedXmlProtocolElement(int dataLength)
		{
			string chunk = newData.Substring(chunkStart, dataLength-chunkStart+1);

			if (oldData.Length > 0) 
			{
				chunk = oldData + chunk;
				oldData = "";
			}

			// Place protocol element in document context so "stream" namespace is always well-defined (bloody stupid .NET xml parser...)
			chunk = docBegin+chunk.Trim()+docEnd;
			chunkStart = dataLength+1;

			doc.LoadXml(chunk);

			XmlProtocolElement newElement = new XmlProtocolElement(doc.DocumentElement.FirstChild);

			if(OnNewXmlProtocolElement != null)
				OnNewXmlProtocolElement(this, newElement);
		}
Beispiel #3
0
		/// <summary>
		/// Event handler for when a new piece of XML has been parsed from the stream.
		/// Determines if the XML is a stanza or stream element and handles it accordingly.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="element"></param>
		private void streamParser_OnNewXmlProtocolElement(object sender, XmlProtocolElement element)
		{
			// Raise event
			if(OnAnyReceive != null) OnAnyReceive(this, element);

			// Handle the XML
			if(element.IsStanza)
				onNewStanza(StanzaFactory.GetStanza(element.InternalXml));
			else
				onNewStreamElement(StreamElementFactory.GetStreamElement(element.InternalXml));
		}
Beispiel #4
0
		private void session_OnAnyReceive(object sender, XmlProtocolElement element)
		{
			logForm.Log("RECV: "+element.ToString());
		}
Beispiel #5
0
		/// <summary>
		/// Event handler for when a stream has just been opened.
		/// At this point, the network is connected and a stream:stream element
		/// has been received.
		/// </summary>
		/// <param name="sender">Object that triggered the event</param>
		/// <param name="e">The stream element associated with the event</param>
		private void streamParser_OnStreamBegin(object sender, XmlProtocolElement e)
		{
			if (streamParser.Doc.DocumentElement != null)
				sessionId = streamParser.Doc.DocumentElement.GetAttribute("id");

			xmppVersion = streamParser.Doc.DocumentElement.HasAttribute("version") ? streamParser.Doc.DocumentElement.GetAttribute("version") : "0.0";

			//Check version is >= 1.0. If not, terminate connection as stated in RFC 3920.
			int xmppMajor = int.Parse(xmppVersion.Split('.')[0]);
			if(xmppMajor < 1)
			{
				StreamErrorElement err = StreamElementFactory.GetStreamErrorElement(StreamErrorType.UnsupportedVersion);
				send(err);
				if(OnError != null) OnError(this, tcpClient.Hostname+":"+tcpClient.Port+" does not support XMPP version 1.0 or higher.");
				Close();
			}

			if(state != SessionState.StartingSession)
			{
				setState(SessionState.Connected);
				if(OnConnected != null) OnConnected(this, "Network connection initiated");
			}
			if(OnAnyReceive != null) OnAnyReceive(this, e);
		}
Beispiel #6
0
		/// <summary>
		/// Event handler for when a stream has ended.
		/// </summary>
		/// <param name="sender">The object that raised this event.</param>
		/// <param name="e">The stream element associated with the event.  Should always be a stream:stream closing tag.</param>
		private void streamParser_OnStreamEnd(object sender, XmlProtocolElement e)
		{
			// Right now we just close the client, but it would be better to leave the connection
			// open until we know if we are going to reinitiate the connection.
			// For now, just call Close() ; Open() and lament your hard lot in life.
			closeTcpClient();
			if(OnAnyReceive != null) OnAnyReceive(this, e);
			if(OnDisconnected != null) OnDisconnected(this, "Network connection terminated");
		}
Beispiel #7
0
		/// <summary>
		/// Event handler for when a new piece of XML has been parsed from the stream.
		/// Determines if the XML is a stanza or stream element and handles it accordingly.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="element"></param>
		private void streamParser_OnNewXmlProtocolElement(object sender, XmlProtocolElement e)
		{
			// Raise event
			if(OnAnyReceive != null) OnAnyReceive(this, e);

			if(e.IsStanza) e = StanzaFactory.GetStanza(e.InternalXml);
			else e = StreamElementFactory.GetStreamElement(e.InternalXml);

			if(e is IQStanza)
			{
				IQStanza iq = (IQStanza)e;
				if(state == AccountManagerState.Registering)
				{
					if(iq.Type == "result")
					{
						// Received required registration fields
						if(iq.Query != null && iq.Query.NamespaceURI == "jabber:iq:register")
						{
							Hashtable regFields = new Hashtable();
							foreach(XmlNode n in iq.Query.ChildNodes)
							{
								regFields.Add(n.Name, n.InnerText);
							}

							// Get field values from user
							regFields = OnRegistrationFieldsReceived(this, regFields);

							// Regiser the account
							send(StanzaFactory.GetRegisterUserStanza(regFields));
						} 
						else if(iq.Query == null)
						{
							if(OnSuccess != null) OnSuccess(this, EventArgs.Empty);
						}
						else
						{
							if(OnError != null) OnError(this, "The server isn't registering accounts in a standard way.");
						}
					}
					else if(iq.Type == "error")
					{
						if(OnFailure != null) OnFailure(this, EventArgs.Empty);
					}
				}
				else if(state == AccountManagerState.Deleting && e is IQStanza)
				{
					if(iq.Type == "result")
					{
						// TODO
					}
					else if(iq.Type == "error")
					{
						// TODO
					}
				}
			}
			else if(e is ResponseElement)
			{
				ResponseElement responseE = (ResponseElement)e;
				switch(responseE.Type)
				{
					case ResponseType.Success:
						if(state == AccountManagerState.Registering)
						{
							// Restart stream in the online state
							streamParser.Reset();
							openStream(domain, "1.0");
							setState(AccountManagerState.Registering);
						}
						break;
					case ResponseType.StreamError:
						if(OnError != null) OnError(this, responseE.ToString());
						break;
					case ResponseType.ProceedTls:
						streamParser.Reset();
						try
						{
							tcpClient.Stream.Flush();
							tcpClient.IsSecure = true;
						}
						catch(IOException err)
						{
							if(OnError != null) OnError(this, err);
							closeTcpClient();
						}
						catch (Exception err)
						{
							if(OnError != null) OnError(this, err);
							Close();
						}
						break;
				}
			}
			else if (e is StreamFeaturesElement)
			{
				StreamFeaturesElement featuresE = (StreamFeaturesElement)e;
				
				switch(featuresE.Type)
				{
					case StreamFeaturesType.StartTls:
						if(state == AccountManagerState.Connected)
						{
							setState(AccountManagerState.StartingTls);
							ResponseElement startTls = StreamElementFactory.GetStartTlsElement();
							send(startTls);
						}
						break;
					case StreamFeaturesType.SaslMechanisms:
						if(state == AccountManagerState.Connected)
						{
							setState(AccountManagerState.Registering);
							IQStanza reqFieldsIq = StanzaFactory.GetRequestRegistrationFieldsStanza();
							send(reqFieldsIq);
						}
						break;
				}
			}
		}
Beispiel #8
0
		/// <summary>
		/// Sends XML to the server.  Raises OnAnySend event.
		/// </summary>
		/// <param name="e">The XML to send</param>
		private void send(XmlProtocolElement e)
		{
			startSending(e.ToString());
			if(OnAnySend != null) OnAnySend(this, e);
		}
Beispiel #9
0
		/// <summary>
		/// Event handler for when a stream has ended.
		/// </summary>
		/// <param name="sender">The object that raised this event.</param>
		/// <param name="e">The stream element associated with the event.  Should always be a stream:stream closing tag.</param>
		private void streamParser_OnStreamEnd(object sender, XmlProtocolElement e)
		{
			closeTcpClient();
			if(OnAnyReceive != null) OnAnyReceive(this, e);
		}
Beispiel #10
0
		private void manager_OnAnyReceive(object sender, XmlProtocolElement element)
		{
			Console.WriteLine("RECV:"+element);
		}
Beispiel #11
0
		private void manager_OnAnySend(object sender, XmlProtocolElement element)
		{
			Console.WriteLine("SEND: "+element);
		}
Beispiel #12
0
		public void session_OnAnySend(object sender, XmlProtocolElement e)
		{
			Console.WriteLine("SENT:"+e);
		}
Beispiel #13
0
		public void session_OnAnyReceive(object sender, XmlProtocolElement e)
		{
			Console.WriteLine("RECV:"+e);
		}