Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/python3
- import sys
- CRLF = "\r\n"
- DEFAULT_QUIT_MESSAGE = "Bye byeeeee~"
- destination = None
- def send(cmd, *args):
- args = list(args)
- arg_list = ""
- if len(args) > 0:
- trailing = " :" + args.pop()
- for arg in args:
- arg_list += " " + arg
- arg_list += trailing
- print(cmd + arg_list, end=CRLF)
- def ping(msg):
- send("PING", msg)
- def pong(msg):
- send("PONG", msg)
- def message(dest, msg):
- send("PRIVMSG", dest, msg)
- def join(dest):
- send("JOIN", dest)
- def part(dest):
- send("PART", dest)
- def nick(name):
- send("NICK", name)
- def user(username, hostname="8", servername="*", realname=None):
- if not realname: realname = username
- send("USER", username, hostname, servername, realname)
- def quit(reason=DEFAULT_QUIT_MESSAGE):
- send("QUIT", reason)
- def command(cmd, args):
- global destination
- cmd = cmd.upper()
- if cmd == "PING":
- if args == None:
- print("Specify a string to ping with.", file=sys.stderr)
- return
- ping(args)
- elif cmd == "MSG":
- if args == None:
- print("Specify a destination and a message to send.", file=sys.stderr)
- return
- args = args.split(" ", 1)
- dest = args[0].strip()
- if len(args) < 2:
- print("Specify a message to send to " + dest, file=sys.stderr)
- return
- message(dest, args[1])
- elif cmd == "QUERY":
- if args == None:
- print("Specify a destination to query.", file=sys.stderr)
- return
- destination = args
- elif cmd == "JOIN":
- if args == None:
- print("Specify the name of a channel to join.", file=sys.stderr)
- return
- join(args)
- elif cmd == "PART":
- if args == None:
- print("Specify the name of a channel to part.", file=sys.stderr)
- return
- part(args)
- elif cmd == "NICK":
- if args == None:
- print("Specify a name to change your nick to.", file=sys.stderr)
- return
- nick(args)
- elif cmd == "USER":
- if args == None:
- print("Specify a username to log in with.", file=sys.stderr)
- return
- args = args.split(" ", 3)
- user(*args)
- elif cmd == "QUIT":
- if args:
- quit(args)
- else:
- quit()
- else:
- print("Unknown command.", file=sys.stderr)
- for line in sys.stdin:
- if len(line) == 0: continue
- if line[0] != '/':
- if not destination:
- print("Nowhere to send the message to!", file=sys.stderr)
- else:
- message(destination, line.strip())
- else:
- line = line[1:]
- line = line.strip()
- if len(line) == 0: continue
- parts = line.split(" ", 1);
- cmd = parts[0]
- if len(parts) > 1:
- args = parts[1].strip()
- else:
- args = None
- command(cmd, args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement