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

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

		}
Пример #3
0
        public void notify(Packet packet) {
			try {
				if (packet.ID.StartsWith("auth_get")) {
					Packet query = packet.getFirstChild("query");
					String token = query.getChildValue("token");
                    int sequence = Convert.ToInt32(query.getChildValue("sequence"));
                    String hash = auth.getZeroKHash(sequence, Encoding.UTF8.GetBytes(token), Encoding.UTF8.GetBytes(jabberModel.Password));
                    jabberModel.addResultHandler("0k_auth_" + Convert.ToString(counter), this);
                    StreamWriter output = packet.Session.Writer;
                    output.Write("<iq type='set' id='0k_auth_");
                    output.Write(Convert.ToString(counter++));
                    output.Write("'><query xmlns='jabber:iq:auth'><username>");
                    output.Write(jabberModel.User);
                    output.Write("</username><resource>");
                    output.Write(jabberModel.Resource);                    
                    output.Write("</resource><hash>");
                    output.Write(hash);
                    output.Write("</hash></query></iq>");
                    output.Flush();
                } else if (packet.Type.Equals("result")) {
					Console.WriteLine("Auth Handler passed");
					packet.Session.Status = Session.SessionStatus.authenticated;
					AuthenticateDelegate ad = new AuthenticateDelegate(jabberModel.gui.Authenticated);
					jabberModel.gui.Invoke(ad);
                } else if (packet.Type.Equals("error")) {
                    Console.WriteLine("Failed to authenticate: " + packet.getChildValue("error"));
					AuthenticationFailedDelegate afd = new AuthenticationFailedDelegate(jabberModel.gui.AuthenticationFailed);
					jabberModel.gui.Invoke(afd);
                }

                else

                {

                    Console.WriteLine("Unknown result: " + packet.ToString());

                }

            }

            catch (IOException ex)

            {

                Console.WriteLine(ex.StackTrace);

            }

        }
		public void sendEmptyNicknameError(Packet packet) {
			try {
				Packet presence = new Packet("presence");
				presence.From = packet.To;
				presence.To = packet.From;

				Packet ePacket = new Packet("error");
				ePacket["code"] = 405.ToString();
				ePacket.Children.Add("Not allowed : nickname must not be empty");
				ePacket.Parent = presence;
				packet.Session.Writer.Write(presence.ToString());
				packet.Session.Writer.Flush();
			} catch (Exception ex) {
			}
		}
		public void sendConflictingUserError(Packet packet) {
			try {
				Packet presence = new Packet("presence");
				presence.From = packet.To;
				presence.To = packet.From;

				Packet ePacket = new Packet("error");
				ePacket["code"] = 409.ToString();
				ePacket.Children.Add("Conflict: cannot sign in twice in the same group");
				ePacket.Parent = presence;
				packet.Session.Writer.Write(presence.ToString());
				packet.Session.Writer.Flush();
			} catch(Exception ex) {
			}
		}
		public void sendConflictingNicknameError(Packet packet) {
			try {
				Packet presence = new Packet("presence");
				presence.From = packet.To;
				presence.To = packet.From;

				Packet ePacket = new Packet("error");
				ePacket["code"] = 409.ToString();
				ePacket.Children.Add("Conflict: nickname taken");
				ePacket.Parent = presence;
				packet.Session.Writer.Write(presence.ToString());
				packet.Session.Writer.Flush();
			} catch (Exception ex) {			
			}
		}
Пример #7
0
        public void updatePresence(Packet presence)
        {
            String type = presence.Type;
            Session session = presence.Session;
            Presence sessionPresence = session.getPresence();
            String recipient = presence.To;
            JabberID recipientID;
            Boolean isUserSent;//it is not used yet, may be used in S2S comunication

            //packet filling begins
            if (recipient == null)
            {   //directed to server
                //server name
                recipientID = new JabberID(Server.JabberServer.server_name);
                isUserSent = true;
            }
            else
            {
                recipientID = new JabberID(recipient);
                if (user.Equals(recipientID.User))
                {
                    isUserSent = false;
                }
                else
                {
                    isUserSent = true;
                }
            }
            String sender = presence.From;
            JabberID SenderID;
            if (sender == null)
            {
                SenderID = session.JID;
            }
            else
            {
                SenderID = new JabberID(sender);
            }
            //added by marko
            presence.From = SenderID.User + "@" + SenderID.Domain;

            //original line:
            //String subscriber = isUserSent ? recipientID.ToString() : SenderID.ToString();
            String subscriber = recipientID.ToString();

            if (type == null)
            {
                type = "available";
            }
            //packet filling ends


            //packet routing begins
            if (type.Equals("available") || type.Equals("unavailable"))
            {
                //user-managed presence updates are delivered untouched
                if (!recipientID.ToString().Equals(Server.JabberServer.server_name))
                {
                    //may need some work;
                    MessageHandler.deliverPacket(presence);
                    return;
                }


                //Server-managed presence updates are forwarded only to subscribers

                //update the session's presence status

                sessionPresence.Availible = type.Equals("available");
                sessionPresence.Show = presence.getChildValue("show");
                sessionPresence.Status = presence.getChildValue("status");
                String priority = presence.getChildValue("priority");
                sessionPresence.Priority = priority;
                if (priority != null)
                {
                    //some code to set the priority of the session

                    try
                    {
                        session.setPriority(int.Parse(priority));
                    }
                    catch (Exception ex)
                    {
                        JabberServer.output.WriteLine("Error in incoming priority, setting priority to default: 1");
                        session.setPriority(0);
                    }
                }

                //deliver to all users presence subscribers
                updateSubscribers(presence);
                informSubscriber();
                return;
            }

            if (type.Equals("probe"))
            {

                //needed for server to server communication!!!!
                JabberServer.output.WriteLine("Roster: We don't handle probes yet " + presence.ToString());
                return;


            }



            //Marko believes it is checked for exceptions
            //Subscriber sendersSubscriberItem = (Subscriber)subscribers[subscriber];
            Subscriber sendersSubscriberItem = new Subscriber();

            Hashtable Rosters = JabberServer.RosterHashtable;//get reference to rosters


            //prepare sender's roster
            UserRoster senderRoster = Rosters[this.user] as UserRoster;//getting senders roster
            if (senderRoster == null)
            {
                senderRoster = new UserRoster();
                senderRoster.user = this.user;
            }
            if (!Rosters.ContainsKey(this.user))
            {
                Rosters.Add(this.user, senderRoster);
            }

            //prepare recipients roster
            UserRoster recipientRoster = Rosters[recipientID.User] as UserRoster;//getting recipients roster
            if (recipientRoster == null)
            {
                recipientRoster = new UserRoster();
                recipientRoster.user = recipientID.User;
            }
            if (!Rosters.ContainsKey(recipientID.User))
            {
                Rosters.Add(recipientID.User, recipientRoster);
            }

            sendersSubscriberItem = senderRoster.subscribers[subscriber] as Subscriber;//extra

            if (sendersSubscriberItem == null)
            {
                //create subscription item
                sendersSubscriberItem = new Subscriber();
                sendersSubscriberItem.subscription = "none";
                senderRoster.subscribers.Add(recipient, sendersSubscriberItem);
                Packet itempacket = new Packet("item");
                itempacket.setAttribute("jid", subscriber);
                senderRoster.items.Add(subscriber, itempacket);
            }

            //begin subscription managment
            if (type.Equals("subscribe"))
            {
                sendersSubscriberItem.ask = type;//set up subscription status
            }
            else if (type.Equals("subscribed"))//if subscription accepted
            {
                sendersSubscriberItem.ask = null;
                if (sendersSubscriberItem.subscription.Equals("from"))
                {
                    sendersSubscriberItem.subscription = "both";
                }
                else if (sendersSubscriberItem.subscription.Equals("none"))
                {
                    sendersSubscriberItem.subscription = "to";
                }

            }
            else if (type.Equals("unsubscribed"))
            {//sender does not want to give presence updates to recipient
                sendersSubscriberItem.ask = null;

                if (sendersSubscriberItem.subscription.Equals("to"))
                {
                    sendersSubscriberItem.subscription = "none";
                }

                else if (sendersSubscriberItem.subscription.Equals("both"))
                {
                    sendersSubscriberItem.subscription = "from";
                }


                //notify recipient of sender's unavailble status:
                Packet unavailiblePacket = new Packet("presence");
                unavailiblePacket.setFrom(presence.From);
                unavailiblePacket.To = presence.To;
                unavailiblePacket.setAttribute("type", "unavailable");
                MessageHandler.deliverPacket(unavailiblePacket);

            }
            else if (type.Equals("unsubscribe"))
            {//sender no longer interested in recieving presence updates from recipient
                sendersSubscriberItem.ask = null;

                if (sendersSubscriberItem.subscription.Equals("both"))
                {
                    sendersSubscriberItem.subscription = "to";
                }
                else if (sendersSubscriberItem.subscription.Equals("from"))
                {
                    sendersSubscriberItem.subscription = "none";
                }

            }
            //update the corresponding changes in <items> table in sender's record used for delivery
            Packet item = (Packet)senderRoster.items[subscriber];
            if (item != null)
            {
                item.setAttribute("subscription", sendersSubscriberItem.subscription);
                item.setAttribute("ask", sendersSubscriberItem.ask);
                Packet iq = new Packet("iq");
                iq.Type = "set";
                Packet query = new Packet("query");
                query.setAttribute("xmlns", "jabber:iq:roster");
                query.setParent(iq);
                item.setParent(query);
                iq.To = this.user + "@" + Server.JabberServer.server_name;

                //Forward the subscription packet to recipient
                MessageHandler.deliverPacketToAll(user, iq);//may need some correction!
            }

            //processing of recipients roster begins
            if (sendersSubscriberItem.ask == null)
            {
                //the recipient user rosters update!
                Subscriber recipientsSubscriberItem = recipientRoster.subscribers[user + "@" + JabberServer.server_name] as Subscriber;//extra
                if (recipientsSubscriberItem == null)
                {
                    //create subscription item
                    recipientsSubscriberItem = new Subscriber();
                    recipientsSubscriberItem.subscription = "none";
                    recipientRoster.subscribers.Add(user + "@" + JabberServer.server_name, recipientsSubscriberItem);
                }

                //if type is subscribe, changes in this roster are not needed!!!

                if (type.Equals("subscribed"))//if subscription accepted
                {
                    recipientsSubscriberItem.ask = null;
                    if (recipientsSubscriberItem.subscription.Equals("none"))
                    {
                        recipientsSubscriberItem.subscription = "from";
                    }

                    else if (recipientsSubscriberItem.subscription.Equals("to"))
                    {
                        recipientsSubscriberItem.subscription = "both";
                    }

                }
                else if (type.Equals("unsubscribed"))
                {//sender no longer interested in giving presence updates to recipient
                    recipientsSubscriberItem.ask = null;
                    if (recipientsSubscriberItem.subscription.Equals("both"))
                    {
                        recipientsSubscriberItem.subscription = "to";
                    }
                    else if (recipientsSubscriberItem.subscription.Equals("from"))
                    {
                        recipientsSubscriberItem.subscription = "none";
                    }

                }
                else if (type.Equals("unsubscribe"))
                {//sender no longer interested in recieving presence updates from recipient
                    recipientsSubscriberItem.ask = null;
                    if (recipientsSubscriberItem.subscription.Equals("to"))
                    {
                        recipientsSubscriberItem.subscription = "none";
                    }

                    else if (recipientsSubscriberItem.subscription.Equals("both"))
                    {
                        recipientsSubscriberItem.subscription = "from";
                    }

                    //notify sender of unavailble status:
                    Packet unavailiblePacket = new Packet("presence");
                    unavailiblePacket.setFrom(presence.To);
                    unavailiblePacket.To = presence.From;
                    unavailiblePacket.setAttribute("type", "unavailable");
                    MessageHandler.deliverPacket(unavailiblePacket);

                }

                //update the corresponding changes in <items> table in sender's record used for delivery
                Packet item2 = (Packet)recipientRoster.items[user + "@" + JabberServer.server_name];
                if (item2 != null)
                {
                    item2.setAttribute("subscription", recipientsSubscriberItem.subscription);
                    item2.setAttribute("ask", recipientsSubscriberItem.ask);
                    Packet iq = new Packet("iq");
                    iq.Type = "set";
                    Packet query = new Packet("query");
                    query.setAttribute("xmlns", "jabber:iq:roster");
                    query.setParent(iq);
                    item2.setParent(query);
                    iq.To = recipientID.User + "@" + recipientID.Domain;
                    //Forward the subscription packet to recipient
                    MessageHandler.deliverPacketToAll(user, iq);//may need some correction!
                }

            }

            MessageHandler.deliverPacket(presence);


        }//UpdatePresense
Пример #8
0
		//end added by marko

		public void sendRosterSet(String jid, String name, string group) {			//hashtable type may be changed
			Packet packet = new Packet("iq");
			packet.Type = "set";
			packet.ID = "roster_set";
			Packet query = new Packet("query");
			query.setAttribute("xmlns", "jabber:iq:roster");
			query.setParent(packet);
			Packet item = new Packet("item");
			item.setAttribute("jid", jid);
			if(name != null) {
				item.setAttribute("name", name);
			}
			item.setParent(query);
			//code to form the packets
			if(group != null) {
				new Packet("group", group).setParent(item);
			}
			session.Writer.Write(packet.ToString());
			session.Writer.Flush();
		}
Пример #9
0
		public void sendRosterSet(String jid, String name, Hashtable groups)//hashtable type may be changed
		{
			Packet packet = new Packet("iq");
			packet.Type = "set";
			packet.ID = "roster_set";
			Packet query = new Packet("query");
			query.setAttribute("xmlns", "jabber:iq:roster");
			query.setParent(packet);
			Packet item = new Packet("item");
			item.setAttribute("jid", jid);
			item.setAttribute("name", name);
			item.setParent(query);
			//code to form the packets
			foreach (object key in groups.Keys) {
				new Packet("group", groups[key].ToString()).setParent(item);
			}
			session.Writer.Write(packet.ToString());
			session.Writer.Flush();
		}
Пример #10
0
		public void sendRosterRemove(String jid) {
			Packet packet = new Packet("iq");
			packet.Type = "set";
			packet.setID("roster_remove");
			Packet query = new Packet("query");
			query.setAttribute("xmlns", "jabber:iq:roster");
			query.Parent = packet;
			Packet item = new Packet("item");
			item.setAttribute("jid", jid);
			item.setAttribute("subscription", "remove");
			item.Parent = query;
			session.Writer.Write(packet.ToString());
			session.Writer.Flush();
		}
Пример #11
0
		//Крај додадено

		//added by marko
		//roster/presence management
		public void sendRosterGet() {
			Packet packet = new Packet("iq");
			packet.Type = "get";
			packet.ID = "roster_get_id";
			Packet query = new Packet("query");
			query.setAttribute("xmlns", "jabber:iq:roster");
			query.setParent(packet);
			session.Writer.Write(packet.ToString());
			session.Writer.Flush();
		}
Пример #12
0
		public void sendPresence(String recipient, String type, String show, String status, String prioriry) {
			Packet packet = new Packet("presence");

			packet.From = User + "@" + ServerName + "/" + Resource;
			if (recipient != null) {
				packet.To = recipient;
			}
			if (type != null) {
				packet.Type = type;
			}
			if (show != null) {
				packet.Children.Add(new Packet("show", show));
			}
			if (status != null) {
				packet.Children.Add(new Packet("status", status));
			}
			if (prioriry != null) {
				packet.Children.Add(new Packet("priority", prioriry));
			}

			packet.From = Me.JabberID;
			session.Writer.Write(packet.ToString());
			session.Writer.Flush();
		}
Пример #13
0
		public void sendMessage(string recipient, string subject, string thread, string type, string id, string body) {
			Packet packet = new Packet("message");
			if (recipient != null) {
				packet.To = (recipient);
			}
			if (id != null) {
				packet.ID = (id);
			}
			if (type != null) {
				packet.Type = (type);
			}
			if (subject != null) {
				packet.Children.Add(new Packet("subject", subject));
			}
			if (thread != null) {
				packet.Children.Add(new Packet("thread", thread));
			}
			if (body != null) {
				packet.Children.Add(new Packet("body", body));
			}
			packet.From = Me.JabberID;
			
			session.Writer.Write(packet.ToString()); ;
			session.Writer.Flush();
		}
        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());
            }
        }