Exemplo n.º 1
0
 public void removeSession(Session session) {
     sessionIndex.Remove(session);
     if (session.getJID() == null) {
         return;
     }
     getUser(session.getJID().getUser()).removeSession(session);
 }
		public override void run() {
			while (true) {
				Socket newSock = serverSocket.Accept();
				Session session = new Session(newSock);

				ProcessThread processor = new ProcessThread(JabberServer.packetQueue, session);
				processor.setDaemon(true);
				processor.start();
			}

		}
		public void addSession(Session session) {
			jidIndex.Add(session.JID.ToString(), session);
			String user = session.JID.User;
			List<Session> resources;
			try {
				resources = userIndex[user];
			} catch (Exception ex) {
				resources = new List<Session>();
				userIndex[user] = resources;
			}
			resources.Add(session);
		}
Exemplo n.º 4
0
        /// <summary>
        /// Главниот дел - хендлерот за пакети
        /// </summary>
        /// <param name="packet"></param>
        public void notify(Packet packet)
        {
		
            String type = packet.Type;
            Packet query = packet.getFirstChild("query");
            username = query.getChildValue("username");
            iq.setID(packet.getID());
            iq.Session = packet.Session;
            iq.getChildren().Clear();
            iq.Type = "result";

            reply = new Packet("query");
            reply.setAttribute("xmlns", "jabber:iq:auth");
            reply.Parent = iq;

            user = userIndex.getUser(username);
            if (user == null)
            {
                sendErrorPacket(404, "User not found");
                return;
            }

            if (userIndex.sessionIndex.ContainsValue(user) == true)
            {
                sendErrorPacket(404, "User already Logged In");
                return;
            }
            

            if (type.Equals("get"))
            {
                sendGetPacket();
                return;
            }
            else if (type.Equals("set"))
            {
                session = packet.Session;
					 if (session.Status != Session.SessionStatus.streaming) {
						 sendErrorPacket(400, "Server name does not match");
						 return;
					 }
                resource = query.getChildValue("resource");
                if (resource == null)
                {
                    sendErrorPacket(404, "You must send resource");
                    return;
                }
                handleSetPacket(query);
                return;
            }
        }
		public void removeSession(Session session) {
			String jidString = session.JID.ToString();
			String user = session.JID.User;

			if (jidIndex.ContainsKey(jidString)) {
				jidIndex.Remove(jidString);
			}
			try {
				List<Session> resources = userIndex[user];
				if (resources.Count <= 1) {
					userIndex.Remove(user);
					return;
				}
				resources.Remove(session);
			} catch (Exception ex) {
				return;
			}
		}
Exemplo n.º 6
0
 public bool containsSession(Session session)
 {
     return sessionIndex.Contains(session);
 }
Exemplo n.º 7
0
 public void addSession(Session session) {
     User user = getUser(session.getJID().getUser());
     user.addSession(session);
     sessionIndex.Add(session, user);
 }
Exemplo n.º 8
0
 public User getUser(Session session) {
     return (User)sessionIndex[session];
 }
		public void process(Session session) {
			//Assembly assem = Assembly.LoadFrom(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\SaxExpat.dll");
			//IXmlReader reader = SaxReaderFactory.CreateReader(assem, null);
			//reader.ContentHandler = this;
			this.session = session;
			//reader.Parse(new ReaderInputSource(session.getReader()));

			XmlReader reader = XmlReader.Create(session.Reader);


            JabberServer.output.WriteLine("JIH:server version");

			reader.MoveToContent();


			try {
				do {
					switch (reader.Depth) {
						case 0:
							switch (reader.NodeType) {
								case XmlNodeType.Element:
									if (reader.Name == "stream:stream") {
										Dictionary<String, String> atts = getAttributes(reader);
										Packet openPacket = new Packet(null, reader.Name, reader.NamespaceURI, atts);
										openPacket.Session = (session);
										String from = atts["from"];
										session.JID = (new JabberID(from));
										packetQ.enqueue(openPacket);

									} else {
										throw new XmlException("Root node must be <stream:stream>");
									}
									break;
								case XmlNodeType.EndElement:
									Packet closePacket = new Packet("/stream:stream");
									closePacket.Session = (session);
									packetQ.enqueue(closePacket);
									break;
							}
							break;
						case 1:
							switch (reader.NodeType) {
								case XmlNodeType.Element:
									Dictionary<String, String> atts = getAttributes(reader);
									packet = new Packet(null, reader.Name, reader.NamespaceURI, atts);
									packet.Session = (session);
									if (reader.IsEmptyElement)
										goto case XmlNodeType.EndElement;
									break;
								case XmlNodeType.EndElement:
									packetQ.enqueue(packet);
									break;
							}
							break;
						default:
							switch (reader.NodeType) {
								case XmlNodeType.Element:
									Dictionary<String, String> atts = getAttributes(reader);
									Packet child = new Packet(packet, reader.Name, reader.NamespaceURI, atts);
									packet = child;
									if (reader.IsEmptyElement)
										goto case XmlNodeType.EndElement;
									break;
									
								case XmlNodeType.EndElement:
									packet = packet.Parent;
									break;
								case XmlNodeType.Text:
									packet.Children.Add(reader.Value);
									break;

							}
							break;
					}
				} while (reader.Read());

			} catch (Exception ex) {
                //JabberServer.output.WriteLine(ex.ToString());
				// Bilokakov problem so xml stream-ot
				// io-exception, invalid xml, zatvoren socket i sl.
				// treba da se napravi clean-up : zatvori stream </stream:stream>, zatvori socket, trgni Session object i sl.
				
				// Ne sekogas znaci greska : posle </stream:stream> se zatvara socket-ot i ne treba da se pravi nisto
 


                UserIndex userIndex = JabberServer.getUserIndex();
                if (!userIndex.containsSession(session))
                {
                    return;
                }
                //clean-up code (copied from CloseStreamHandler)
                try
                {
                    //      Log.trace("Closing session");
                    //Session session = packet.Session;

                    //session.Writer.Write("</stream:stream> ");
                    //session.Writer.Flush();

                    //notify other subscribers that user is unavailble
                    //by Marko
                    //begin
                    JabberID userid = session.getJID();

                    Packet unavailiblePacket = new Packet("presence");
                    unavailiblePacket.setFrom(userid.ToString());
                    unavailiblePacket.setAttribute("type", "unavailable");
                    userIndex.getUser(userid.User).getRoster().updateSubscribers(unavailiblePacket);
                    //it is not tested, but it should work
                    //end



						  //send groupchat presence unavailable & remove from groups
						  String jid = userid.ToString();

						  Packet presence = new Packet("presence");
						  presence.Type = "unavailable";
						  foreach (KeyValuePair<String, GroupChatManager.Group> kvp in GroupChatManager.Manager.groups) {

							  GroupChatManager.Group group = kvp.Value;
							  try {

								  String nick = group.jid2nick[jid]; // test whether the user is in the group
								  presence.From = group.JabberID + "/" + nick;
								  GroupChatManager.Manager.removeUser(group, jid);   // first remove then deliver so that the packet does not come back
								  GroupChatManager.Manager.deliverToGroup(group, presence);

							  } catch (Exception e) {
							  }
						  }
						  // end groupchar clean-up	

                    //session.Socket.Close();
                    userIndex.removeSession(session);
                    //      Log.trace("Closed session");
                }
                catch (Exception e)
                {
                    //      Log.error("CloseStreamHandler: ",ex);
                    userIndex.removeSession(packet.Session);
                    //      Log.trace("Exception closed session");
                }



			}

			
		}
Exemplo n.º 10
0
		public ProcessThread(PacketQueue queue, Session session, JabberModel model) {
			packetQueue = queue;
			this.session = session;
			this.model = model;
		}
Exemplo n.º 11
0
		public void connect() {
			TcpClient tempClient = new TcpClient(ServerAddress, Convert.ToInt32(Port));	// TcpClient eases connection
			session = new Session(tempClient.Client);									// Get the socket of the TcpClient
			session.Status = (Session.SessionStatus.connected);
			(new ProcessThread(packetQueue, session, this)).start();
			string senderJabID = User + "@" + ServerName + "/" + Resource;
			StreamWriter output = session.Writer;
			session.JID = (new JabberID(User, ServerName, Resource));
			output.Write("<?xml version='1.0' encoding='utf-8' ?>");
			output.Write("<stream:stream to='");
			output.Write(ServerName);
			output.Write("' from='");
			output.Write(senderJabID);
			output.Write("' xmlns='jabber:client' ");
			output.Write("xmlns:stream='http://etherx.jabber.org/streams'>");
			output.Flush();
		}
Exemplo n.º 12
0
		public void setSession(Session session) {
			this.session = session;
		}
Exemplo n.º 13
0
 public void changePriority(Session session) {
     if (activeSession.getPriority() < session.getPriority()) {
         activeSession = session;
     }
 }
Exemplo n.º 14
0
        public void removeSession(Session session) {
            resources.Remove(session.getJID().getResource());
            activeSession = null;

            foreach (Session sess in resources.Values) {
                if (sess.getPriority() > activeSession.getPriority()) {
                    activeSession = sess;
                }
            }
        }
Exemplo n.º 15
0
        public void addSession(Session session) {
            resources.Add(session.getJID().getResource(), session); // key, value
            if (activeSession == null) {
                activeSession = session;
            } else if (activeSession.getPriority() < session.getPriority()) {
                activeSession = session;
            }


			//isprakjanje na Roster (ne e spored stadard)

			Packet packet = new Packet("iq");
			packet["type"] = "set";
			JabberID jidTo = session.getJID();
			packet.To = jidTo.User + "@" + jidTo.Domain;

			getRoster().getPacket().setParent(packet);
			MessageHandler.deliverPacket(packet);


			// kraj na isprakjanje a roster





            deliverMessages();
        }
Exemplo n.º 16
0
		public ProcessThread(PacketQueue queue, Session session) {
			packetQueue = queue;
			this.session = session;
		}
		public void process(Session session) {
			//Assembly assem = Assembly.LoadFrom(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\SaxExpat.dll");
			//IXmlReader reader = SaxReaderFactory.CreateReader(assem, null);
			//reader.ContentHandler = this;
			this.session = session;
			//reader.Parse(new ReaderInputSource(session.getReader()));

			XmlReader reader = XmlReader.Create(session.Reader);

			reader.MoveToContent();


			try {
				do {
					switch (reader.Depth) {
						case 0:
							switch (reader.NodeType) {
								case XmlNodeType.Element:
									if (reader.Name == "stream:stream") {
										Dictionary<String, String> atts = getAttributes(reader);
										Packet openPacket = new Packet(null, reader.Name, reader.NamespaceURI, atts);
										openPacket.Session = (session);
										String from = atts["from"];
										session.JID = (new JabberID(from));
										packetQ.enqueue(openPacket);

									} else {
										throw new XmlException("Root node must be <stream:stream>");
									}
									break;
								case XmlNodeType.EndElement:
									Packet closePacket = new Packet("/stream:stream");
									closePacket.Session = (session);
									packetQ.enqueue(closePacket);
									break;
							}
							break;
						case 1:
							switch (reader.NodeType) {
								case XmlNodeType.Element:
									Dictionary<String, String> atts = getAttributes(reader);
									packet = new Packet(null, reader.Name, reader.NamespaceURI, atts);
									packet.Session = (session);
									if (reader.IsEmptyElement)
										goto case XmlNodeType.EndElement;
									break;
								case XmlNodeType.EndElement:
									packetQ.enqueue(packet);
									break;
							}
							break;
						default:
							switch (reader.NodeType) {
								case XmlNodeType.Element:
									Dictionary<String, String> atts = getAttributes(reader);
									Packet child = new Packet(packet, reader.Name, reader.NamespaceURI, atts);
									packet = child;
									if (reader.IsEmptyElement)
										goto case XmlNodeType.EndElement;
									break;
									
								case XmlNodeType.EndElement:
									packet = packet.Parent;
									break;
								case XmlNodeType.Text:
									packet.Children.Add(reader.Value);
									break;

							}
							break;
					}
				} while (reader.Read());

			} catch (Exception ex) {
				// Bilokakov problem so xml stream-ot
				// io-exception, invalid xml, zatvoren socket i sl.
				// treba da se napravi clean-up : zatvori stream </stream:stream>, zatvori socket, trgni Session object i sl.				
				// Ne sekogas znaci greska : posle </stream:stream> se zatvara socket-ot i ne treba da se pravi nisto
				throw ex;
			}

			
		}