public void notify(Packet packet) {
            Console.WriteLine("in message handler");
			String type = packet.Type == null ? "normal" : packet.Type;
			Console.WriteLine("Received " + type + " message: " + packet.getChildValue("body"));
			Console.WriteLine("    To: " + packet.To);
			Console.WriteLine("  From: " + packet.From);
			JabberID jid = new JabberID(packet.From);

			if (jid.User != null && jid.User.EndsWith(".group")) { // groupchat message
				//void ReceiveGroupMessage(String groupJID, String userNick, String message)
				
				String user = jid.User;
				String groupJID = user.Substring(0, user.LastIndexOf(".group"));
				String userNick = jid.Resource;

				//ReceiveGroupMessage(groupJID, userNick, packet.getChildValue("body");
				RecieveGroupMessageDelegate rgmd = new RecieveGroupMessageDelegate(model.gui.ReceiveGroupMessage);
				model.gui.Invoke(rgmd, new Object[] { groupJID, userNick, packet.getChildValue("body") });
			} else { // regular message

				RecieveMessageDelegate del = new RecieveMessageDelegate(model.gui.ReceiveMessage);
				model.gui.Invoke(del, new Object[] { jid, packet.getChildValue("body") });
				//model.gui.ReceiveMessage(jid, packet.getChildValue("body"));
			}
		}
Ejemplo n.º 2
0
		public Session getSession(JabberID jabberID) {
			String jidString = jabberID.ToString();
			Session session = null;
			try {
				session = jidIndex[jidString];
				return session;
			} catch (Exception ex) {
				try {
					List<Session> resources = userIndex[jabberID.User];
					return resources[0];
				} catch (Exception e) {
					return null;
				}
			}
		}
		public bool isChatPacket(Packet packet) {
			JabberID recipient;
			if (packet.To == null) {
				return false;
			}
			recipient = new JabberID(packet.To);
			
			//try {
			//    recipient = new JabberID(packet.To);
			//} catch (Exception ex) {
			//    return false;
			//}
			
			String user = recipient.User;
			if (user == null) {
				return false;
			}
			return user.EndsWith(".group") && recipient.equalsDomain(JabberServer.server_name);
		}
Ejemplo n.º 4
0
 public void setJID(JabberID jid)
 {
     this.jid = jid;
 }
Ejemplo n.º 5
0
        }//update roster
        public void informSubscriber()
        {
            //needs to be checked!
            if ((Server.JabberServer.RosterHashtable[this.user] as UserRoster) != null)
            {
                foreach (object obj in (Server.JabberServer.RosterHashtable[this.user] as UserRoster).subscribers.Keys)
                {
                    Subscriber sub = (Server.JabberServer.RosterHashtable[this.user] as UserRoster).subscribers[obj] as Subscriber;
                    if (sub.subscription.Equals("from") || sub.subscription.Equals("both"))//inform sender of his subscribers status!
                    {
                        JabberID myid = new JabberID(obj.ToString());//other's subscribers id
                        if (myid.Domain.Equals(Server.JabberServer.server_name))//if from this server
                        {
                            //new code!
                           
                            
                            
                            
                            
                            
                            if (Server.JabberServer.getUserIndex().getUser(myid.User).getSessions().Values==null)
                            {
                                // if Server.JabberServer.getUserIndex().getUser(myid.User).getSessions()
                                //user is offline
                                Packet presencePacket = new Packet("presence");
                                presencePacket.From = myid.ToString();
                                presencePacket.To = new JabberID(this.user + "@" + Server.JabberServer.server_name).ToString();
                                presencePacket.setAttribute("type", "unavailable");
                                MessageHandler.deliverPacket(presencePacket);

                            }// if (Server.JabberServer.getUserIndex().getUser(myid.User).getSessions().Values!=null)
                            else
                            {
                                foreach (Object obj2 in (Server.JabberServer.getUserIndex().getUser(myid.User).getSessions().Values))
                                {//for each active session
                                    Session sess = obj2 as Session;
 
                                    Packet presencePacket = new Packet("presence");
                                    presencePacket.From = myid.ToString();
                                    presencePacket.To = new JabberID(this.user + "@" + Server.JabberServer.server_name).ToString();
                                    if (sess.getPresence().isAvailible())
                                    {
                                        presencePacket.setAttribute("type", "available");
                                        if(sess.getPresence().getShow()!=null)
                                            presencePacket.Children.Add(new Packet("show",sess.getPresence().getShow()));
 
                                        
                                        if(sess.getPresence().getStatus()!=null)
                                            presencePacket.Children.Add(new Packet("status",sess.getPresence().getStatus()));


                                        if (sess.getPresence().getPriority() != null)
                                            presencePacket.Children.Add(new Packet("priority", sess.getPresence().getPriority()));
 
                                        //other info may be added!
                                    }
                                    else
                                        presencePacket.setAttribute("type", "unavailable");
                                    MessageHandler.deliverPacket(presencePacket);

                                }//for each active session
                            
                              
                            }

                        }//  if(myid.Domain.Equals(Server.JabberServer.server_name))
                        else
                        {
                            //generate probe for S2S communication!@TODO
                        }
                        
                    }//if (sub.subscription.Equals("from") || sub.subscription.Equals("both"))
                }//foreach (object obj in (Server.JabberServer.RosterHashtable[this.user] as UserRoster).subscribers.Keys)

            }//if ((Server.JabberServer.RosterHashtable[this.user] as UserRoster) != null)


        }
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
Ejemplo n.º 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
Ejemplo n.º 8
0
        public System.IO.StreamWriter getWriter(JabberID jabberID) {
//			String resource = jabberID.Resource;
//			User user = getUser(jabberID.User);
            return getUser(jabberID.getUser()).getWriter(jabberID.getResource());
        }
Ejemplo n.º 9
0
		public void setJID(JabberID jid) {
			this.jid = jid;
		}
Ejemplo n.º 10
0
		public Boolean equalsDomain(JabberID testJid) {
			return equalsDomain(testJid.domain);
		}
		public void joinGroup(Group group, Packet packet) {
			JabberID jid = new JabberID(packet.To);
			String sender = packet.From;

			group.jid2nick.Add(sender, jid.Resource);
			group.nick2jid.Add(jid.Resource, sender);
			updatePresence(group, packet);

			foreach (KeyValuePair<String, Packet> pair in group.jid2presence) {
				Packet p = pair.Value;
				if (p.From != jid.ToString()) {
					p.To = sender;
					MessageHandler.deliverPacket(p);
				}
			}
			serverMessage(group, jid.Resource + " has joined the group");
		}
Ejemplo n.º 12
0
 public Boolean equalsDomain(JabberID testJid)
 {
     return(equalsDomain(testJid.domain));
 }
Ejemplo n.º 13
0
 public Boolean equalsUser(JabberID testJid)
 {
     return(equalsUser(testJid.user));
 }
Ejemplo n.º 14
0
 public Boolean equals(JabberID jid)
 {
     return(equalsUser(jid) && equalsDomain(jid) && equalsResource(jid));
 }
Ejemplo n.º 15
0
 public Boolean equalsResource(JabberID testJid)
 {
     return(equalsResource(testJid.resource));
 }
Ejemplo n.º 16
0
		public Boolean equals(JabberID jid) {
			return equalsUser(jid) && equalsDomain(jid) && equalsResource(jid);
		}
Ejemplo n.º 17
0
		public Boolean equalsResource(JabberID testJid) {
			return equalsResource(testJid.resource);
		}
Ejemplo n.º 18
0
		public Boolean equalsUser(JabberID testJid) {
			return equalsUser(testJid.user);
		}
		public void handleChatMessage(Packet packet) {
			JabberID recipient = new JabberID(packet.To);
			Group group = this[recipient.User];

			packet.From = group.JabberID + "/" + group.jid2nick[packet.From];

			if (recipient.Resource == null) {
				deliverToGroup(group, packet);

			} else {
				packet.To = group.nick2jid[recipient.Resource];
				MessageHandler.deliverPacket(packet);
			}
		}
Ejemplo n.º 20
0
		// Invokes a method to receive message in a conversation window, delegate is used for cross thread calls
		public void ReceiveMessage(JabberID from, string body) {
			Console.WriteLine("Gui received message from: " + from);
			if(!conversations.ContainsKey(from.User + "@" + from.Domain)) {		// Check if there is already open conversation window
				Contact c;
				if(contacts.ContainsKey(from.User + "@" + from.Domain))			// Check if sender is in Contact list
					c = contacts[from.User + "@" + from.Domain];
				else
					c = new Contact(from.User + "@" + from.Domain);
				conversations[from.User + "@" + from.Domain] = new ConverstationWindow(c, this);	// Open new conversation window
				conversations[from.User + "@" + from.Domain].Show();								// Show if new window
			} else {
				if(!conversations[from.User + "@" + from.Domain].Visible)
					conversations[from.User + "@" + from.Domain].Show();
				conversations[from.User + "@" + from.Domain].Activate();							// Just activate if already opened
			}
			conversations[from.User + "@" + from.Domain].ReceiveMessage(body);						// Receive message
		}
		public void handleChatPresence(Packet packet) {
			JabberID recipient = new JabberID(packet.To);
			if (!isChatPacket(packet)) {
				return;
			}

			Group group = this[recipient.User];
			String nick = recipient.Resource;
			String jid = packet.From;
			if (String.IsNullOrEmpty(nick)) {
				sendEmptyNicknameError(packet);
				return;
			}


			if (group.nick2jid.ContainsKey(nick)) {
				if (group.nick2jid[nick] == packet.From) {

					updatePresence(group, packet);
				} else {
					sendConflictingNicknameError(packet);
				}
			} else {
				if(group.jid2nick.ContainsKey(jid)) {

					sendConflictingUserError(packet);
					
				} else {
					joinGroup(group, packet);
				}
			}
		}
		public void notify(Packet packet) {
			String from = packet["from"];

		
			String type = packet["type"];
			if (type == null) {
				type = "available";
			}

			String show = packet.getChildValue("show");
			if (show == null) {
				show = "chat";
			}

			String statusMessage = packet.getChildValue("status");
			
			//test whether the presence is a groupchat presence or regular presence
			JabberID jid = new JabberID(from);
			String user = jid.User;
			if (user != null && user.EndsWith(".group")) { // groupchat presence
				Packet error = packet.getFirstChild("error"); // possible error packet
				if (error != null) {  // there was an error
					String code = error["code"];
					String description = error.getValue();
					ShowGroupErrorDelegate sged = new ShowGroupErrorDelegate(model.gui.ShowGroupError);
					model.gui.Invoke(sged, new Object[] { code, description });
					//delegate : ShowError(String code, String description);
					return;
				}

				// delegate signature : void UpdateGroupPresence(String groupJID, String userNick, Show show, String statusMessage) 
				UpdateGroupPresenceDelegate ugpd = new UpdateGroupPresenceDelegate(model.gui.UpdateGroupPresence);

				Goodware.Jabber.GUI.Show showStatus = Show.chat;
				String groupName = user.Substring(0, user.LastIndexOf(".group"));
				String userNick = jid.Resource;
				

				if (show.Equals("chat")) {
					showStatus = Show.chat;
				} else if (show.Equals("away")) {
					showStatus = Show.away;
				} else if (show.Equals("xa")) {
					showStatus = Show.xa;
				} else if (show.Equals("dnd")) {
					showStatus = Show.dnd;
				}


				if (type.Equals("available")) {
					// UpdateGroupPresence(groupName, userNick, showStatus, statusMessage);
					model.gui.Invoke(ugpd, new Object[] { groupName, userNick, showStatus, statusMessage });
					// UpdateGroupPresence(groupName, userNick, showStatus, statusMessage);
				} else if (type.Equals("unavailable")) {
					RemoveGroupMemberDelegate rgmd = new RemoveGroupMemberDelegate(model.gui.RemoveGroupMember);
					model.gui.Invoke(rgmd, new Object[] {groupName, userNick});
					// RemoveGroupMember(groupName, userNick);
				}



			} else { // regular presence


				GetContactDelegate del = new GetContactDelegate(model.gui.GetContact);
				Contact contact = (Contact)model.gui.Invoke(del, new Object[] { from });

				UpdateContactPresenceDelegate ucpd = new UpdateContactPresenceDelegate(model.gui.UpdateContactPresence);

				//two cases : presence update & presence subscription
				if (type.Equals("available") || type.Equals("unavailable")) {  // presence update
					Status status;

					if (type.Equals("unavailable")) {
						status = Status.unavailable;
					} else if (show.Equals("chat")) {
						status = Status.chat;
					} else if (show.Equals("away")) {
						status = Status.away;
					} else if (show.Equals("xa")) {
						status = Status.xa;
					} else if (show.Equals("dnd")) {
						status = Status.dnd;
					} else {
						status = Status.unavailable; // some default - execution should never come to this case
					}
					model.gui.Invoke(ucpd, new Object[] { from, status, statusMessage }); 	//delegate : UpdateContactPresence(from, Status.unavailable, null)
				} else {  // presence subscription or unsubscription

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

						if (contact == null) { // this is the first invitation
							AcceptInvitationDelegate aid = new AcceptInvitationDelegate(model.gui.AcceptInvitation);
							if ((bool)model.gui.Invoke(aid, new Object[] { from })) { //invitation accepted
								model.sendPresence(from, "subscribed", null, null, null);
								model.sendPresence(from, "subscribe", null, null, null);
							} else { //invitation refused
								model.sendPresence(from, "unsubscribed", null, null, null);
							}
						} else {  // the contact is already invited
							model.sendPresence(from, "subscribed", null, null, null);
							//contact.Status = Status.unavailable;
							model.gui.Invoke(ucpd, new Object[] { from, Status.unavailable, null }); 	//delegate : UpdateContactPresence(from, Status.unavailable, null)
							model.sendPresence(model.Me.Status, model.Me.StatusMessage);
							//model.sendPresence - presence packet with current presence information for the user (type, status, show)
						}

					} else if (type.Equals("subscribed")) {

						Status status = contact.Status;
						if (status == Status.inviteSent) { //the second stage in the "three-way handshake"
							// do Nothing : immediately after this packet comes a "subscribe" packet and then things are handled
						} else if (status == Status.inviteAccepted) { // the third stage in the "three-way handshake"
							//contact.Status = Status.unavailable;
							model.gui.Invoke(ucpd, new Object[] { from, Status.unavailable, null });   // delegate : UpdateContactPresence(from, Status.unavailable, null)
						}

					} else if (type.Equals("unsubscribe")) {

						// do Nothing : immediately after this packet comes an "unsubscribed" packet and then things are handled
					
					} else if (type.Equals("unsubscribed")) {

						RemoveContactDelegate rcd = new RemoveContactDelegate(model.gui.RemoveContact);
						model.gui.Invoke(rcd, new Object[] { from });

						// delegate : void removeContact(String jid) - actual invocation : removeContact(from)
						// removes the contact <jid> from the structures containing the contacts
					
					} // type of presence

				} // presence update or subscription

			} // groupchat presence or regular presence
		}