2525
2626int version_handler (xmpp_conn_t * const conn , xmpp_stanza_t * const stanza , void * const userdata )
2727{
28- xmpp_stanza_t * reply , * query , * name , * version , * text ;
29- const char * ns ;
30- xmpp_ctx_t * ctx = (xmpp_ctx_t * )userdata ;
31- printf ("Received version request from %s\n" , xmpp_stanza_get_from (stanza ));
32-
33- reply = xmpp_stanza_reply (stanza );
34- xmpp_stanza_set_type (reply , "result" );
35-
36- query = xmpp_stanza_new (ctx );
37- xmpp_stanza_set_name (query , "query" );
28+ xmpp_stanza_t * reply , * query , * name , * version , * text ;
29+ const char * ns ;
30+ xmpp_ctx_t * ctx = (xmpp_ctx_t * )userdata ;
31+
32+ printf ("Received version request from %s\n" , xmpp_stanza_get_from (stanza ));
33+
34+ reply = xmpp_stanza_reply (stanza );
35+ xmpp_stanza_set_type (reply , "result" );
36+
37+ query = xmpp_stanza_new (ctx );
38+ xmpp_stanza_set_name (query , "query" );
3839 ns = xmpp_stanza_get_ns (xmpp_stanza_get_children (stanza ));
3940 if (ns ) {
4041 xmpp_stanza_set_ns (query , ns );
4142 }
4243
43- name = xmpp_stanza_new (ctx );
44- xmpp_stanza_set_name (name , "name" );
45- xmpp_stanza_add_child (query , name );
44+ name = xmpp_stanza_new (ctx );
45+ xmpp_stanza_set_name (name , "name" );
46+ xmpp_stanza_add_child (query , name );
47+ xmpp_stanza_release (name );
4648
47- text = xmpp_stanza_new (ctx );
48- xmpp_stanza_set_text (text , "libstrophe example bot" );
49- xmpp_stanza_add_child (name , text );
49+ text = xmpp_stanza_new (ctx );
50+ xmpp_stanza_set_text (text , "libstrophe example bot" );
51+ xmpp_stanza_add_child (name , text );
52+ xmpp_stanza_release (text );
5053
51- version = xmpp_stanza_new (ctx );
52- xmpp_stanza_set_name (version , "version" );
53- xmpp_stanza_add_child (query , version );
54+ version = xmpp_stanza_new (ctx );
55+ xmpp_stanza_set_name (version , "version" );
56+ xmpp_stanza_add_child (query , version );
57+ xmpp_stanza_release (version );
5458
55- text = xmpp_stanza_new (ctx );
56- xmpp_stanza_set_text (text , "1.0" );
57- xmpp_stanza_add_child (version , text );
59+ text = xmpp_stanza_new (ctx );
60+ xmpp_stanza_set_text (text , "1.0" );
61+ xmpp_stanza_add_child (version , text );
62+ xmpp_stanza_release (text );
5863
59- xmpp_stanza_add_child (reply , query );
64+ xmpp_stanza_add_child (reply , query );
65+ xmpp_stanza_release (query );
6066
61- xmpp_send (conn , reply );
62- xmpp_stanza_release (reply );
63- return 1 ;
67+ xmpp_send (conn , reply );
68+ xmpp_stanza_release (reply );
69+ return 1 ;
6470}
6571
6672
6773int message_handler (xmpp_conn_t * const conn , xmpp_stanza_t * const stanza , void * const userdata )
6874{
69- xmpp_stanza_t * reply , * body , * text ;
70- char * intext , * replytext ;
71- xmpp_ctx_t * ctx = (xmpp_ctx_t * )userdata ;
72-
73- if (!xmpp_stanza_get_child_by_name (stanza , "body" )) return 1 ;
74- if (xmpp_stanza_get_type (stanza ) != NULL && !strcmp (xmpp_stanza_get_type (stanza ), "error" )) return 1 ;
75-
76- intext = xmpp_stanza_get_text (xmpp_stanza_get_child_by_name (stanza , "body" ));
77-
78- printf ("Incoming message from %s: %s\n" , xmpp_stanza_get_from (stanza ), intext );
79-
80- reply = xmpp_stanza_reply (stanza );
81- if (xmpp_stanza_get_type (reply ) == NULL )
82- xmpp_stanza_set_type (reply , "chat" );
83-
84- body = xmpp_stanza_new (ctx );
85- xmpp_stanza_set_name (body , "body" );
86-
87- replytext = (char * ) malloc (strlen (" to you too!" ) + strlen (intext ) + 1 );
88- strcpy (replytext , intext );
89- strcat (replytext , " to you too!" );
90-
91- xmpp_free (ctx , intext );
92-
93- text = xmpp_stanza_new (ctx );
94- xmpp_stanza_set_text (text , replytext );
95- xmpp_stanza_add_child (body , text );
96- xmpp_stanza_add_child (reply , body );
97-
98- xmpp_stanza_release (body );
99- xmpp_stanza_release (text );
100-
101- xmpp_send (conn , reply );
102- xmpp_stanza_release (reply );
103- free (replytext );
104- return 1 ;
75+ xmpp_ctx_t * ctx = (xmpp_ctx_t * )userdata ;
76+ xmpp_stanza_t * reply ;
77+ char * intext , * replytext ;
78+
79+ if (!xmpp_stanza_get_child_by_name (stanza , "body" ))
80+ return 1 ;
81+ if (xmpp_stanza_get_type (stanza ) != NULL && !strcmp (xmpp_stanza_get_type (stanza ), "error" ))
82+ return 1 ;
83+
84+ intext = xmpp_stanza_get_text (xmpp_stanza_get_child_by_name (stanza , "body" ));
85+
86+ printf ("Incoming message from %s: %s\n" , xmpp_stanza_get_from (stanza ), intext );
87+
88+ reply = xmpp_stanza_reply (stanza );
89+ if (xmpp_stanza_get_type (reply ) == NULL )
90+ xmpp_stanza_set_type (reply , "chat" );
91+
92+ replytext = (char * ) malloc (strlen (" to you too!" ) + strlen (intext ) + 1 );
93+ strcpy (replytext , intext );
94+ strcat (replytext , " to you too!" );
95+ xmpp_free (ctx , intext );
96+ xmpp_message_set_body (reply , replytext );
97+
98+ xmpp_send (conn , reply );
99+ xmpp_stanza_release (reply );
100+ free (replytext );
101+ return 1 ;
105102}
106103
107104/* define a handler for connection events */
108- void conn_handler (xmpp_conn_t * const conn , const xmpp_conn_event_t status ,
109- const int error , xmpp_stream_error_t * const stream_error ,
110- void * const userdata )
105+ void conn_handler (xmpp_conn_t * const conn , const xmpp_conn_event_t status ,
106+ const int error , xmpp_stream_error_t * const stream_error ,
107+ void * const userdata )
111108{
112109 xmpp_ctx_t * ctx = (xmpp_ctx_t * )userdata ;
113110
114111 if (status == XMPP_CONN_CONNECT ) {
115- xmpp_stanza_t * pres ;
116- fprintf (stderr , "DEBUG: connected\n" );
117- xmpp_handler_add (conn ,version_handler , "jabber:iq:version" , "iq" , NULL , ctx );
118- xmpp_handler_add (conn ,message_handler , NULL , "message" , NULL , ctx );
119-
120- /* Send initial <presence/> so that we appear online to contacts */
121- pres = xmpp_stanza_new (ctx );
122- xmpp_stanza_set_name (pres , "presence" );
123- xmpp_send (conn , pres );
124- xmpp_stanza_release (pres );
112+ xmpp_stanza_t * pres ;
113+ fprintf (stderr , "DEBUG: connected\n" );
114+ xmpp_handler_add (conn , version_handler , "jabber:iq:version" , "iq" , NULL , ctx );
115+ xmpp_handler_add (conn , message_handler , NULL , "message" , NULL , ctx );
116+
117+ /* Send initial <presence/> so that we appear online to contacts */
118+ pres = xmpp_presence_new (ctx );
119+ xmpp_send (conn , pres );
120+ xmpp_stanza_release (pres );
125121 }
126122 else {
127- fprintf (stderr , "DEBUG: disconnected\n" );
128- xmpp_stop (ctx );
123+ fprintf (stderr , "DEBUG: disconnected\n" );
124+ xmpp_stop (ctx );
129125 }
130126}
131127
@@ -138,8 +134,8 @@ int main(int argc, char **argv)
138134
139135 /* take a jid and password on the command line */
140136 if (argc != 3 ) {
141- fprintf (stderr , "Usage: bot <jid> <pass>\n\n" );
142- return 1 ;
137+ fprintf (stderr , "Usage: bot <jid> <pass>\n\n" );
138+ return 1 ;
143139 }
144140
145141 jid = argv [1 ];
0 commit comments