Skip to content

Commit 387d5de

Browse files
committed
refactor notice handling
1 parent 4290e42 commit 387d5de

4 files changed

Lines changed: 6 additions & 97 deletions

File tree

MedalBOT/BotContext.cs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,6 @@ public class BotContext
5353
public Dictionary<string, DateTime> TimedMutes { get; set; }
5454
= new(StringComparer.OrdinalIgnoreCase);
5555

56-
// Track pending service requests (command ID → requester info)
57-
public Dictionary<string, (string RequesterNick, bool IsDiscord)> PendingServiceRequests { get; set; }
58-
= new();
59-
60-
// Buffer for accumulating ChanServ responses
61-
public Dictionary<string, System.Collections.Generic.List<string>> ServiceResponseBuffer { get; set; }
62-
= new();
63-
64-
// Track timeout for each pending request (requestId → lastReceivedTime)
65-
public Dictionary<string, DateTime> ServiceResponseTimeouts { get; set; }
66-
= new();
67-
6856
private readonly object _voicedLock = new();
6957
private readonly object _muteLock = new();
7058
private const string MutedIdsFile = "muted_ids.txt";
@@ -263,25 +251,5 @@ private string ExtractIdent(string hostmask)
263251
if (at <= 0) return null;
264252
return hostmask.Substring(0, at);
265253
}
266-
267-
public void TrackServiceRequest(string commandId, string requesterNick, bool isDiscord)
268-
{
269-
PendingServiceRequests[commandId] = (requesterNick, isDiscord);
270-
}
271-
272-
public bool GetServiceRequest(string commandId, out string requesterNick, out bool isDiscord)
273-
{
274-
requesterNick = null;
275-
isDiscord = false;
276-
277-
if (PendingServiceRequests.TryGetValue(commandId, out var request))
278-
{
279-
requesterNick = request.RequesterNick;
280-
isDiscord = request.IsDiscord;
281-
PendingServiceRequests.Remove(commandId);
282-
return true;
283-
}
284-
return false;
285-
}
286254
}
287255
}

MedalBOT/Commands/ChanServCommand.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,8 @@ public class ChanServCommand : ICommand
6464

6565
private (bool, string) HandleBanList(BotContext ctx, string senderNick, bool isDiscord)
6666
{
67-
string commandId = $"blist_{DateTime.UtcNow.Ticks}_{senderNick}";
68-
ctx.TrackServiceRequest(commandId, senderNick, isDiscord);
69-
ctx.ServiceResponseTimeouts[commandId] = DateTime.UtcNow;
7067
ctx.Writer?.WriteLine($"PRIVMSG ChanServ :bans {ctx.Channel}");
71-
ctx.Logger?.Log($"[CHANSERV] {senderNick} requested banlist for {ctx.Channel} (ID: {commandId})");
68+
ctx.Logger?.Log($"[CHANSERV] {senderNick} requested banlist for {ctx.Channel}");
7269
return (false, null);
7370
}
7471

MedalBOT/Commands/SpamServCommand.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,8 @@ public class SpamServCommand : ICommand
7878

7979
private (bool, string) HandleWordList(BotContext ctx, string senderNick, bool isDiscord)
8080
{
81-
string commandId = $"wordlist_{DateTime.UtcNow.Ticks}_{senderNick}";
82-
ctx.TrackServiceRequest(commandId, senderNick, isDiscord);
83-
ctx.ServiceResponseTimeouts[commandId] = DateTime.UtcNow;
8481
ctx.Writer?.WriteLine($"PRIVMSG SpamServ :listbadwords {ctx.Channel}");
85-
ctx.Logger?.Log($"[SPAMSERV] {senderNick} requested badwords list for {ctx.Channel} (ID: {commandId})");
82+
ctx.Logger?.Log($"[SPAMSERV] {senderNick} requested badwords list for {ctx.Channel}");
8683
return (false, null);
8784
}
8885

MedalBOT/Program.cs

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -140,71 +140,18 @@ static async Task Main()
140140
ParseWhoResponse(ctx, line);
141141
}
142142

143-
// Capture ChanServ/SpamServ NOTICE replies (sent to bot nick for service requests)
144-
if (line.Contains("NOTICE") && (line.Contains("ChanServ") || line.Contains("SpamServ")) && ctx.PendingServiceRequests.Count > 0)
143+
// Forward ChanServ/SpamServ NOTICE messages directly to Discord
144+
if (line.Contains("NOTICE") && (line.Contains("ChanServ") || line.Contains("SpamServ")))
145145
{
146146
string noticeContent = MessageParser.GetMessage(line);
147147
if (!string.IsNullOrWhiteSpace(noticeContent))
148148
{
149-
// Append to ALL active request buffers (ChanServ replies are sequential)
150-
foreach (var requestId in ctx.PendingServiceRequests.Keys.ToList())
151-
{
152-
// Initialize buffer if needed
153-
if (!ctx.ServiceResponseBuffer.ContainsKey(requestId))
154-
ctx.ServiceResponseBuffer[requestId] = new System.Collections.Generic.List<string>();
155-
156-
// Add to buffer
157-
ctx.ServiceResponseBuffer[requestId].Add(noticeContent);
158-
159-
// Update timeout for this request
160-
ctx.ServiceResponseTimeouts[requestId] = DateTime.UtcNow;
161-
}
162-
163-
ctx.Logger?.Log($"[SERVICE] Buffering response to {ctx.PendingServiceRequests.Count} active request(s): {noticeContent}");
149+
ctx.Logger?.Log($"[SERVICE NOTICE] {noticeContent}");
150+
await ctx.Discord?.SendMessage($"[SERVICE] {noticeContent}");
164151
}
165152
continue;
166153
}
167154

168-
// Check for timed-out service responses (2 second timeout)
169-
const int timeoutMs = 2000;
170-
var expiredRequests = ctx.ServiceResponseTimeouts
171-
.Where(kvp => (DateTime.UtcNow - kvp.Value).TotalMilliseconds > timeoutMs)
172-
.Select(kvp => kvp.Key)
173-
.ToList();
174-
175-
foreach (var requestId in expiredRequests)
176-
{
177-
if (ctx.PendingServiceRequests.TryGetValue(requestId, out var requestInfo) &&
178-
ctx.ServiceResponseBuffer.TryGetValue(requestId, out var responses) &&
179-
responses.Count > 0)
180-
{
181-
string requesterNick = requestInfo.RequesterNick;
182-
bool isDiscord = requestInfo.IsDiscord;
183-
184-
ctx.Logger?.Log($"[SERVICE TIMEOUT] Request {requestId} complete: {responses.Count} lines for {requesterNick}");
185-
186-
string fullResponse = string.Join("\n", responses);
187-
188-
if (isDiscord)
189-
{
190-
ctx.Logger?.Log($"[SERVICE RELAY] Sending to Discord for {requesterNick}...");
191-
await ctx.Discord?.SendMessage($"**{requesterNick}**:\n```\n{fullResponse}\n```");
192-
}
193-
else
194-
{
195-
foreach (var resp in responses)
196-
{
197-
writer.WriteLine($"PRIVMSG {ctx.Channel} :{resp}");
198-
}
199-
}
200-
201-
// Clean up this specific request
202-
ctx.PendingServiceRequests.Remove(requestId);
203-
ctx.ServiceResponseBuffer.Remove(requestId);
204-
ctx.ServiceResponseTimeouts.Remove(requestId);
205-
}
206-
}
207-
208155
if (line.Contains(" QUIT "))
209156
{
210157
string quitter = MessageParser.GetNick(line);

0 commit comments

Comments
 (0)