Untitled


SUBMITTED BY: Guest

DATE: April 9, 2013, 8:32 p.m.

FORMAT: Text only

SIZE: 7.8 kB

HITS: 1147

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Diagnostics;
  9. namespace MuteBot
  10. {
  11. struct IrcUser {
  12. public string nick;
  13. public string user;
  14. public string host;
  15. public string channel;
  16. public double unMuteTime;
  17. }
  18. class MainClass
  19. {
  20. private static List<IrcUser> punishees = new List<IrcUser>();
  21. private static StreamWriter swrite;
  22. private static StreamReader sread;
  23. public static void Main (string[] args)
  24. {
  25. // Configurable bot vars
  26. string nickname = "NiiForcer";
  27. string server = "127.0.0.1";
  28. int port = 1025;
  29. // Socket vars
  30. NetworkStream sstream;
  31. TcpClient irc;
  32. // Generic bot vars
  33. bool botOn = true;
  34. string line; // incoming line
  35. string[] splitLine; // array of line, expoded by \s
  36. string fucker = "KickedByA";
  37. try
  38. {
  39. // Connect
  40. irc = new TcpClient(server, port);
  41. sstream = irc.GetStream();
  42. sread = new StreamReader(sstream);
  43. swrite = new StreamWriter(sstream);
  44. }
  45. catch (Exception e)
  46. {
  47. Console.WriteLine("Error connecting to {0}: {1}", server, e);
  48. return;
  49. }
  50. // Identify
  51. swrite.WriteLine("USER {0} {0} {0} :{1}", nickname, nickname);
  52. swrite.WriteLine("NICK {0}", nickname);
  53. swrite.WriteLine("PASS ShanCerv:shad0w"); //auth to znc
  54. swrite.Flush();
  55. while (botOn)
  56. {
  57. if ((line = sread.ReadLine()) != null)
  58. {
  59. Console.WriteLine(line);
  60. splitLine = line.Split(' ');
  61. if(splitLine[0].StartsWith(":GlaD0S!") || splitLine[0].StartsWith(":NiiForcer!")) {
  62. Console.WriteLine("Line ignored!");
  63. continue;
  64. }
  65. if (splitLine.Length > 0)
  66. {
  67. switch (splitLine[1])
  68. {
  69. case "366":
  70. Console.WriteLine("Connected to ponychat :)");
  71. break;
  72. case "MODE":
  73. if (splitLine[3] == "-v") {
  74. MuteUser(splitLine[4], splitLine[2], MakeUserFromString(line).nick);
  75. Console.WriteLine("MUTER: " + MakeUserFromString(line).nick);
  76. } else if (splitLine[3] == "+v") {
  77. UnMuteUser(splitLine[4], MakeUserFromString(line).nick);
  78. }
  79. break;
  80. case "JOIN":
  81. if(AllowVoiceUser(line)) {
  82. SendLine("MODE {0} +v {1}", new object[] {splitLine[2].Substring(1), MakeUserFromString(line).nick});
  83. Console.WriteLine("User {0} voiced", MakeUserFromString(line).nick);
  84. } else {
  85. //SendLine("NOTICE {0} :Please ask a mod to be voiced. in #tulpa.mods", MakeUserFromString(line).nick);
  86. //SendLine("PRIVMSG #tulpa.mods :mod mods admin swashy Purlox Shockk Tesseract: {0} has evaded a mute. ", MakeUserFromString(line).nick);
  87. }
  88. swrite.Flush();
  89. break;
  90. }
  91. //CheckExpiredMutes();
  92. if (splitLine[0] == "PING")
  93. {
  94. SendLine("PONG {0}", splitLine[1]);
  95. swrite.Flush();
  96. }
  97. }
  98. //CheckExpiredMutes();
  99. } else {
  100. //CheckExpiredMutes();
  101. }
  102. //CheckExpiredMutes();
  103. }
  104. // Clean up
  105. swrite.Close();
  106. sread.Close();
  107. irc.Close();
  108. }
  109. private static string GetSpokenLine(string line)
  110. {
  111. if (line.Split(':').Length >= 2)
  112. return line.Split(':')[2];
  113. return "";
  114. }
  115. private static IrcUser MakeUserFromString (string user)
  116. {
  117. IrcUser ret = new IrcUser();
  118. user = user.Split(' ')[0];
  119. int bang, at;
  120. bang = user.IndexOf("!");
  121. at = user.IndexOf("@");
  122. ret.nick = user.Substring(1,bang-1);
  123. ret.user = user.Substring(bang+1, (at-bang)-1);
  124. ret.host = user.Substring(at+1);
  125. return ret;
  126. }
  127. private static bool AllowVoiceUser (string line)
  128. {
  129. IrcUser joinee = MakeUserFromString (line);
  130. foreach (IrcUser user in punishees) {
  131. if (user.host.Equals (joinee.host)) {
  132. return false;
  133. }
  134. }
  135. return true;
  136. }
  137. private static IrcUser DoWhoisLookupOnUser (string nick)
  138. {
  139. IrcUser lookup = new IrcUser ();
  140. lookup.nick = nick;
  141. SendLine ("WHO {0}", nick);
  142. string whoLine = sread.ReadLine ();
  143. sread.ReadLine ();
  144. Console.Out.WriteLine ("Who: " + whoLine);
  145. string[] whoLineSplit = whoLine.Split (' ');
  146. if (whoLineSplit [2].Equals ("MODE")) {
  147. return DoWhoisLookupOnUser (nick);
  148. }
  149. try {
  150. lookup.user = whoLineSplit [4];
  151. lookup.host = whoLineSplit [5];
  152. } catch (Exception e) {
  153. e.ToString();
  154. return DoWhoisLookupOnUser(nick);
  155. }
  156. return lookup;
  157. }
  158. private static void SendLine(string line)
  159. {
  160. swrite.WriteLine(line);
  161. swrite.Flush();
  162. Console.WriteLine(">>> " + line);
  163. }
  164. private static void SendLine (string line, object thing)
  165. {
  166. SendLine(String.Format(line, new object[] {thing}));
  167. }
  168. private static void SendLine(string line, object[] stuff)
  169. {
  170. SendLine(String.Format(line, stuff));
  171. }
  172. private bool SameUser(IrcUser a, IrcUser b) {
  173. return (a.host.Equals(b.host));
  174. }
  175. private static void MuteUser (string nick, string channel, string muter)
  176. {
  177. IrcUser punishee = new IrcUser ();
  178. punishee.nick = nick;
  179. punishee.channel = channel;
  180. punishee = DoWhoisLookupOnUser (punishee.nick);
  181. if (punishees.Contains (punishee)) {
  182. SendLine("PRIVMSG #tulpa_moderation :{3} nearly tried to double mute {0}!{1}@{2} :< Report this bug to Niichan please", new object[] { punishee.nick, punishee.user, punishee.host, muter });
  183. return;
  184. }
  185. punishee.unMuteTime = unix_timestamp() + 300;
  186. punishees.Add(punishee);
  187. SendLine("NOTICE {0} :You have been devoiced, you may not speak in {1} until a moderator revoices you", new object[] {punishee.nick, channel});
  188. SendLine("MODE {1} -v {0}", new object[] {nick, channel});
  189. SendLine("PRIVMSG #tulpa_moderation :{0}!{1}@{2} added to devoice list by {3}.", new object[] { punishee.nick, punishee.user, punishee.host, muter });
  190. Console.WriteLine("{0}!{1}@{2} added to devoice list\n", new object[] { punishee.nick, punishee.user, punishee.host });
  191. }
  192. private static void UnMuteUser (string nick, string muter) {
  193. int i = 0;
  194. IrcUser toDelete = new IrcUser();
  195. toDelete.nick = nick;
  196. toDelete = DoWhoisLookupOnUser(toDelete.nick);
  197. foreach (IrcUser user in punishees) {
  198. if(user.host.Equals(toDelete.host)) {
  199. punishees.RemoveAt(i);
  200. SendLine("NOTICE {0} :You have been revoiced, you may speak again in #tulpa_ot", user.nick);
  201. SendLine("PRIVMSG #tulpa_moderation :{0}!{1}@{2} removed from devoice list by {3}.", new object[] { user.nick, user.user, user.host, muter });
  202. break;
  203. }
  204. i++;
  205. }
  206. }
  207. public static double unix_timestamp() {
  208. TimeSpan unix_time = (System.DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
  209. return unix_time.TotalSeconds;
  210. }
  211. }
  212. }

comments powered by Disqus