Exemple #1
0
        private RoomsViewModel GetUserRooms(User user)
        {
            string       homeserverUrl = "https://matrix.org";
            MatrixClient client;
            string       username = user.Username;
            string       password = user.Password;

            client = new MatrixClient(homeserverUrl);
            MatrixLoginResponse login = client.LoginWithPassword(username, password);


            client.StartSync();
            RoomsViewModel rmv = new RoomsViewModel();

            rmv.rooms = client.GetAllRooms();


            return(rmv);
        }
Exemple #2
0
        public ActionResult Read(string ID)
        {
            //RoomsViewModel rooms = TempData.Get<RoomsViewModel>("key");
            //MatrixAPI api = new MatrixAPI("https://matrix.org");
            //MessagesViewModel mvm = new MessagesViewModel();
            //ChunkedMessages messages = api.GetRoomMessages(ID);

            string userName = ViewData["userName"].ToString();
            string token    = ViewData["token"].ToString();

            string       homeserverUrl = "https://matrix.org";
            MatrixClient client;

            client = new MatrixClient(homeserverUrl);
            client.LoginWithToken(userName, token);

            RoomsViewModel rmv = new RoomsViewModel();

            rmv.rooms = client.GetAllRooms();
            // mvm.messages = ID.Messages;


            return(View());
        }
Exemple #3
0
    public void MatrixRoutine()
    {
        var storage = new Storage();

        client = new MatrixClient(storage.Server);
        cache  = new Cache(client);

        try{
            client.UseExistingToken(storage.UserId, storage.Token);
            this.user = client.GetUser();
            Gtk.Application.Invoke(delegate {
                var statusContext = statusbar.GetContextId("matrix");
                statusbar.Push(statusContext, "Logged in to " + storage.Server + " as " + storage.UserId);
                displayName.Text = user.DisplayName;
            });
        }catch (Exception loginException) {
            Gtk.Application.Invoke(delegate {
                var statusContext = statusbar.GetContextId("matrix");
                statusbar.Push(statusContext, "Login failed: " + loginException.Message);
            });

            storage.Token = null;
            storage.Save();
        }

        var rooms = client.GetAllRooms();

        channelList.AppendColumn("Icon", new Gtk.CellRendererPixbuf(), "pixbuf", 0);
        channelList.AppendColumn("Artist", new Gtk.CellRendererText(), "text", 1);


        var liststore = new ListStore(typeof(Gdk.Pixbuf), typeof(string));

        int roomIndex = 0;

        foreach (var channel in rooms)
        {
            var label = getRoomLabel(channel);
            var icon  = AvatarGenerator.createRoomAvatar(label, channel.Encryption != null);
            liststore.AppendValues(icon, label);
            this.rooms.Add(roomIndex, channel);
            roomIndex++;

            foreach (var member in channel.Members)
            {
                if (!this.users.ContainsKey(member.Key))
                {
                    this.users.Add(member.Key, member.Value);
                }
            }
        }

        channelList.Model = liststore;
        var avatar       = cache.GetAvatarObject(user.AvatarURL);
        var avatarScaled = Utils.resizeImage(System.Drawing.Image.FromStream(avatar), 48, 48);

        profileImage.Pixbuf = Utils.bitmapToPixbuf(avatarScaled);

        foreach (var member in this.users)
        {
            if (member.Value.avatar_url != null)
            {
                var memberAvatar = cache.GetAvatarObject(member.Value.avatar_url);
                var scaled       = Utils.resizeImage(System.Drawing.Image.FromStream(memberAvatar), 32, 32);
                this.avatars.Add(member.Key, scaled);
            }
        }
        refreshRoomList();
        Console.WriteLine("Matrix thread done");
    }
Exemple #4
0
        public MatrixBot(
            string cmdprefix,
            string host,
            string user,
            string pass,
            string[] rooms,
            bool AutoAcceptInvite = false,
            StatusOptions statusOptions = null
            )
        {
            prefix = cmdprefix;
            Client = new MatrixClient (host);
            Client.AddStateEventType ("uk.half-shot.status", typeof(HSStatusEvent));

            this.AutoAcceptInvite = AutoAcceptInvite;

            Client.OnInvite += (roomid, joined) => {
                if (AutoAcceptInvite) {
                    MatrixRoom room = Client.JoinRoom (roomid);
                    if (room != null) {
                        room.OnMessage += Room_OnMessage;
                    }

                }
            };

            Client.LoginWithPassword (user, pass);

            MatrixRoom[] existingRooms = Client.GetAllRooms ();
            foreach (string roomid in rooms) {
                MatrixRoom room = roomid.StartsWith ("#") ? Client.GetRoomByAlias (roomid) : Client.GetRoom (roomid);
                if (existingRooms.Contains (room)) {
                    continue;
                }
                if (room == null) {
                    room = Client.JoinRoom (roomid);
                    if (room != null) {
                        Console.WriteLine ("\tJoined " + roomid);
                    } else {
                        Console.WriteLine ("\tCouldn't find " + roomid);
                    }
                }
            }

            existingRooms = Client.GetAllRooms ();
            foreach (MatrixRoom room in existingRooms) {
                room.OnMessage += Room_OnMessage;
            }

            //Find commands
            foreach (MethodInfo method in typeof(T).GetMethods(BindingFlags.Static|BindingFlags.Public)) {
                BotCmd cmd = method.GetCustomAttribute<BotCmd> ();
                if (cmd != null) {
                    Cmds.Add (cmd, method);
                }
                if (method.GetCustomAttribute<BotFallback> () != null) {
                    if (fallback != null) {
                        Console.WriteLine ("WARN: You have more than one fallback command set, overwriting previous");
                    }
                    fallback = method;
                }
            }

            if (statusOptions != null && statusOptions.StartAutomatically) {
                //Start Status Thread.
                statusOpts = statusOptions;
                StartStatusReporting(statusOptions.IntervalMinutes);

            }
        }
Exemple #5
0
 public void NotifyAll(string text)
 {
     foreach(MatrixRoom room in Client.GetAllRooms()){
         room.SendNotice(text);
     }
 }