Logo Search packages:      
Sourcecode: sbnc version File versions  Download package

void CUser::Attach ( CClientConnection Client  ) 

Attach

Attaches a client connection to the user.

Parameters:
Client the client for this user

Definition at line 265 of file User.cpp.

References AddClientConnection(), CClientConnection::ChangeNick(), CIRCConnection::GetChannels(), CIRCConnection::GetCurrentNick(), CVector< Type >::GetLength(), GetLog(), CCore::GetModules(), CCore::GetMotd(), CChannel::GetName(), CClientConnection::GetNick(), CClientConnection::GetPeerName(), GetServer(), CIRCConnection::GetServer(), GetSuspendReason(), CIRCConnection::GetUsermodes(), GetUsername(), CLog::IsEmpty(), IsLocked(), IsQuitted(), CClientConnection::Kill(), CCore::Log(), m_Clients, m_IRC, m_Name, CClientConnection::ParseLine(), CLog::PlayToUser(), CClientConnection::Privmsg(), ScheduleReconnect(), CObject< ObjectType, OwnerType >::SetOwner(), and CConnection::WriteLine().

Referenced by CClientConnection::ValidateUser().

                                            {
      char *Out;
      const char *Reason, *AutoModes, *IrcNick, *MotdText;
      CLog *Motd;
      unsigned int i;
      bool Added = false;
      bool FirstClient;

      if (IsLocked()) {
            Reason = GetSuspendReason();

            if (Reason == NULL) {
                  Client->Kill("*** You cannot attach to this user.");
            } else {
                  asprintf(&Out, "*** Your account is suspended. Reason: %s", Reason);

                  if (Out == NULL) {
                        LOGERROR("asprintf() failed.");

                        Client->Kill("*** Your account is suspended.");
                  } else {
                        Client->Kill(Out);

                        free(Out);
                  }
            }

            g_Bouncer->Log("Login for user %s failed: User is locked.", m_Name);

            return;
      }

      FirstClient = (m_Clients.GetLength() == 0);

      Client->SetOwner(this);

      Motd = new CLog("sbnc.motd");

      if (m_IRC != NULL) {
            if (FirstClient) {
                  m_IRC->WriteLine("AWAY");
            }

            AutoModes = CacheGetString(m_ConfigCache, automodes);

            if (AutoModes != NULL && m_IRC->GetCurrentNick() != NULL && FirstClient) {
                  m_IRC->WriteLine("MODE %s +%s", m_IRC->GetCurrentNick(), AutoModes);
            }

            IrcNick = m_IRC->GetCurrentNick();

            if (IrcNick != NULL) {
                  if (Client->GetNick() != NULL && strcmp(Client->GetNick(), IrcNick) != 0) {
                        m_IRC->WriteLine("NICK :%s", Client->GetNick());
                  }

                  Client->ChangeNick(IrcNick);

                  Client->WriteLine(":%s 001 %s :Welcome to the Internet Relay Network %s", m_IRC->GetServer(), IrcNick, IrcNick);

                  if (Motd->IsEmpty()) {
                        Client->WriteLine(":%s 422 %s :MOTD File is missing", m_IRC->GetServer(), IrcNick);
                  } else{
                        Motd->PlayToUser(Client, Log_Motd);
                  }

                  Client->ParseLine("SYNTH VERSION-FORCEREPLY");

                  if (m_IRC->GetUsermodes() != NULL) {
                        Client->WriteLine(":%s!%s@%s MODE %s +%s", IrcNick, GetUsername(), Client->GetPeerName(), IrcNick, m_IRC->GetUsermodes());
                  }

                  AddClientConnection(Client);
                  Added = true;

                  CChannel **Channels;

                  Channels = (CChannel **)malloc(sizeof(CChannel *) * m_IRC->GetChannels()->GetLength());

                  CHECK_ALLOC_RESULT(Channels, malloc) {
                        return;
                  } CHECK_ALLOC_RESULT_END;

                  mmark(Channels);

                  i = 0;
                  while (hash_t<CChannel *> *ChannelHash = m_IRC->GetChannels()->Iterate(i++)) {
                        Channels[i - 1] = ChannelHash->Value;
                  }

                  int (*SortFunction)(const void *p1, const void *p2) = NULL;

                  const char *SortMode = CacheGetString(m_ConfigCache, channelsort);

                  if (SortMode == NULL || strcasecmp(SortMode, "cts") == 0 || strcasecmp(SortMode, "") == 0) {
                        SortFunction = ChannelTSCompare;
                  } else if (strcasecmp(SortMode, "alpha") == 0) {
                        SortFunction = ChannelNameCompare;
                  } else if (strcasecmp(SortMode, "custom") == 0) {
                        const CVector<CModule *> *Modules = g_Bouncer->GetModules();

                        for (i = 0; i < Modules->GetLength(); i++) {
                              SortFunction = (int (*)(const void *p1, const void *p2))(*Modules)[i]->Command("sorthandler", GetUsername());

                              if (SortFunction != NULL) {
                                    break;
                              }
                        }
                  }

                  if (SortFunction == NULL) {
                        SortFunction = ChannelTSCompare;
                  }

                  qsort(Channels, m_IRC->GetChannels()->GetLength(), sizeof(CChannel *), SortFunction);

                  i = 0;
                  for (i = 0; i < m_IRC->GetChannels()->GetLength(); i++) {
                        Client->WriteLine(":%s!%s@%s JOIN %s", m_IRC->GetCurrentNick(), GetUsername(), Client->GetPeerName(), Channels[i]->GetName());

                        asprintf(&Out, "TOPIC %s", Channels[i]->GetName());

                        if (Out == NULL) {
                              LOGERROR("asprintf() failed.");

                              Client->Kill("Internal error.");
                        } else {
                              Client->ParseLine(Out);
                              free(Out);
                        }

                        asprintf(&Out, "NAMES %s", Channels[i]->GetName());

                        if (Out == NULL) {
                              LOGERROR("asprintf() failed.");

                              Client->Kill("Internal error.");
                        } else {
                              Client->ParseLine(Out);
                              free(Out);
                        }
                  }

                  free(Channels);
            }
      } else {
            if (!Motd->IsEmpty()) {
                  Motd->PlayToUser(Client, Log_Motd);
            }

            if (IsQuitted() != 2) {
                  ScheduleReconnect(0);
            }
      }

      if (!Added) {
            AddClientConnection(Client);
      }

      MotdText = g_Bouncer->GetMotd();

      if (MotdText != NULL) {
            asprintf(&Out, "Message of the day: %s", MotdText);
            
            if (Out == NULL) {
                  LOGERROR("asprintf() failed.");
            } else {
                  Client->Privmsg(Out);

                  free(Out);
            }
      }

      if (m_IRC == NULL) {
            if (GetServer() == NULL) {
                  Client->Privmsg("You haven't set an IRC server yet. Use /sbnc set server <Hostname> <Port> to do that now.");
                  Client->Privmsg("Use /sbnc help to see a list of available commands.");
            } else if (IsQuitted() == 2) {
                  Client->Privmsg("You are not connected to an irc server. Use /sbnc jump to reconnect now.");
            }
      }

      if (!GetLog()->IsEmpty()) {
            Client->Privmsg("You have new messages. Use '/msg -sBNC read' to view them.");
      }
}


Generated by  Doxygen 1.6.0   Back to index