1 | #ifndef __msn_switchboardserver_h__ |
2 | #define __msn_switchboardserver_h__ |
3 | |
4 | /* |
5 | * switchboardserver.h |
6 | * libmsn |
7 | * |
8 | * Created by Mark Rowe on Mon Mar 22 2004. |
9 | * Refactored by Tiago Salem Herrmann on 08/2007. |
10 | * Copyright (c) 2004 Mark Rowe. All rights reserved. |
11 | * Copyright (c) 2007 Tiago Salem Herrmann. All rights reserved |
12 | * |
13 | * This program is free software; you can redistribute it and/or modify |
14 | * it under the terms of the GNU General Public License as published by |
15 | * the Free Software Foundation; either version 2 of the License, or |
16 | * (at your option) any later version. |
17 | * |
18 | * This program is distributed in the hope that it will be useful, |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21 | * GNU General Public License for more details. |
22 | * |
23 | * You should have received a copy of the GNU General Public License |
24 | * along with this program; if not, write to the Free Software |
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
26 | */ |
27 | |
28 | #include <msn/message.h> |
29 | #include <msn/authdata.h> |
30 | #include <msn/connection.h> |
31 | #include <msn/passport.h> |
32 | #include <msn/p2p.h> |
33 | #include <string> |
34 | #include <cassert> |
35 | |
36 | #include "libmsn_export.h" |
37 | |
38 | namespace MSN |
39 | { |
40 | class NotificationServerConnection; |
41 | class FileTransferConnectionP2P; |
42 | |
43 | /** Represents a connection to a MSN switchboard. |
44 | */ |
45 | class LIBMSN_EXPORT SwitchboardServerConnection : public Connection |
46 | { |
47 | private: |
48 | typedef void (SwitchboardServerConnection::*SwitchboardServerCallback)(std::vector<std::string> & args, int trid, void *); |
49 | typedef void (SwitchboardServerConnection::*SwitchboardServerCallback2)(std::vector<std::string> & args, int trid, unsigned int sessionID); |
50 | |
51 | typedef struct |
52 | { |
53 | int chunks; |
54 | int receivedChunks; |
55 | std::string mime; |
56 | std::string body; |
57 | } MultiPacketSession; |
58 | |
59 | public: |
60 | class AuthData : public ::MSN::AuthData |
61 | { |
62 | public: |
63 | std::string sessionID; |
64 | bool direct_connection; |
65 | std::string cookie; |
66 | const void *tag; |
67 | |
68 | AuthData(Passport & username_, const std::string & sessionID_, |
69 | const std::string & cookie_, const void *tag_=NULL) : |
70 | ::MSN::AuthData(username_), sessionID(sessionID_), cookie(cookie_), tag(tag_) {}; |
71 | |
72 | AuthData(Passport & username_, const void *tag_=NULL) : |
73 | ::MSN::AuthData(username_), sessionID("" ), cookie("" ), tag(tag_) {}; |
74 | }; |
75 | |
76 | SwitchboardServerConnection::AuthData auth; |
77 | |
78 | /** A list of the users in this switchboard session. |
79 | */ |
80 | std::list<Passport> users; |
81 | |
82 | P2P p2p; |
83 | |
84 | SwitchboardServerConnection(AuthData & auth_, NotificationServerConnection &); |
85 | virtual ~SwitchboardServerConnection(); |
86 | virtual void dispatchCommand(std::vector<std::string> & args); |
87 | |
88 | /** Return a connection that is associated with @a fd. |
89 | * |
90 | * If @a fd is equal to @p sock, @c this is returned. Otherwise |
91 | * connectionWithSocket is sent to each FileTransferConnection |
92 | * until a match is found. |
93 | * |
94 | * @return The matching connection, if found. Otherwise, @c NULL. |
95 | */ |
96 | Connection *connectionWithSocket(void *sock); |
97 | |
98 | std::map<std::string, MultiPacketSession> MultiPacketSessions; |
99 | |
100 | /** Add a FileTransferConnection to the list of associated connections. |
101 | */ |
102 | void addFileTransferConnectionP2P(FileTransferConnectionP2P *); |
103 | |
104 | /** Remove a FileTransferConnection from the list of associated connections. |
105 | */ |
106 | void removeFileTransferConnectionP2P(FileTransferConnectionP2P *); |
107 | |
108 | /** Send a typing notification to the switchboard server. |
109 | */ |
110 | void sendTypingNotification(); |
111 | |
112 | /** Send a text action |
113 | */ |
114 | void sendAction(std::string action); |
115 | |
116 | /** Send a Voice Clip |
117 | */ |
118 | void sendVoiceClip(std::string msnobject); |
119 | |
120 | /** Send a Wink |
121 | */ |
122 | void sendWink(std::string msnobject); |
123 | |
124 | /** Send Ink |
125 | */ |
126 | void sendInk(std::string image); |
127 | |
128 | /** Send an emoticon |
129 | */ |
130 | void sendEmoticon(std::string alias, std::string file); |
131 | |
132 | /** Send a nudge |
133 | */ |
134 | void sendNudge(); |
135 | |
136 | /** Send a keep alive message |
137 | */ |
138 | void sendKeepAlive(); |
139 | |
140 | /** Send a file |
141 | */ |
142 | void sendFile(MSN::fileTransferInvite ft); |
143 | |
144 | /** Invite @a userName into this conversation. |
145 | */ |
146 | void inviteUser(Passport userName); |
147 | |
148 | /** Response to a file transfer invitation |
149 | */ |
150 | void fileTransferResponse(unsigned int sessionID, std::string filename, bool response); |
151 | |
152 | /** Cancel a file transfer in progress |
153 | */ |
154 | void cancelFileTransfer(unsigned int sessionID); |
155 | |
156 | virtual void connect(const std::string & hostname, unsigned int port); |
157 | virtual void disconnect(); |
158 | |
159 | /** Send formatted message, returns the trID |
160 | */ |
161 | virtual int sendMessage(const Message *msg); |
162 | |
163 | /** Send plain text message, returns the trID |
164 | */ |
165 | virtual int sendMessage(const std::string & s); |
166 | |
167 | /** Add @a cb as a callback that will be called when a response is received |
168 | * a transaction ID of @a trid. |
169 | */ |
170 | virtual void addCallback(SwitchboardServerCallback, int trid, void *data); |
171 | |
172 | // callback of msg acks |
173 | virtual void addP2PCallback(SwitchboardServerCallback2, int trid, unsigned int sessionID); |
174 | |
175 | /** Remove callbacks for transaction ID @a trid. |
176 | */ |
177 | virtual void removeCallback(int trid); |
178 | |
179 | virtual void removeP2PCallback(int trid); |
180 | |
181 | virtual void socketConnectionCompleted(); |
182 | |
183 | enum SwitchboardServerState |
184 | { |
185 | SB_DISCONNECTED, |
186 | SB_CONNECTING, |
187 | SB_CONNECTED, |
188 | SB_WAITING_FOR_USERS, |
189 | SB_READY |
190 | }; |
191 | |
192 | SwitchboardServerState connectionState() const { return this->_connectionState; }; |
193 | virtual NotificationServerConnection *myNotificationServer() { return ¬ificationServer; }; |
194 | void callback_continueTransfer(std::vector<std::string> & args, int trid, unsigned int sessionID); |
195 | |
196 | /** Request an emoticon |
197 | */ |
198 | void requestEmoticon(unsigned int id, std::string filename, std::string msnobject, std::string alias); |
199 | |
200 | /** Request a voice clip |
201 | */ |
202 | void requestVoiceClip(unsigned int id, std::string filename, std::string msnobject); |
203 | |
204 | /** Request a wink |
205 | */ |
206 | void requestWink(unsigned int id, std::string filename, std::string msnobject); |
207 | |
208 | /** Request a display picture |
209 | */ |
210 | void requestDisplayPicture(unsigned int id, std::string filename, std::string msnobject); |
211 | protected: |
212 | virtual void handleIncomingData(); |
213 | SwitchboardServerState _connectionState; |
214 | |
215 | void setConnectionState(SwitchboardServerState s) { this->_connectionState = s; }; |
216 | void assertConnectionStateIs(SwitchboardServerState s) { assert(this->_connectionState == s); }; |
217 | void assertConnectionStateIsNot(SwitchboardServerState s) { assert(this->_connectionState != s); }; |
218 | void assertConnectionStateIsAtLeast(SwitchboardServerState s) { assert(this->_connectionState >= s); }; |
219 | private: |
220 | NotificationServerConnection & notificationServer; |
221 | std::list<FileTransferConnectionP2P *> _fileTransferConnectionsP2P; |
222 | |
223 | std::map<int, std::pair<SwitchboardServerCallback, void *> > callbacks; |
224 | std::map<int, std::pair<SwitchboardServerCallback2, unsigned int> > callbacks2; |
225 | |
226 | static std::map<std::string, void (SwitchboardServerConnection::*)(std::vector<std::string> &)> commandHandlers; |
227 | static std::map<std::string, void (SwitchboardServerConnection::*)(std::vector<std::string> &, std::string, std::string)> messageHandlers; |
228 | void registerCommandHandlers(); |
229 | void registerMessageHandlers(); |
230 | void handle_BYE(std::vector<std::string> & args); |
231 | void handle_JOI(std::vector<std::string> & args); |
232 | void handle_NAK(std::vector<std::string> & args); |
233 | void handle_MSG(std::vector<std::string> & args); |
234 | |
235 | void callback_InviteUsers(std::vector<std::string> & args, int trid, void * data); |
236 | void callback_AnsweredCall(std::vector<std::string> & args, int trid, void * data); |
237 | void callback_messageACK(std::vector<std::string> & args, int trid, void * data); |
238 | |
239 | void handleInvite(Passport from, const std::string & friendly, const std::string & mime, const std::string & body); |
240 | void handleNewInvite(Passport & from, const std::string & friendly, const std::string & mime, const std::string & body); |
241 | void message_plain(std::vector<std::string> & args, std::string mime, std::string body); |
242 | void message_invitation(std::vector<std::string> & args, std::string mime, std::string body); |
243 | void message_typing_user(std::vector<std::string> & args, std::string mime, std::string body); |
244 | void message_p2p(std::vector<std::string> & args, std::string mime, std::string body); |
245 | void message_datacast(std::vector<std::string> & args, std::string mime, std::string body); |
246 | void message_emoticon(std::vector<std::string> & args, std::string mime, std::string body); |
247 | public: |
248 | void message_ink(std::vector<std::string> & args, std::string mime, std::string body); |
249 | |
250 | friend class Connection; |
251 | }; |
252 | } |
253 | #endif |
254 | |