Advertisement
Guest User

user2irc

a guest
Mar 4th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. #!/bin/python3
  2.  
  3. import sys
  4.  
  5. CRLF = "\r\n"
  6. DEFAULT_QUIT_MESSAGE = "Bye byeeeee~"
  7. destination = None
  8.  
  9. def send(cmd, *args):
  10.     args = list(args)
  11.     arg_list = ""
  12.     if len(args) > 0:
  13.         trailing = " :" + args.pop()
  14.         for arg in args:
  15.             arg_list += " " + arg
  16.         arg_list += trailing
  17.     print(cmd + arg_list, end=CRLF)
  18.  
  19. def ping(msg):
  20.     send("PING", msg)
  21.  
  22. def pong(msg):
  23.     send("PONG", msg)
  24.  
  25. def message(dest, msg):
  26.     send("PRIVMSG", dest, msg)
  27.  
  28. def join(dest):
  29.     send("JOIN", dest)
  30.  
  31. def part(dest):
  32.     send("PART", dest)
  33.  
  34. def nick(name):
  35.     send("NICK", name)
  36.  
  37. def user(username, hostname="8", servername="*", realname=None):
  38.     if not realname: realname = username
  39.     send("USER", username, hostname, servername, realname)
  40.  
  41. def quit(reason=DEFAULT_QUIT_MESSAGE):
  42.     send("QUIT", reason)
  43.  
  44. def command(cmd, args):
  45.     global destination
  46.     cmd = cmd.upper()
  47.     if cmd == "PING":
  48.         if args == None:
  49.             print("Specify a string to ping with.", file=sys.stderr)
  50.             return
  51.         ping(args)
  52.     elif cmd == "MSG":
  53.         if args == None:
  54.             print("Specify a destination and a message to send.", file=sys.stderr)
  55.             return
  56.         args = args.split(" ", 1)
  57.         dest = args[0].strip()
  58.         if len(args) < 2:
  59.             print("Specify a message to send to " + dest, file=sys.stderr)
  60.             return
  61.         message(dest, args[1])
  62.     elif cmd == "QUERY":
  63.         if args == None:
  64.             print("Specify a destination to query.", file=sys.stderr)
  65.             return
  66.         destination = args
  67.     elif cmd == "JOIN":
  68.         if args == None:
  69.             print("Specify the name of a channel to join.", file=sys.stderr)
  70.             return
  71.         join(args)
  72.     elif cmd == "PART":
  73.         if args == None:
  74.             print("Specify the name of a channel to part.", file=sys.stderr)
  75.             return
  76.         part(args)
  77.     elif cmd == "NICK":
  78.         if args == None:
  79.             print("Specify a name to change your nick to.", file=sys.stderr)
  80.             return
  81.         nick(args)
  82.     elif cmd == "USER":
  83.         if args == None:
  84.             print("Specify a username to log in with.", file=sys.stderr)
  85.             return
  86.         args = args.split(" ", 3)
  87.         user(*args)
  88.     elif cmd == "QUIT":
  89.         if args:
  90.             quit(args)
  91.         else:
  92.             quit()
  93.     else:
  94.         print("Unknown command.", file=sys.stderr)
  95.  
  96. for line in sys.stdin:
  97.     if len(line) == 0: continue
  98.     if line[0] != '/':
  99.         if not destination:
  100.             print("Nowhere to send the message to!", file=sys.stderr)
  101.         else:
  102.             message(destination, line.strip())
  103.     else:
  104.         line = line[1:]
  105.         line = line.strip()
  106.         if len(line) == 0: continue
  107.         parts = line.split(" ", 1);
  108.         cmd = parts[0]
  109.         if len(parts) > 1:
  110.             args = parts[1].strip()
  111.         else:
  112.             args = None
  113.         command(cmd, args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement