Beispiel #1
0
        void ShowMessage(object sender, MessageReceivedEventArgs args)
        {
            var message = args.Message;
            var chat = args.Chat;
            bool processed = true;

            if (message.Type == Campfire.MessageType.TimestampMessage)
                return;

            CampfirePersonModel person;
            lock (Users) {
                GetUserDetails(message.User_Id); /* Make sure we know who this is */
                person = Users[message.User_Id];
            }

            var bld = CreateMessageBuilder();
            bld.TimeStamp = message.Created_At.DateTime;

            switch (message.Type) {
                case Campfire.MessageType.EnterMessage:
                    // TRANSLATOR: {0} is the name of the room
                    FormatEvent(bld, person, String.Format(_("has joined {0}"), chat.Name));
                    lock (chat) {
                        if (chat.GetPerson(person.ID) == null)
                            Session.AddPersonToGroupChat(chat, person);
                    }
                    break;
                case Campfire.MessageType.KickMessage:
                case Campfire.MessageType.LeaveMessage:
                    // TRANSLATOR: {0} is the name of the room
                    FormatEvent(bld, person, String.Format(_("has left {0}"), chat.Name));
                    lock (chat) {
                        if (chat.GetPerson(person.ID) != null)
                            Session.RemovePersonFromGroupChat(chat, person);
                    }
                    break;
                case Campfire.MessageType.LockMessage:
                    // TRANSLATOR: {0} is the name of the room
                    FormatEvent(bld, person, String.Format(_("has locked {0}"), chat.Name));
                    break;
                case Campfire.MessageType.UnlockMessage:
                    // TRANSLATOR: {0} is the name of the room
                    FormatEvent(bld, person, String.Format(_("has unlocked {0}"), chat.Name));
                    break;
                case Campfire.MessageType.TopicChangeMessage:
                    var topic = CreateMessageBuilder().AppendMessage(message.Body);
                    Session.UpdateTopicInGroupChat(chat, topic.ToMessage());
                    FormatEvent(bld, person, _("has changed the topic"));
                    break;
                case Campfire.MessageType.UploadMessage:
                    FormatUpload(bld, person, chat, message);
                    break;
                case Campfire.MessageType.TextMessage:
                case Campfire.MessageType.PasteMessage:
                    processed = false;
                    break;
                default:
                    FormatEvent(bld, person, String.Format(_("has performed an unknown action"), chat.Name));
                    break;
            }

            if (processed) {
                Session.AddMessageToChat(chat, bld.ToMessage());
                return;
            }

            bool mine = person == Me;

            // Don't double-post the messages we've sent
            if (mine && message.Id <= LastSentId)
                return;

            if (mine)
                bld.AppendSenderPrefix(Me);
            else
                bld.AppendNick(person).AppendSpace();

            if (message.Type == Campfire.MessageType.TextMessage ||
                message.Type == Campfire.MessageType.TweetMessage) {
                bld.AppendMessage(message.Body);
            } else if (message.Type == Campfire.MessageType.PasteMessage) {
                bld.AppendText("\n");
                foreach (string part in message.Body.Split('\n')) {
                    bld.AppendText("    {0}\n", part);
                }
            }

            if (!mine)
                bld.MarkHighlights();

            Session.AddMessageToChat(chat, bld.ToMessage());
        }
Beispiel #2
0
        public void ParseStream()
        {
            Request = HttpWebRequest.Create(
                String.Format("https://streaming.campfirenow.com/room/{0}/live.json", Chat.ID)) as HttpWebRequest;
            Request.Credentials = Cred;
            Request.PreAuthenticate = true;
            var res = Request.GetResponse() as HttpWebResponse;

            using (StreamReader reader = new StreamReader(res.GetResponseStream()))
            {
                /* Stupid but easy way to figure out when we've reached the end of a JSON object */
                int brackets = 0;
                StringBuilder bld = new StringBuilder();

                while (!reader.EndOfStream) {
                    var line = reader.ReadLine();

                    for (int i = 0; i < line.Length; i++) {
                        bld.Append(line[i]);
                        switch (line[i]) {
                            case '{':
                                brackets++;
                                break;
                            case '}':
                                brackets--;
                                break;
                            default:
                                continue;
                        }

                        if (brackets == 0) {
                            var str = bld.ToString().Trim();
                            bld.Length = 0;
                            var message = JsonSerializer.DeserializeFromString<Message>(str);
                            if (MessageReceived != null) {
                                var args = new MessageReceivedEventArgs(Chat, message);
                                MessageReceived(this, args);
                            }
                            LastMessage = message.Id;
                        }
                    }
                }

                reader.Close();
            }

            res.Close();
            Request = null;
        }
Beispiel #3
0
        void FillHole()
        {
            var client = new JsonServiceClient(BaseUri.AbsoluteUri);
            client.Credentials = Cred;
            var messages = client.Get<MessagesResponse>(
                String.Format("/room/{0}.json?since={1}", Chat.ID, LastMessage)).Messages;

            if (messages == null)
                return;

            foreach (var message in messages) {
                if (MessageReceived != null) {
                    var args = new MessageReceivedEventArgs(Chat, message);
                    MessageReceived(this, args);
                }
                LastMessage = message.Id;
            }
        }
        void ShowMessage(object sender, MessageReceivedEventArgs args)
        {
            var  message   = args.Message;
            var  chat      = args.Chat;
            bool processed = true;

            if (message.Type == Campfire.MessageType.TimestampMessage)
            {
                return;
            }


            CampfirePersonModel person;

            lock (Users) {
                GetUserDetails(message.User_Id); /* Make sure we know who this is */
                person = Users[message.User_Id];
            }

            var bld = CreateMessageBuilder();

            bld.TimeStamp = message.Created_At.DateTime;

            switch (message.Type)
            {
            case Campfire.MessageType.EnterMessage:
                // TRANSLATOR: {0} is the name of the room
                FormatEvent(bld, person, String.Format(_("has joined {0}"), chat.Name));
                lock (chat) {
                    if (chat.GetPerson(person.ID) == null)
                    {
                        Session.AddPersonToGroupChat(chat, person);
                    }
                }
                break;

            case Campfire.MessageType.KickMessage:
            case Campfire.MessageType.LeaveMessage:
                // TRANSLATOR: {0} is the name of the room
                FormatEvent(bld, person, String.Format(_("has left {0}"), chat.Name));
                lock (chat) {
                    if (chat.GetPerson(person.ID) != null)
                    {
                        Session.RemovePersonFromGroupChat(chat, person);
                    }
                }
                break;

            case Campfire.MessageType.LockMessage:
                // TRANSLATOR: {0} is the name of the room
                FormatEvent(bld, person, String.Format(_("has locked {0}"), chat.Name));
                break;

            case Campfire.MessageType.UnlockMessage:
                // TRANSLATOR: {0} is the name of the room
                FormatEvent(bld, person, String.Format(_("has unlocked {0}"), chat.Name));
                break;

            case Campfire.MessageType.TopicChangeMessage:
                var topic = CreateMessageBuilder().AppendMessage(message.Body);
                Session.UpdateTopicInGroupChat(chat, topic.ToMessage());
                FormatEvent(bld, person, _("has changed the topic"));
                break;

            case Campfire.MessageType.UploadMessage:
                FormatUpload(bld, person, chat, message);
                break;

            case Campfire.MessageType.TextMessage:
            case Campfire.MessageType.PasteMessage:
                processed = false;
                break;

            default:
                FormatEvent(bld, person, String.Format(_("has performed an unknown action"), chat.Name));
                break;
            }

            if (processed)
            {
                Session.AddMessageToChat(chat, bld.ToMessage());
                return;
            }

            bool mine = person == Me;

            // Don't double-post the messages we've sent
            if (mine && message.Id <= LastSentId)
            {
                return;
            }

            if (mine)
            {
                bld.AppendSenderPrefix(Me);
            }
            else
            {
                bld.AppendNick(person).AppendSpace();
            }

            if (message.Type == Campfire.MessageType.TextMessage ||
                message.Type == Campfire.MessageType.TweetMessage)
            {
                bld.AppendMessage(message.Body);
            }
            else if (message.Type == Campfire.MessageType.PasteMessage)
            {
                bld.AppendText("\n");
                foreach (string part in message.Body.Split('\n'))
                {
                    bld.AppendText("    {0}\n", part);
                }
            }

            if (!mine)
            {
                bld.MarkHighlights();
            }

            Session.AddMessageToChat(chat, bld.ToMessage());
        }