Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. """
  3. trying to implement monadic IRC Bot.
  4.  
  5. """
  6.  
  7. import socket
  8.  
  9. server = "irc.efnet.org"
  10. port = 6667
  11.  
  12. nick = "mond"
  13. user = "mond"
  14. real = "mond"
  15.  
  16. channels = ["#wololo"]
  17. msg = "Yo blud"
  18.  
  19.  
  20. class IRCMonad(object):
  21.     def __init__(self, v, e=None):
  22.         self.v = v
  23.         self.e = e
  24.  
  25.     def __repr__(self):
  26.         if self.v is not None:
  27.             return repr(self.v)
  28.         else:
  29.             return repr(self.e)
  30.  
  31.     def bind(self, f):
  32.         if self.v is not None:
  33.             try:
  34.                 ret = f(self.v)
  35.             except Exception as e:
  36.                 return IRCMonad(None, e)
  37.  
  38.             if not isinstance(ret, IRCMonad):
  39.                 return IRCMonad(ret)
  40.             else:
  41.                 return ret
  42.         else:
  43.             return IRCMonad(None, self.e)
  44.  
  45.     def __rshift__(self, bindee):
  46.         return self.bind(bindee)
  47.  
  48.  
  49. def init():
  50.     options = {}
  51.     options["channels"] = []
  52.     options["server"] = server
  53.     options["port"] = port
  54.     options["nick"] = nick
  55.     options["user"] = user
  56.     options["real"] = real
  57.     options["channels"] += channels
  58.     options["msg"] = msg
  59.     return options
  60.  
  61. def connect(options):
  62.     options["sock"] = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  63.     options["sock"].connect((options["server"], options["port"]))
  64.     print(options)
  65.     return options
  66.  
  67. def ident(options):
  68.     options["sock"].send(bytes( "USER {} 0 *: {}\r\n".format(options['user'], options['real']), 'utf-8'))
  69.     options["sock"].send(bytes("NICK {}\r\n".format(options["nick"]), 'utf-8'))
  70.  
  71.     print(options)
  72.     return options
  73.  
  74.  
  75. def join(options):
  76.     for c in options["channels"]:
  77.         options["sock"].send(bytes("JOIN :{}\r\n".format(c), 'utf-8'))
  78.  
  79.     print(options)
  80.     return options
  81.  
  82.  
  83. def privmsg(options):
  84.     for c in options["channels"]:
  85.         options["sock"].send(bytes("PRIVMSG {} :{}\r\n".format(c, options["msg"]), 'utf-8'))
  86.     print(options)
  87.     return options
  88.  
  89. def listen(options):
  90.     while True:
  91.         data = options["sock"].recv(1024).decode('utf-8', 'ignore')
  92.         print(data)
  93.     return options
  94.  
  95.  
  96. if __name__=='__main__':
  97.     print( \
  98.         IRCMonad(init()) >> connect >> ident >> join >> privmsg >> listen \
  99.         )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement