Untitled


SUBMITTED BY: Guest

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

FORMAT: C#

SIZE: 10.6 kB

HITS: 1190

  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. namespace MuteBot
  8. {
  9. struct IrcUser {
  10. public string nick;
  11. public string user;
  12. public string host;
  13. public string channel;
  14. public string muter;
  15. public double unMuteTime;
  16. }
  17. class MainClass
  18. {
  19. private static List<IrcUser> punishees = new List<IrcUser>();
  20. private static StreamWriter swrite;
  21. private static StreamReader sread;
  22. public static void Main (string[] args)
  23. {
  24. // Configurable bot vars
  25. string nickname = "NiiForcer";
  26. string server = "127.0.0.1";
  27. int port = 6668;
  28. // Socket vars
  29. NetworkStream sstream;
  30. TcpClient irc;
  31. // Generic bot vars
  32. bool botOn = true;
  33. string line; // incoming line
  34. string[] splitLine; // array of line, expoded by \s
  35. try
  36. {
  37. // Connect
  38. irc = new TcpClient(server, port);
  39. sstream = irc.GetStream();
  40. sread = new StreamReader(sstream);
  41. swrite = new StreamWriter(sstream);
  42. }
  43. catch (Exception e)
  44. {
  45. Console.WriteLine("Error connecting to {0}: {1}", server, e);
  46. return;
  47. }
  48. // Identify
  49. swrite.WriteLine("USER {0} {0} {0} :{1}", nickname, nickname);
  50. swrite.WriteLine("NICK {0}", nickname);
  51. //auth to znc
  52. swrite.Flush();
  53. SendLine("PRIVMSG #tulpa_moderation :Version flip made!");
  54. while (botOn)
  55. {
  56. if ((line = sread.ReadLine()) != null)
  57. {
  58. splitLine = line.Split(' ');
  59. if(splitLine[0].StartsWith(":GlaD0S!") || splitLine[0].StartsWith(":NiiForcer!")) {
  60. Console.WriteLine("Line ignored!");
  61. continue;
  62. }
  63. if (splitLine.Length > 0)
  64. {
  65. switch (splitLine[1])
  66. {
  67. case "KICK":
  68. //:swashy!~llehs.eih@Rizon-7396194D.om.om.cox.net KICK #tulpa.info Tulpa :Tulpa
  69. IrcUser kicker = MakeUserFromString(line);
  70. if (!kicker.nick.Equals("Yggdrasil")) {
  71. SendLine("PRIVMSG #tulpa_moderation :{0} kicked {1} from {2}", new object[] { kicker.nick, splitLine[3], splitLine[2] });
  72. } else {
  73. SendLine("PRIVMSG #tulpa_moderation :{0} was banned.", splitLine[3]);
  74. }
  75. break;
  76. case "366":
  77. Console.WriteLine("Connected to ponychat :)");
  78. break;
  79. case "MODE":
  80. if (splitLine[3] == "-v") {
  81. MuteUser(splitLine[4], splitLine[2], MakeUserFromString(line).nick);
  82. SendLine("NOTICE {0} :You should explain the reason for the mute of {1} in #tulpa_moderation along with duration. Thanks :)", new object[] { MakeUserFromString(line).nick, splitLine[4] });
  83. Console.WriteLine("MUTER: " + MakeUserFromString(line).nick);
  84. } else if (splitLine[3] == "+v") {
  85. UnMuteUser(splitLine[4], MakeUserFromString(line).nick);
  86. }
  87. break;
  88. case "JOIN":
  89. if(AllowVoiceUser(line)) {
  90. SendLine("MODE {0} +v {1}", new object[] {splitLine[2].Substring(1), MakeUserFromString(line).nick});
  91. Console.WriteLine("User {0} voiced", MakeUserFromString(line).nick);
  92. } else {
  93. SendLine("NOTICE {0} :You were muted. You will be unmuted when a mod thinks you should be.", MakeUserFromString(line).nick);
  94. SendLine("PRIVMSG #tulpa_moderation :Oh goody {0} has attempted to evade a mute. Shame.", MakeUserFromString(line).nick);
  95. }
  96. swrite.Flush();
  97. break;
  98. case "PRIVMSG":
  99. if(GetSpokenLine(line).Equals("!listprint") && splitLine[2].Equals("#tulpa_moderation")) {
  100. IrcUser s = MakeUserFromString(line);
  101. foreach(IrcUser user in punishees) {
  102. SendLine("PRIVMSG {4} :367 {0}!{1}@{2} on {3}\n", new object[] { user.nick, user.user, user.host, user.channel, s.nick });
  103. }
  104. SendLine("PRIVMSG {0} :368 End of channel mute list", s.nick);
  105. SendLine("PRIVMSG #tulpa_moderation :{0} Please see my many pm's", s.nick);
  106. }
  107. if(GetSpokenLine(line).Equals("!anhero") && splitLine[2].Equals("#tulpa_moderation")) {
  108. throw new Exception();
  109. }
  110. break;
  111. case "NICK":
  112. if(AllowVoiceUser(line)) {
  113. SendLine("MODE {0} +v {1}", new object[] {splitLine[2].Substring(1), MakeUserFromString(line).nick});
  114. Console.WriteLine("User {0} voiced", MakeUserFromString(line).nick);
  115. } else {
  116. SendLine("MODE {0} -v {1}", new object[] {splitLine[2].Substring(1), MakeUserFromString(line).nick});
  117. SendLine("NOTICE {0} :You were muted. You will be unmuted when a mod thinks you should be. Changing nicks to evade mutes is a bad idea.", MakeUserFromString(line).nick);
  118. SendLine("PRIVMSG #tulpa_moderation :Oh goody {0} has attempted to evade a mute by nickchange. Shame.", MakeUserFromString(line).nick);
  119. }
  120. swrite.Flush();
  121. break;
  122. }
  123. //CheckExpiredMutes();
  124. if (splitLine[0] == "PING")
  125. {
  126. SendLine("PONG {0}", splitLine[1]);
  127. swrite.Flush();
  128. }
  129. }
  130. Console.WriteLine(line);
  131. //CheckExpiredMutes();
  132. } else {
  133. //CheckExpiredMutes();
  134. }
  135. //CheckExpiredMutes();
  136. }
  137. // Clean up
  138. swrite.Close();
  139. sread.Close();
  140. irc.Close();
  141. }
  142. private static string GetSpokenLine(string line)
  143. {
  144. if (line.Split(':').Length >= 2)
  145. return line.Split(':')[2];
  146. return "";
  147. }
  148. private static IrcUser MakeUserFromString (string user)
  149. {
  150. IrcUser ret = new IrcUser();
  151. user = user.Split(' ')[0];
  152. int bang, at;
  153. bang = user.IndexOf("!");
  154. at = user.IndexOf("@");
  155. ret.nick = user.Substring(1,bang-1);
  156. ret.user = user.Substring(bang+1, (at-bang)-1);
  157. ret.host = user.Substring(at+1);
  158. return ret;
  159. }
  160. private static bool AllowVoiceUser (string line)
  161. {
  162. IrcUser joinee = MakeUserFromString (line);
  163. foreach (IrcUser user in punishees) {
  164. if (user.host.Equals (joinee.host) || user.nick.Equals (joinee.nick)) {
  165. return false;
  166. }
  167. }
  168. return true;
  169. }
  170. private static IrcUser DoWhoisLookupOnUser (string nick)
  171. {
  172. IrcUser lookup = new IrcUser ();
  173. lookup.nick = nick;
  174. SendLine ("WHO {0}", nick);
  175. string whoLine = sread.ReadLine ();
  176. sread.ReadLine ();
  177. Console.Out.WriteLine ("Who: " + whoLine);
  178. string[] whoLineSplit = whoLine.Split (' ');
  179. if (whoLineSplit [2].Equals ("MODE")) {
  180. return DoWhoisLookupOnUser (nick);
  181. }
  182. try {
  183. lookup.user = whoLineSplit [4];
  184. lookup.host = whoLineSplit [5];
  185. } catch (Exception e) {
  186. e.ToString();
  187. return DoWhoisLookupOnUser(nick);
  188. }
  189. return lookup;
  190. }
  191. private static void SendLine(string line)
  192. {
  193. swrite.WriteLine(line);
  194. swrite.Flush();
  195. Console.WriteLine(">>> " + line);
  196. }
  197. private static void SendLine (string line, object thing)
  198. {
  199. SendLine(String.Format(line, new object[] {thing}));
  200. }
  201. private static void SendLine(string line, object[] stuff)
  202. {
  203. SendLine(String.Format(line, stuff));
  204. }
  205. private bool SameUser(IrcUser a, IrcUser b) {
  206. return (a.host.Equals(b.host));
  207. }
  208. private static void MuteUser (string nick, string channel, string muter)
  209. {
  210. IrcUser punishee = new IrcUser ();
  211. punishee.nick = nick;
  212. punishee.channel = channel;
  213. punishee = DoWhoisLookupOnUser (punishee.nick);
  214. foreach(IrcUser user in punishees) {
  215. if (user.nick.Equals(punishee.nick) &&
  216. user.user.Equals(punishee.user) &&
  217. user.host.Equals(punishee.host))
  218. {
  219. return;
  220. }
  221. }
  222. punishee.unMuteTime = unix_timestamp() + 300;
  223. punishee.muter = muter;
  224. punishees.Add(punishee);
  225. SendLine("NOTICE {0} :You have been devoiced, you may not speak in {1} until a moderator revoices you", new object[] {punishee.nick, channel});
  226. SendLine("MODE {1} -v {0}", new object[] {nick, channel});
  227. DateTime unMuteTime = UnixTimeStampToDateTime(punishee.unMuteTime).ToUniversalTime();
  228. SendLine("PRIVMSG #tulpa_moderation :{0}!{1}@{2} added to devoice list by {3} on {4}, should be revoiced at {5}, {6} UCT.", new object[] { punishee.nick, punishee.user, punishee.host, muter, channel, unMuteTime.ToLongTimeString(), unMuteTime.ToLongDateString() });
  229. Console.WriteLine("{0}!{1}@{2} added to devoice list\n", new object[] { punishee.nick, punishee.user, punishee.host });
  230. }
  231. private static void UnMuteUser (string nick, string muter) {
  232. int i = 0;
  233. IrcUser toDelete = new IrcUser();
  234. toDelete.nick = nick;
  235. toDelete = DoWhoisLookupOnUser(toDelete.nick);
  236. foreach (IrcUser user in punishees) {
  237. if(user.host.Equals(toDelete.host) || user.nick.Equals(toDelete.nick)) {
  238. punishees.RemoveAt(i);
  239. SendLine("NOTICE {0} :You have been revoiced, you may speak again.", user.nick);
  240. SendLine("PRIVMSG #tulpa_moderation :{0}!{1}@{2} removed from devoice list by {3} on.", new object[] { user.nick, user.user, user.host, muter, user.channel });
  241. break;
  242. }
  243. i++;
  244. }
  245. }
  246. public static double unix_timestamp()
  247. {
  248. TimeSpan unix_time = (System.DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
  249. return unix_time.TotalSeconds;
  250. }
  251. public static DateTime UnixTimeStampToDateTime( double unixTimeStamp )
  252. {
  253. // Unix timestamp is seconds past epoch
  254. System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0);
  255. dtDateTime = dtDateTime.AddSeconds( unixTimeStamp ).ToLocalTime();
  256. return dtDateTime;
  257. }
  258. }
  259. }

comments powered by Disqus