Ejemplo n.º 1
0
        public void ClientSync(bool ConnectionFailureTimeout = false)
        {
            ThrowIfNotSupported();
            string url = "/_matrix/client/r0/sync?timeout=" + SyncTimeout;

            if (!String.IsNullOrEmpty(syncToken))
            {
                url += "&since=" + syncToken;
            }
            MatrixRequestError error = mbackend.Get(url, true, out var response);

            if (error.IsOk)
            {
                try {
                    MatrixSync sync = JsonConvert.DeserializeObject <MatrixSync> (response.ToString(), event_converter);
                    ProcessSync(sync);
                    IsConnected = true;
                } catch (Exception e) {
                    Console.WriteLine(e.InnerException);
                    throw new MatrixException("Could not decode sync", e);
                }
            }
            else if (ConnectionFailureTimeout)
            {
                IsConnected = false;
                Console.Error.WriteLine("Couldn't reach the matrix home server during a sync.");
                Console.Error.WriteLine(error.ToString());
                Thread.Sleep(BadSyncTimeout);
            }
            if (RunningInitialSync)
            {
                RunningInitialSync = false;
            }
        }
Ejemplo n.º 2
0
        public PublicRooms PublicRooms(int limit, string since, string server)
        {
            ThrowIfNotSupported();
            var qs = HttpUtility.ParseQueryString(string.Empty);

            if (limit != 0)
            {
                qs.Set("limit", limit.ToString());
            }
            if (since != "")
            {
                qs.Set("since", since);
            }
            if (server != "")
            {
                qs.Set("server", server);
            }
            MatrixRequestError error = mbackend.Get($"/_matrix/client/r0/publicRooms?{qs}", true, out var result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());
            }
            return(result.ToObject <PublicRooms>());
        }
Ejemplo n.º 3
0
        public void DeleteFromRoomDirectory(string alias)
        {
            MatrixRequestError error = mbackend.Delete($"/_matrix/client/r0/directory/room/{alias}", true, out var _);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());
            }
        }
Ejemplo n.º 4
0
        public void RoomLeave(string roomid)
        {
            MatrixRequestError error = mbackend.Post(String.Format("/_matrix/client/r0/rooms/{0}/leave", System.Uri.EscapeDataString(roomid)), true, null, out var result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());
            }
        }
Ejemplo n.º 5
0
        public ChunkedMessages GetRoomMessages(string roomId)
        {
            MatrixRequestError error = mbackend.Get($"/_matrix/client/r0/rooms/{roomId}/messages?limit=100&dir=b", true, out var result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());
            }
            return(JsonConvert.DeserializeObject <ChunkedMessages> (result.ToString(), event_converter));
        }
Ejemplo n.º 6
0
        public List <string> GetJoinedRooms()
        {
            MatrixRequestError error = mbackend.Get("/_matrix/client/r0/joined_rooms", true, out var result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());
            }
            return((result as JObject)?.GetValue("joined_rooms").ToObject <List <string> >());
        }
Ejemplo n.º 7
0
        public MatrixLoginResponse ClientLogin(MatrixLogin login)
        {
            MatrixRequestError error = mbackend.Post("/_matrix/client/r0/login", false, JObject.FromObject(login), out var result);

            if (error.IsOk)
            {
                return(result.ToObject <MatrixLoginResponse> ());
            }
            throw new MatrixException(error.ToString()); //TODO: Need a better exception
        }
Ejemplo n.º 8
0
        public Dictionary <string, MatrixProfile> GetJoinedMembers(string roomId)
        {
            MatrixRequestError error = mbackend.Get($"/_matrix/client/r0/rooms/{roomId}/joined_members", true, out var result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());
            }
            return((result as JObject)?.GetValue("joined").ToObject <Dictionary <string, MatrixProfile> >());
        }
Ejemplo n.º 9
0
        public void InviteToRoom(string roomid, string userid)
        {
            JObject            msgData = JObject.FromObject(new { user_id = userid });
            MatrixRequestError error   = mbackend.Post(String.Format("/_matrix/client/r0/rooms/{0}/invite", System.Uri.EscapeDataString(roomid)), true, msgData, out var result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());                 //TODO: Need a better exception
            }
        }
Ejemplo n.º 10
0
        public virtual void RoomStateSend(string roomid, string type, MatrixRoomStateEvent message, string key = "")
        {
            JObject            msgData = ObjectToJson(message);
            MatrixRequestError error   = mbackend.Put(String.Format("/_matrix/client/r0/rooms/{0}/state/{1}/{2}", System.Uri.EscapeDataString(roomid), type, key), true, msgData, out var result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());                 //TODO: Need a better exception
            }
        }
Ejemplo n.º 11
0
        public RoomTags RoomGetTags(string roomid)
        {
            MatrixRequestError error = mbackend.Get($"/_matrix/client/r0/user/{user_id}/rooms/{roomid}/tags", true, out var result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());
            }
            return(result.ToObject <RoomTags>());
        }
Ejemplo n.º 12
0
        public MatrixEvent[] GetRoomState(string roomId)
        {
            ThrowIfNotSupported();
            MatrixRequestError error = mbackend.Get($"/_matrix/client/r0/rooms/{roomId}/state", true, out var result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());
            }
            return(JsonConvert.DeserializeObject <MatrixEvent[]> (result.ToString(), event_converter));
        }
Ejemplo n.º 13
0
        public void RoomPutTag(string roomid, string tag, double order)
        {
            JObject req = new JObject();

            req["order"] = order;
            MatrixRequestError error = mbackend.Put($"/_matrix/client/r0/user/{user_id}/rooms/{roomid}/tags/{tag}", true, req, out var result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());
            }
        }
Ejemplo n.º 14
0
        public void ClientSetAvatar(string userid, string avatar_url)
        {
            JObject request = new JObject();

            request.Add("avatar_url", JToken.FromObject(avatar_url));
            MatrixRequestError error = mbackend.Put(string.Format("/_matrix/client/r0/profile/{0}/avatar_url", Uri.EscapeUriString(userid)), true, request, out var response);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString()); //TODO: Need a better exception
            }
        }
Ejemplo n.º 15
0
        public virtual string RoomStateSend(string roomid, string type, MatrixRoomStateEvent message, string key = "")
        {
            ThrowIfNotSupported();
            JObject            msgData = ObjectToJson(message);
            MatrixRequestError error   = mbackend.Put(String.Format("/_matrix/client/r0/rooms/{0}/state/{1}/{2}", Uri.EscapeDataString(roomid), type, key), true, msgData, out var result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());                 //TODO: Need a better exception
            }
            return(result["event_id"].ToObject <string>());
        }
Ejemplo n.º 16
0
        public MemoryStream MediaDownload(string serverName, string contentId)
        {
            MemoryStream       result = new MemoryStream();
            var                url    = String.Format("/_matrix/media/r0/download/{0}/{1}", serverName, contentId);
            MatrixRequestError error  = mbackend.GetBlob(url, false, out result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());                 //TODO: Need a better exception
            }
            return(result);
        }
Ejemplo n.º 17
0
        public MatrixEventContent GetRoomStateType(string roomId, string type)
        {
            ThrowIfNotSupported();
            MatrixRequestError error = mbackend.Get($"/_matrix/client/r0/rooms/{roomId}/state/{type}/", true, out var result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());
            }

            return(event_converter.GetContent(result as JObject, new JsonSerializer(), type));
        }
Ejemplo n.º 18
0
        public string MediaUpload(string contentType, byte[] data)
        {
            MatrixRequestError error = mbackend.Post("/_matrix/media/r0/upload", true, data, new Dictionary <string, string>()
            {
                { "Content-Type", contentType }
            }, out var result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());
            }
            return((result as JObject)?.GetValue("content_uri").ToObject <string>());
        }
Ejemplo n.º 19
0
        public MatrixVersions ClientVersions()
        {
            MatrixRequestError error = mbackend.Get("/_matrix/client/versions", false, out var result);

            if (error.IsOk)
            {
                var res = result.ToObject <MatrixVersions>();
                versions = res;
                return(res);
            }

            throw new MatrixException(error.ToString());             //TODO: Need a better exception
        }
Ejemplo n.º 20
0
        public void ClientSetDisplayName(string userid, string displayname)
        {
            JObject response;
            JObject request = new JObject();

            request.Add("displayname", JToken.FromObject(displayname));
            MatrixRequestError error = mbackend.Put(string.Format("/_matrix/client/r0/profile/{0}/displayname", Uri.EscapeUriString(userid)), true, request, out response);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());                 //TODO: Need a better exception
            }
        }
Ejemplo n.º 21
0
        public string MediaUpload(string contentType, byte[] data)
        {
            JObject            result = new JObject();
            MatrixRequestError error  = mbackend.Post("/_matrix/media/r0/upload", true, data, new Dictionary <string, string>()
            {
                { "Content-Type", contentType }
            }, out result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());                 //TODO: Need a better exception
            }
            return(result.GetValue("content_uri").ToObject <string>());
        }
Ejemplo n.º 22
0
        public void ClientLogin(MatrixLogin login)
        {
            JObject            result;
            MatrixRequestError error = mbackend.Post("/_matrix/client/r0/login", false, JObject.FromObject(login), out result);

            if (error.IsOk)
            {
                current_login = result.ToObject <MatrixLoginResponse> ();
                SetLogin(current_login);
            }
            else
            {
                throw new MatrixException(error.ToString());                 //TODO: Need a better exception
            }
        }
Ejemplo n.º 23
0
        public void RegisterUserAsAS(string user)
        {
            if (!IsAS)
            {
                throw new MatrixException("This client is not registered as a application service client. You can't create new appservice users");
            }
            JObject request = JObject.FromObject(new {
                type = "m.login.application_service",
                user = user
            });

            MatrixRequestError error = mbackend.Post("/_matrix/client/r0/register", true, request, out var result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());                 //TODO: Need a better exception
            }
        }
Ejemplo n.º 24
0
        public void RoomTypingSend(string roomid, bool typing, int timeout = 0)
        {
            JObject msgData;

            if (timeout == 0)
            {
                msgData = JObject.FromObject(new { typing = typing });
            }
            else
            {
                msgData = JObject.FromObject(new { typing = typing, timeout = timeout });
            }
            MatrixRequestError error = mbackend.Put(
                $"/_matrix/client/r0/rooms/{System.Uri.EscapeDataString(roomid)}/typing/{System.Uri.EscapeDataString(user_id)}", true, msgData, out _);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());
            }
        }
Ejemplo n.º 25
0
        public void RoomTypingSend(string roomid, bool typing, int timeout = 0)
        {
            JObject msgData;
            JObject result;

            if (timeout == 0)
            {
                msgData = JObject.FromObject(new { typing = typing });
            }
            else
            {
                msgData = JObject.FromObject(new { typing = typing, timeout = timeout });
            }
            MatrixRequestError error = mbackend.Put(String.Format("/_matrix/client/r0/rooms/{0}/typing/{1}", System.Uri.EscapeDataString(roomid), System.Uri.EscapeDataString(user_id)), true, msgData, out result);

            if (!error.IsOk)
            {
                throw new MatrixException(error.ToString());                 //TODO: Need a better exception
            }
        }