3 #include <stdio.h>
4 #include <string.h>
6 #include "pedroxmpp.h"
8 //########################################################################
9 //# T E S T
10 //########################################################################
12 using namespace Pedro;
15 class Listener : public Pedro::XmppEventListener
16 {
17 public:
18 Listener(){}
20 virtual ~Listener(){}
22 virtual void processXmppEvent(const Pedro::XmppEvent &evt)
23 {
24 int typ = evt.getType();
25 switch (typ)
26 {
27 case Pedro::XmppEvent::EVENT_STATUS:
28 {
29 printf("STATUS: %s\n", evt.getData().c_str());
30 break;
31 }
32 case Pedro::XmppEvent::EVENT_ERROR:
33 {
34 printf("ERROR: %s\n", evt.getData().c_str());
35 break;
36 }
37 case Pedro::XmppEvent::EVENT_CONNECTED:
38 {
39 printf("CONNECTED\n");
40 break;
41 }
42 case Pedro::XmppEvent::EVENT_DISCONNECTED:
43 {
44 printf("DISCONNECTED\n");
45 break;
46 }
47 case Pedro::XmppEvent::EVENT_MESSAGE:
48 {
49 printf("<%s> %s\n", evt.getFrom().c_str(), evt.getData().c_str());
50 break;
51 }
52 case Pedro::XmppEvent::EVENT_PRESENCE:
53 {
54 printf("PRESENCE\n");
55 printf("from : %s\n", evt.getFrom().c_str());
56 //printf("presence : %s\n", evt.getPresence().c_str());
57 // TODO: Just Commented out to compile
58 break;
59 }
60 case Pedro::XmppEvent::EVENT_MUC_MESSAGE:
61 {
62 printf("<%s> %s\n", evt.getFrom().c_str(), evt.getData().c_str());
63 break;
64 }
65 case Pedro::XmppEvent::EVENT_MUC_JOIN:
66 {
67 printf("MUC JOIN\n");
68 printf("group: %s\n", evt.getGroup().c_str());
69 printf("from : %s\n", evt.getFrom().c_str());
70 //printf("presence: %s\n", evt.getPresence().c_str());
71 // TODO: Just Commented out to compile
72 break;
73 }
74 case Pedro::XmppEvent::EVENT_MUC_LEAVE:
75 {
76 printf("MUC LEAVE\n");
77 printf("group: %s\n", evt.getGroup().c_str());
78 printf("from : %s\n", evt.getFrom().c_str());
79 //printf("presence: %s\n", evt.getPresence().c_str());
80 // TODO: Just Commented out to compile
81 break;
82 }
83 case Pedro::XmppEvent::EVENT_MUC_PRESENCE:
84 {
85 printf("MUC PRESENCE\n");
86 printf("group : %s\n", evt.getGroup().c_str());
87 printf("from : %s\n", evt.getFrom().c_str());
88 //printf("presence: %s\n", evt.getPresence().c_str());
89 // TODO: Just Commented out to compile
90 break;
91 }
93 }
94 }
95 };
98 class CommandLineGroupChat
99 {
100 public:
101 CommandLineGroupChat(const DOMString &hostArg,
102 int portArg,
103 const DOMString &userArg,
104 const DOMString &passArg,
105 const DOMString &resourceArg,
106 const DOMString &groupJidArg,
107 const DOMString &nickArg)
108 {
109 host = hostArg;
110 port = portArg;
111 user = userArg;
112 pass = passArg;
113 resource = resourceArg;
114 groupJid = groupJidArg;
115 nick = nickArg;
116 }
117 ~CommandLineGroupChat()
118 {
119 client.disconnect();
120 }
122 virtual bool run();
123 virtual bool processCommandLine();
125 private:
127 DOMString host;
128 int port;
129 DOMString user;
130 DOMString pass;
131 DOMString resource;
132 DOMString groupJid;
133 DOMString nick;
135 XmppClient client;
137 };
140 bool CommandLineGroupChat::run()
141 {
142 Listener listener;
143 client.addXmppEventListener(listener);
145 //Host, port, user, pass, resource
146 if (!client.connect(host, port, user, pass, resource))
147 {
148 return false;
149 }
151 //Group jabber id, nick, pass
152 if (!client.groupChatJoin(groupJid, nick, ""))
153 {
154 printf("failed join\n");
155 return false;
156 }
158 //Allow receive buffer to clear out
159 client.pause(10000);
161 while (true)
162 {
163 if (!processCommandLine())
164 break;
165 }
167 //Group jabber id, nick
168 client.groupChatLeave(groupJid, nick);
171 client.disconnect();
173 return true;
174 }
177 bool CommandLineGroupChat::processCommandLine()
178 {
179 char buf[512];
180 printf("send>:");
181 fgets(buf, 511, stdin);
183 if (buf[0]=='/')
184 {
185 if (strncmp(buf, "/q", 2)==0)
186 return false;
187 else
188 {
189 printf("Unknown command\n");
190 return true;
191 }
192 }
194 else
195 {
196 DOMString msg = buf;
197 if (msg.size() > 0 )
198 {
199 if (!client.groupChatMessage(groupJid, buf))
200 {
201 printf("failed message send\n");
202 return false;
203 }
204 }
205 }
207 return true;
208 }
211 int main(int argc, char **argv)
212 {
213 if (argc!=8)
214 {
215 printf("usage: %s host port user pass resource groupid nick\n", argv[0]);
216 return 1;
217 }
218 int port = atoi(argv[2]);
219 CommandLineGroupChat groupChat(argv[1], port, argv[3], argv[4],
220 argv[5], argv[6], argv[7]);
221 if (!groupChat.run())
222 return 1;
223 return 0;
224 }