A native .NET C# library for developing Matrix clients.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.2 KiB

using System;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using MatrixDotNetLib;
using Newtonsoft.Json;
namespace MatrixDotNetCmd
{
class Program
{
static void Main(string[] args)
{
// start output
Console.WriteLine("MatrixDotNetLib v0.1 - A .NET Core library for Matrix");
// create a new Matrix session
MatrixSessionManager session = new MatrixSessionManager();
// input server
Console.Write("Enter Matrix server FQDN:");
string server = Console.ReadLine();
// input username
Console.Write("Enter Matrix username:");
string user = Console.ReadLine();
// input pass
string pass = "";
Console.Write("Enter Matrix password: ");
// capture key input and replace with * in console session
ConsoleKeyInfo key;
do
{
key = Console.ReadKey(true);
// Backspace Should Not Work
if (key.Key != ConsoleKey.Backspace)
{
pass += key.KeyChar;
Console.Write("*");
}
else
{
Console.Write("\b");
}
}
// stop once user hits Enter
while (key.Key != ConsoleKey.Enter);
pass = pass.Trim();
Console.WriteLine();
Console.WriteLine("Logging in...");
// log in to Matrix and get a token
// response.Token
try
{
// create a new response object; trim password to remove added \r character
MatrixLoginResponse response = session.Login(server, user, pass);
// notify user is logged in
Console.WriteLine("Logged in!");
// show the room menu
RoomMenu();
}
catch(Exception ex)
{
// something bad happened
Console.WriteLine("Error!");
// display the error code and message
Console.WriteLine(ex.Message);
}
void RoomMenu()
{
MatrixUserRooms rooms = session.GetRooms();
// prompt user to choose a room
// i guess we should find an alias for each room, if it exists
string roomList = "Select a room:";
for (int i = 0; i < rooms.Joined.Count(); i++)
{
string roomId = rooms.Joined[i];
// get the aliases for the room
MatrixRoomAliases roomAliases = session.GetRoomAliases(roomId);
if (roomAliases.Aliases.Count() == 0)
{
roomList += "\n[" + i + "] " + roomId;
}
else
{
roomList += "\n[" + i + "] " + roomAliases.Aliases[0];
}
}
roomList += "\n";
Console.Write(roomList);
string roomListItem = Console.ReadLine();
if (roomListItem.Trim() == "")
{
// user didn't select a room, now what?
Console.WriteLine("You didn't select a room. Type /join #roomId to join a room, or /join to view a list of public rooms.");
string stdIn = Console.ReadLine();
SlashInput(stdIn);
}
else
{
int roomListInt = Convert.ToInt32(roomListItem);
string theRoomId = rooms.Joined[roomListInt];
}
}
void SlashInput(string stdIn)
{
// read the input and figure out what to do with it
switch (stdIn)
{
case "/join":
RoomMenu();
break;
case "":
break;
}
}
}
}
}