public bool DispatchKeyDownEvent(System.Windows.Forms.Keys ckey) { if (KeyDownEvent == null) { return(false); } bool ret = false; foreach (KeyDownEventDelegate del in KeyDownEvent.GetInvocationList()) { try { ret = del(ckey); } catch (Exception e) { ChiConsole.WriteError("Error calling console key handler", e); } if (ret == true) { break; } } return(ret); }
public ColorMessage DispatchPromptEvent(ColorMessage colorPrompt) { if (PromptEvent == null) { return(colorPrompt); } foreach (PromptEventDelegate del in PromptEvent.GetInvocationList()) { try { colorPrompt = del(colorPrompt); } catch (Exception e) { ChiConsole.WriteError("Error calling prompt handler", e); } if (colorPrompt == null) { break; } } return(colorPrompt); }
static void RunPythonScript(string filePath) { string file = Path.GetFileName(filePath); EngineModule module = s_pythonEngine.CreateModule("<" + file + ">", false); try { string wrapCode = @"import sys try: execfile(r""" + filePath + @""") except Exception: etype, value, tb = sys.exc_info() print ""%s: %s"" % (etype.__name__, value) while tb is not None: f = tb.tb_frame lineno = tb.tb_lineno co = f.f_code filename = co.co_filename name = co.co_name print ' File ""%s"", line %d, in %s' % (filename,lineno,name) tb = tb.tb_next "; s_pythonEngine.Execute(wrapCode, module); } catch (Exception e) { ChiConsole.WriteError("Error running script " + file, e, file); } }
public ColorMessage DispatchOutputMessage(ColorMessage colorMessage) { if (OutputEvent == null) { return(colorMessage); } foreach (OutputEventDelegate del in OutputEvent.GetInvocationList()) { try { colorMessage = del(colorMessage); } catch (Exception e) { ChiConsole.WriteError("Error calling output handler", e); } if (colorMessage == null) { break; } } return(colorMessage); }
public string DispatchInputEvent(string input) { if (InputEvent == null) { return(input); } foreach (InputEventDelegate del in InputEvent.GetInvocationList()) { try { input = del(input); } catch (Exception e) { ChiConsole.WriteError("Error calling input handler", e); } if (input == null) { break; } } return(input); }
int HelpCmd(string input) { if (input.Length == 0) { ChiConsole.WriteLine("Commands"); ChiConsole.WriteLine("--------"); string[] keys = new string[m_commandMap.Count]; m_commandMap.Keys.CopyTo(keys, 0); Array.Sort <string>(keys); foreach (string key in keys) { if (m_commandMap[key].m_help != null) { ChiConsole.WriteLine("{0} - {1}", key, m_commandMap[key].m_help); } else { ChiConsole.WriteLine("{0}", key); } } } else { if (m_commandMap.ContainsKey(input)) { CommandData data = m_commandMap[input]; if (data.m_longhelp == null) { ChiConsole.WriteLine("No help available."); } else { ChiConsole.WriteLine(""); ChiConsole.WriteLine("/{0} - {1}", input, data.m_help); ChiConsole.WriteLine(""); ChiConsole.WriteLine(data.m_longhelp); } } else { ChiConsole.WriteLine("No such command."); } } return(0); }
public void DispatchTelnetEvent(Telnet.TelnetCodes code, Telnet.TelnetOpts opt) { if (TelnetEvent == null) { return; } try { TelnetEvent(code, opt); } catch (Exception e) { ChiConsole.WriteError("Error calling telnet handler", e); } }
public void DispatchConnectEvent(Exception exception) { if (ConnectEvent == null) { return; } try { ConnectEvent(exception); } catch (Exception e) { ChiConsole.WriteError("Error calling connect handler", e); } }
public void DispatchDisconnectEvent() { if (DisconnectEvent == null) { return; } try { DisconnectEvent(); } catch (Exception e) { ChiConsole.WriteError("Error calling disconnect handler", e); } }
public override void Write(byte[] buffer, int offset, int count) { string str = Encoding.Default.GetString(buffer, offset, count); m_stringBuilder.Append(str); string[] lines = m_stringBuilder.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None); if (lines.Length > 1) { for (int i = 0; i < lines.Length - 1; i++) { string line = lines[i]; ChiConsole.WriteLine("% " + line); } m_stringBuilder = new StringBuilder(lines[lines.Length - 1]); } }
public ColorMessage HandleColorMessage(ColorMessage colorMessage) { foreach (Trigger trigger in m_triggerList) { if (trigger.Regex == null) { continue; } Match match = trigger.Regex.Match(colorMessage.Text); if (match.Success == false) { continue; } bool gag = false; try { gag = trigger.Action(colorMessage, match, trigger.UserData); } catch (Exception e) { ChiConsole.WriteError("Failed to run trigger action for trigger ID " + trigger.TriggerID.ToString(), e); } if (trigger.IsGag || gag) { colorMessage = null; break; } if (trigger.IsFallThrough == false) { break; } } return(colorMessage); }
bool HandleKey(Keys key) { bool fallthrough = true; if (m_keyBindingMap.ContainsKey(key)) { KeyBinding binding = m_keyBindingMap[key]; try { fallthrough = binding.Run(); } catch (Exception e) { ChiConsole.WriteError("Error calling key handler for key " + key.ToString(), e); fallthrough = false; } } return(!fallthrough); }
static void RunChiScript(string filePath) { string file = Path.GetFileName(filePath); string[] lines = File.ReadAllLines(filePath); int lineNum = 1; foreach (string line in lines) { if (line.Length > 0 && line[0] != '#') { int res = s_commandManager.RunCommand(line); if (res != 0) { ChiConsole.WriteLine("{0}({1}): Command failed", file, lineNum); break; } } lineNum++; } }
public static void RunScript(string file) { string filePath = ExpandPath(file); if (filePath == null) { ChiConsole.WriteLine("Script not found: {0}", file); return; } if (Path.GetExtension(filePath) == ".py") { RunPythonScript(filePath); } else if (Path.GetExtension(filePath) == ".bc") { RunChiScript(filePath); } else { ChiConsole.WriteLine("Script not found: {0}", file); } }
public int RunCommand(string input) { string cmd; if (input.Length == 0) { return(0); } if (input[0] != '/') { ChiConsole.WriteLine("Input is not a command: {0}", input); return(-1); } int idx = input.IndexOf(' '); if (idx == -1) { cmd = input.Substring(1); input = ""; } else { cmd = input.Substring(1, idx - 1); input = input.Substring(idx + 1); } if (!m_commandMap.ContainsKey(cmd)) { List <string> l = new List <string>(); foreach (string c in m_commandMap.Keys) { if (c.StartsWith(cmd)) { l.Add(c); } } if (l.Count == 0) { ChiConsole.WriteLine("Unknown command {0}", cmd); return(-1); } else if (l.Count > 1) { ChiConsole.WriteLine("Ambigious command. ({0})", String.Join(", ", l.ToArray())); return(-1); } else { cmd = l[0]; } } CommandData cmdData = m_commandMap[cmd]; int res = -1; try { res = cmdData.m_handler(input); } catch (Exception e) { ChiConsole.WriteError("Error handling command " + cmd, e); } return(res); }
private void HandleIAC(byte[] data, int len, ref int pos, ref int scanned) { #if TELNET_DEBUG ChiConsole.WriteLine(((TelnetCodes)data[pos]).ToString() + " "); #endif if (pos + 1 >= len) { #if TELNET_DEBUG ChiConsole.WriteLine("Uncomplete telnet sequence"); #endif // skip IAC. We will try again when we receive more data pos++; return; } if (System.Enum.IsDefined(TelnetCodes.ABORT.GetType(), data[pos + 1]) == false) { ChiConsole.WriteLine("Unknown telnet code " + data[pos + 1]); // consume IAC pos++; scanned = pos; return; } TelnetCodes telCode = (TelnetCodes)data[pos + 1]; #if TELNET_DEBUG ChiConsole.WriteLine(telCode.ToString()); #endif if (telCode == TelnetCodes.DO || telCode == TelnetCodes.DONT || telCode == TelnetCodes.WILL || telCode == TelnetCodes.WONT) { if (pos + 2 >= len) { #if TELNET_DEBUG ChiConsole.WriteLine("Uncomplete telnet sequence"); #endif // skip IAC and DO/DONT. We will try again when we receive more data pos += 2; return; } if (System.Enum.IsDefined(TelnetOpts.TELOPT_ECHO.GetType(), data[pos + 2]) == false) { ChiConsole.WriteLine("Unknown telnet option " + data[pos + 2]); // consume IAC pos++; scanned = pos; return; } TelnetOpts telOpt = (TelnetOpts)data[pos + 2]; #if TELNET_DEBUG ChiConsole.WriteLine(telOpt.ToString()); #endif if (telnetEvent != null) { telnetEvent(telCode, telOpt); } pos += 3; scanned = pos; return; } else if (telCode == TelnetCodes.GA) { string str = m_stringBuilder.ToString(); if (promptEvent != null) { promptEvent(str); } m_stringBuilder = new StringBuilder(); pos += 2; scanned = pos; return; } else { if (telnetEvent != null) { telnetEvent(telCode, 0); } pos += 2; scanned = pos; return; } }
public static ColorMessage ParseAnsi(string text, ref AnsiTextStyle currentStyle) { StringBuilder stringBuilder = new StringBuilder(text.Length); List <ColorMessage.MetaData> metaData = new List <ColorMessage.MetaData>(); int pos = 0; int oldPos = 0; AnsiTextStyle previousStyle = currentStyle; if (!currentStyle.IsEmpty) { ColorMessage.MetaData md = new ColorMessage.MetaData(stringBuilder.Length, currentStyle.ToTextStyle()); metaData.Add(md); } while (pos < text.Length) { if (text[pos] == '\t') { stringBuilder.Append(' ', 4); pos++; continue; } if (text[pos] != ESC) { stringBuilder.Append(text[pos]); pos++; continue; } oldPos = pos; pos++; // skip ESC if (pos >= text.Length) { stringBuilder.Append(text.Substring(oldPos, pos - oldPos)); continue; } if (text[pos] == '[') { pos++; // skip [ if (pos >= text.Length) { ChiConsole.WriteLineLow("Incomplete ansi sequence"); stringBuilder.Append(text.Substring(oldPos, pos - oldPos)); continue; } int seqStart = pos; while (pos < text.Length && ((text[pos] >= '0' && text[pos] <= '9') || text[pos] == ';')) { pos++; } if (pos == text.Length) { ChiConsole.WriteLineLow("Incomplete ansi sequence"); stringBuilder.Append(text.Substring(oldPos, pos - oldPos)); continue; } if (text[pos] == 'm') { int seqEnd = pos; pos++; // skip m string str2 = text.Substring(seqStart, seqEnd - seqStart); string[] arr = str2.Split(';'); if (str2.Length == 0) { arr = new string[] { "0" } } ; for (int i = 0; i < arr.Length; i++) { int num = System.Int16.Parse(arr[i]); switch (num) { case 0: // normal currentStyle.Fg = -1; currentStyle.Bg = -1; currentStyle.IsBold = false; currentStyle.IsInverse = false; break; case 1: // bold currentStyle.IsBold = true; break; case 7: // inverse currentStyle.IsInverse = true; break; case 30: case 31: case 32: case 33: case 34: case 35: case 36: case 37: currentStyle.Fg = num - 30; break; case 38: if (arr.Length != 3 || i != 0 || arr[1] != "5") { ChiConsole.WriteLineLow("Illegal 256 color ansi code: {0}", str2); break; } currentStyle.Fg = byte.Parse(arr[2]); i += 3; break; case 39: // default color currentStyle.Fg = -1; break; case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: currentStyle.Bg = num - 40; break; case 48: if (arr.Length != 3 || i != 0 || arr[1] != "5") { ChiConsole.WriteLineLow("Illegal 256 color ansi code: {0}", str2); break; } currentStyle.Bg = byte.Parse(arr[2]); i += 3; break; case 49: // default color currentStyle.Bg = -1; break; default: ChiConsole.WriteLineLow("Unknown ansi code {0}", num); break; } } if (!previousStyle.IsSame(currentStyle)) { ColorMessage.MetaData md = new ColorMessage.MetaData(stringBuilder.Length, currentStyle.ToTextStyle()); metaData.Add(md); } previousStyle = currentStyle; } else if (text[pos] == 'H') { pos++; } else if (text[pos] == 'J') { pos++; } else { ChiConsole.WriteLine("Unknown ansi command: {0}", text[pos]); } } } return(new ColorMessage(stringBuilder.ToString(), metaData)); }