Пример #1
0
        // TODO : deliverPacketToAll()

        public static void deliverPacket(Packet packet)
        {
            try {
                String       recipient = packet.To;
                StreamWriter output;

                if (recipient == null)
                {
                    output = packet.getSession().getWriter();
                    if (output == null)
                    {
                        JabberServer.output.WriteLine("Undeliverable packet " + packet.ToString());
                        return;
                    }
                }
                else
                {
                    if (userIndex.containsUser(recipient))
                    {
                        output = userIndex.getWriter(recipient);
                    }
                    else
                    {
                        return;
                    }
//				JabberServer.output.WriteLine("String"+userIndex.getUser(recipient).ToString());
                }
                if (output != null)
                {
                    JabberServer.output.WriteLine("Delivering packet: " + packet.ToString());
                    output.Write(packet.ToString()); // smeneto od DarkoA
                    output.Flush();                  // smeneto od DarkoA
                }
                else
                {
                    //begin change by marko
                    if (!packet.Element.Equals("presence") || (packet["type"] != null && (packet["type"].Equals("subscribe") || packet["type"].Equals("subscribed") || packet["type"].Equals("unsubscribe") || packet["type"].Equals("unsubscribed"))))
                    {   //update presence packets (available & unavailable) don't need to be stored for offline use
                        JabberServer.output.WriteLine("Store & forward: " + packet.ToString());
                        User user = userIndex.getUser(new JabberID(recipient).getUser());
                        user.storeMessage(packet);
                    }
                    //end change by marko
                }
            } catch (Exception ex) {
                JabberServer.output.WriteLine(ex.StackTrace);


                JabberServer.output.WriteLine("MessageHandler: ", ex);
            }
        }
Пример #2
0
        public void notify(Packet packet)
        {
            packet.To   = null;
            packet.From = null;
            if (packet.getSession().getStatus() != Session.SessionStatus.authenticated)
            {
                //can not send message:user is not authenticated
                //Error Monitoring must be implemented
                MessageHandler.deliverPacket(packet);
                return;
            }
            User user = userIndex.getUser(packet.getSession());

            if (packet.Type.Equals("set"))
            {
                user.getRoster().updateRoster(packet);
                return;
            }
            if (packet.Type.Equals("get"))
            {
                packet.Type = "result";
                JabberID jidTo = packet.getSession().getJID();
                packet.To = jidTo.User + "@" + jidTo.Domain;
                // packet.From = null;
                packet.removeAttribute("from");
                packet.getChildren().Clear();
                user.getRoster().getPacket().setParent(packet);
                MessageHandler.deliverPacket(packet);
                return;
            }
            //error in packet
            MessageHandler.deliverPacket(packet);
        }
Пример #3
0
        public void notify(Packet packet)
        {
            if (packet.Session.Status != Session.SessionStatus.authenticated)
            {
                //Log.info("PresenceHandler: Not authenticated" + packet.ToString());
                //send an error message Not Authenticated!;

                packet.To   = null;
                packet.From = null;
                //ErrorTool.setError(packet, 401, "You must be authenticated to send presence");
                MessageHandler.deliverPacket(packet);
            }
            else if (chatMan.isChatPacket(packet))
            {
                //Log.trace("PresenceHandler: groupchat presence");
                chatMan.handleChatPresence(packet);
            }
            else
            {
                //Log.trace("PresenceHandler: delivering presence packet " + packet.ToString());
                // Od Marko
                User user = userIndex.getUser(packet.getSession().getJID().getUser());
                user.getRoster().updatePresence(packet);
            }
        }
Пример #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;
            }
        }
Пример #5
0
        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");
                }
            }
        }
Пример #6
0
        public void notify(Packet packet)
        {
            JabberServer.output.WriteLine("Register handling" + packet.ToString());

            String type  = packet.getType();
            Packet query = packet.getFirstChild("query");

            if (type.Equals("get"))
            {
                required.setSession(packet.getSession());
                required.setID(packet.getID());
                MessageHandler.deliverPacket(required);
                return;
            }
            else if (type.Equals("set"))
            {
                String username = query.getChildValue("username");
                User   user     = userIndex.getUser(username);
                if (user != null)
                {
                    if (packet.getSession().getStatus() != Session.SessionStatus.authenticated || !username.Equals(packet.getSession().getJID().getUser()))
                    {
                        Packet iq = new Packet("iq");
                        iq.setSession(packet.getSession());
                        iq.setID(packet.getID());
                        iq.Type = "error";
                        ErrorTool.setError(iq, 401, "User account already exists");
                        MessageHandler.deliverPacket(iq);
                        return;
                    }
                }
                else
                {
                    user = userIndex.addUser(username);
                }
                user.setPassword(query.getChildValue("password"));
                user.setHash(query.getChildValue("hash"));
                user.setSequence(query.getChildValue("sequence"));
                user.setToken(query.getChildValue("token"));
                if (user.getHash() == null || user.getSequence() == null || user.getToken() == null)
                {
                    if (user.getPassword() != null)
                    {
                        user.setToken("randomtoken");// ovde smeni
                        user.setSequence("99");
                        user.setHash(auth.getZeroKHash(100, Encoding.UTF8.GetBytes(user.getToken()), Encoding.UTF8.GetBytes(user.getPassword())
                                                       /* ovde da se proveri isprakjanjeto !!! */
                                                       ));
                    }
                }
                else
                {
                    // Adjust sequence number to be ready for next request.
                    // Book readers.  In the book this was listed earlier resulting in a thrown
                    // exception if 0k registration was not used.  This fixes it.  :)
                    int i = (int.Parse(user.getSequence()) - 1);
                    user.setSequence(i.ToString());
                }
                Packet iqpacket = new Packet("iq");
                iqpacket.setSession(packet.getSession());
                iqpacket.setID(packet.getID());
                iqpacket.setType("result");
                MessageHandler.deliverPacket(iqpacket);

                // Temporarily needed as we'll use registration as authentication until Chp7.
                // packet.getSession().getJID().setResource("none");
                // userIndex.addSession(packet.getSession());
                //Log.trace("Register successfully registered " + username + " with password " + query.getChildValue("password"));
                JabberServer.output.WriteLine("Register successfully registered " + username + " with password " + query.getChildValue("password"));
            }
            else
            {
                JabberServer.output.WriteLine("Register ignoring " + packet.ToString());
            }
        }
Пример #7
0
        public void notify(Packet packet)
        {
            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 ex)
                    {
                    }
                }
                // end groupchar clean-up



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