-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
147 lines (113 loc) · 5.17 KB
/
Program.cs
File metadata and controls
147 lines (113 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System.Runtime.CompilerServices;
using M = Spectre.Console.Markup;
IDisposable responseMessageDataReadySubscription;
Write(new FigletText("NG-Bot v1.0").LeftJustified().Color(Color.Purple));
MarkupLineInterpolated($"[grey]{new string('-', System.Console.WindowWidth)}[/]");
var serverManager = new ServerManager();
var messageProcessor = new CommandProcessor();
await serverManager.StartAsync();
var client = new DeltaRpcClient(serverManager.ServerProcess.StandardInput, serverManager.ServerProcess.StandardOutput);
var systemInfo = client.GetSystemInfo();
MarkupLineInterpolated($"[yellow]{M.Escape(systemInfo.DeltaChatCoreVersion)}[/] / x{M.Escape(systemInfo.Architecture ?? "")} / {M.Escape(systemInfo.CpuCount.ToString())} Threads");
int currentAccountId;
var accountIds = client.GetAllAccountIds();
MarkupLine("[red]Ignoreing all accounts but the first one for now[/]");
if (accountIds.Length == 0)
{
MarkupLine("[red]No accounts have been configured yet, adding now[/]");
currentAccountId = client.AddAccount();
//The idea here is that you use the official client to create an account
//and then backup that account to a .tar file. Next you call ImportBackup
//with the path to that tar file in order to import the account
//You only have to do this once.
//client.ImportBackup(currentAccountId, "enter path to your backup here", "");
}
else
{
currentAccountId = accountIds[0];
MarkupLineInterpolated($"[yellow]Using existing account: {currentAccountId}[/]");
}
client.ConfigureAccountSetting(currentAccountId, AccountSetting.BOT, "1");
var isAccountConfigured = client.IsAccountConfigured(currentAccountId);
//If you did not import from a backup the account may not yet be configured
//It's possible to configure the e-mail address and password, but that won't
//actually work since this method does not take into account the public/private keys
//It's added here more as an example
// if (!isAccountConfigured)
// {
// MarkupLine("[red]The current account has not yet been configured[/]");
// client.ConfigureAccountSetting(currentAccountId, AccountSetting.MAIL_ADDRESS, "");
// client.ConfigureAccountSetting(currentAccountId, AccountSetting.MAIL_PASSWORD, "");
// MarkupLineInterpolated($"[yellow]Mail Adddress and Password set[/]");
// var result = client.ConfigureAccount(currentAccountId);
// isAccountConfigured = client.IsAccountConfigured(currentAccountId);
// if (isAccountConfigured)
// {
// MarkupLine("[yellow]The account is now configured[/]");
// }
// else
// {
// //If your account still isn't configured at this point
// //There's not much else we can do, so we just exit.
// MarkupLine("[red]Failed to configure the acccount.[/], Press any key to exit.");
// System.Console.ReadKey();
// Environment.Exit(1);
// }
// }
responseMessageDataReadySubscription = messageProcessor.ResponseMessageReady.Subscribe(HandleResponseMessage);
client.StartIOForAccount(currentAccountId);
while (true)
{
try
{
var deltaEvent = client.GetNextEvent();
switch (deltaEvent.@event.kind.ToString())
{
case EventType.INCOMING_MSG:
await ProcessMessage(deltaEvent.@event.msgId);
break;
case EventType.INFO:
MarkupLineInterpolated($"{M.Escape(DateTime.Now.ToShortTimeString())}: {M.Escape(deltaEvent.@event.msg.ToString())}");
break;
case EventType.WARNING:
MarkupLineInterpolated($"[red3_1]{M.Escape(DateTime.Now.ToShortTimeString())}[/]: {M.Escape(deltaEvent.@event.msg.ToString())}");
break;
case EventType.ERROR:
MarkupLineInterpolated($"[red1]{M.Escape(DateTime.Now.ToShortTimeString())} [/]: {M.Escape(deltaEvent.@event.msg.ToString())}");
break;
case EventType.CONNECTIVITY_CHANGED:
MarkupLineInterpolated($"[darkgoldenrod]Connectivity changed[/]");
break;
case EventType.IMAP_INBOX_IDLE:
MarkupLineInterpolated($"[lightsteelblue1]IMAP indbox idle[/]");
break;
case EventType.INCOMING_MSG_BUNCH:
MarkupLine("Incoming message bunch");
break;
default:
MarkupLineInterpolated($"[lightgoldenrod1]Unknown event kind[/]: {M.Escape(deltaEvent.@event.kind.ToString())}");
break;
}
}
catch (Exception ex)
{
MarkupLineInterpolated($"Unhandled exception while handling event: {ex.ToString()}");
}
}
async Task ProcessMessage(int messageId)
{
var chatMessage = client.GetMessage(currentAccountId, messageId);
if (chatMessage.FromId != SpecialContactId.Self && !chatMessage.IsBot && !chatMessage.IsInfo)
{
await messageProcessor.ProcessChatMessageAsync(chatMessage);
}
else
{
MarkupLineInterpolated($"[fuchsia]{M.Escape(chatMessage.Text)}[/]");
}
}
void HandleResponseMessage(MessageDataAndChatId messageDataAndChatId)
{
client.SendMessage(currentAccountId, messageDataAndChatId.ChatId, messageDataAndChatId.messageData);
}