Beispiel #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;
            }
        }
Beispiel #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>());
        }
Beispiel #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());
            }
        }
Beispiel #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());
            }
        }
Beispiel #5
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> >());
        }
Beispiel #6
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
        }
Beispiel #7
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> >());
        }
Beispiel #8
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
            }
        }
Beispiel #9
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
            }
        }
Beispiel #10
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>());
        }
Beispiel #11
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));
        }
Beispiel #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));
        }
Beispiel #13
0
        public void ClientTokenRefresh(string refreshToken)
        {
            JObject request = new JObject();

            request.Add("refresh_token", refreshToken);
            MatrixRequestError error = mbackend.Post("/_matrix/r0/tokenrefresh", true, request, out var response);

            if (!error.IsOk)
            {
                throw new MatrixServerError(error.MatrixErrorCode.ToString(), error.MatrixError);
            }
        }
Beispiel #14
0
        public virtual MatrixProfile ClientProfile(string userid)
        {
            ThrowIfNotSupported();
            MatrixRequestError error = mbackend.Get("/_matrix/client/r0/profile/" + userid, true, out var response);

            if (error.IsOk)
            {
                return(response.ToObject <MatrixProfile> ());
            }

            return(null);
        }
Beispiel #15
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());
            }
        }
Beispiel #16
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
            }
        }
Beispiel #17
0
        public string ClientJoin(string roomid)
        {
            ThrowIfNotSupported();
            MatrixRequestError error = mbackend.Post(String.Format("/_matrix/client/r0/join/{0}", Uri.EscapeDataString(roomid)), true, null, out var result);

            if (error.IsOk)
            {
                roomid = result ["room_id"].ToObject <string> ();
                return(roomid);
            }
            return(null);
        }
Beispiel #18
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>());
        }
Beispiel #19
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);
        }
Beispiel #20
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));
        }
Beispiel #21
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>());
        }
Beispiel #22
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
        }
Beispiel #23
0
        public string[] ClientVersions()
        {
            MatrixRequestError error = mbackend.Get("/_matrix/client/versions", false, out var result);

            if (error.IsOk)
            {
                return((result as JObject)?.GetValue("versions").ToObject <string[]> ());
            }
            else
            {
                throw new MatrixException("Non OK result returned from request");                 //TODO: Need a better exception
            }
        }
Beispiel #24
0
        private MatrixRequestError RoomSend(MatrixAPIPendingEvent msg)
        {
            JObject            msgData = ObjectToJson(msg.content);
            MatrixRequestError error   = mbackend.Put(String.Format("/_matrix/client/r0/rooms/{0}/send/{1}/{2}", System.Uri.EscapeDataString(msg.room_id), msg.type, msg.txnId), true, msgData, out var result);

            #if DEBUG
            if (!error.IsOk)
            {
                Console.WriteLine(error.GetErrorString());
            }
            #endif
            return(error);
        }
Beispiel #25
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
            }
        }
Beispiel #26
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>());
        }
Beispiel #27
0
        public MatrixProfile ClientProfile(string userid)
        {
            JObject            response;
            MatrixRequestError error = mbackend.Get("/_matrix/client/r0/profile/" + userid, true, out response);

            if (error.IsOk)
            {
                return(response.ToObject <MatrixProfile> ());
            }
            else
            {
                return(null);
            }
        }
Beispiel #28
0
        public string ClientJoin(string roomid)
        {
            JObject            result;
            MatrixRequestError error = mbackend.Post(String.Format("/_matrix/client/r0/join/{0}", System.Uri.EscapeDataString(roomid)), true, null, out result);

            if (error.IsOk)
            {
                roomid = result ["room_id"].ToObject <string> ();
                return(roomid);
            }
            else
            {
                return(null);
            }
        }
Beispiel #29
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
            }
        }
Beispiel #30
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
            }
        }