[39] | 1 | /* |
---|
| 2 | * MangChat By |Death| And Cybrax |
---|
| 3 | * |
---|
| 4 | * This Program Is Free Software; You Can Redistribute It And/Or Modify It Under The Terms |
---|
| 5 | * Of The GNU General Public License |
---|
| 6 | * Written and Developed by Cybrax. cybraxvd@gmail.com |
---|
| 7 | * |Death| <death@hell360.net>, Lice <lice@yeuxverts.net>, Dj_baby & Sanaell, Tase |
---|
| 8 | * With Help And Support From The MaNGOS Project Community. |
---|
| 9 | * PLEASE RETAIN THE COPYRIGHT OF THE AUTHORS. |
---|
| 10 | */ |
---|
| 11 | #include "IRCClient.h" |
---|
| 12 | #include "World.h" |
---|
| 13 | #include "ObjectMgr.h" |
---|
| 14 | #include "MapManager.h" |
---|
| 15 | |
---|
| 16 | #include "Policies/SingletonImp.h" |
---|
| 17 | INSTANTIATE_SINGLETON_1( IRCClient ); |
---|
| 18 | |
---|
| 19 | #ifdef WIN32 |
---|
| 20 | #define Delay(x) Sleep(x) |
---|
| 21 | #else |
---|
| 22 | #define Delay(x) sleep(x / 1000) |
---|
| 23 | #endif |
---|
| 24 | // IRCClient Constructor |
---|
| 25 | IRCClient::IRCClient() |
---|
| 26 | { |
---|
| 27 | for(int i = 0;i > 5;i++) |
---|
| 28 | sIRC.Script_Lock[i] = false; |
---|
| 29 | } |
---|
| 30 | // IRCClient Destructor |
---|
| 31 | IRCClient::~IRCClient(){} |
---|
| 32 | |
---|
| 33 | // ZThread Entry This function is called when the thread is created in Master.cpp (mangosd) |
---|
| 34 | void IRCClient::run() |
---|
| 35 | { |
---|
| 36 | sIRC.iLog.WriteLog(" %s : IRC bot started.", sIRC.iLog.GetLogDateTimeStr().c_str()); |
---|
| 37 | |
---|
| 38 | // before we begin we wait a few |
---|
| 39 | // mangos is still starting up. |
---|
| 40 | ZThread::Thread::sleep(500); |
---|
| 41 | int cCount = 0; |
---|
| 42 | // Clean Up MySQL Tables |
---|
| 43 | sLog.outString("Cleaning up IRC_Inchan table..."); |
---|
| 44 | WorldDatabase.PExecute("DELETE FROM `IRC_Inchan`"); |
---|
| 45 | sIRC._Max_Script_Inst = 0; |
---|
| 46 | // Create a loop to keep the thread running untill active is set to false |
---|
| 47 | while(sIRC.Active && !World::m_stopEvent) |
---|
| 48 | { |
---|
| 49 | // Initialize socket library |
---|
| 50 | if(this->InitSock()) |
---|
| 51 | { |
---|
| 52 | // Connect To The IRC Server |
---|
| 53 | sLog.outString("IRC: Connecting to %s Try # %d ******", sIRC._Host.c_str(), cCount); |
---|
| 54 | if(this->Connect(sIRC._Host.c_str(), sIRC._Port)) |
---|
| 55 | { |
---|
| 56 | // On connection success reset the connection counter |
---|
| 57 | cCount = 0; |
---|
| 58 | sLog.outString("IRC connected and logging in"); |
---|
| 59 | // Login to the IRC server |
---|
| 60 | if(this->Login(sIRC._Nick, sIRC._User, sIRC._Pass)) |
---|
| 61 | { |
---|
| 62 | sLog.outString("IRC logged in and running"); |
---|
| 63 | // While we are connected to the irc server keep listening for data on the socket |
---|
| 64 | while(sIRC.Connected && !World::m_stopEvent){ sIRC.SockRecv(); } |
---|
| 65 | } |
---|
| 66 | sLog.outString("Connection to IRC server lost!"); |
---|
| 67 | } |
---|
| 68 | // When an error occures or connection lost cleanup |
---|
| 69 | Disconnect(); |
---|
| 70 | // Increase the connection counter |
---|
| 71 | cCount++; |
---|
| 72 | // if MAX_CONNECT_ATTEMPT is reached stop trying |
---|
| 73 | if(sIRC._MCA != 0 && cCount == sIRC._MCA) |
---|
| 74 | sIRC.Active = false; |
---|
| 75 | // If we need to reattempt a connection wait WAIT_CONNECT_TIME milli seconds before we try again |
---|
| 76 | if(sIRC.Active) |
---|
| 77 | ZThread::Thread::sleep(sIRC._wct); |
---|
| 78 | } |
---|
| 79 | else |
---|
| 80 | { |
---|
| 81 | // Socket could not initialize cancel |
---|
| 82 | sIRC.Active = false; |
---|
| 83 | sLog.outError("IRC: Could not initialize socket"); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | // we need to keep the thread alive or mangos will crash |
---|
| 87 | // when sending chat or join/leave channels. |
---|
| 88 | // even when we are not connected the functions must still |
---|
| 89 | // be availlable where chat is sent to so we keep it running |
---|
| 90 | while(!World::m_stopEvent){}; |
---|
| 91 | } |
---|