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.

229 lines
7.4 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
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(false);
}
catch(Exception ex)
{
// something bad happened
Console.WriteLine("Error!");
// display the error code and message
Console.WriteLine(ex.Message);
}
void RoomMenu(bool getPublic)
{
// prompt user to choose a room
// i guess we should find an alias for each room, if it exists
string roomList = "Select a room:";
List<string> rooms = new List<string>();
OrderedDictionary roomz = new OrderedDictionary();
if(getPublic)
{
// get public room directory
MatrixRoomDirectory pubRooms = session.GetPublicRooms();
// display first page of rooms
// I might complete this but rn I do not care enough
foreach(MatrixRoomEntry rm in pubRooms.Rooms)
{
if (rm.Alias != null)
{
rooms.Add(rm.Alias);
roomz.Add(rm.RoomId, rm.Alias);
}
else if (rm.Aliases != null)
{
rooms.Add(rm.Aliases.First());
roomz.Add(rm.RoomId, rm.Aliases.First());
}
else
{
rooms.Add(rm.RoomId);
roomz.Add(rm.RoomId, rm.RoomId);
}
}
}
else
{
MatrixUserRooms urooms = session.GetRooms();
foreach(string room in urooms.Joined)
{
roomz.Add(room, room);
}
rooms = urooms.Joined.ToList<string>();
}
for (int i = 0; i < rooms.Count(); i++)
{
string roomId = rooms[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];
roomz[roomId] = roomAliases.Aliases[0];
}
}
//roomList += "\n\n";
Console.WriteLine(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, /join to view a list of public rooms, or /mine to view a list of your joined rooms");
string stdIn = Console.ReadLine();
SlashInput(stdIn);
}
else
{
int roomListInt = Convert.ToInt32(roomListItem);
string theRoomId = roomz.Cast<DictionaryEntry>().ElementAt(1).Key.ToString();
string theRoomAlias = roomz[roomListInt].ToString();
// now we can join the room or something
MatrixRoom theRoom = session.JoinRoom(theRoomId);
// with this room we can sync for events
}
}
void SlashInput(string stdIn)
{
// read the input and figure out what to do with it
switch (stdIn)
{
case "/join":
RoomMenu(true);
break;
case "/mine":
RoomMenu(false);
break;
case "/quit":
System.Environment.Exit(0);
break;
}
}
}
}
}
//// simplest implementation
//// might not work for UWP
//// sauce: https://stackoverflow.com/questions/5527316/how-to-set-the-content-of-an-httpwebrequest-in-c
//HttpContent requestContent = new StringContent(requestJson, Encoding.UTF8, "application/json");
//HttpClient client = new HttpClient();
//client.BaseAddress = new Uri(loginUrl);
//client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//HttpResponseMessage responseMessage = client.PostAsync(loginUrl, requestContent).Result;
//string responseString = responseMessage.Content.ReadAsStringAsync().Result;
//if(responseString.Contains("errcode"))
//{
// // deserialize into error object
// MatrixError error = JsonConvert.DeserializeObject<MatrixError>(responseString);
// // convert error object to a string
// string errMsg = error.ErrorCode + ": " + error.ErrorMessage;
// // throw exception (can be caught and handled gracefully)
// throw new Exception(errMsg);
//}
//else
//{
// MatrixLoginResponse response = JsonConvert.DeserializeObject<MatrixLoginResponse>(responseString);
// return response;
//}