From cb46a86567b6b71aa3254712d1f65f6a652d6ff7 Mon Sep 17 00:00:00 2001 From: Fanir Date: Thu, 27 Feb 2014 12:05:38 +0100 Subject: [PATCH] initial commit --- main.py | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100755 main.py diff --git a/main.py b/main.py new file mode 100755 index 0000000..0d3bfb8 --- /dev/null +++ b/main.py @@ -0,0 +1,142 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# main.py +# +# Copyright 2014 Fanir +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. +# +# + + +import sys +import socket +import string + + +class pircbot(): + def encode(self, textstring): + for codec in self.encodings: + try: return textstring.encode(codec) + except UnicodeDecodeError: continue + return textstring.encode(self.encodings[0], 'ignore') + + def decode(self, textstring): + for codec in self.encodings: + try: return textstring.decode(codec) + except UnicodeDecodeError: continue + return textstring.decode(self.encodings[0], 'ignore') + + connected = False + + def __init__(self, nicknames, server, port=6667, ident="pircbot", realname="pircbot", encodings=("utf-8", "latin-1")): + self.nicknames = nicknames + self.server = server + self.port = port + self.ident = ident + self.realname = realname + self.encodings = encodings + + def connect(self): + s=socket.socket( ) + try: + s.connect((self.server, self.port)) + except socket.error as e: + print("Fehler: %s" % e) + return + s.send(self.encode("NICK %s\r\n" % self.nicknames[0])) + s.send(self.encode("USER %s %s bla :%s\r\n" % (self.ident, self.server, self.realname))) + self.connected = True + readbuffer="" + while 1: + readbuffer=readbuffer+self.decode(s.recv(1024)) + temp=readbuffer.split("\n") + readbuffer=temp.pop( ) + + for line in temp: + line=line.rstrip() + line=line.split(" ") + + if(line[0]=="PING"): + s.send(self.encode("PONG %s\r\n" % line[1])) + else: + print(" ".join(line)) + + def quit(self, reason=""): + pass + + def join(self, channel): + pass + + def part(self, channel): + pass + + + + +def startbot(): + bot = pircbot(nicknames=(NICKNAME, ALT_NICKNAME), server=SERVER, port=PORT, + ident=IDENT, realname=REALNAME, encodings=ENCODINGS) + bot.connect() + bot.quit() + + +def parseargs(): + """ + Parses comand line arguments + """ + import argparse + p = argparse.ArgumentParser( + description = "i think my desc is missing") + p.add_argument("action", default="help", choices = ["start", "stop"], help="What to do?") + #p.add_argument("--daemon", "-d", type = bool, choices = [1, 0], default=1, help="Daemonize, Default: 1") + + return p.parse_args() + + +def main(): + args = parseargs() + if args.action == "start": + startbot() + elif args.action == "stop": print("nope!") + return 0 + + +if __name__ == '__main__': + + ################ + ### SETTINGS ### + ################ + + + IDENT = "chalkbot" + NICKNAME = "chalkbot" + ALT_NICKNAME = "chalkbot_" + REALNAME = "A ChalkBot Instance" + # Command for registering with nickserv, without leading slash + NICKSERVCMD = "" + + SERVER = "fanir.de" + PORT = 6667 + + ENCODINGS = ('utf-8', 'latin-1', 'iso-8859-1', 'cp1252') + + CHANNELS = ["#bots"] + + + main() +