r105994 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105993‎ | r105994 | r105995 >
Date:08:23, 13 December 2011
Author:petrb
Status:deferred
Tags:
Comment:
Inserted some code it's work in progress this is irc bot, for some dev channels
Modified paths:
  • /trunk/tools/wmib/Program.cs (added) (history)

Diff [purge]

Index: trunk/tools/wmib/Program.cs
@@ -0,0 +1,383 @@
 2+//This program is free software: you can redistribute it and/or modify
 3+//it under the terms of the GNU General Public License as published by
 4+//the Free Software Foundation, either version 3 of the License, or
 5+//(at your option) any later version.
 6+
 7+//This program is distributed in the hope that it will be useful,
 8+//but WITHOUT ANY WARRANTY; without even the implied warranty of
 9+//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 10+//GNU General Public License for more details.
 11+
 12+// Created by Petr Bena benapetr@gmail.com
 13+
 14+using System;
 15+using System.Collections.Generic;
 16+using System.Linq;
 17+using System.Text;
 18+using System.Net;
 19+
 20+
 21+namespace wmib
 22+{
 23+ class Program
 24+ {
 25+ public static bool Log(string msg )
 26+ {
 27+ Console.WriteLine("LOG: " + msg);
 28+ return false;
 29+ }
 30+ static void Main(string[] args)
 31+ {
 32+ Log("Connecting");
 33+ irc.Connect();
 34+ }
 35+ }
 36+
 37+ public static class config
 38+ {
 39+ public class channel
 40+ {
 41+ public string name;
 42+ public bool logged;
 43+ public string log;
 44+ public irc.dictionary Keys = new irc.dictionary();
 45+ public irc.trust Users;
 46+ public channel(string Name)
 47+ {
 48+ logged = true;
 49+ name = Name;
 50+ log = Name + ".txt";
 51+ Keys.Load(name);
 52+ Users = new irc.trust(name);
 53+ }
 54+ }
 55+ /// <summary>
 56+ /// Network
 57+ /// </summary>
 58+ public static string network = "irc.freenode.net";
 59+ public static string username = "wm-bot";
 60+ /// <summary>
 61+ ///
 62+ /// </summary>
 63+ public static string version = "wikimedia bot v. 1.0.1";
 64+ /// <summary>
 65+ /// User name
 66+ /// </summary>
 67+ public static string name = "wm-bot";
 68+ /// <summary>
 69+ /// Channels
 70+ /// </summary>
 71+ public static channel[] channels = { new channel("#bhfsjhhdhgf"), new channel( "#wikimedia-testbot") };
 72+ }
 73+
 74+ public static class irc
 75+ {
 76+ private static System.Net.Sockets.NetworkStream data;
 77+ public static System.IO.StreamReader rd;
 78+ private static System.IO.StreamWriter wd;
 79+ private static List<user> User = new List<user>();
 80+
 81+ public class user
 82+ {
 83+ public user(string level, string name)
 84+ {
 85+ this.level = level;
 86+ this.name = name;
 87+ }
 88+ public string name;
 89+ public string level;
 90+ }
 91+
 92+ public class trust
 93+ {
 94+ private List<user> Users = new List<user>();
 95+ public string _Channel;
 96+ public string File;
 97+ public trust(string channel)
 98+ {
 99+ // Load
 100+ File = _Channel + "_user";
 101+ if (!System.IO.File.Exists(File))
 102+ {
 103+ // Create db
 104+ Program.Log("Creating user file for " + channel);
 105+ System.IO.File.WriteAllText(File, "");
 106+ }
 107+ string[] db = System.IO.File.ReadAllLines(channel + "_user");
 108+ this._Channel = channel;
 109+ foreach (string x in db)
 110+ {
 111+ if (x.Contains("|"))
 112+ {
 113+ string[] info = x.Split('|');
 114+ string level = info[1];
 115+ string name = info[0];
 116+ Users.Add(new user(level, name));
 117+ }
 118+ }
 119+ }
 120+ public bool Save()
 121+ {
 122+ System.IO.File.WriteAllText(File, "");
 123+ foreach (user u in Users)
 124+ {
 125+ System.IO.File.AppendAllText(File, u.name + "|" + u.level + "\n");
 126+ }
 127+ return true;
 128+ }
 129+
 130+ public bool addUser(string level, string user)
 131+ {
 132+ Users.Add(new user(level, user));
 133+ Save();
 134+ return true;
 135+ }
 136+
 137+ public bool delUser(string user)
 138+ {
 139+ foreach (user u in Users)
 140+ {
 141+ if (u.name == user)
 142+ {
 143+ if (u.level == "admin")
 144+ {
 145+ Message("This user is admin which can't be deleted from db, sorry", _Channel);
 146+ return true;
 147+ }
 148+ Users.Remove(u);
 149+ }
 150+ }
 151+ Save();
 152+ return true;
 153+ }
 154+
 155+ public user getUser(string user)
 156+ {
 157+ foreach (user b in Users)
 158+ {
 159+ if (b.name == user)
 160+ {
 161+ return b;
 162+ }
 163+ }
 164+ return new user("null", "");
 165+ }
 166+ public bool matchLevel(int level, string rights)
 167+ {
 168+ if (level == 2)
 169+ {
 170+ return (rights == "admin");
 171+ }
 172+ if (level == 1)
 173+ {
 174+ return (rights == "trusted" || rights == "admin");
 175+
 176+ }
 177+ return false;
 178+ }
 179+
 180+ public bool isApproved(string User, string Host, string command)
 181+ {
 182+ user current = getUser(User);
 183+ if (current.level == "null")
 184+ {
 185+ return false;
 186+ }
 187+
 188+ if (command == "alias_key")
 189+ {
 190+ return matchLevel(1, current.level);
 191+ }
 192+ if (command == "new_key")
 193+ {
 194+ return matchLevel(1, current.level);
 195+ }
 196+ if (command == "shutdown")
 197+ {
 198+ return matchLevel(1, current.level);
 199+ }
 200+ if (command == "delete_key")
 201+ {
 202+ return matchLevel(1, current.level);
 203+ }
 204+ if (command == "trust")
 205+ {
 206+ return matchLevel(1, current.level);
 207+ }
 208+ if (command == "trustadd")
 209+ {
 210+ return matchLevel(1, current.level);
 211+ }
 212+ if (command == "trustdel")
 213+ {
 214+ return matchLevel(1, current.level);
 215+ }
 216+ return false;
 217+ }
 218+ }
 219+
 220+ public class dictionary
 221+ {
 222+ public Dictionary<string, string> text = new Dictionary<string, string>();
 223+ public void Load(string channel)
 224+ {
 225+ string file = channel + ".db";
 226+ if (!System.IO.File.Exists(file))
 227+ {
 228+ // Create db
 229+ System.IO.File.WriteAllText(file, "");
 230+ }
 231+ }
 232+ public void setKey(string text, string key, string user, config.channel channel)
 233+ {
 234+
 235+ }
 236+ public void aliasKey(string key, string alias, string user, config.channel channel)
 237+ {
 238+
 239+ }
 240+ public void rmKey(string key, string user, config.channel channel)
 241+ {
 242+
 243+ }
 244+ }
 245+
 246+ public static config.channel getChannel(string name)
 247+ {
 248+ foreach (config.channel current in config.channels)
 249+ {
 250+ if (current.name == name)
 251+ {
 252+ return current;
 253+ }
 254+ }
 255+ return null;
 256+ }
 257+
 258+ public static bool Message(string message, string channel)
 259+ {
 260+ wd.WriteLine("PRIVMSG " + channel + " :" + message);
 261+ wd.Flush();
 262+ return true;
 263+ }
 264+
 265+ public static int modifyRights(string message, config.channel channel, string user, string host)
 266+ {
 267+ if (message.StartsWith("@trustadd"))
 268+ {
 269+ string[] rights_info = message.Split(' ');
 270+ if (channel.Users.isApproved(user, host, "trustadd"))
 271+ {
 272+ channel.Users.addUser(rights_info[2], rights_info[1]);
 273+ }
 274+ else
 275+ {
 276+ Message("You are not autorized to perform this, sorry", channel.name);
 277+
 278+ }
 279+ }
 280+ if (message.StartsWith("@trusted"))
 281+ {
 282+
 283+ }
 284+ if (message.StartsWith("@trustdel"))
 285+ {
 286+ string[] rights_info = message.Split(' ');
 287+ string x = rights_info[1];
 288+ if (channel.Users.isApproved(user, host, "trustdel"))
 289+ {
 290+ channel.Users.delUser(rights_info[1]);
 291+ }
 292+ else
 293+ {
 294+ Message("You are not autorized to perform this, sorry", channel.name);
 295+ }
 296+ }
 297+ return 0;
 298+ }
 299+
 300+ public static void chanLog(string message, config.channel channel, string user, string host)
 301+ {
 302+ if (channel.logged)
 303+ {
 304+ string log = "\n" + "[" + System.DateTime.Today.Hour + ":" + System.DateTime.Today.Minute + ":" + System.DateTime.Today.Second + "] " + "<" + user + "> " + message;
 305+ System.IO.File.AppendAllText(channel.log, log);
 306+ }
 307+ }
 308+
 309+ public static bool getMessage(string channel, string nick, string host, string message)
 310+ {
 311+ config.channel curr = getChannel(channel);
 312+ if (curr != null)
 313+ {
 314+ chanLog(message, curr, nick, host);
 315+ modifyRights(message, curr, nick, host);
 316+ }
 317+
 318+
 319+ return false;
 320+ }
 321+
 322+ public static int Connect()
 323+ {
 324+ data = new System.Net.Sockets.TcpClient(config.network, 6667).GetStream();
 325+ rd = new System.IO.StreamReader(data, System.Text.Encoding.UTF8);
 326+ wd = new System.IO.StreamWriter(data);
 327+
 328+ wd.WriteLine("USER " + config.name + " 8 * :" + config.name);
 329+ wd.WriteLine("NICK " + config.username);
 330+
 331+ //System.Threading.Thread.Sleep(2000);
 332+
 333+ foreach (config.channel ch in config.channels)
 334+ {
 335+ wd.WriteLine("JOIN " + ch.name);
 336+ }
 337+ wd.Flush();
 338+ string text = "";
 339+ string nick = "";
 340+ string host = "";
 341+ string message = "";
 342+ string channel = "";
 343+
 344+ while (true)
 345+ {
 346+ while (!rd.EndOfStream)
 347+ {
 348+ text = rd.ReadLine();
 349+ if (text.StartsWith(":"))
 350+ {
 351+ if (text.Contains("PRIVMSG"))
 352+ {
 353+ // we got a message here :)
 354+ if (text.Contains("!") && text.Contains("@"))
 355+ {
 356+ nick = text.Substring(1, text.IndexOf("!") - 1);
 357+ host = text.Substring(text.IndexOf("@") + 1, text.IndexOf(" ", text.IndexOf("@")) - 1 - text.IndexOf("@"));
 358+ }
 359+ if (text.Substring(text.IndexOf("PRIVMSG ", text.IndexOf(" "), text.IndexOf("PRIVMSG "))).Contains("#"))
 360+ {
 361+ channel = text.Substring(text.IndexOf("#"), text.IndexOf(" ", text.IndexOf("#")) - text.IndexOf("#"));
 362+ message = text.Substring(text.IndexOf("PRIVMSG"));
 363+ message = message.Substring(message.IndexOf(":") + 1);
 364+ getMessage(channel, nick, host, message);
 365+ }
 366+ else
 367+ {
 368+ // private message
 369+ }
 370+ }
 371+ }
 372+ Console.Write(text + "\n\n\n");
 373+ System.Threading.Thread.Sleep(50);
 374+ }
 375+ }
 376+ return 0;
 377+ }
 378+ public static int Disconnect()
 379+ {
 380+ wd.Flush();
 381+ return 0;
 382+ }
 383+ }
 384+}

Status & tagging log