1 #ifndef __msn_switchboardserver_h__
2 #define __msn_switchboardserver_h__
8 * Created by Mark Rowe on Mon Mar 22 2004.
9 * Copyright (c) 2004 Mark Rowe. All rights reserved.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <msn/message.h>
27 #include <msn/authdata.h>
28 #include <msn/connection.h>
29 #include <msn/passport.h>
35 class NotificationServerConnection
;
36 class FileTransferConnection
;
37 class FileTransferInvitation
;
40 /** Represents a connection to a MSN switchboard.
42 class SwitchboardServerConnection
: public Connection
44 friend class FileTransferConnection
;
45 friend class FileTransferInvitation
;
47 typedef void (SwitchboardServerConnection::*SwitchboardServerCallback
)(std::vector
<std::string
> & args
, int trid
, void *);
49 class AuthData
: public ::MSN::AuthData
52 std::string sessionID
;
56 AuthData(Passport
& username_
, const std::string
& sessionID_
,
57 const std::string
& cookie_
, const void *tag_
=NULL
) :
58 ::MSN::AuthData(username_
), sessionID(sessionID_
), cookie(cookie_
), tag(tag_
) {};
60 AuthData(Passport
& username_
, const void *tag_
=NULL
) :
61 ::MSN::AuthData(username_
), sessionID(""), cookie(""), tag(tag_
) {};
64 SwitchboardServerConnection::AuthData auth
;
66 /** A list of the users in this switchboard session.
68 std::list
<Passport
> users
;
70 /** Invitations extended but not responded to.
72 std::list
<Invitation
*> invitationsSent
;
74 /** Invitations received but not responded to.
76 std::list
<Invitation
*> invitationsReceived
;
78 SwitchboardServerConnection(AuthData
& auth_
, NotificationServerConnection
&);
79 virtual ~SwitchboardServerConnection();
80 virtual void dispatchCommand(std::vector
<std::string
> & args
);
82 /** Return a connection that is associated with @a fd.
84 * If @a fd is equal to @p sock, @c this is returned. Otherwise
85 * connectionWithSocket is sent to each FileTransferConnection
86 * until a match is found.
88 * @return The matching connection, if found. Otherwise, @c NULL.
90 Connection
*connectionWithSocket(int fd
);
92 /** Return a list of all FileTransferConnection's associated with this
95 std::list
<FileTransferConnection
*> & fileTransferConnections() const;
97 /** Add a FileTransferConnection to the list of associated connections.
99 void addFileTransferConnection(FileTransferConnection
*);
101 /** Remove a FileTransferConnection from the list of associated connections.
103 void removeFileTransferConnection(FileTransferConnection
*);
105 /** Remove the FileTransferConnection that is associated with FileTransferInvitation
106 * @a inv from the list of associated connections.
108 void removeFileTransferConnection(FileTransferInvitation
*inv
);
110 /** Send a typing notification to the switchboard server.
112 void sendTypingNotification();
114 /** Invite @a userName into this conversation.
116 void inviteUser(Passport userName
);
118 virtual void connect(const std::string
& hostname
, unsigned int port
);
119 virtual void disconnect();
120 virtual void sendMessage(const Message
*msg
);
121 virtual void sendMessage(const std::string
& s
);
123 FileTransferInvitation
*sendFile(const std::string path
);
125 /** Add @a cb as a callback that will be called when a response is received
126 * a transaction ID of @a trid.
128 virtual void addCallback(SwitchboardServerCallback
, int trid
, void *data
);
130 /** Remove callbacks for transaction ID @a trid.
132 virtual void removeCallback(int trid
);
134 Invitation
*invitationWithCookie(const std::string
& cookie
);
136 virtual void socketConnectionCompleted();
138 enum SwitchboardServerState
143 SB_WAITING_FOR_USERS
,
147 SwitchboardServerState
connectionState() const { return this->_connectionState
; };
148 virtual NotificationServerConnection
*myNotificationServer() { return ¬ificationServer
; };
150 virtual void handleIncomingData();
151 SwitchboardServerState _connectionState
;
153 void setConnectionState(SwitchboardServerState s
) { this->_connectionState
= s
; };
154 void assertConnectionStateIs(SwitchboardServerState s
) { assert(this->_connectionState
== s
); };
155 void assertConnectionStateIsNot(SwitchboardServerState s
) { assert(this->_connectionState
!= s
); };
156 void assertConnectionStateIsAtLeast(SwitchboardServerState s
) { assert(this->_connectionState
>= s
); };
158 NotificationServerConnection
& notificationServer
;
159 std::list
<FileTransferConnection
*> _fileTransferConnections
;
160 std::map
<int, std::pair
<SwitchboardServerCallback
, void *> > callbacks
;
162 static std::map
<std::string
, void (SwitchboardServerConnection::*)(std::vector
<std::string
> &)> commandHandlers
;
163 void registerCommandHandlers();
164 void handle_BYE(std::vector
<std::string
> & args
);
165 void handle_JOI(std::vector
<std::string
> & args
);
166 void handle_NAK(std::vector
<std::string
> & args
);
167 void handle_MSG(std::vector
<std::string
> & args
);
169 void callback_InviteUsers(std::vector
<std::string
> & args
, int trid
, void * data
);
170 void callback_AnsweredCall(std::vector
<std::string
> & args
, int trid
, void * data
);
172 void handleInvite(Passport from
, const std::string
& friendly
, const std::string
& mime
, const std::string
& body
);
173 void handleNewInvite(Passport
& from
, const std::string
& friendly
, const std::string
& mime
, const std::string
& body
);
174 friend class Connection
;