Code

d938a25fd76dfc6830b2e6d8c3dc21284f16ab69
[inkscape.git] / src / jabber_whiteboard / pedroxmpp.h
1 #ifndef __XMPP_H__
2 #define __XMPP_H__
3 /*
4  * API for the Pedro mini-XMPP client.
5  *
6  * Authors:
7  *   Bob Jamison
8  *
9  * Copyright (C) 2005 Bob Jamison
10  *
11  *  This library is free software; you can redistribute it and/or
12  *  modify it under the terms of the GNU Lesser General Public
13  *  License as published by the Free Software Foundation; either
14  *  version 2.1 of the License, or (at your option) any later version.
15  *
16  *  This library 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 GNU
19  *  Lesser General Public License for more details.
20  *
21  *  You should have received a copy of the GNU Lesser General Public
22  *  License along with this library; if not, write to the Free Software
23  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24  */
26 #include <stdio.h>
27 #include <vector>
29 #include <string>
31 #include "pedrodom.h"
33 namespace Pedro
34 {
36 typedef std::string DOMString;
39 //########################################################################
40 //# X M P P    E V E N T
41 //########################################################################
42 class XmppUser
43 {
44 public:
45     XmppUser()
46         {
47         }
48     XmppUser(const DOMString &jidArg, const DOMString &nickArg)
49         {
50         jid  = jidArg;
51         nick = nickArg;
52         }
53     XmppUser(const DOMString &jidArg,          const DOMString &nickArg,
54              const DOMString &subscriptionArg, const DOMString &groupArg)
55         {
56         jid          = jidArg;
57         nick         = nickArg;
58         subscription = subscriptionArg;
59         group        = groupArg;
60         }
61     XmppUser(const XmppUser &other)
62         {
63         jid          = other.jid;
64         nick         = other.nick;
65         subscription = other.subscription;
66         group        = other.group;
67         show         = other.show;
68         }
69     XmppUser &operator=(const XmppUser &other)
70         {
71         jid          = other.jid;
72         nick         = other.nick;
73         subscription = other.subscription;
74         group        = other.group;
75         show         = other.show;
76         return *this;
77         }
78     virtual ~XmppUser()
79         {}
80     DOMString jid;
81     DOMString nick;
82     DOMString subscription;
83     DOMString group;
84     DOMString show;
85 };
87 class XmppEvent
88 {
90 public:
92 typedef enum
93     {
94     EVENT_NONE,
95     EVENT_STATUS,
96     EVENT_ERROR,
97     EVENT_CONNECTED,
98     EVENT_DISCONNECTED,
99     EVENT_PRESENCE,
100     EVENT_ROSTER,
101     EVENT_MESSAGE,
102     EVENT_MUC_JOIN,
103     EVENT_MUC_LEAVE,
104     EVENT_MUC_PRESENCE,
105     EVENT_MUC_MESSAGE,
106     EVENT_STREAM_RECEIVE_INIT,
107     EVENT_STREAM_RECEIVE,
108     EVENT_STREAM_RECEIVE_CLOSE,
109     EVENT_FILE_ACCEPTED,
110     EVENT_FILE_RECEIVE
111     } XmppEventType;
114     /**
115      *
116      */
117     XmppEvent(int type);
119     /**
120      *
121      */
122     XmppEvent(const XmppEvent &other);
124     /**
125      *
126      */
127     virtual XmppEvent &operator=(const XmppEvent &other);
129     /**
130      *
131      */
132     virtual ~XmppEvent();
134     /**
135      *
136      */
137     virtual void assign(const XmppEvent &other);
139     /**
140      *
141      */
142     virtual int getType() const;
145     /**
146      *
147      */
148     virtual DOMString getIqId() const;
151     /**
152      *
153      */
154     virtual void setIqId(const DOMString &val);
156     /**
157      *
158      */
159     virtual DOMString getStreamId() const;
162     /**
163      *
164      */
165     virtual void setStreamId(const DOMString &val);
167     /**
168      *
169      */
170     virtual bool getPresence() const;
173     /**
174      *
175      */
176     virtual void setPresence(bool val);
178     /**
179      *
180      */
181     virtual DOMString getShow() const;
184     /**
185      *
186      */
187     virtual void setShow(const DOMString &val);
189     /**
190      *
191      */
192     virtual DOMString getStatus() const;
194     /**
195      *
196      */
197     virtual void setStatus(const DOMString &val);
199     /**
200      *
201      */
202     virtual DOMString getTo() const;
204     /**
205      *
206      */
207     virtual void setTo(const DOMString &val);
209     /**
210      *
211      */
212     virtual DOMString getFrom() const;
214     /**
215      *
216      */
217     virtual void setFrom(const DOMString &val);
219     /**
220      *
221      */
222     virtual DOMString getGroup() const;
224     /**
225      *
226      */
227     virtual void setGroup(const DOMString &val);
229     /**
230      *
231      */
232     virtual Element *getDOM() const;
235     /**
236      *
237      */
238     virtual void setDOM(const Element *val);
240     /**
241      *
242      */
243     virtual std::vector<XmppUser> getUserList() const;
245     /**
246      *
247      */
248     virtual void setUserList(const std::vector<XmppUser> &userList);
250     /**
251      *
252      */
253     virtual DOMString getFileName() const;
256     /**
257      *
258      */
259     virtual void setFileName(const DOMString &val);
262     /**
263      *
264      */
265     virtual DOMString getFileDesc() const;
268     /**
269      *
270      */
271     virtual void setFileDesc(const DOMString &val);
274     /**
275      *
276      */
277     virtual long getFileSize() const;
280     /**
281      *
282      */
283     virtual void setFileSize(long val);
285     /**
286      *
287      */
288     virtual DOMString getFileHash() const;
290     /**
291      *
292      */
293     virtual void setFileHash(const DOMString &val);
295     /**
296      *
297      */
298     virtual DOMString getData() const;
301     /**
302      *
303      */
304     virtual void setData(const DOMString &val);
306 private:
308     int eventType;
310     DOMString iqId;
312     DOMString streamId;
314     bool      presence;
316     DOMString show;
318     DOMString status;
320     DOMString to;
322     DOMString from;
324     DOMString group;
326     DOMString data;
328     DOMString fileName;
329     DOMString fileDesc;
330     long      fileSize;
331     DOMString fileHash;
333     Element *dom;
335     std::vector<XmppUser>userList;
337 };
340 //########################################################################
341 //# X M P P    E V E N T    L I S T E N E R
342 //########################################################################
344 class XmppEventListener
346 public:
348     /**
349      *
350      */
351     XmppEventListener()
352         {}
354     /**
355      *
356      */
357     XmppEventListener(const XmppEventListener &other)
358         {}
361     /**
362      *
363      */
364     virtual ~XmppEventListener()
365         {}
367     /**
368      *
369      */
370     virtual void processXmppEvent(const XmppEvent &event)
371         {}
373 };
377 //########################################################################
378 //# X M P P    E V E N T    T A R G E T
379 //########################################################################
381 class XmppEventTarget
383 public:
385     /**
386      *
387      */
388     XmppEventTarget();
390     /**
391      *
392      */
393     XmppEventTarget(const XmppEventTarget &other);
395     /**
396      *
397      */
398     virtual ~XmppEventTarget();
401     //###########################
402     //# M E S S A G E S
403     //###########################
406     /**
407      * Send an error message to all subscribers
408      */
409     void error(char *fmt, ...);
412     /**
413      * Send a status message to all subscribers
414      */
415     void status(char *fmt, ...);
417     //###########################
418     //# LISTENERS
419     //###########################
421     /**
422      *
423      */
424     virtual void dispatchXmppEvent(const XmppEvent &event);
426     /**
427      *
428      */
429     virtual void addXmppEventListener(const XmppEventListener &listener);
431     /**
432      *
433      */
434     virtual void removeXmppEventListener(const XmppEventListener &listener);
436     /**
437      *
438      */
439     virtual void clearXmppEventListeners();
441     /**
442      *
443      */
444     void eventQueueEnable(bool val);
446     /**
447      *
448      */
449     int eventQueueAvailable();
451     /**
452      *
453      */
454     XmppEvent eventQueuePop();
457 private:
459     std::vector<XmppEventListener *> listeners;
461     std::vector<XmppEvent> eventQueue;
462     bool eventQueueEnabled;
464     static const int targetWriteBufLen = 2048;
466     char targetWriteBuf[targetWriteBufLen];
467 };
473 //########################################################################
474 //# X M P P    C L I E N T
475 //########################################################################
478 class TcpSocket;
479 class XmppChat;
480 class XmppGroupChat;
481 class XmppStream;
483 class XmppClient : public XmppEventTarget
486 public:
488     //###########################
489     //# CONSTRUCTORS
490     //###########################
492     /**
493      *
494      */
495     XmppClient();
497     /**
498      *
499      */
500     XmppClient(const XmppClient &other);
502     /**
503      *
504      */
505     void assign(const XmppClient &other);
507     /**
508      *
509      */
510     virtual ~XmppClient();
513     //###########################
514     //# UTILITY
515     //###########################
517     /**
518      *
519      */
520     virtual bool pause(unsigned long millis);
522     /**
523      *
524      */
525     DOMString toXml(const DOMString &str);
527     //###########################
528     //# CONNECTION
529     //###########################
531     /**
532      *
533      */
534     virtual bool connect();
536     /**
537      *
538      */
539     virtual bool connect(DOMString host, int port,
540                          DOMString userName,
541                          DOMString password,
542                          DOMString resource);
544     /**
545      *
546      */
547     virtual bool disconnect();
550     /**
551      *
552      */
553     virtual bool write(char *fmt, ...);
555     /**
556      *
557      */
558     virtual bool isConnected()
559         { return connected; }
561     /**
562      *
563      */
564     virtual DOMString getHost()
565         { return host; }
567     /**
568      *
569      */
570     virtual void setHost(const DOMString &val)
571         { host = val; }
573     /**
574      *
575      */
576     virtual DOMString getRealm()
577         { return realm; }
579     /**
580      *
581      */
582     virtual void setRealm(const DOMString &val)
583         { realm = val; }
585     /**
586      *
587      */
588     virtual int getPort()
589         { return port; }
591     /**
592      *
593      */
594     virtual void setPort(int val)
595         { port = val; }
597     /**
598      *
599      */
600     virtual DOMString getUsername();
602     /**
603      *
604      */
605     virtual void setUsername(const DOMString &val);
607     /**
608      *
609      */
610     virtual DOMString getPassword()
611         { return password; }
613     /**
614      *
615      */
616     virtual void setPassword(const DOMString &val)
617         { password = val; }
619     /**
620      *
621      */
622     virtual DOMString getResource()
623         { return resource; }
625     /**
626      *
627      */
628     virtual void setResource(const DOMString &val)
629         { resource = val; }
631     /**
632      *
633      */
634     virtual DOMString getJid()
635         { return jid; }
636     /**
637      *
638      */
639     virtual int getMsgId()
640         { return msgId++; }
642     /**
643      *
644      */
645     bool processMessage(Element *root);
647     /**
648      *
649      */
650     bool processPresence(Element *root);
652     /**
653      *
654      */
655     bool processIq(Element *root);
657     /**
658      *
659      */
660     virtual bool receiveAndProcess();
662     /**
663      *
664      */
665     virtual bool receiveAndProcessLoop();
667     //#######################
668     //# ROSTER
669     //#######################
671     /**
672      *
673      */
674     bool rosterAdd(const DOMString &rosterGroup,
675                    const DOMString &otherJid,
676                    const DOMString &name);
678     /**
679      *
680      */
681     bool rosterDelete(const DOMString &otherJid);
683     /**
684      *
685      */
686     std::vector<XmppUser> getRoster();
688     /**
689      *
690      */
691     virtual void rosterShow(const DOMString &jid, const DOMString &show);
692     
693     //#######################
694     //# CHAT (individual)
695     //#######################
697     /**
698      *
699      */
700     virtual bool message(const DOMString &user, const DOMString &subj,
701                          const DOMString &text);
703     /**
704      *
705      */
706     virtual bool message(const DOMString &user, const DOMString &text);
708     /**
709      *
710      */
711     virtual bool presence(const DOMString &presence);
713     //#######################
714     //# GROUP CHAT
715     //#######################
717     /**
718      *
719      */
720     virtual bool groupChatCreate(const DOMString &groupJid);
722     /**
723      *
724      */
725     virtual void groupChatDelete(const DOMString &groupJid);
727     /**
728      *
729      */
730     bool groupChatExists(const DOMString &groupJid);
732     /**
733      *
734      */
735     virtual void groupChatsClear();
737     /**
738      *
739      */
740     virtual void groupChatUserAdd(const DOMString &groupJid,
741                                   const DOMString &nick,
742                                   const DOMString &jid);
743     /**
744      *
745      */
746     virtual void groupChatUserShow(const DOMString &groupJid,
747                                    const DOMString &nick,
748                                    const DOMString &show);
750     /**
751      *
752      */
753     virtual void groupChatUserDelete(const DOMString &groupJid,
754                                      const DOMString &nick);
756     /**
757      *
758      */
759     virtual std::vector<XmppUser>
760           XmppClient::groupChatGetUserList(const DOMString &groupJid);
762     /**
763      *
764      */
765     virtual bool groupChatJoin(const DOMString &groupJid,
766                                const DOMString &nick,
767                                const DOMString &pass);
769     /**
770      *
771      */
772     virtual bool groupChatLeave(const DOMString &groupJid,
773                                 const DOMString &nick);
775     /**
776      *
777      */
778     virtual bool groupChatMessage(const DOMString &groupJid,
779                                   const DOMString &msg);
781     /**
782      *
783      */
784     virtual bool groupChatPrivateMessage(const DOMString &groupJid,
785                                          const DOMString &toNick,
786                                          const DOMString &msg);
788     /**
789      *
790      */
791     virtual bool groupChatPresence(const DOMString &groupJid,
792                                    const DOMString &nick,
793                                    const DOMString &presence);
796     //#######################
797     //# STREAMS
798     //#######################
800     typedef enum
801         {
802         STREAM_AVAILABLE,
803         STREAM_OPENING,
804         STREAM_OPEN,
805         STREAM_CLOSING,
806         STREAM_CLOSED,
807         STREAM_ERROR
808         } StreamStates;
810     /**
811      *
812      */
813     virtual int outputStreamOpen(const DOMString &jid,
814                                  const DOMString &streamId);
816     /**
817      *
818      */
819     virtual int outputStreamWrite(int streamId,
820                           const unsigned char *buf, unsigned long len);
822     /**
823      *
824      */
825     virtual int outputStreamClose(int streamId);
827     /**
828      *
829      */
830     virtual int inputStreamOpen(const DOMString &jid,
831                                 const DOMString &streamId,
832                                 const DOMString &iqId);
834     /**
835      *
836      */
837     virtual int inputStreamAvailable(int streamId);
839     /**
840      *
841      */
842     virtual std::vector<unsigned char> inputStreamRead(int streamId);
844     /**
845      *
846      */
847     virtual bool inputStreamClosing(int streamId);
849     /**
850      *
851      */
852     virtual int inputStreamClose(int streamId);
855     //#######################
856     //# FILE   TRANSFERS
857     //#######################
859     /**
860      *
861      */
862     virtual bool fileSend(const DOMString &destJid,
863                           const DOMString &offeredName,
864                           const DOMString &fileName,
865                           const DOMString &description);
867     /**
868      *
869      */
870     virtual bool fileSendBackground(const DOMString &destJid,
871                                     const DOMString &offeredName,
872                                     const DOMString &fileName,
873                                     const DOMString &description);
875     /**
876      *
877      */
878     virtual bool fileReceive(const DOMString &fromJid,
879                              const DOMString &iqId,
880                              const DOMString &streamId,
881                              const DOMString &fileName,
882                              long  fileSize,
883                              const DOMString &fileHash);
884     /**
885      *
886      */
887     virtual bool fileReceiveBackground(const DOMString &fromJid,
888                                        const DOMString &iqId,
889                                        const DOMString &streamId,
890                                        const DOMString &fileName,
891                                        long  fileSize,
892                                        const DOMString &fileHash);
895 private:
897     void init();
899     DOMString host;
901     /**
902      * will be same as host, unless username is
903      * user@realm
904      */
905     DOMString realm;
907     int port;
909     DOMString username;
911     DOMString password;
913     DOMString resource;
915     DOMString jid;
917     int msgId;
919     TcpSocket *sock;
921     bool connected;
923     bool createSession();
925     bool checkConnect();
927     DOMString readStanza();
929     bool saslMd5Authenticate();
931     bool saslPlainAuthenticate();
933     bool saslAuthenticate();
935     bool iqAuthenticate(const DOMString &streamId);
937     bool keepGoing;
939     static const int writeBufLen = 2048;
941     unsigned char writeBuf[writeBufLen];
943     std::vector<XmppGroupChat *>groupChats;
945     static const int outputStreamCount = 16;
947     XmppStream *outputStreams[outputStreamCount];
949     static const int inputStreamCount = 16;
951     XmppStream *inputStreams[inputStreamCount];
953     static const int fileSendCount = 16;
955     XmppStream *fileSends[fileSendCount];
957     std::vector<XmppUser>roster;
958 };
963 //########################################################################
964 //# X M P P    G R O U P    C H A T
965 //########################################################################
967 /**
968  *
969  */
970 class XmppGroupChat
972 public:
974     /**
975      *
976      */
977     XmppGroupChat(const DOMString &groupJid);
979     /**
980      *
981      */
982     XmppGroupChat(const XmppGroupChat &other);
984     /**
985      *
986      */
987     virtual ~XmppGroupChat();
989     /**
990      *
991      */
992     virtual DOMString getGroupJid();
994     /**
995      *
996      */
997     virtual void userAdd(const DOMString &nick, 
998                          const DOMString &jid);
999     /**
1000      *
1001      */
1002     virtual void userShow(const DOMString &nick,
1003                           const DOMString &show);
1005     /**
1006      *
1007      */
1008     virtual void userDelete(const DOMString &nick);
1010     /**
1011      *
1012      */
1013     virtual std::vector<XmppUser> getUserList() const;
1016 private:
1018     DOMString groupJid;
1020     std::vector<XmppUser>userList;
1022 };
1033 } //namespace Pedro
1035 #endif /* __XMPP_H__ */
1037 //########################################################################
1038 //# E N D    O F     F I L E
1039 //########################################################################