ZNZNCOOP

mess_client

May 5th, 2014
221
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. server_id=1
  2. function open()
  3.   local bOpen, sFreeSide = false, nil
  4.   for n,sSide in pairs(rs.getSides()) do
  5.     if peripheral.getType( sSide ) == "modem" then
  6.       sFreeSide = sSide
  7.       if rednet.isOpen( sSide ) then
  8.         bOpen = true
  9.         break
  10.       end
  11.     end
  12.   end
  13.   if not bOpen then
  14.     if sFreeSide then
  15.       print( "No modem active. Opening "..sFreeSide.." modem" )
  16.       rednet.open( sFreeSide )
  17.       return true
  18.     else
  19.       print( "No modem attached" )
  20.       return false
  21.     end
  22.   end
  23.   return true
  24. end
  25. local rdmess=false
  26. function ShowMenu()
  27.   term.clear()
  28.   if rdmess then
  29.     local x,y=term.getSize()
  30.     term.setCursorPos(1, y)
  31.     term.write('<Del>-Delete <Right>-Next message')
  32.   end
  33.   term.setCursorPos(1, 1)
  34.   print('+'..string.rep('-',49)..'+')
  35.   print('|F1 New user | F2  Login |F3 Check mes|F4 Read mes|')
  36.   print('+'..string.rep('-',49)..'+')
  37.   print('|F5 Send mess| F6  News  | F9 Logout  | F10  Exit |')
  38.   print('+'..string.rep('-',49)..'+')
  39. end
  40. function ShowWin(text)
  41.   if text then
  42.     if term.isColour() then term.setTextColour( colours.yellow ) end
  43.     term.setCursorPos(math.ceil((term.getSize()-#text) / 2), 8) print(text)
  44.   end
  45.   if term.isColour() then term.setTextColour( colours.lime ) end
  46.   term.setCursorPos(11, 10) print('Nick:')
  47.   term.setCursorPos(7, 12)  print('Password:')
  48.   if term.isColour() then term.setTextColour( colours.white ) end
  49.   for i=9,13 do
  50.     term.setCursorPos(17, i)
  51.     if i % 2 == 1 then print('+----------------+')
  52.     else print('|                |')
  53.     end
  54.   end
  55.   term.setCursorPos(19, 10) local nick=read()
  56.   term.setCursorPos(19, 12) local pswd=read('*')
  57.   return nick,pswd
  58. end
  59.  
  60. eventKey={}
  61. local work=true
  62. eventKey[keys.f1]= function ()
  63.   local user,pwd=ShowWin('Registration new user')
  64.   rednet.send(server_id,'adduser user='..user..' pwd='..pwd)
  65. end
  66. eventKey[keys.f2]= function ()
  67.   local user,pwd=ShowWin('Enter your nick and password')
  68.   rednet.send(server_id,'login user='..user..' pwd='..pwd)
  69. end
  70. eventKey[keys.f3]= function ()
  71.   rednet.send(server_id,'checkmess')
  72. end
  73. eventKey[keys.f4]= function ()
  74.   rednet.send(server_id,'readfirst')
  75.   rdmess=true
  76. end
  77. eventKey[keys.f5]= function ()
  78.   rednet.send(server_id,'users')
  79.   local id,users
  80.   repeat
  81.     id,users=rednet.receive(1)
  82.     if not id then
  83.       print('No ansver from server')
  84.       return
  85.     end
  86.   until id==server_id
  87.   local userstab={}
  88.   for name in users:gmatch('%S+') do
  89.     table.insert(userstab, name)
  90.   end
  91.   table.sort(userstab)
  92.   write('Destination user nick:')
  93.   local user=read(nil, userstab)
  94.   write('Enter text:')
  95.   local mess=read()
  96.   rednet.send(server_id,'sendmess user='..user..' '..mess)
  97. end
  98. eventKey[keys.f6]= function ()
  99.   rednet.send(server_id,'news')
  100. end
  101. eventKey[keys.f9]= function ()
  102.   rednet.send(server_id,'logout')
  103. end
  104. eventKey[keys.f10]= function ()
  105.   eventKey[keys.f9]()
  106.   work=false
  107. end
  108.  
  109. eventKey[keys.right]= function ()
  110.   if rdmess then
  111.     rednet.send(server_id,'readmess')
  112.   end
  113. end
  114.  
  115. eventKey[keys.delete]= function ()
  116.   if rdmess then
  117.     rednet.send(server_id,'delmess')
  118.   end
  119. end
  120.  
  121. function os.pullEvent()
  122.   return os.pullEventRaw()
  123. end
  124.  
  125. if not open() then return end
  126. while work do
  127.   ShowMenu()
  128. --  rednet.send(server_id,'ask')
  129.   while true do
  130.     local event,p1,p2,p3=os.pullEvent()
  131.     if event=='rednet_message' then
  132.       if p1==server_id then
  133.         print(p2)
  134.         if rdmess and #p2>0 and p2:sub(1,1)~='<' then
  135.           rdmess=false
  136.           local x,y=term.getSize()
  137.           term.setCursorPos(1, y)
  138.           term.clearLine()
  139.         end
  140.       end
  141.     elseif event=='key' then
  142.       if eventKey[p1]~=nil then
  143.         eventKey[p1]()
  144.         break
  145.       end
  146.     elseif event=='mouse_click' and p1==1 then
  147.       if p2>1 and p2<14 then
  148.         if p3==2 then eventKey[keys.f1]() end
  149.         if p3==4 then eventKey[keys.f5]() end
  150.       end
  151.       if p2>14 and p2<26 then
  152.         if p3==2 then eventKey[keys.f2]() end
  153.         if p3==4 then eventKey[keys.f6]() end
  154.       end
  155.       if p2>26 and p2<39 then
  156.         if p3==2 then eventKey[keys.f3]() end
  157.         if p3==4 then eventKey[keys.f9]() end
  158.       end
  159.       if p2>39 and p2<51 then
  160.         if p3==2 then eventKey[keys.f4]() end
  161.         if p3==4 then eventKey[keys.f10]() end
  162.       end
  163.       break
  164.     end
  165.   end
  166. end
RAW Paste Data