Overview | Package | Class | Source | Class tree | Glossary | UnrealScript Documentation |
previous class next class | frames no frames |
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 00024 00025 00026 00027 00028 00029 00030 00031 00032 00033 00034 00035 00036 00037 00038 00039 00040 00041 00042 00043 00044 00045 00046 00047 00048 00049 00050 00051 00052 00053 00054 00055 00056 00057 00058 00059 00060 00061 00062 00063 00064 00065 00066 00067 00068 00069 00070 00071 00072 00073 00074 00075 00076 00077 00078 00079 00080 00081 00082 00083 00084 00085 00086 00087 00088 00089 00090 00091 00092 00093 00094 00095 00096 00097 00098 00099 00100 00101 00102 00103 00104 00105 00106 00107 00108 00109 00110 00111 00112 00113 00114 00115 00116 00117 00118 00119 00120 00121 00122 00123 00124 00125 00126 00127 00128 00129 00130 00131 00132 00133 00134 00135 00136 00137 00138 00139 00140 00141 00142 00143 00144 00145 00146 00147 00148 00149 00150 00151 00152 00153 00154 00155 00156 00157 00158 00159 00160 00161 00162 00163 00164 00165 00166 00167 00168 00169 00170 00171 00172 00173 00174 00175 00176 00177 00178 00179 00180 00181 00182 00183 00184 00185 00186 00187 00188 00189 00190 00191 00192 00193 00194 00195 00196 00197 00198 00199 00200 00201 00202 00203 00204 00205 00206 00207 00208 00209 00210 00211 00212 00213 00214 00215 00216 00217 00218 00219 00220 00221 00222 00223 00224 00225 00226 00227 00228 00229 00230 00231 00232 00233 00234 00235 00236 00237 00238 00239 00240 00241 00242 00243 00244 00245 00246 00247 00248 00249 00250 00251 00252 00253 00254 00255 00256 00257 00258 00259 00260 00261 00262 00263 00264 00265 00266 00267 00268 00269 00270 00271 00272 00273 00274 00275 00276 00277 00278 00279 00280 00281 00282 00283 00284 00285 00286 00287 00288 00289 00290 00291 00292 00293 00294 00295 00296 00297 00298 00299 00300 00301 00302 00303 00304 00305 00306 00307 00308 00309 00310 00311 00312 00313 00314 00315 00316 00317 00318 00319 00320 00321 00322 00323 00324 00325 00326 00327 00328 00329 00330 00331 00332 00333 00334 00335 00336 00337 00338 00339 00340 00341 00342 00343 00344 00345 00346 00347 00348 00349 00350 00351 00352 00353 00354 00355 00356 00357 00358 00359 00360 00361 00362 00363 00364 00365 00366 00367 00368 00369 00370 00371 00372 00373 00374 00375 00376 00377 00378 00379 00380 00381 00382 00383 00384 00385 00386 00387 00388 00389 00390 00391 00392 00393 00394 00395 00396 00397 00398 00399 00400 00401 00402 00403 00404 00405 00406 00407 00408 00409 00410 00411 00412 00413 00414 00415 00416 00417 00418 00419 00420 00421 00422 00423 00424 00425 00426 00427 00428 00429 00430 00431 00432 00433 00434 00435 00436 00437 00438 00439 00440 00441 00442 00443 00444 00445 00446 00447 00448 00449 00450 00451 00452 00453 00454 00455 00456 00457 00458 00459 00460 00461 00462 00463 00464 00465 00466 00467 00468 00469 00470 00471 00472 00473 00474 00475 00476 00477 00478 00479 00480 00481 00482 00483 00484 00485 00486 00487 00488 00489 00490 00491 00492 00493 00494 00495 00496 00497 00498 00499 00500 00501 00502 00503 00504 00505 00506 00507 00508 00509 00510 00511 00512 00513 00514 00515 00516 00517 00518 00519 00520 00521 00522 00523 00524 00525 00526 00527 00528 00529 00530 00531 00532 00533 00534 00535 00536 00537 00538 00539 00540 00541 00542 00543 00544 00545 00546 00547 00548 00549 00550 00551 00552 00553 00554 00555 00556 00557 00558 00559 00560 00561 00562 00563 00564 00565 00566 00567 00568 00569 00570 00571 00572 00573 00574 00575 00576 00577 00578 00579 00580 00581 00582 00583 00584 00585 00586 00587 00588 00589 00590 00591 00592 00593 00594 00595 00596 00597 00598 00599 00600 00601 00602 00603 00604 00605 00606 00607 00608 00609 00610 00611 00612 00613 00614 00615 00616 00617 00618 00619 00620 00621 00622 00623 00624 00625 00626 00627 00628 00629 00630 00631 00632 00633 00634 00635 00636 00637 00638 00639 00640 00641 00642 00643 00644 00645 00646 00647 00648 00649 00650 00651 00652 00653 00654 00655 00656 00657 00658 00659 00660 00661 00662 00663 00664 00665 00666 00667 00668 00669 00670 00671 00672 00673 00674 00675 00676 00677 00678 00679 00680 00681 00682 00683 00684 00685 00686 00687 00688 00689 00690 00691 00692 00693 00694 00695 00696 00697 00698 00699 00700 00701 00702 00703 00704 00705 00706 00707 00708 00709 00710 00711 00712 00713 00714 00715 00716 00717 00718 00719 00720 00721 00722 00723 00724 00725 00726 00727 00728 00729 00730 00731 00732 00733 00734 00735 00736 00737 00738 00739 00740 00741 00742 00743 00744 00745 00746 00747 00748 00749 00750 00751 00752 00753 00754 00755 00756 00757 00758 00759 00760 00761 00762 00763 00764 00765 00766 00767 00768 00769 00770 00771 00772 00773 00774 00775 00776 00777 00778 00779 00780 00781 00782 00783 00784 00785 00786 00787 00788 00789 00790 00791 00792 00793 00794 00795 00796 00797 00798 00799 00800 00801 00802 00803 00804 00805 00806 00807 00808 00809 00810 00811 00812 00813 00814 00815 00816 00817 00818 00819 00820 00821 00822 00823 00824 00825 00826 00827 00828 00829 00830 00831 00832 00833 00834 00835 00836 00837 00838 00839 00840 00841 00842 00843 00844 00845 00846 00847 00848 00849 00850 00851 00852 00853 00854 00855 00856 00857 00858 00859 00860 00861 00862 00863 00864 00865 00866 00867 00868 00869 00870 00871 00872 00873 00874 00875 00876 00877 00878 00879 00880 00881 00882 00883 00884 00885 00886 00887 00888 00889 00890 00891 00892 00893 00894 00895 00896 00897 00898 00899 00900 00901 00902 00903 00904 00905 00906 00907 00908 00909 00910 00911 00912 00913 00914 00915 00916 00917 00918 00919 00920 00921 00922 00923 00924 00925 00926 00927 00928 00929 00930 00931 00932 00933 00934 00935 00936 00937 00938 00939 00940 00941 00942 00943 00944 00945 00946 00947 00948 00949 00950 00951 00952 00953 00954 00955 00956 00957 00958 00959 00960 00961 00962 00963 00964 00965 00966 00967 00968 00969 00970 00971 00972 00973 00974 00975 00976 00977 00978 00979 00980 00981 00982 00983 00984 00985 00986 00987 00988 00989 00990 00991 00992 00993 00994 00995 00996 00997 00998 00999 01000 01001 01002 01003 01004 01005 01006 01007 01008 01009 01010 01011 01012 01013 01014 01015 01016 01017 01018 01019 01020 01021 01022 01023 01024 01025 01026 01027 01028 01029 01030 01031 01032 01033 01034 01035 01036 01037 01038 01039 01040 01041 01042 01043 01044 01045 01046 01047 01048 01049 01050 01051 01052 01053 01054 01055 01056 01057 01058 01059 01060 01061 01062 01063 01064 01065 01066 01067 01068 01069 01070 01071 01072 01073 01074 01075 01076 01077 01078 01079 01080 01081 01082 01083 01084 01085 01086 01087 01088 01089 01090 01091 01092 01093 01094 01095 01096 01097 01098 01099 01100 01101 01102 01103 01104 01105 01106 01107 01108 01109 01110 01111 01112 01113 01114 01115 01116 01117 01118 01119 01120 01121 01122 01123 01124 01125 01126 01127 01128 01129 01130 01131 01132 01133 01134 01135 01136 01137 01138 01139 01140 01141 01142 01143 01144 01145 01146 01147 01148 01149 01150 01151 01152 01153 01154 01155 01156 01157 01158 01159 01160 01161 01162 01163 01164 01165 01166 01167 01168 01169 01170 01171 01172 01173 01174 01175 01176 01177 01178 01179 01180 01181 01182 01183 01184 01185 01186 01187 01188 01189 01190 01191 01192 01193 01194 01195 01196 01197 01198 01199 01200 01201 01202 01203 01204 01205 01206 01207 01208 01209 01210 01211 01212 01213 01214 01215 01216 01217 01218 01219 01220 01221 01222 01223 01224 01225 01226 01227 01228 01229 01230 01231 01232 01233 01234 01235 01236 01237 01238 01239 01240 01241 01242 01243 01244 01245 01246 01247 01248 01249 01250 01251 01252 01253 01254 01255 01256 01257 01258 01259 01260 01261 01262 01263 01264 01265 01266 01267 01268 01269 01270 01271 01272 01273 01274 01275 01276 01277 01278 01279 01280 01281 01282 01283 01284 01285 01286 01287 01288 01289 01290 01291 01292 01293 01294 01295 01296 01297 01298 01299 01300 01301 01302 01303 01304 01305 01306 01307 01308 01309 01310 01311 01312 01313 01314 01315 01316 01317 01318 01319 01320 01321 01322 01323 01324 01325 01326 01327 01328 01329 01330 01331 01332 01333 01334 01335 01336 01337 01338 01339 01340 01341 01342 01343 01344 01345 01346 01347 01348 01349 01350 01351 01352 01353 01354 01355 01356 01357 01358 01359 |
/****************************************************************************** Copyright (c) 2005-2007 by Wormbo <wormbo@online.de> $Id$ Manages an IRC connection. ******************************************************************************/ class WIR_IRCConnection extends WIR_IRCBase config; //============================================================================= // Constants //============================================================================= /** Number of milliseconds per week. Used to handle time intervals at Level.DayOfWeek wrap-around */ const MILLISECONDS_PER_WEEK = 604800000; //const MILLISECONDS_PER_DAY = 86400000; //============================================================================= // Enums //============================================================================= enum EModeType { MT_Unknown, MT_Join, MT_Part, MT_Quit, MT_Kick, MT_Nick, }; //============================================================================= // Structs //============================================================================= struct TKeyValuePair { var string Key; var string Value; }; struct TSentBytes { var int Timestamp; var int Length; }; struct TMessage { var string Message; var int Timestamp; var bool bImportant; }; //============================================================================= // Localization //============================================================================= var localized string MsgBotJoinChat; var localized string MsgBotJoinChatTwoWay; var localized string MsgBotJoinReport; var localized string MsgBotJoinReportChat; var localized string MsgBotJoinReportChatTwoWay; var localized string MsgBotFlooding; //============================================================================= // Configuration //============================================================================= var config string LastTopicTemplate; //============================================================================= // Configured Variables //============================================================================= // flood protection var int FloodProtectCommandPenalty; var int FloodProtectMessagePenalty; var int FloodProtectMaxPenalty; var int FloodProtectBytes; var int FloodProtectBytesDuration; var int MaxLenMessageQueue; // automatically restore connections var bool bAutoReconnect; var int ReconnectDelay; var bool bAutoRejoin; var int ConnectTimeout; // debug logging var string DebugLogFileName; var bool bDebugStartMatch; //============================================================================= // Variables //============================================================================= var MutWormbot MyMutator; // connection parameters var string ServerName "DNS name of IRC server."; var string ServerPassword "Password required to connect to the server."; var IpAddr ServerAddress "Resolved server address and port."; var string UserNick "User's current nick name"; var string UserIdent "User ident"; var string RealServerName "DNS name of IRC server as reported by the server itself."; // registration handler var WIR_RegistrationHandler RegistrationHandler "Handles the registration on the IRC server."; // channel parameters var string ChannelName "Name of the channel to join after login and registration."; var string ChannelKey "Password required to join the channel."; var string ChannelTopic "Channel topic received from the server."; // current state var bool bRegistered "Whether this client already registered on the server."; var bool bNoChannelColor; var bool bNoChannelCodes; var int TimestampNow "Current timestamp."; var int TimestampTimeout "Tracks timeouts."; var int ChannelJoinTime "Timestamp when the channel was joined to delay the first message."; var array<TKeyValuePair> NetworkParameters "Parameters used by this server."; // flood protection var int SendLimitTime "Simple flood protection. If this is more than 10s ahead of the current timestamp, nothing is sent this tick."; var array<TSentBytes> SentBytes "Bytes sent in the last few seconds."; var array<string> CommandQueue "Holds the commands to be sent as soon as the flood protection allows."; var array<TMessage> MessageQueue "Holds other messages to be sent when no commands are queued."; // debug log file var FileLog LogFile "Log file handle"; //----------------------------------------------------------------------------- // Delegates //----------------------------------------------------------------------------- //== delegate OnTimeout ======================================================= /** Called on timeout. Used internally. */ //============================================================================= delegate OnTimeout() { warn("OnTimeout delegate not assigned"); } //== delegate OnChat ========================================================== /** Called when someone in the channel talks. */ //============================================================================= delegate OnChat(string Nick, string Message); //== delegate OnMode ========================================================== /** Called when something in the channel changes. */ //============================================================================= delegate OnMode(EModeType Type, string Nick, optional string Message); //----------------------------------------------------------------------------- // Interface functions //----------------------------------------------------------------------------- //== SetConnection ============================================================ /** Sets the registration handler class. */ //============================================================================= function SetConnection(string Server, int Port, string Password, string Nick, string Ident) { local bool bNewServer, bNewNick, bNewIdent; bNewServer = ServerName != Server || ServerAddress.Port != Port; bNewNick = UserNick != Nick; bNewIdent = UserIdent != Ident; ServerName = Server; RealServerName = Server; ServerAddress.Port = Port; ServerPassword = Password; UserNick = Nick; UserIdent = Ident; if ( bNewServer ) Disconnect(True, "Changing Servers"); else if ( bNewIdent ) Disconnect(True, "Changing User Ident"); else if ( bNewNick ) SetNick(UserNick); } //== SetNick ================================================================ /** changes the client's nick name. */ //=========================================================================== function SetNick(string NewNick) { UserNick = NewNick; } //== SetRegistrationHandler =================================================== /** Sets the registration handler class. */ //============================================================================= function SetRegistrationDetails(class<WIR_RegistrationHandler> HandlerClass, string RegistrationDetails) { local WIR_RegistrationHandler NewHandler; if ( HandlerClass == None ) HandlerClass = class'WIR_RegistrationHandler'; if ( RegistrationHandler != None && RegistrationHandler.Class == HandlerClass ) { log("IRC registration handler class" @ HandlerClass @ "already set."); return; } NewHandler = new(Self) HandlerClass; if ( NewHandler == None ) log("Error creating IRC registration handler class" @ HandlerClass); if ( NewHandler != None ) { if ( RegistrationHandler != None ) RegistrationHandler = None; RegistrationHandler = NewHandler; } else if ( RegistrationHandler == None ) { RegistrationHandler = new(Self) class'WIR_RegistrationHandler'; } else { return; } RegistrationHandler.SetDetails(RegistrationDetails); } //== Connect ================================================================== /** Try to establish a connection to the IRC server. */ //============================================================================= function Connect(); //== Disconnect =============================================================== /** Disconnect from the currently used IRC server. */ //============================================================================= function Disconnect(bool bReconnect, string QuitMessage); //== JoinChannel ============================================================== /** Join a channel. */ //============================================================================= function JoinChannel(string Channel, string Key) { ChannelName = Channel; ChannelKey = Key; } //== IsTargetChannel ============================================================== /** Return, whether the channel is a target channel. */ //============================================================================= function bool IsTargetChannel(string Target) { local array<string> Channels; Split(ChannelName, ",", Channels); while(Channels.Length > 0) { if (Channels[0] ~= Target) return true; Channels.Remove(0, 1); } return false; } //== PartChannel ============================================================== /** Parts the channel. */ //============================================================================= function PartChannel() { ChannelName = ""; ChannelKey = ""; } //== ImportantMessagesQueued ========================================================= /** Returns whether there are currently important messages in the queue. */ //============================================================================= function bool ImportantMessagesQueued() { local int i; for (i = 0; i < MessageQueue.Length; ++i) if ( MessageQueue[i].bImportant ) return true; return false; } //== SendMessage ============================================================== /** Sends a message to the channel. */ //============================================================================= function SendMessage(string Message, optional bool bImportant) { Message = class'WIR_Util'.static.Trim(Message); if ( Message != "" && ChannelName != "" ) SendCommand("PRIVMSG" @ ChannelName @ ":" $ Message, False, bImportant); } //== GetNetworkParameter ====================================================== /** Returns true if the specified parameter is set on this network and sets Value. */ //============================================================================= function bool GetNetworkParameter(string Key, out string Value) { local int i; for (i = 0; i < NetworkParameters.Length; i++) { //log(NetworkParameters[i].Key); if ( NetworkParameters[i].Key ~= Key ) { Value = NetworkParameters[i].Value; return true; } } return false; } //== Chomp ==================================================================== /** Removes a leading ":" from a string. */ //============================================================================= function string Chomp(string S) { if ( Left(S, 1) == ":" ) return Mid(S, 1); return S; } //----------------------------------------------------------------------------- // Internal functions //----------------------------------------------------------------------------- //== PostBeginPlay ============================================================ /** Initializes the IRC socket. */ //============================================================================= event PostBeginPlay() { Super.PostBeginPlay(); if (LastTopicTemplate != "") { LastTopicTemplate = class'WIR_Util'.static.EscapeIRCCodes(LastTopicTemplate); } MyMutator = MutWormbot(Owner); LinkMode = MODE_Line; ReceiveMode = RMODE_Event; } //== IsFlooding =============================================================== /** Returns whether the client is flodding the server with messages. */ //============================================================================= function bool IsFlooding(int LenNextCommand) { local int i, NumBytes; if ( SendLimitTime - TimestampNow < FloodProtectMaxPenalty * 1000 ) { //log("No Line Flood."@SendLimitTime - TimestampNow); return false; } for (i = 0; i < SentBytes.Length; i++) { if ( SentBytes[i].Timestamp > TimestampNow - FloodProtectBytesDuration * 1000 ) NumBytes += SentBytes[i].Length; else SentBytes.Remove(i, SentBytes.Length - i); } if ( NumBytes + Min(LenNextCommand, FloodProtectBytes) < FloodProtectBytes ) { //log("No Byte Flood."@NumBytes + Min(LenNextCommand, FloodProtectBytes)); return false; } return true; } //== SendCommand ============================================================== /** Queues a command for sending. */ //============================================================================= function SendCommand(string Command, bool bIsCommand, optional bool bImportant) { if ( Command != "" ) { if ( bIsCommand ) { if ( CommandQueue.Length == 0 || CommandQueue[CommandQueue.Length-1] != Command ) CommandQueue[CommandQueue.Length] = Command; } else if ( MessageQueue.Length == 0 || MessageQueue[MessageQueue.Length-1].Message != Command ) { MessageQueue.Insert(MessageQueue.Length, 1); MessageQueue[MessageQueue.Length - 1].Message = Command; MessageQueue[MessageQueue.Length - 1].bImportant = bImportant; MessageQueue[MessageQueue.Length - 1].Timestamp = TimestampNow; } } } //== SendText ================================================================= /** Logs the sent text. */ //============================================================================= function int SendText(string Line) { local int i; Line = class'WIR_Util'.static.StripColorCodes(class'WIR_Util'.static.UnescapeIRCCodes(Line)); DebugLog("<<--" @ Line); if ( SendLimitTime < TimestampNow ) SendLimitTime = TimestampNow; if ( Left(Line, 8) ~= "PRIVMSG " ) { SendLimitTime += FloodProtectMessagePenalty; if ( bNoChannelCodes ) Line = class'WIR_Util'.static.StripIRCCodes(Line); else if ( bNoChannelColor ) Line = class'WIR_Util'.static.StripIRCCodes(Line, True); } else SendLimitTime += FloodProtectCommandPenalty; i = Super.SendText(Line); SentBytes.Insert(0, 1); SentBytes[0].Length = i; SentBytes[0].Timestamp = SendLimitTime; return i; } //== ReceivedLine ============================================================= /** Logs the received text. */ //============================================================================= event ReceivedLine(string Line) { local string Server, Command, Target; DebugLog("-->>" @ Line); if ( IsInState('ConnectionEstablished') ) { if ( Left(Line, 1) == ":" ) { if ( Divide(Line, " ", Server, Line) && Divide(Line, " ", Command, Line) ) { if ( !Divide(Line, " ", Target, Line) ) { Target = Line; Line = ""; } if ( int(Command) > 0 ) HandleNumericReply(int(Command), Server, Target, Line); else HandleStringReply(Command, Server, Target, Line); return; } } else if ( Divide(Line, " ", Command, Line) && Command == Caps(Command) ) { HandleCommand(Command, Line); return; } DebugLog("???? Unknown message format"); } } //== HandleCommand ============================================================ /** Handles a string command received from the server. */ //============================================================================= function HandleCommand(string Command, string Parameters) { switch (Command) { case "PING": SendText("PONG" @ Parameters); return; case "ERROR": log("IRC Received Error:" @ Chomp(Parameters)); //Close(); //Disconnect(True, ""); return; default: if ( !bRegistered && RegistrationHandler != None ) RegistrationHandler.ReceivedCommand(Command, Parameters); } } //== HandleStringReply ============================================================ /** Handles a string message received from the server. */ //============================================================================= function HandleStringReply(string Command, string Server, string Target, string Parameters) { local string Nick; if ( !bRegistered && RegistrationHandler != None ) RegistrationHandler.ReceivedString(Command, Server, Target, Parameters); if ( Command ~= "TOPIC" ) { ChannelTopic = class'WIR_Util'.static.EscapeIRCCodes(Chomp(Parameters)); if ( MyMutator.bSetReportChannelTopic && IsTargetChannel(Target) && MyMutator.ReportConnection == Self && class'WIR_Util'.static.ParseIRCUserAddress(Chomp(Server), Nick) && Nick != UserNick ) UpdateTopic(); } } //== HandleNumericReply ======================================================= /** Handles a numeric message received from the server. */ //============================================================================= function HandleNumericReply(int Command, string Server, string Target, string Parameters) { if ( Command != 433 && Target != UserNick ) { log("Nick changed from" @ UserNick @ "to" @ Target, Name); UserNick = Target; } if ( !bRegistered && RegistrationHandler != None ) RegistrationHandler.ReceivedNumeric(Command, Server, Target, Parameters); if ( Command == RPL_TOPIC ) { Divide(Parameters, " :", Parameters, ChannelTopic); ChannelTopic = class'WIR_Util'.static.EscapeIRCCodes(ChannelTopic); if ( MyMutator.bSetReportChannelTopic && IsTargetChannel(Parameters) && MyMutator.ReportConnection == Self ) UpdateTopic(); } else if ( Command == RPL_NOTOPIC ) { ChannelTopic = ""; if ( MyMutator.bSetReportChannelTopic && IsTargetChannel(Parameters) && MyMutator.ReportConnection == Self ) UpdateTopic(); } } //== UpdateTopic ============================================================== /** Updates the channel topic. */ //============================================================================= function UpdateTopic(optional bool bLeaving) { local string BotPart, UserPart; local string NewTopic, OldTopic; local array<string> Parts; local int i, TopicLen, NewTopicLen; local bool bTopicMatches; local string TopicLenString; NewTopic = Repl(MyMutator.ReportChannelTopicTemplate, "%%chan%%", ChannelName, True); OldTopic = ChannelTopic; if ( GetNetworkParameter("topiclen", TopicLenString) ) TopicLen = int(TopicLenString); if ( InStr(NewTopic, "%%botpart%%") == -1 ) { log("ERROR: No '%%botpart%%' in ReportChannelTopicTemplate - channel topic will not be set."); return; } if ( bLeaving || MyMutator.bMatchEnded || !MyMutator.bMatchStarted && Level.Game.GetNumPlayers() == 0 ) { BotPart = MyMutator.ReportChannelTopicBotIdle; } else { BotPart = MyMutator.ReportChannelTopicBotPart; BotPart = Repl(BotPart, "%%game%%", Level.Game.GameName, True); BotPart = Repl(BotPart, "%%map%%", Level.Title, True); } if ( InStr(LastTopicTemplate, "%%userpart%%") != -1 && OldTopic != "" ) { // We need to identify the userpart in the current topic. bTopicMatches = True; // To do this we first take apart the topic template... if ( Split(LastTopicTemplate, "%%", Parts) > 0 ) { for (i = Parts.Length - 1; i > 0; --i) { if ( Parts[i] != "botpart" && Parts[i-1] != "botpart" && Parts[i] != "userpart" && Parts[i-1] != "userpart" ) { Parts[i-1] $= "%%" $ Parts[i]; Parts.Remove(i, 1); } //else //log("Parts:"@Parts[i]); } //log("Parts:"@Parts[0]); // ...and then match the individual parts against the current topic. // If neither botpart nor userpart of the template are the beginning of the topic, try matching the first part. if ( Parts[0] != "botpart" && Parts[0] != "userpart" ) { if ( Left(OldTopic, Len(Parts[0])) != Parts[0] ) bTopicMatches = False; // doesn't match else OldTopic = Mid(OldTopic, Len(Parts[0])); } if ( bTopicMatches ) { // We have a match, now try matching the last part. i = Parts.Length - 1; if ( Parts[i] != "botpart" && Parts[i] != "userpart" ) { if ( Right(OldTopic, Len(Parts[i])) != Parts[i] ) bTopicMatches = False; // doesn't match else OldTopic = Left(OldTopic, Len(OldTopic) - Len(Parts[i])); } if ( bTopicMatches ) { // The topic still matches. Let's see if we can find the middle part as well. i = InStr(OldTopic, Parts[2]); if ( i != -1 ) { // Alright, we found it. if ( Parts[1] == "userpart" ) { // This one's easy: The user part is at the beginning of the remaining string. UserPart = Left(OldTopic, i); } else { // This one's not difficult either: The user part is at the end of the remaining string. UserPart = Mid(OldTopic, i + Len(Parts[2])); } } else bTopicMatches = False; } } if ( !bTopicMatches ) UserPart = ChannelTopic; } else return; } if ( UserPart == "" ) UserPart = MyMutator.ReportChannelTopicUserPart; LastTopicTemplate = NewTopic; SaveConfig(); // need to temporarily unescape codes to find the correct length UserPart = class'WIR_Util'.static.UnescapeIRCCodes(UserPart); NewTopic = class'WIR_Util'.static.UnescapeIRCCodes(Repl(NewTopic, "%%botpart%%", BotPart, True)); NewTopicLen = Len(Repl(NewTopic, "%%userpart%%", UserPart, True)); if ( TopicLen > 0 && NewTopicLen > TopicLen ) UserPart = Left(UserPart, Len(UserPart) - (NewTopicLen - TopicLen)); // now escape codes again because SendText() should handle unescaping them NewTopic = class'WIR_Util'.static.EscapeIRCCodes(Repl(NewTopic, "%%userpart%%", UserPart, True)); if ( NewTopic != ChannelTopic ) { if ( bLeaving ) SendText("TOPIC"@ChannelName@":"$NewTopic); // no time to queue the command else SendCommand("TOPIC"@ChannelName@":"$NewTopic, True); } } //== Registered =============================================================== /** Called when registration was completed. */ //============================================================================= function Registered(); //== SetTimeout =============================================================== /** Sets a timeout delay. */ //============================================================================= function SetTimeout(float TimeoutSeconds) { if ( int(TimeoutSeconds *= 1000) > 0 ) { TimestampTimeout = TimestampNow + int(TimeoutSeconds); if (TimestampTimeout > MILLISECONDS_PER_WEEK) TimestampTimeout = Max(TimestampTimeout - MILLISECONDS_PER_WEEK, 1); } else warn("Illegal TimeoutSeconds:" @ TimeoutSeconds); } //== ClearTimeout ============================================================= /** Clears a timeout delay. */ //============================================================================= function ClearTimeout() { TimestampTimeout = 0; } //== Tick ===================================================================== /** Updates the current timestamp. */ //============================================================================= event Tick(float DeltaTime) { local int TimestampLast, i, TimestampDelta; TimestampLast = TimestampNow; TimestampNow = Level.MilliSecond + 1000 * (Level.Second + 60 * (Level.Minute + 60 * (Level.Hour + 24 * Level.DayOfWeek))); if ( TimestampNow < TimestampLast ) { // new week? TimestampDelta = TimestampLast - TimestampNow; if (TimestampDelta > (MILLISECONDS_PER_WEEK >>> 2)) { // probably yes TimestampDelta = MILLISECONDS_PER_WEEK; } else { // was just a time correction, it seems TimestampDelta--; } TimestampLast = TimestampLast - TimestampDelta; // make sure the sent bytes records are relative to the new day do { SentBytes[i].Timestamp = SentBytes[i].Timestamp - TimestampDelta; } until (++i == SentBytes.Length); } if ( TimestampTimeout > 0 && TimestampTimeout <= TimestampNow && TimestampNow - TimestampTimeout < (MILLISECONDS_PER_WEEK >>> 2) ) { ClearTimeout(); OnTimeout(); } } //== DebugLog ================================================================= /** Log debug information. */ //============================================================================= function DebugLog(string S) { if ( LogFile == None && DebugLogFileName != "" ) { foreach DynamicActors(class'FileLog', LogFile) if ( WIR_IRCConnection(LogFile.Owner) != None ) break; if ( LogFile == None || WIR_IRCConnection(LogFile.Owner) == None ) LogFile = Spawn(class'FileLog', self); if ( LogFile != None ) { LogFile.OpenLog(DebugLogFileName); LogFile.Logf(""); LogFile.Logf("=============================================================================="); LogFile.Logf(""); } } if ( LogFile != None ) LogFile.Logf(TimestampNow @ Eval(UserNick != "", Usernick, string(Name)) @ S); //log(TimestampNow @ Eval(UserNick != "", Usernick, string(Name)) @ S); } //----------------------------------------------------------------------------- // state NotConnected //----------------------------------------------------------------------------- auto state NotConnected { //== Disconnect ============================================================= /** Already disconnected. Try to reconnect if desired. */ //=========================================================================== function Disconnect(bool bReconnect, string QuitMessage) { if ( bReconnect ) Connect(); } //== Connect ================================================================ /** Already disconnected. Try to reconnect if desired. */ //=========================================================================== function Connect() { if ( ServerAddress.Addr != 0 && ServerAddress.Port > 0 ) { if ( BindPort() == 0 ) log("ERROR - Can't bind port for IRC connection."); else Open(ServerAddress); OnTimeout = Connect; SetTimeout(ConnectTimeout); } else if ( ServerName != "" && ServerAddress.Port > 0 ) { Resolve(ServerName); } else { log("ERROR - Missing server address or port."); } } //== Resolved =============================================================== /** Server address resolved. Try to connect. */ //=========================================================================== event Resolved(IpAddr Address) { if ( Address.Addr != 0 ) { ServerAddress.Addr = Address.Addr; Connect(); } else log("ERROR - Resolving resulted in 0.0.0.0"); } //== ResolveFailed ========================================================== /** Resolving server address failed. */ //=========================================================================== event ResolveFailed() { log("ERROR - Could not resolve server address."); } //== Opened ================================================================= /** The server connection has been opened successfully. */ //=========================================================================== event Opened() { ClearTimeout(); GotoState('ConnectionEstablished'); } Reconnect: log("Waiting before reconnecting", Name); ServerAddress.Addr = 0; Begin: OnTimeout = Connect; SetTimeout(ReconnectDelay); NetworkParameters.Length = 0; } //----------------------------------------------------------------------------- // state ConnectionEstablished //----------------------------------------------------------------------------- state ConnectionEstablished { //== SetNick ================================================================ /** Changes the client's nick name. */ //=========================================================================== function SetNick(string NewNick) { Global.SetNick(NewNick); SendCommand("NICK" @ NewNick, True); } //== HandleNumericReply ========================================================== /** Detects numeric 001 as confirmation for login. */ //=========================================================================== function HandleNumericReply(int Command, string Server, string Target, string Parameters) { local array<string> Params; local int i; if ( Server != RealServerName ) RealServerName = Chomp(Server); if ( Command == 1 && !IsInState('LoggedIn') ) GotoState('LoggedIn'); else if ( Command == ERR_NICKNAMEINUSE ) { SetNick(Usernick $ "`"); } else if ( Command == 1 ) { Usernick = Target; // this is the name we really have } else if ( Command == 5 ) { Split(Parameters, " ", Params); while (Params.Length > 0 && Left(Params[0], 1) != ":") { NetworkParameters.Insert(0, 1); i = InStr(Params[0], "="); if ( i > 0 ) { NetworkParameters[0].Key = Left(Params[0], i); NetworkParameters[0].Value = Mid(Params[0], i+1); //log("Network parameter:"@NetworkParameters[0].Key@"="@NetworkParameters[0].Value); } else { NetworkParameters[0].Key = Params[0]; NetworkParameters[0].Value = ""; //log("Network parameter:"@NetworkParameters[0].Key@"(empty parameter)"); } Params.Remove(0, 1); } } Global.HandleNumericReply(Command, Server, Target, Parameters); } //== Disconnect ============================================================= /** Disconnect from the server. */ //=========================================================================== function Disconnect(bool bReconnect, string QuitMessage) { SendText("QUIT :" $ QuitMessage); if ( bReconnect ) GotoState('NotConnected', 'Reconnect'); else GotoState('NotConnected'); } //== Tick =================================================================== /** Send commands when flood protection allows it. */ //=========================================================================== event Tick(float DeltaTime) { Global.Tick(DeltaTime); if ( CommandQueue.Length > 0 && !IsFlooding(Len(CommandQueue[0])) ) { SendText(Left(CommandQueue[0], FloodProtectBytes)); CommandQueue.Remove(0, 1); } } //== Closed ================================================================= /** The server connection has been closed. */ //=========================================================================== event Closed() { ClearTimeout(); if ( CommandQueue.Length > 0 ) CommandQueue.Remove(0, CommandQueue.Length); GotoState('NotConnected', 'Reconnect'); } //== BeginState ============================================================= /** Clear the queues and network info. */ //=========================================================================== event BeginState() { Super.BeginState(); //NetworkParameters.Length = 0; CommandQueue.Length = 0; } Begin: log("Connected to IRC server", Name); if ( ServerPassword != "" ) SendText("PASS" @ ServerPassword); SendText("NICK" @ UserNick); SendText("USER" @ UserIdent @ "localhost" @ UserNick @ ":" $ class'MutWormbot'.default.FriendlyName @ "on" @ Level.GRI.ServerName); } //----------------------------------------------------------------------------- // state LoggedIn //----------------------------------------------------------------------------- state LoggedIn extends ConnectionEstablished { //== Registered ============================================================= /** Called when registeration was successful. */ //=========================================================================== function Registered() { GotoState('RegisterationComplete'); } Begin: log("Logged in to" @ RealServerName, Name); if ( RegistrationHandler == None ) SetRegistrationDetails(None, ""); else RegistrationHandler.Reset(); if ( RegistrationHandler != None ) RegistrationHandler.Register(); SetTimeout(30); OnTimeout = Registered; } //----------------------------------------------------------------------------- // state Registered //----------------------------------------------------------------------------- state RegisterationComplete extends LoggedIn { //== Registered ============================================================= /** Use the global non-state inplementation. */ //=========================================================================== function Registered() { Global.Registered(); } //== JoinChannel ============================================================ /** Join a channel. */ //=========================================================================== function JoinChannel(string Channel, string Key) { Global.JoinChannel(Channel, Key); if ( ChannelName != "" ) { if ( ChannelKey != "" ) SendCommand("JOIN"@ChannelName@ChannelKey, true); else SendCommand("JOIN"@ChannelName, true); } } //== HandleStringReply ====================================================== /** Detects when the client joined the channel. */ //=========================================================================== function HandleStringReply(string Command, string Server, string Target, string Parameters) { local string Nick; if ( Command == "JOIN" && class'WIR_Util'.static.ParseIRCUserAddress(Chomp(Server), Nick) ) { if ( IsTargetChannel(Chomp(Target)) ) { Target = Chomp(Target); // adjust capitalization if ( Nick ~= UserNick ) GotoState('InChannel'); else OnMode(MT_Join, Nick); } } Super.HandleStringReply(Command, Server, Target, Parameters); } Begin: Sleep(0.0); if ( ChannelName != "" ) JoinChannel(ChannelName, ChannelKey); } //----------------------------------------------------------------------------- // state InChannel //----------------------------------------------------------------------------- state InChannel extends RegisterationComplete { //== BeginState ============================================================= /** Clear the queues and network info. */ //=========================================================================== event BeginState() { local WIR_IRCConnection OtherConnection; local string Msg; local GameReplicationInfo GRI; GRI = Level.Game.GameReplicationInfo; Super.BeginState(); if ( MyMutator.ReportConnection == Self ) { OtherConnection = MyMutator.ChatConnection; if ( MyMutator.ChatConnection == Self ) { if ( MyMutator.bEnableTwoWayChat ) Msg = MsgBotJoinReportChatTwoWay; else Msg = MsgBotJoinReportChat; } else Msg = MsgBotJoinReport; } else if ( MyMutator.ChatConnection == Self ) { OtherConnection = MyMutator.ReportConnection; if ( MyMutator.bEnableTwoWayChat ) Msg = MsgBotJoinChatTwoWay; else Msg = MsgBotJoinChat; } Msg = Repl(Msg, "%m", class'MutWormbot'.default.FriendlyName, True); SendCommand("PRIVMSG" @ ChannelName @ ":" $ Repl(Msg, "%s", GRI.ServerName, True), True); if ( OtherConnection != None && OtherConnection != Self && OtherConnection.IsInState('InChannel') && OtherConnection.ServerName ~= ServerName && OtherConnection.ChannelName ~= ChannelName ) return; if ( MyMutator.MyGameHandler != None ) { Msg = MyMutator.MyGameHandler.GetGameInfoString(); if ( Msg != "" ) SendCommand("PRIVMSG" @ ChannelName @ ":" $ Msg, True); } } //== Tick =================================================================== /** Send commands and messages to the channel when flood protection allows it. */ //=========================================================================== event Tick(float DeltaTime) { local int i; Global.Tick(DeltaTime); if ( CommandQueue.Length > 0 ) { if ( !IsFlooding(Len(CommandQueue[0])) ) { SendText(Left(CommandQueue[0], FloodProtectBytes)); CommandQueue.Remove(0, 1); } } else if ( MessageQueue.Length > 0 && TimestampNow - ChannelJoinTime > 1000 ) { if ( MessageQueue.Length > MaxLenMessageQueue && MyMutator.bMatchStarted && !MyMutator.bMatchEnded ) { for (i = MessageQueue.Length - 1; i >= 0; --i) if ( !MessageQueue[i].bImportant ) MessageQueue.Remove(i, 1); SendMessage(MsgBotFlooding); } if ( !IsFlooding(Len(MessageQueue[0].Message)) ) { SendText(Left(MessageQueue[0].Message, FloodProtectBytes)); MessageQueue.Remove(0, 1); } } } //== HandleStringReply ====================================================== /** Detects when the client joined the channel. */ //=========================================================================== function HandleStringReply(string Command, string Server, string Target, string Parameters) { local string Nick, Left, Right; class'WIR_Util'.static.ParseIRCUserAddress(Chomp(Server), Nick); if ( IsTargetChannel(Target) ) { //log(Command@Nick@Parameters); switch (Command) { case "MODE": if ( Parameters ~= ("+o" @ Usernick) && MyMutator.bSetReportChannelTopic && MyMutator.ReportConnection == Self ) UpdateTopic(); break; case "PRIVMSG": if ( Nick != UserNick ) OnChat(Nick, Chomp(Parameters)); break; case "PART": if ( Nick != UserNick ) OnMode(MT_Part, Nick, Chomp(Parameters)); else GotoState('RegisterationComplete'); break; case "KICK": Divide(Parameters, " ", Left, Right); if ( Left ~= Usernick ) GotoState('RegisterationComplete'); OnMode(MT_Kick, Nick, Parameters); break; } } else if ( Nick != UserNick ) { if ( Command == "NICK" ) OnMode(MT_Nick, Nick, Chomp(Target)); if ( Command == "QUIT" ) OnMode(MT_Quit, Nick, Chomp(Eval(Parameters != "", Target @ Parameters, Target))); } Super.HandleStringReply(Command, Server, Target, Parameters); } //== JoinChannel ============================================================ /** Join a channel. */ //=========================================================================== function JoinChannel(string Channel, string Key) { if ( ChannelName ~= Channel ) return; PartChannel(); Global.JoinChannel(Channel, Key); } //== Disconnect ============================================================= /** Disconnect from the server. */ //=========================================================================== function Disconnect(bool bReconnect, string QuitMessage) { if ( MyMutator.bSetReportChannelTopic && MyMutator.ReportConnection == Self ) UpdateTopic(True); Super.Disconnect(bReconnect, QuitMessage); } //== PartChannel ============================================================ /** Parts the channel. */ //=========================================================================== function PartChannel() { SendCommand("PART"@ChannelName, True); Global.PartChannel(); } Begin: log("Joined channel" @ ChannelName, Name); ChannelJoinTime = TimestampNow; Sleep(5.0); if ( bDebugStartMatch && Level.Game.IsInState('PendingMatch') ) Level.Game.StartMatch(); Sleep(0.0); if ( MyMutator.bSetReportChannelTopic && !MyMutator.bMatchStarted && MyMutator.ReportConnection == Self ) UpdateTopic(); } //============================================================================= // Default properties //============================================================================= defaultproperties { bAlwaysTick = True MsgBotJoinChat = "[^B]%m[^B] - relaying chat from [^B]%s[^B]" MsgBotJoinChatTwoWay = "[^B]%m[^B] - relaying chat from and to [^B]%s[^B]" MsgBotJoinReport = "[^B]%m[^B] - reporting from [^B]%s[^B]" MsgBotJoinReportChat = "[^B]%m[^B] - reporting and relaying chat from [^B]%s[^B]" MsgBotJoinReportChatTwoWay = "[^B]%m[^B] - reporting and relaying chat from and to [^B]%s[^B]" MsgBotFlooding = "[^B]I had to drop some messages to catch up.[^B]" } |
Overview | Package | Class | Source | Class tree | Glossary | UnrealScript Documentation |
previous class next class | frames no frames |