Subversion Repositories Kolibri OS

Rev

Rev 4824 | Rev 6026 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2014. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  IRC client for KolibriOS                                       ;;
  7. ;;                                                                 ;;
  8. ;;   Written by hidnplayr@kolibrios.org,                           ;;
  9. ;;     text encoder/decoder by Clevermouse.                        ;;
  10. ;;                                                                 ;;
  11. ;;         GNU GENERAL PUBLIC LICENSE                              ;;
  12. ;;          Version 2, June 1991                                   ;;
  13. ;;                                                                 ;;
  14. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  15.  
  16. version equ '0.23'
  17.  
  18. ; connection status
  19. STATUS_DISCONNECTED     = 0
  20. STATUS_RESOLVING        = 1
  21. STATUS_CONNECTING       = 2
  22. STATUS_CONNECTED        = 3
  23.  
  24. ; window flags
  25. FLAG_UPDATED            = 1 shl 0
  26. FLAG_RECEIVING_NAMES    = 1 shl 1
  27. FLAG_SCROLL_LOW         = 1 shl 2
  28.  
  29. ; window types
  30. WINDOWTYPE_NONE         = 0
  31. WINDOWTYPE_SERVER       = 1
  32. WINDOWTYPE_CHANNEL      = 2
  33. WINDOWTYPE_CHAT         = 3
  34. WINDOWTYPE_LIST         = 4
  35. WINDOWTYPE_DCC          = 5
  36.  
  37. ; supported encodings
  38. CP866                   = 0
  39. CP1251                  = 1
  40. UTF8                    = 2
  41.  
  42. ; settings
  43. USERCMD_MAX_SIZE        = 400
  44.  
  45. WIN_MIN_X               = 600
  46. WIN_MIN_Y               = 170
  47.  
  48. TEXT_X                  = 5
  49. TEXT_Y                  = TOP_Y + 2
  50.  
  51. TOP_Y                   = 24
  52. BOTTOM_Y                = 15
  53.  
  54. MAX_WINDOWS             = 20
  55. MAX_USERS               = 4096
  56. TEXT_BUFFERSIZE         = 1024*1024
  57.  
  58. MAX_NICK_LEN            = 32
  59. MAX_REAL_LEN            = 32    ; realname
  60. MAX_SERVER_NAME         = 256
  61.  
  62. MAX_CHANNEL_LEN         = 40
  63. MAX_CHANNELS            = 37
  64.  
  65. MAX_COMMAND_LEN         = 512
  66.  
  67. TIMESTAMP               = 3     ; 3 = hh:mm:ss, 2 = hh:mm, 0 = no timestamp
  68.  
  69. MAX_WINDOWNAME_LEN      = 256
  70.  
  71. WINDOW_BTN_START        = 100
  72. WINDOW_BTN_CLOSE        = 2
  73. WINDOW_BTN_LIST         = 3
  74.  
  75. SCROLLBAR_WIDTH         = 14
  76. USERLIST_WIDTH          = 100
  77.  
  78. FONT_HEIGHT             = 9
  79. FONT_WIDTH              = 6
  80.  
  81. format binary as ""
  82.  
  83. use32
  84.  
  85.         org     0x0
  86.  
  87.         db      'MENUET01'              ; 8 byte id
  88.         dd      1                       ; header version
  89.         dd      START                   ; program start
  90.         dd      I_END                   ; program image size
  91.         dd      IM_END+2048             ; required amount of memory
  92.         dd      IM_END+2048
  93.         dd      param
  94.         dd      path
  95.  
  96. include "../../macros.inc"
  97. include "../../proc32.inc"
  98. include "../../dll.inc"
  99. include "../../network.inc"
  100. include "../../struct.inc"
  101. include "../../develop/libraries/box_lib/trunk/box_lib.mac"
  102.  
  103. struct  window
  104.         data_ptr        dd ?
  105.         flags           db ?
  106.         type            db ?
  107.         name            rb MAX_WINDOWNAME_LEN
  108.         users           dd ?
  109.         users_scroll    dd ?
  110.         selected        dd ?            ; selected user, 0 if none selected
  111.  
  112.         text_start      dd ?            ; pointer to current textbox data
  113.         text_end        dd ?
  114.         text_print      dd ?            ; pointer to first character to print on screen
  115.         text_line_print dd ?            ; line number of that character
  116.         text_write      dd ?            ; write pointer
  117.         text_lines      dd ?            ; total number of lines
  118.         text_scanned    dd ?            ; pointer to beginning of unscanned data (we still need to count number of lines, insert newline characters,..)
  119.  
  120. ends
  121.  
  122. struct  window_data
  123.         text            rb TEXT_BUFFERSIZE
  124.         names           rb MAX_NICK_LEN * MAX_USERS
  125. ends
  126.  
  127. include "encodings.inc"
  128. include "window.inc"
  129. include "serverparser.inc"
  130. include "userparser.inc"
  131. include "socket.inc"
  132. include "gui.inc"
  133. include "users.inc"
  134. include "textbox.inc"
  135.  
  136.  
  137. START:
  138.  
  139.         mcall   68, 11                  ; init heap so we can allocate memory dynamically
  140.  
  141. ; wanted events
  142.         mcall   40, EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_STACK+EVM_MOUSE+EVM_MOUSE_FILTER
  143.  
  144. ; load libraries
  145.         stdcall dll.Load, @IMPORT
  146.         test    eax, eax
  147.         jnz     exit
  148.  
  149. ; find path to main settings file (ircc.ini)
  150.         mov     edi, path               ; Calculate the length of zero-terminated string
  151.         xor     al, al
  152.         mov     ecx, 1024
  153.         repne   scasb
  154.         dec     edi
  155.         mov     eax, '.ini'
  156.         stosd
  157.         xor     al, al
  158.         stosb
  159.  
  160. ; Fill the window buffer with zeros
  161.         mov     edi, windows
  162.         mov     ecx, (sizeof.window*MAX_WINDOWS+3)/4
  163.         xor     eax, eax
  164.         rep     stosd
  165.  
  166. ; clear command area too
  167.         mov     edi, servercommand
  168.         mov     ecx, 600/4
  169.         rep     stosd
  170.  
  171. ; allocate window data block
  172.         mov     ebx, windows
  173.         call    window_create_textbox
  174.         test    eax, eax
  175.         jz      error
  176.         mov     [ebx + window.type], WINDOWTYPE_SERVER
  177.  
  178. ; get system colors
  179.         mcall   48, 3, colors, 40
  180.  
  181. ; set edit box and scrollbar colors
  182.         mov     eax, [colors.work]
  183.         mov     [scroll1.bg_color], eax
  184.         mov     [scroll2.bg_color], eax
  185.  
  186.         mov     eax, [colors.work_button]
  187.         mov     [scroll1.front_color], eax
  188.         mov     [scroll2.front_color], eax
  189.  
  190.         mov     eax, [colors.work_text]
  191.         mov     [scroll1.line_color], eax
  192.         mov     [scroll2.line_color], eax
  193.  
  194.         mov     [scroll1.type], 1               ; 0 = simple, 1 = skinned
  195.         mov     [scroll2.type], 1
  196.  
  197. ; get settings from ini
  198.         invoke  ini.get_str, path, str_user, str_nick, user_nick, MAX_NICK_LEN, default_nick
  199.         invoke  ini.get_str, path, str_user, str_real, user_real_name, MAX_REAL_LEN, default_real
  200.         invoke  ini.get_str, path, str_user, str_quitmsg, quit_msg, 250, default_quit
  201.  
  202. ; Welcome user
  203.         mov     esi, str_welcome
  204.         call    print_asciiz
  205.  
  206. ; Check if parameter contains an URL
  207.         cmp     byte[param], 0
  208.         je      @f
  209.         mov     esi, param
  210.         mov     ecx, 1024
  211.         call    cmd_usr_server.now
  212.   @@:
  213.  
  214. ; Draw window a first time, so we can figure out skin size
  215.         call    draw_window
  216.  
  217. redraw:
  218.         call    draw_window
  219.  
  220. mainloop:
  221.         mcall   10              ; wait for event
  222.  
  223.         dec     eax
  224.         jz      redraw
  225.  
  226.         dec     eax
  227.         jz      main_window_key
  228.  
  229.         dec     eax
  230.         jz      button
  231.  
  232.         cmp     al, 3
  233.         je      mouse
  234.  
  235.         call    process_network_event
  236.  
  237.         mov     edi, [window_active]
  238.         test    [edi + window.flags], FLAG_UPDATED
  239.         jz      .no_update
  240.         call    draw_channel_text
  241.         mov     edi, [window_active]
  242.         cmp     [edi + window.type], WINDOWTYPE_CHANNEL
  243.         jne     .no_update
  244.         call    draw_channel_list
  245.   .no_update:
  246.         call    highlight_updated_tabs
  247.  
  248.         jmp     mainloop
  249.  
  250. button:
  251.  
  252.         mcall   17              ; get id
  253.         ror     eax, 8
  254.  
  255.         cmp     ax, 1           ; close program
  256.         je      exit
  257.  
  258.         cmp     ax, WINDOW_BTN_CLOSE
  259.         jne     @f
  260.         call    cmd_usr_close_window
  261.         jmp     mainloop
  262.  
  263.   @@:
  264.         cmp     ax, WINDOW_BTN_LIST
  265.         jne     @f
  266.  
  267.         push    eax
  268.  
  269.         mcall   37, 1           ; Get mouse position
  270.         sub     ax, TEXT_Y
  271.         mov     bl, FONT_HEIGHT
  272.         div     bl
  273.         and     eax, 0x000000ff
  274.         inc     eax
  275.         add     eax, [scroll1.position]
  276.         mov     ebx, [window_active]
  277.         mov     [ebx + window.selected], eax
  278.  
  279.         call    draw_channel_list
  280.  
  281.         pop     eax
  282.         test    eax, 1 shl 25   ; Right mouse button pressed?
  283.         jz      mainloop
  284.  
  285. ; TODO: check if selected nick is my nick!
  286.  
  287. ; Right mouse BTN was pressed, open chat window
  288.         mov     ebx, [window_active]
  289.         mov     eax, [ebx + window.selected]
  290.         dec     eax
  291.         imul    eax, MAX_NICK_LEN
  292.         mov     ebx, [ebx + window.data_ptr]
  293.         lea     esi, [ebx + window_data.names + eax]
  294.         call    window_open
  295.         test    ebx, ebx
  296.         jz      mainloop
  297.         mov     [window_active], ebx
  298.         call    redraw
  299.  
  300.         jmp     mainloop
  301.  
  302.   @@:
  303.         sub     ax, WINDOW_BTN_START
  304.         jb      exit
  305.  
  306.         cmp     ax, MAX_WINDOWS
  307.         ja      exit
  308.  
  309. ; Save users scrollbar position
  310.         push    [scroll1.position]
  311.         mov     edx, [window_active]
  312.         pop     [edx + window.users_scroll]
  313.  
  314. ; OK, time to switch to another window.
  315.         mov     dx, sizeof.window
  316.         mul     dx
  317.         shl     edx, 16
  318.         mov     dx, ax
  319.         add     edx, windows
  320.         cmp     [edx + window.type], WINDOWTYPE_NONE
  321.         je      exit
  322.         mov     [window_active], edx
  323.  
  324.         push    [edx + window.text_line_print]
  325.         pop     [scroll2.position]
  326.  
  327.         push    [edx + window.users_scroll]
  328.         pop     [scroll1.position]
  329.  
  330.         call    draw_window
  331.         jmp     mainloop
  332.  
  333. exit:
  334.  
  335.         cmp     [socketnum], 0
  336.         je      @f
  337.         mov     esi, quit_msg
  338.         call    quit_server
  339.   @@:
  340.  
  341. error:
  342.  
  343.         mcall   -1
  344.  
  345.  
  346.  
  347. main_window_key:
  348.  
  349.         mcall   2
  350.  
  351.         push    dword edit1
  352.         call    [edit_box_key]
  353.  
  354. ;        cmp     ah, 178
  355. ;        jne     .no_up
  356. ;
  357. ;        jmp     mainloop
  358. ;
  359. ;
  360. ;  .no_up:
  361. ;        cmp     ah, 177
  362. ;        jne     .no_down
  363. ;
  364. ;        jmp     mainloop
  365. ;
  366. ;  .no_down:
  367.         cmp     ah, 13          ; enter
  368.         jne     no_send2
  369.  
  370.         call    user_parser
  371.  
  372.         mov     eax, [edit1.size]
  373.  
  374.         mov     [edit1.size], 0
  375.         mov     [edit1.pos], 0
  376.  
  377.         push    dword edit1
  378.         call    [edit_box_draw]
  379.  
  380.         call    draw_channel_text
  381.  
  382.         jmp     mainloop
  383.   no_send2:
  384.  
  385.         jmp     mainloop
  386.  
  387. mouse:
  388.         push    dword edit1
  389.         call    [edit_box_mouse]
  390.  
  391. ;        mcall   37, 7
  392. ;        movsx   eax, ax
  393. ;        add     [scroll2.position], eax
  394.  
  395. ; TODO: check if scrollbar is active?
  396.         mov     edi, [window_active]
  397.         cmp     [edi + window.type], WINDOWTYPE_CHANNEL
  398.         jne     @f
  399.         push    [scroll1.position]
  400.         push    dword scroll1
  401.         call    [scrollbar_mouse]
  402.         pop     eax
  403.         cmp     eax, [scroll1.position] ; did the scrollbar move?
  404.         je      @f
  405.         call    draw_channel_list
  406.   @@:
  407.  
  408. ; TODO: check if scrollbar is active?
  409.         mov     edi, [window_active]
  410.         mov     eax, [edi + window.text_lines]
  411.         cmp     eax, [textbox_height]
  412.         jbe     @f
  413.         push    dword scroll2
  414.         call    [scrollbar_mouse]
  415.         mov     edi, [window_active]
  416.         and     [edi+window.flags], not FLAG_SCROLL_LOW
  417.         mov     edx, [scroll2.position]
  418.         add     edx, [scroll2.cur_area]
  419.         sub     edx, [scroll2.max_area]
  420.         jne     .not_low
  421.         or      [edi+window.flags], FLAG_SCROLL_LOW
  422.   .not_low:
  423.         mov     edx, [scroll2.position]
  424.         sub     edx, [edi + window.text_line_print]
  425.         je      @f
  426.         call    draw_channel_text.scroll_to_pos
  427.   @@:
  428.  
  429.         jmp     mainloop
  430.  
  431.  
  432. ; DATA AREA
  433.  
  434. encoding_text:
  435. db      'CP866 '
  436. db      'CP1251'
  437. db      'UTF-8 '
  438. encoding_text_len = 6
  439.  
  440. join_header             db 3, '3* ', 0
  441. quit_header             db 3, '5* ', 0
  442. nick_header             db 3, '2* ', 0
  443. kick_header             db 3, '5* ', 0
  444. mode_header             db 3, '2* ', 0
  445. part_header             db 3, '5* ', 0
  446. topic_header            db 3, '3* ', 0
  447. action_header           db 3, '6* ', 0
  448. ctcp_header             db 3, '13-> [', 0
  449. msg_header              db 3, '7-> *', 0
  450. ctcp_version            db '] VERSION', 10, 0
  451. ctcp_ping               db '] PING', 10, 0
  452. ctcp_time               db '] TIME', 10, 0
  453.  
  454. has_left_channel        db ' has left ', 0
  455. joins_channel           db ' has joined ', 0
  456. is_now_known_as         db ' is now known as ', 0
  457. has_quit_irc            db ' has quit IRC', 10, 0
  458.  
  459. sets_mode               db ' sets mode ', 0
  460. str_kicked              db ' is kicked from ', 0
  461. str_by                  db ' by ', 0
  462. str_nickchange          db 'Nickname is now ', 0
  463. str_realchange          db 'Real name is now ', 0
  464. str_talking             db 'Now talking in ', 0
  465. str_topic               db 'Topic is "', 0
  466. str_topic_end           db '"', 10, 0
  467. str_setby               db 'Set by ', 0
  468.  
  469. str_connecting          db 3, '3* Connecting to ', 0
  470. str_sockerr             db 3, '5* Socket error', 10, 0
  471. str_dnserr              db 3, '5* Unable to resolve hostname', 10, 0
  472. str_refused             db 3, '5* Connection refused', 10, 0
  473. str_srv_disconnected    db 3, '5* Server disconnected', 10, 0
  474. str_disconnected        db 3, '5* Disconnected', 10, 0
  475. str_reconnect           db 3, '5* Connection reset by user', 10, 0
  476. str_notconnected        db 3, '5* Not connected to server', 10, 0
  477. str_notchannel          db 3, '5* You are not on a channel', 10, 0
  478.  
  479. str_1                   db 3, '13 -', 0
  480. str_2                   db '- ', 0
  481.  
  482. str_list                db 'list', 0
  483.  
  484. str_help                db 'The following commands are available:', 10
  485.                         db 10
  486.                         db '/nick <nick>        : change nickname', 10
  487.                         db '/real <real name>   : change real name', 10
  488.                         db '/server <address>   : connect to server', 10
  489.                         db '/code <code>        : change codepage (cp866, cp1251, or utf8)', 10
  490.                         db '/join <channel>     : join a channel', 10
  491.                         db '/part <channel>     : part from a channel', 10
  492.                         db '/quit               : quit server', 10
  493.                         db '/msg <user>         : send a private message', 10
  494.                         db '/ctcp <user>        : send a message using client to client protocol', 10
  495.                         db 10
  496.                         db 'Other commands are send straight to server.', 10
  497.                         db 10, 0
  498.  
  499. str_welcome             db 3, '3 ___', 3, '7__________', 3, '6_________  ', 3, '4         __   __               __', 10
  500.                         db 3, '3|   \', 3, '7______   \', 3, '6_   ___ \ ', 3, '4   ____ |  | |__| ____   _____/  |_', 10
  501.                         db 3, '3|   |', 3, '7|       _/', 3, '6    \  \/ ', 3, '4 _/ ___\|  | |  |/ __ \ /    \   __\', 10
  502.                         db 3, '3|   |', 3, '7|    |   \', 3, '6     \____', 3, '4 \  \___|  |_|  \  ___/|   |  \  |', 10
  503.                         db 3, '3|___|', 3, '7|____|_  /', 3, '6\______  /', 3, '4  \___  >____/__|\___  >___|  /__|', 10
  504.                         db 3, '3     ', 3, '7       \/ ', 3, '6       \/ ', 3, '4      \/             \/     \/', 10
  505.                         db 10
  506.                         db 'Welcome to the KolibriOS IRC client v', version, 10
  507.                         db 10
  508.                         db 'Type /help for help', 10, 10, 0
  509.  
  510. str_version             db 'VERSION '
  511. str_programname         db 'KolibriOS IRC client v', version, 0
  512.  
  513. str_user                db 'user', 0
  514. str_nick                db 'nick', 0
  515. str_real                db 'realname', 0
  516. str_email               db 'email', 0
  517. str_quitmsg             db 'quitmsg', 0
  518.  
  519. default_nick            db 'kolibri_user', 0
  520. default_real            db 'Kolibri User', 0
  521. default_quit            db 'KolibriOS forever', 0
  522.  
  523. irc_colors              dd 0xffffff     ;  0 white
  524.                         dd 0x000000     ;  1 black
  525.                         dd 0x00007f     ;  2 blue (navy)
  526.                         dd 0x009300     ;  3 green
  527.                         dd 0xff0000     ;  4 red
  528.                         dd 0x7f0000     ;  5 brown (maroon)
  529.                         dd 0x9c009c     ;  6 purple
  530.                         dd 0xfc7f00     ;  7 olive
  531.                         dd 0xffff00     ;  8 yellow
  532.                         dd 0x00fc00     ;  9 light green
  533.                         dd 0x009393     ; 10 teal
  534.                         dd 0x00ffff     ; 11 cyan
  535.                         dd 0x0000fc     ; 12 royal blue
  536.                         dd 0xff00ff     ; 13 pink
  537.                         dd 0x7f7f7f     ; 14 grey
  538.                         dd 0xd4d0c4     ; 15 light grey (silver)
  539.  
  540. sockaddr1:
  541.         dw AF_INET4
  542. .port   dw 0x0b1a       ; 6667          FIXMEEEEEE
  543. .ip     dd 0
  544.         rb 10
  545.  
  546.  
  547. status                  dd STATUS_DISCONNECTED
  548.  
  549. window_active           dd windows
  550. window_print            dd windows
  551.  
  552. align 4
  553. @IMPORT:
  554.  
  555. library network,        'network.obj',\
  556.         libini,         'libini.obj',\
  557.         boxlib,         'box_lib.obj'
  558.  
  559. import  network,\
  560.         getaddrinfo,    'getaddrinfo',\
  561.         freeaddrinfo,   'freeaddrinfo',\
  562.         inet_ntoa,      'inet_ntoa'
  563.  
  564. import  libini,\
  565.         ini.get_str,    'ini_get_str',\
  566.         ini.get_int,    'ini_get_int'
  567.  
  568. import  boxlib,\
  569.         edit_box_draw,  'edit_box',\
  570.         edit_box_key,   'edit_box_key',\
  571.         edit_box_mouse, 'edit_box_mouse',\
  572.         scrollbar_draw, 'scrollbar_v_draw',\
  573.         scrollbar_mouse,'scrollbar_v_mouse'
  574.  
  575.         ;         width, left, top
  576. edit1   edit_box  0, 0, 0, 0xffffff, 0x6f9480, 0, 0, 0, USERCMD_MAX_SIZE, usercommand, mouse_dd, ed_always_focus, 25, 25
  577.         ;         xsize, xpos, ysize, ypos, btn_height, max, cur, pos, bgcol, frcol, linecol
  578. scroll1 scrollbar SCROLLBAR_WIDTH, 0, 0, TOP_Y, SCROLLBAR_WIDTH, 0, 0, 0, 0, 0, 0, 1
  579. scroll2 scrollbar SCROLLBAR_WIDTH, 0, 0, TOP_Y, SCROLLBAR_WIDTH, 0, 0, 0, 0, 0, 0, 1
  580.  
  581. usercommand     db '/server chat.freenode.net', 0
  582.                 rb MAX_COMMAND_LEN
  583.  
  584. I_END:
  585.  
  586. utf8_bytes_rest dd ?            ; bytes rest in current UTF8 sequence
  587. utf8_char       dd ?            ; first bits of current UTF8 character
  588.  
  589. packetbuf       rb 1024         ; buffer for packets to server
  590. path            rb 1024
  591. param           rb 1024
  592.  
  593. servercommand   rb 600
  594.  
  595. thread_info     process_information
  596. xsize           dd ?
  597. ysize           dd ?
  598. mouse_dd        dd ?
  599.  
  600. textbox_height  dd ?            ; in characters
  601. textbox_width   dd ?            ; in characters, not pixels ;)
  602.  
  603. colors          system_colors
  604.  
  605. irc_server_name rb MAX_SERVER_NAME      ; TODO: move this server URL into window struct
  606. socketnum       dd ?                    ; TODO: same for socket
  607.  
  608. user_nick       rb MAX_NICK_LEN
  609. user_real_name  rb MAX_REAL_LEN
  610. quit_msg        rb 250
  611.  
  612. windows         rb MAX_WINDOWS*sizeof.window
  613.  
  614. IM_END: