A unf. social network done poorly.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.0KB

  1. #! /bin/bash
  2. ## The IM Server.
  3. ####
  4. ## Taken from some guy. Will modify in the future.
  5. import SocketServer
  6. _port = 8881
  7. _clients = {}
  8. # a connected client
  9. class Client:
  10. # queue of messages sent to this client
  11. queue = []
  12. def __init__(self, _sock, _src, _dest):
  13. print "Creating IM client"
  14. self.socket = _sock
  15. print "Incoming socket: %s" % self.socket
  16. self.user = _src
  17. print "Username: " + self.user
  18. # buddies should be a list
  19. self.buddy = _dest
  20. print "Buddy: " + self.buddy
  21. print "Created IM client"
  22. # the server handling requests
  23. class Broker(SocketServer.BaseRequestHandler):
  24. def handle(self):
  25. print "Connected from", self.client_address
  26. while True:
  27. receivedData = self.request.recv(8192)
  28. if not receivedData:
  29. break
  30. # if handshake packet, extract client details
  31. if receivedData.startswith('@@@',0,3):
  32. print "Received handshake packet"
  33. # strip handshake code
  34. receivedData = receivedData.replace('@@@', '', 1).lstrip()
  35. l = receivedData.split('##',1)
  36. socket = self.request
  37. src = l[0]
  38. dest = l[1]
  39. c = Client(socket, src, dest)
  40. # use username as key on hashmap
  41. _clients[src] = c
  42. # send success message
  43. socket.sendall('AUTH_OK')
  44. print "Client " + src + " authenticated"
  45. # if polling packet, extract sender details and send messages
  46. if receivedData.startswith('$$$',0,3):
  47. # strip polling message
  48. print "Received polling packet"
  49. src = receivedData.replace('$$$', '', 1).lstrip()
  50. # only poll if more than 1 user
  51. if len(_clients) > 1:
  52. # use username as key on hashmap
  53. _clients[src] = c
  54. if len(c.queue) < 1:
  55. c.socket.sendall(" ")
  56. else:
  57. msgs = ""
  58. for q in c.queue:
  59. msgs += q + '\n'
  60. # send queued messages
  61. c.socket.sendall(msgs)
  62. c.queue = []
  63. print "Sent all pending messages for " + c.user
  64. else:
  65. socket.sendall(" ")
  66. # if message packet, extract data and append to target queue
  67. if receivedData.startswith('###',0,3):
  68. print "Received message packet"
  69. receivedData = receivedData.replace('###', '', 1).lstrip()
  70. l = receivedData.split('##',1)
  71. src = l[0]
  72. text = l[1]
  73. if text.strip != "":
  74. print "Message not empty"
  75. # extract client
  76. clientSrc = _clients[src]
  77. # ...and its buddy
  78. clientDest = _clients[clientSrc.buddy]
  79. msg = src+": "+text
  80. print "Appended message to queue of " + clientSrc.buddy
  81. clientDest.queue.append(msg)
  82. print "Queue of: " + clientDest.user + " = %s" % clientDest.queue
  83. clientDest.socket.sendall(" ")
  84. else:
  85. if len(_clients) < 2:
  86. self.request.sendall(receivedData)
  87. for c in _clients.values():
  88. if self.request == c.socket:
  89. c.socket.close()
  90. # remove from hashmap
  91. del _clients[c.user]
  92. print "Removed " + c.user + " from hashmap"
  93. print "Disconnected from", self.client_address
  94. srv = SocketServer.ThreadingTCPServer(('',_port),Broker)
  95. print "Started IIM server on port %d" % _port
  96. srv.serve_forever()