Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2004-2025 ZNC, see the NOTICE file for details. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #include <znc/Server.h> |
18 | | |
19 | | CServer::CServer(const CString& sName, unsigned short uPort, |
20 | | const CString& sPass, bool bSSL, bool bUnixSocket) |
21 | 0 | : m_sName(sName), |
22 | 0 | m_uPort((uPort) ? uPort : (unsigned short)6667), |
23 | 0 | m_sPass(sPass), |
24 | 0 | m_bSSL(bSSL), |
25 | 0 | m_bUnixSocket(bUnixSocket) {} |
26 | | |
27 | 0 | CServer::~CServer() {} |
28 | | |
29 | 0 | bool CServer::IsValidHostName(const CString& sHostName) { |
30 | 0 | return (!sHostName.empty() && !sHostName.Contains(" ")); |
31 | 0 | } |
32 | | |
33 | 0 | const CString& CServer::GetName() const { return m_sName; } |
34 | 0 | unsigned short CServer::GetPort() const { return m_uPort; } |
35 | 0 | const CString& CServer::GetPass() const { return m_sPass; } |
36 | 0 | bool CServer::IsSSL() const { return m_bSSL; } |
37 | 0 | bool CServer::IsUnixSocket() const { return m_bUnixSocket; } |
38 | | |
39 | 0 | CString CServer::GetString(bool bIncludePassword) const { |
40 | 0 | CString sResult; |
41 | 0 | if (m_bUnixSocket) { |
42 | 0 | sResult = "unix:" + CString(m_bSSL ? "ssl:" : "") + m_sName; |
43 | 0 | } else { |
44 | 0 | sResult = m_sName + " " + CString(m_bSSL ? "+" : "") + CString(m_uPort); |
45 | 0 | } |
46 | 0 | sResult += |
47 | 0 | CString(bIncludePassword ? (m_sPass.empty() ? "" : " " + m_sPass) : ""); |
48 | 0 | return sResult; |
49 | 0 | } |
50 | | |
51 | 0 | CServer CServer::Parse(CString sLine) { |
52 | 0 | bool bSSL = false; |
53 | 0 | sLine.Trim(); |
54 | |
|
55 | 0 | if (sLine.TrimPrefix("unix:")) { |
56 | 0 | if (sLine.TrimPrefix("ssl:")) { |
57 | 0 | bSSL = true; |
58 | 0 | } |
59 | |
|
60 | 0 | CString sPath = sLine.Token(0); |
61 | 0 | CString sPass = sLine.Token(1, true); |
62 | 0 | return CServer(sPath, 0, sPass, bSSL, true); |
63 | 0 | } |
64 | | |
65 | 0 | CString sHost = sLine.Token(0); |
66 | 0 | CString sPort = sLine.Token(1); |
67 | |
|
68 | 0 | if (sPort.TrimPrefix("+")) { |
69 | 0 | bSSL = true; |
70 | 0 | } |
71 | |
|
72 | 0 | unsigned short uPort = sPort.ToUShort(); |
73 | 0 | CString sPass = sLine.Token(2, true); |
74 | |
|
75 | 0 | return CServer(sHost, uPort, sPass, bSSL, false); |
76 | 0 | } |
77 | | |
78 | 0 | bool CServer::operator==(const CServer& o) const { |
79 | 0 | if (m_sName != o.m_sName) return false; |
80 | 0 | if (m_uPort != o.m_uPort) return false; |
81 | 0 | if (m_sPass != o.m_sPass) return false; |
82 | 0 | if (m_bSSL != o.m_bSSL) return false; |
83 | 0 | if (m_bUnixSocket != o.m_bUnixSocket) return false; |
84 | 0 | return true; |
85 | 0 | } |
86 | | |
87 | 0 | bool CServer::operator<(const CServer& o) const { |
88 | 0 | if (m_sName < o.m_sName) return true; |
89 | 0 | if (m_sName > o.m_sName) return false; |
90 | 0 | if (m_uPort < o.m_uPort) return true; |
91 | 0 | if (m_uPort > o.m_uPort) return false; |
92 | 0 | if (m_sPass < o.m_sPass) return true; |
93 | 0 | if (m_sPass > o.m_sPass) return false; |
94 | 0 | if (m_bSSL < o.m_bSSL) return true; |
95 | 0 | if (m_bSSL > o.m_bSSL) return false; |
96 | 0 | if (m_bUnixSocket < o.m_bUnixSocket) return true; |
97 | 0 | if (m_bUnixSocket > o.m_bUnixSocket) return false; |
98 | 0 | return false; |
99 | 0 | } |