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);


        }
		public void notify(Packet packet) {
            /* od Ch 7
            if (packet.Session.Status != Session.SessionStatus.authenticated) {
                JabberServer.output.WriteLine("Dropping packet (no auth): " + packet.ToString());
                return;
            } */
            String recipient = packet.To;

            if (recipient == null) { // to server
                JabberServer.output.WriteLine("Dropping packet: " + packet.ToString());
                return;
            }

            if (recipient.Equals(JabberServer.server_name, StringComparison.OrdinalIgnoreCase )) { // to server
                JabberServer.output.WriteLine("Dropping packet: " + packet.ToString());
                return;
            }

            // Fill in sender as resource that sent message (anti-spoofing)
            packet.setFrom(packet.getSession().getJID().ToString());

            if (packet.getAttribute("type")!=null && packet.getAttribute("type").Equals("groupchat",StringComparison.OrdinalIgnoreCase)) {
                if (chatMan.isChatPacket(packet)) {
                    chatMan.handleChatMessage(packet);
                } else {
                    JabberServer.output.WriteLine("Dropping packet: " + packet.ToString());
                }
                return;
            }

            JabberServer.output.WriteLine("Delivering packet To: " + packet.To);
            deliverPacket(packet);

		}
Ejemplo n.º 3
0
 public Session getSession()
 {
     if (session != null)
     {
         return(session);
     }
     if (parent != null)
     {
         return(parent.getSession());
     }
     return(null);
 }
        // 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);
            }

		}
		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);
            }        
        }
Ejemplo n.º 6
0
        }//getPacket

        public void updateRoster(Packet packet)
        {
            //Extract the query packet
            Packet rosterQuery = packet.getFirstChild("query");
            rosterQuery.setAttribute("xmlns", "jabber:iq:roster");
            foreach (object rosterItems in rosterQuery.getChildren())
            {
                //for each <item> packet in the query
                Object child = rosterItems;
                if (child is Packet)
                {
                    Packet itemPacket = child as Packet;
                    String subJID = itemPacket.getAttribute("jid");



                    //create Subscriber object if needed
                    //Subscriber sendersSubscriberItem = this.subscribers[subJID] as Subscriber;
                    if ((Server.JabberServer.RosterHashtable[this.user] as UserRoster) == null)
                    {
                        Server.JabberServer.RosterHashtable[this.user] = new UserRoster();
                    }
                    Subscriber sub = (Server.JabberServer.RosterHashtable[this.user] as UserRoster).subscribers[subJID] as Subscriber;
                    if (sub == null)
                    {
                        sub = new Subscriber();
                        sub.subscription = "none";
                        sub.ask = null;
                        (Server.JabberServer.RosterHashtable[this.user] as UserRoster).subscribers.Add(subJID, sub);
                    }

                    if (itemPacket.getAttribute("subscription") != null)
                    {
                        //signal to remove
                        if (itemPacket.getAttribute("subscription").Equals("remove"))
                        {
                            //remove from subscribers item
                            JabberID otherID = new JabberID(subJID);
                            (Server.JabberServer.RosterHashtable[this.user] as UserRoster).subscribers.Remove(otherID.ToString());
                            if ((Server.JabberServer.RosterHashtable[this.user] as UserRoster).items.ContainsKey(subJID))
                            {
                                //remove <item> packet
                                (Server.JabberServer.RosterHashtable[this.user] as UserRoster).items.Remove(subJID);
                            }

                            if ((Server.JabberServer.RosterHashtable[otherID.User] as UserRoster) == null)
                            {
                                Server.JabberServer.RosterHashtable[otherID.User] = new UserRoster();
                            }
                            //set only subscription=none from other item
                            sub = ((Server.JabberServer.RosterHashtable[otherID.User] as UserRoster).subscribers[this.user + "@" + Server.JabberServer.server_name]) as Subscriber;
                            if (sub == null)
                            {
                                sub = new Subscriber();
                            }
                            sub.subscription = "none";
                            sub.ask = null;
                            (Server.JabberServer.RosterHashtable[otherID.User] as UserRoster).subscribers[this.user + "@" + Server.JabberServer.server_name] = sub;
                            //(Server.JabberServer.RosterHashtable[subJID] as UserRoster).subscribers.Add(subJID, sendersSubscriberItem);
                            //}
                            if ((Server.JabberServer.RosterHashtable[otherID.User] as UserRoster).items.ContainsKey(this.user + "@" + Server.JabberServer.server_name))
                            {
                                //update <item> packet
                                //itemPacket.removeAttribute("subscription");

                                Packet mypacket = (Server.JabberServer.RosterHashtable[otherID.User] as UserRoster).items[this.user + "@" + Server.JabberServer.server_name] as Packet;
                                mypacket.setAttribute("subscription", "none");
                                mypacket.setAttribute("ask", null);
                                (Server.JabberServer.RosterHashtable[otherID.User] as UserRoster).items[this.user + "@" + Server.JabberServer.server_name] = mypacket;
                            }
                        }



                    }
                    else
                    {

                        //update <item> with the subscriber info for roster push
                        itemPacket.setAttribute("subscription", sub.subscription);
                        itemPacket.setAttribute("ask", sub.ask);

                        //Packet test = items[subJID];
                        if (!(Server.JabberServer.RosterHashtable[this.user] as UserRoster).items.ContainsKey(subJID))
                        {
                            //store updated <item> packet
                            (Server.JabberServer.RosterHashtable[this.user] as UserRoster).items.Add(subJID, itemPacket);
                        }
                        else
                        {
                            (Server.JabberServer.RosterHashtable[this.user] as UserRoster).items[subJID] = itemPacket;
                        }
                    }
                }
            }
            //Roster push
            packet.Type = "set";
            JabberID jidTo = packet.getSession().getJID();
            packet.To = jidTo.User + "@" + jidTo.Domain;
            packet.removeAttribute("from");
            //needs to be verified
            MessageHandler.deliverPacket(packet);
        }//update roster
        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());
            }
        }