143 lines
3.8 KiB
Python
143 lines
3.8 KiB
Python
|
#!/usr/bin/env python3
|
||
|
# -*- coding: utf-8 -*-
|
||
|
#
|
||
|
# main.py
|
||
|
#
|
||
|
# Copyright 2014 Fanir <projects@mail.fanir.de>
|
||
|
#
|
||
|
# 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()
|
||
|
|