Example #1
0
        private static void fillDataSet(DataSet ds)
        {
/*
 *                      DataRow user = ds.Tables["User"].NewRow();
 *                      user["username"] = "******";
 *                      user["password"] = "******";
 *
 *                      ds.Tables["User"].Rows.Add(user);
 *
 *                      DataRow item = ds.Tables["Item"].NewRow();
 *                      item["username"] = "******";
 *                      item["jid"] = "bojan@localhost";
 *                      item["name"] = "Bojan";
 *                      item["group"] = "Grupa";
 *                      ds.Tables["Item"].Rows.Add(item);
 */

            DataTable usersTable = ds.Tables["User"];
            DataRow   newUser    = null;
            Hashtable usersHash  = JabberServer.getUserIndex().userIndex;

            foreach (User u in usersHash.Values)
            {
                newUser             = usersTable.NewRow();
                newUser["username"] = u.getUsername();
                newUser["password"] = u.getPassword();
                newUser["hash"]     = u.getHash();
                newUser["sequence"] = u.getSequence();
                newUser["token"]    = u.getToken();
                usersTable.Rows.Add(newUser);
            }


            DataTable itemsTable = ds.Tables["Item"];
            DataRow   newItem    = null;
            Hashtable rosterHash = JabberServer.RosterHashtable;

            foreach (UserRoster ur in rosterHash.Values)
            {
                String user = ur.user;
                foreach (Packet p in ur.items.Values)
                {
                    String jid          = p["jid"];
                    String subscription = p["subscription"];
                    String name         = p["name"];
                    String group        = p.getChildValue("group");

                    if (subscription.Equals("both"))
                    {
                        newItem             = itemsTable.NewRow();
                        newItem["username"] = user;
                        newItem["jid"]      = jid;
                        newItem["name"]     = name;
                        newItem["group"]    = group;
                        itemsTable.Rows.Add(newItem);
                    }
                }
            }
        }
        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");
                }
            }
        }