Exemplo n.º 1
0
		/// <summary>
		/// Threaded message
		/// </summary>
		/// <param name="fromJID"></param>
		/// <param name="to"></param>
		/// <param name="lang"></param>
		/// <param name="body"></param>
		/// <param name="thread">Thread</param>
		/// <returns></returns>
		public static MessageStanza GetChatMessageStanza(string fromJID, string to, string lang, MessageBody[] bodies, string thread)
		{
			return GetChatMessageStanza(fromJID, to, lang, bodies, null, thread);
		}
Exemplo n.º 2
0
		/// <summary>
		/// Threaded message with subject
		/// </summary>
		/// <param name="fromJID"></param>
		/// <param name="to"></param>
		/// <param name="lang"></param>
		/// <param name="body"></param>
		/// <param name="subject"></param>
		/// <param name="thread">Thread</param>
		/// <returns></returns>
		public static MessageStanza GetChatMessageStanza(string fromJID, string to, string lang, MessageBody[] bodies, MessageSubject[] subjects, string thread)
		{
			if(fromJID == null) throw new ArgumentNullException("fromJID");
			if(to == null) throw new ArgumentNullException("to");
			if(lang == null) throw new ArgumentNullException("lang");

			StringWriter xmlString = new StringWriter();
			XmlTextWriter xmlWriter = new XmlTextWriter(xmlString);

			xmlWriter.WriteStartElement("message");
			xmlWriter.WriteAttributeString("type", "chat");
			xmlWriter.WriteAttributeString("from", fromJID);
			xmlWriter.WriteAttributeString("to", to);
			xmlWriter.WriteAttributeString("xml:lang", lang);
			if(bodies != null)
			{
				foreach(MessageBody body in bodies)
				{
					xmlWriter.WriteStartElement("body");
					xmlWriter.WriteAttributeString("xml:lang", body.Language);
					xmlWriter.WriteString(body.Body);
					xmlWriter.WriteEndElement();
				}
			}
			if(subjects != null)
			{
				foreach(MessageSubject subject in subjects)
				{
					xmlWriter.WriteStartElement("subject");
					xmlWriter.WriteAttributeString("xml:lang", subject.Language);
					xmlWriter.WriteString(subject.Subject);
					xmlWriter.WriteEndElement();
				}
			}
			if(thread != null && thread.Length > 1)
			{
				xmlWriter.WriteStartElement("thread");
				xmlWriter.WriteString(thread);
				xmlWriter.WriteEndElement();
			}
			xmlWriter.WriteEndElement();
			XmlDocument xml = new XmlDocument();
			xml.LoadXml(xmlString.ToString());
			
			return new MessageStanza(xml.DocumentElement, MessageStanzaType.Chat);
		}
Exemplo n.º 3
0
		/// <summary>
		/// Message stanza with subject
		/// </summary>
		/// <param name="fromJID"></param>
		/// <param name="to"></param>
		/// <param name="lang"></param>
		/// <param name="body">multiple bodies with different languages possible. body[i,0] is language
		/// and body[i,1] is the message</param>
		/// <param name="subject">multiple subjects with different languages possible. subject[i,0] is language
		/// and subject[i,1] is the text</param>
		/// <returns></returns>
		public static MessageStanza GetChatMessageStanza(string fromJID, string to, string lang, MessageBody[] bodies, MessageSubject[] subjects)
		{
			return GetChatMessageStanza(fromJID, to, lang, bodies, subjects, null);
		}
Exemplo n.º 4
0
		public static MessageStanza GetChatMessageStanza(string fromJID, string to, string lang, MessageBody body, MessageSubject subject, string thread)
		{
			MessageSubject[] subjects = null;
			MessageBody[] bodies = null;
			if(subject != null)
			{
				subjects = new MessageSubject[1];
				subjects[0] = subject;
			}
			if(body != null)
			{
				bodies = new MessageBody[1];
				bodies[0] = body;
			}
			return GetChatMessageStanza(fromJID, to, lang, bodies, subjects, thread);
		}
Exemplo n.º 5
0
		public static MessageStanza GetChatMessageStanza(string fromJID, string to, string lang, MessageBody body)
		{
			MessageBody[] bodies = null;
			if(body != null)
			{
				bodies = new MessageBody[1];
				bodies[0] = body;
			}
			return GetChatMessageStanza(fromJID, to, lang, bodies, null, null);
		}
Exemplo n.º 6
0
		public static MessageStanza GetChatMessageStanza(string fromJID, string to, string lang, string body, string subject, string thread)
		{
			MessageBody[] bodies = null;
			MessageSubject[] subjects = null;
			if(body != null)
			{
				bodies = new MessageBody[1];
				bodies[0] = new MessageBody();
				bodies[0].Body = body;
				bodies[0].Language = lang;
			}
			if(subject != null)
			{
				subjects = new MessageSubject[1];
				subjects[0] = new MessageSubject();
				subjects[0].Subject = subject;
				subjects[0].Language = lang;
			}

			return GetChatMessageStanza(fromJID, to, lang, bodies, subjects, thread);
		}
Exemplo n.º 7
0
		public ConsoleApp()
		{
			MentalisTcpClient tcpClient = new MentalisTcpClient(host, int.Parse(port));
			//SecurableTcpClient tcpClient = new MonoTcpClient(host, int.Parse(port));
			session = new Session(tcpClient);
			
			session.OnAnyReceive += new XmlProtocolElementHandler(session_OnAnyReceive);
			session.OnAnySend += new XmlProtocolElementHandler(session_OnAnySend);
			session.OnError += new SessionEventHandler(session_OnError);

			//get open args from console
//			Console.Write("Host:");
//			host = Console.ReadLine();
//			Console.Write("Port:");
//			port = Console.ReadLine();
//			Console.Write("Username:"******"Domain:");
//			domain = Console.ReadLine();
//			Console.Write("Password:"******"Resource:");
//			resource = Console.ReadLine();

			if(!session.Open(domain, resource, username, password, 30000))
			{
				Console.WriteLine("Login failure! Press any key...");
				Console.Read();
				return;
			}

			Console.Write("\nType command (help roster add delete chat chats presence xml exit):");
			string cmd = Console.ReadLine();
			cmd = cmd.Trim();
			while(!cmd.Equals("exit"))
			{				
				//process commands
				string[] tokens = cmd.Split(' ');
				//roster
				if(tokens[0].Equals("roster"))
				{
					session.SendStanza(StanzaFactory.GetRosterStanza(session.JabberID));
					Console.WriteLine("Roster retrieved");
				}
				//add [email protected] name [group]
				if(tokens[0].Equals("add"))
				{
					if(tokens.Length==3) session.SendStanza(StanzaFactory.GetAddFriendStanza(session.JabberID,
						tokens[1],tokens[2],""));
					else session.SendStanza(StanzaFactory.GetAddFriendStanza(session.JabberID,
							 tokens[1],tokens[2],tokens[3]));
				}
				//delete [email protected]
				if(tokens[0].Equals("delete"))
				{
					session.SendStanza(StanzaFactory.GetDeleteFriendStanza(session.JabberID,tokens[1]));
				}
				//chat [email protected] body [lang]
				if(tokens[0].Equals("chat"))
				{
					MessageBody body = new MessageBody();
					body.Body = tokens[2];
					body.Language = "en";
					if(tokens.Length==4) body.Language = tokens[3];
					session.SendStanza(StanzaFactory.GetChatMessageStanza(session.JabberID, tokens[1], body.Language, body));
				}
				//chats [email protected] subject body [subj lang] [body lang]
				if(tokens[0].Equals("chats"))
				{
					MessageBody body = new MessageBody();
					MessageSubject subject = new MessageSubject();
					body.Language = "en";
					subject.Language = "en";
					if(tokens.Length == 6) 
					{
						subject.Language = tokens[4];
						body.Language = tokens[5];
					}
					if(tokens.Length == 5)
					{
						subject.Language = tokens[4];
					}
					session.SendStanza(StanzaFactory.GetChatMessageStanza(session.JabberID, tokens[1], body.Language, body, subject));
				}
				//presence away [message message_language]
				if(tokens[0].Equals("presence"))
				{
					string language = "en";
					if(tokens.Length>=3)
					{
						language = tokens[3];
						string[,] status = new string[1,2];
						status[0,0]=language;
						status[0,1]=tokens[2];
						session.SendStanza(StanzaFactory.GetPresenceBroadcastStanza(tokens[1],language,status));
						}
					else
					{
						session.SendStanza(StanzaFactory.GetPresenceBroadcastStanza(tokens[1],language));
					}
				}
				//xml any_xml
				if(tokens[0].Equals("xml"))
				{
					//session.startSending(cmd.Substring(tokens[0].Length));
					Console.WriteLine("Sorry, not allowed!");
				}
				//help
				if(tokens[0].Equals("help"))
				{
					Console.WriteLine("Commands:");
					Console.WriteLine("roster: shows roster");
					Console.WriteLine("add: add a new friend. add <*****@*****.**>, <Name> [, <group>]");
					Console.WriteLine("delete: delete a friend. delete <*****@*****.**>");
					Console.WriteLine("chat: send a message. chat <*****@*****.**>, <body> [, <body language>]");
					Console.WriteLine("chats: send a message with a subject. chats <*****@*****.**>, <subject>, <body> [, <subject language>][, <body language]");
					Console.WriteLine("presence: broadcasts presence. presence <show> [, <status>, <status language>]");
					Console.WriteLine("exit: close console");
					Console.WriteLine("xml <raw xml>: send the raw xml");
				}
				// close
				if(tokens[0].Equals("restart"))
				{
					session.Close();
					
					tcpClient = new MentalisTcpClient(host, int.Parse(port));
					session = new Session(tcpClient);

					session.OnAnyReceive += new XmlProtocolElementHandler(session_OnAnyReceive);
					session.OnAnySend += new XmlProtocolElementHandler(session_OnAnySend);
					session.OnError += new SessionEventHandler(session_OnError);

					if(!session.Open(domain, resource, username, password, System.Threading.Timeout.Infinite))
					{
						Console.WriteLine("Login failure! Press any key...");
						Console.Read();
						return;
					}
				}
				//wait for next command
				Console.Write("\nType command:");
				cmd = Console.ReadLine();
			}
			//typed exit
			//Done!
		}