Work / Virtual Machine / Example Source Code
2012-01-05 19:14:19
IRC Bot
Here is the vm source code of an irc bot. It can currently connect to a network (irc.quakenet.org), join a channel (#uml) and repond to ping requests. Not much else at the moment but I will soon add triggers (i.e. !remind etc) to it to make it useful:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 | ; $Header: $ stacksize 4096 segment .data newlineStr db "\n", 0 spaceStr db " ", 0 colonStr db ":", 0 nickStr db "NICK sekaitaBot\r\n", 0 userStr db "USER sekaitaBot 8 * :virtual machine ircbot\r\n", 0 pingStr db "PING :", 0 pongStr db "PONG :", 0 joinStr db "JOIN #stoner.uk\r\n", 0 partStr db "PART #stoner.uk\r\n", 0 messageStr db "PRIVMSG", 0 noticeStr db "NOTICE", 0 errorStr db "!! Error occurred\n", 0 exceptionStr db "!! Unhandled exception occurred\n", 0 connectingMsg0 db "!! Connecting to '", 0 connectingMsg1 db "' on port '", 0 connectingMsg2 db "'...", 0 connectErrMsg db "!! Unable to initialise connection...\n", 0 errorCodeMsg db "!! Error code: ", 0 disconnectMsg db "!! Disconnecting from server...\n", 0 connClosedMsg db "!! Connection has been closed...\n", 0 connLostMsg db "!! Connection lost...\n", 0 inputPrefix db "<< ", 0 outputPrefix db ">> ", 0 dbg1 db "## appending buffers\n", 0 dbg2 db "## resizing buffer\n", 0 readLineLoopStr db "readline loop\n", 0 mainLoopStr db "main loop\n", 0 rlStr0 db "readline write offset: ", 0 rlStr1 db "readline received data: ", 0 sdStr0 db "sending data: ", 0 sdStr1 db "data length: ", 0 plStr0 db "is ping request\n", 0 plStr1 db "isnt ping request\n", 0 segment .text import net.connect import net.disconnect import net.send import net.recv import net.canSend import net.canRecv import net.getLastError import std.memset import std.memcpy import std.memcmp import std.memchr import std.strlen import std.strncpy import std.strstr import std.indexof import std.substr import std.cout import strlib.StrToInt import strlib.IntToStr shared printStr shared printInt export main ; ------------------------------------- ; void main(char* address = "irc.quakenet.org:6667") main: nvar 0, argStr nvar 1, arg1Str nvar 2, arg2Str nvar 3, parseRes nvar 4, connectResult // eter frame and setup variables entf 5, 1 movi arg1Str, 0 movi arg2Str, 0 // set exception handler sexh .exception // parse argument string into parts push argStr pshd colonStr padr arg1Str padr arg2Str call parseArgs popr parseRes, 4 // if the return value is zero, an error occurred jz parseRes, .error // if either are null, an error occurred jz arg1Str, .error jz arg2Str, .error // print out what we are connecting to push arg1Str push arg2Str call printConnectingMsg popn 2 // initialise the connection push arg1Str push arg2Str call initialiseConnection popr connectResult, 2 jz connectResult, .error // enter the main loop push connectResult call mainLoop2 // lrtn .exception: // exception handler pshd exceptionStr farc std.cout popn 1 jmp .finally .error: // exception handler pshd errorStr farc std.cout popn 1 jmp .finally .finally: del arg1Str del arg2Str lrtn ; ------------------------------------- initialiseConnection: // parameters nvar 0, addressStr nvar 1, portNumStr // local variables nvar 2, portNumInt nvar 3, connID nvar 4, sendResult nvar 5, errorCode // entf 5, 2 // sexh .errorOccurred // convert the port number into an integer push portNumStr farc strlib.StrToInt popr portNumInt, 1 // connect to the specified server push addressStr push portNumInt farc net.connect popr connID, 2 // exit if a connection wasnt made jlz connID, .errorOccurred // send nick information to server push connID call sendNick popr sendResult, 1 // jlz sendResult, .errorOccurred // send user information to server push connID call sendUser popr sendResult, 1 // jlz sendResult, .errorOccurred // try to join a channel //push connID //call joinChannel //popr sendResult, 1 //// //jlz sendResult, .errorOccurred // lret connID .errorOccurred: pshd connectErrMsg call printStr popn 1 farc net.getLastError pop errorCode pshd errorCodeMsg call printStr popn 1 push errorCode pshi 16 farc strlib.IntToStr farc std.cout popn 3 pshd newlineStr call printStr popn 1 push connID call disconnect lrti 0 ; ------------------------------------- sendNick: nvar 0, connID nvar 1, nickStrLen nvar 2, sendResult nvar 3, canSend entf 4, 1 pshd nickStr farc std.strlen popr nickStrLen, 1 .cantSend: // see if data can be sent push connID farc net.canSend popr canSend, 1 // // loop until the data can be sent jz canSend, .cantSend // if an error occurred, give up jlz canSend, .errorOccurred push connID pshd nickStr push nickStrLen call sendData popr sendResult, 3 // return the number of bytes sent lret sendResult .errorOccurred: lrti -1 ; ------------------------------------- sendUser: nvar 0, connID nvar 1, sendResult nvar 2, userStrLen entf 3, 1 pshd userStr farc std.strlen popr userStrLen, 1 push connID pshd userStr push userStrLen call sendData popr sendResult, 2 // return the number of bytes sent lret sendResult //push connID //farc net.canSend //popr canSend, 1 ; ------------------------------------- joinChannel: nvar 0, connID nvar 1, sendResult nvar 2, joinStrLen entf 3, 1 pshd userStr farc std.strlen popr joinStrLen, 1 push connID pshd joinStr push joinStrLen call sendData popr sendResult, 2 // return the number of bytes sent lret sendResult ; ------------------------------------- mainLoop2: nvar 0, connID nvar 1, recvResult nvar 2, sendResult nvar 3, readLineResult nvar 4, errorCode // return codes nvar 5, codeConnLost nvar 6, codeConnError nvar 7, dataBuffer nvar 8, dataBufferSize nvar 9, newlineOffs nvar 10, lineStr nvar 11, lineLength entf 12, 1 movi codeConnLost, -2 movi codeConnError, -1 movi dataBufferSize, 0 movi dataBuffer, 0 .loop: // pshd mainLoopStr // call printStr // popn 1 // try to read stuff from the connection push connID padr dataBuffer push dataBufferSize call readLine popr readLineResult, 3 // connection was closed (-2) jcmp readLineResult, codeConnLost, .connectionClosed // error on connection (-1) jlz readLineResult, .errorEncountered // else a line of text was read from the data stream mov dataBufferSize, readLineResult // while data contains a newline //push dataBuffer //push dataBufferSize //call containsNewline //popr newlineOffs, 3 push connID padr dataBuffer push dataBufferSize call processData popr dataBufferSize, 3 //mov newlineOffs, readLineResult //// get the length of the first line in the buffer //mov lineLength, newlineOffs //sub lineLength, dataBuffer //inc lineLength //// extract the line from the buffer //push lineStr //push dataBuffer //pshi 0 //push lineLength //farc std.substr //popn 4 // check the data buffer to see if its a ping request .noDataReceived: jmp .loop .connectionClosed: pshd connClosedMsg farc std.cout popn 1 .loopEnd: del dataBuffer lrtn .errorEncountered: pshd connLostMsg farc std.cout popn 1 jmp .loopEnd ; ------------------------------------- ; // reads data from the connection until ; // a newline '\n' is sent - returns the ; // character index of the first newline ; int readLine(uint connID, char** buff, uint size) readLine: // parameters nvar 0, connID nvar 1, dataBufferPtr nvar 2, dataBufferSize // local variables nvar 3, recvBuffer nvar 4, recvBufferSize nvar 5, recvDataSize nvar 6, dataBufferOffs nvar 7, containsNewln nvar 8, codeConnLost nvar 9, codeConnError nvar 10, dataBuffer entf 11, 3 // setup a temporary buffer to store received data movi recvBufferSize, 1024 new recvBuffer, recvBufferSize // start writing into the data buffer at its beginning movi dataBufferOffs, 0 // setup error codes movi codeConnLost, -2 movi codeConnError, -1 .loop: // pshd readLineLoopStr // call printStr // popn 1 // try to read data from the connection push connID push recvBuffer push recvBufferSize call recvData popr recvDataSize, 3 // if there is no data to receive jz recvDataSize, .loop // or the connection was closed (-2) jcmp recvDataSize, codeConnLost, .connectionClosed // or there was an error on the connection (-1) jcmp recvDataSize, codeConnError, .errorEncountered // else data was received // copy received data into passed in buffer push dataBufferPtr push dataBufferSize push recvBuffer push recvDataSize push dataBufferOffs call appendBuffers popr dataBufferSize, 5 // increment the write offset to account for appended data add dataBufferOffs, recvDataSize //pshd rlStr0 //call printStr //push dataBufferOffs //call printInt //pshd rlStr1 //call printStr //push recvBuffer //call printStr //popn 4 // NOTICE AUTH :*** Looking up your hostname jz dataBufferPtr, .nullDataBuffer // dereference the pointer load dataBuffer, dataBufferPtr // store the new buffer pointer // stor dataBufferPtr, dataBuffer // search the buffer for a newline push dataBuffer push dataBufferSize call containsNewline popr containsNewln, 2 // if the buffer does not contain a newline char, loop jz containsNewln, .loop // return the size of the buffer (only if the buffer contains a newline) lret dataBufferSize .nullDataBuffer: jmp .loop .connectionClosed: lrti -2 .errorEncountered: pshd connLostMsg farc std.cout popn 1 lrti -1 ; ------------------------------------- ; // ; void processData(byte** buff, uint size) processData: nvar 0, connID nvar 1, buffPtr nvar 2, size // nvar 3, buff nvar 4, nlOffset nvar 5, lineBuff nvar 6, lineBuffSize // entf 7, 3 // .loop: load buff, buffPtr // does the data contain a newline? push buff push size call containsNewline popr nlOffset, 2 // if the buffer contains a newline char jnz nlOffset, .newlineFound // else return jmp .finished .newlineFound: // get the offset relative to buff sub nlOffset, buff inc nlOffset // set the temp buffer to be +1 in size (for \0) mov lineBuffSize, nlOffset inc lineBuffSize new lineBuff, lineBuffSize // copy out the newline push lineBuff push buff pshi 0 push nlOffset farc std.substr popn 4 // remove the copied out data from the buffer push buffPtr push size push nlOffset call lefttrimBuffer popr size, 3 // process the line push connID push lineBuff push lineBuffSize call processLine popn 3 // loop until nothing is left jmp .loop .finished: // return the size of the resized buffer lret size ; ------------------------------------- processLine: // parameters nvar 0, connID nvar 1, buff nvar 2, size // local variables nvar 3, isPing nvar 4, retVal // entf 5, 3 // print out the received line of data push buff call printInput popn 1 .pingTest: // determine if the line is a ping request push buff push size call isPingReq popr isPing, 1 // if it is a ping request jnz isPing, .isPing // else continue .isntPing: //pshd plStr1 //call printStr //popn 1 jmp .finished .isPing: // //pshd plStr0 //call printStr //popn 1 // construct a pong response push connID push buff push size call sendPong popr retVal, 3 .finished: lrtn ; ------------------------------------- isPingReq: // parameters nvar 0, buff nvar 1, size // local variables nvar 3, pingBuff nvar 4, pingBuffSz nvar 5, isPing entf 6, 2 // get the first 6 characters of the buffer (7 for \0) movi pingBuffSz, 7 new pingBuff, pingBuffSz dec pingBuffSz push pingBuff push buff push pingBuffSz farc std.memcpy // if they are equal to 'PING :' push pingBuff pshd pingStr push pingBuffSz farc std.memcmp popr isPing, 3 del pingBuff jz isPing, .isPing lrti 0 .isPing: lrti 1 ; ------------------------------------- sendPong: // parameters nvar 0, connID nvar 1, buff nvar 2, size // local variables nvar 3, pongData nvar 4, pongDataSz nvar 5, prefixLen nvar 6, retVal nvar 7, resp nvar 8, respLen nvar 9, dataOffs // entf 10, 3 // create a buffer to hold the response mov respLen, size new resp, respLen // get the length of the pong prefix pshd pongStr farc std.strlen popr prefixLen, 1 // copy the pongStr string into the response buffer push resp pshd pongStr push prefixLen farc std.memcpy popn 3 // get the pointer to where to start writing th pong data mov dataOffs, resp add dataOffs, prefixLen // get the size of the unique ping data mov pongDataSz, size sub pongDataSz, prefixLen // copy the data into the buffer padr dataOffs push pongDataSz push buff push prefixLen push pongDataSz call subBuffer popr retVal, 5 // jz retVal, .errorOccurred // push connID push resp push size call sendData popr retVal, 3 // del resp // jz retVal, .errorOccurred push connID call joinChannel popn 1 lrti 1 .errorOccurred: lrti 0 ; ------------------------------------- containsNewline: nvar 0, buff nvar 1, size // nvar 2, nlOffset // entf 3, 2 // check if the data contain a newline push buff pshi 10 ; newline character value (0x0A) push size farc std.memchr popr nlOffset, 3 lret nlOffset ; ------------------------------------- disconnect: nvar 0, connID entf 1, 1 pshd disconnectMsg call printStr popn 1 farc net.disconnect lrtn ; ------------------------------------- ; char* parseArgs(char* src, char* separator, char** arg1, char** arg2) parseArgs: // parameters nvar 0, srcStr nvar 1, separatorStr nvar 2, arg1ptr nvar 3, arg2ptr // local variables nvar 4, srcLength nvar 5, sepOffset nvar 6, arg1str nvar 7, arg1len nvar 8, arg2str nvar 9, arg2len // enter a new stack frame entf 10, 4 // set exception handler sexh .exceptionHandler // get the length of the source string push srcStr farc std.strlen popr srcLength, 1 // find the offset of the separator character ':' push srcStr push separatorStr pshi 0 farc std.indexof popr sepOffset, 3 // allocate space for the first argument mov arg1len, sepOffset inc arg1len new arg1str, arg1len // get the first argument push arg1str push srcStr pshi 0 push sepOffset farc std.substr popn 4 // increment the offset to skip over the seaprator cahracter inc sepOffset // allocate space for the second argument mov arg2len, srcLength sub arg2len, sepOffset inc arg2len new arg2str, arg2len // get the second argument push arg2str push srcStr push sepOffset push arg2len farc std.substr popn 4 jmp .finished .exceptionHandler: pshd errorStr farc std.cout popn 1 .missingSeparator: // return null if the separator couldnt be found or there was an error lrti 0 .finished: stor arg1ptr, arg1str stor arg2ptr, arg2str // return 1 to indicate success lrti 1 ; ------------------------------------- printConnectingMsg: entf 2, 2 // print out connection message pshd connectingMsg0 call printStr push 0 call printStr pshd connectingMsg1 call printStr push 1 call printStr pshd connectingMsg2 call printStr pshd newlineStr call printStr popn 6 lrtn ; ------------------------------------- printStr: entf 1, 1 push 0 farc std.cout popn 1 lrtn ; ------------------------------------- printInt: entf 1, 1 push 0 pshi 10 farc strlib.IntToStr farc std.cout popn 3 pshd newlineStr call printStr popn 1 lrtn ; ------------------------------------- printInput: nvar 0, buff entf 1, 1 pshd inputPrefix farc std.cout popn 1 push buff farc std.cout popn 1 lrtn ; ------------------------------------- printOutput: nvar 0, buff entf 1, 1 pshd outputPrefix farc std.cout popn 1 push buff farc std.cout popn 1 lrtn ; ------------------------------------- stayAlive: retn ; ------------------------------------- partChannel: retn ; ------------------------------------- sendData: nvar 0, connID nvar 1, dataPtr nvar 2, dataLen nvar 3, sendResult nvar 4, canSend entf 5, 3 //pshd sdStr0 //call printStr //push dataPtr //call printStr //pshd sdStr1 //call printStr //push dataLen //call printInt //popn 4 // can data be written to the connection? push connID farc net.canSend popr canSend, 1 // if zero, no data available jz canSend, .cantSend // if less than zero, error occurred jlz canSend, .errorEncountered push dataPtr farc std.strlen popr dataLen, 1 push connID push dataPtr push dataLen farc net.send popr sendResult, 3 // push dataPtr call printOutput popn 1 // return the number of bytes sent lret sendResult .cantSend: lrti 0 .errorEncountered: lrti -1 ; ------------------------------------- ; // returns the number of bytes read ; int recvData(uint connID, char* buff, uint size) recvData: nvar 0, connID nvar 1, buff nvar 2, size nvar 3, canRecv nvar 4, recvResult entf 5, 3 // is data available to be read from the connection? push connID farc net.canRecv popr canRecv, 1 // if zero, no data available jz canRecv, .noData // if less than zero, error occurred jlz canRecv, .errorEncountered // fill the data buffer with null push buff pshi 0 push size farc std.memset popn 3 // populate the data buffer with data from the socket push connID push buff push size farc net.recv popr recvResult, 3 // if nothing was read, exit jz recvResult, .connectionClosed // if the result is < 0, the conn closed or there was an error jlz recvResult, .errorEncountered lret recvResult .noData: lrti 0 .errorEncountered: lrti -1 .connectionClosed: lrti -2 ; ------------------------------------- quit: retn ; ------------------------------------- memset: nvar 0, buff nvar 1, size // nvar 2, offs nvar 3, dptr nvar 4, wval nvar 5, diff // entf 6, 2 // movi offs, 0 movi wval, 0 mov dptr, buff // .loop: stor dptr, wval inc dptr mov diff, dptr sub diff, buff jg diff, size, .endLoop jmp .loop .endLoop: lrtn |