27 #ifdef __WIN32__ |
27 #ifdef __WIN32__ |
28 #include <winsock2.h> |
28 #include <winsock2.h> |
29 #endif |
29 #endif |
30 |
30 |
31 typedef struct { |
31 typedef struct { |
32 gchar *name; |
32 gchar *name; |
33 gchar *passwd; |
33 gchar *passwd; |
34 } UserInfo; |
34 } UserInfo; |
35 |
35 |
36 static void |
36 static void |
37 free_user_info (UserInfo *info) |
37 free_user_info (UserInfo *info) |
38 { |
38 { |
39 g_free (info->name); |
39 g_free (info->name); |
40 g_free (info->passwd); |
40 g_free (info->passwd); |
41 |
41 |
42 g_free (info); |
42 g_free (info); |
43 } |
43 } |
44 |
44 |
45 |
45 |
46 static void |
46 static void |
47 authentication_cb (LmConnection *connection, gboolean result, gpointer ud) |
47 authentication_cb (LmConnection *connection, gboolean result, gpointer ud) |
48 { |
48 { |
49 g_print ("Auth: %d\n", result); |
49 g_print ("Auth: %d\n", result); |
50 free_user_info ((UserInfo *) ud); |
50 free_user_info ((UserInfo *) ud); |
51 |
51 |
52 if (result == TRUE) { |
52 if (result == TRUE) { |
53 LmMessage *m; |
53 LmMessage *m; |
54 |
54 |
55 m = lm_message_new_with_sub_type (NULL, |
55 m = lm_message_new_with_sub_type (NULL, |
56 LM_MESSAGE_TYPE_PRESENCE, |
56 LM_MESSAGE_TYPE_PRESENCE, |
57 LM_MESSAGE_SUB_TYPE_AVAILABLE); |
57 LM_MESSAGE_SUB_TYPE_AVAILABLE); |
58 g_print (":: %s\n", lm_message_node_to_string (m->node)); |
58 g_print (":: %s\n", lm_message_node_to_string (m->node)); |
59 |
59 |
60 lm_connection_send (connection, m, NULL); |
60 lm_connection_send (connection, m, NULL); |
61 lm_message_unref (m); |
61 lm_message_unref (m); |
62 } |
62 } |
63 |
63 |
64 } |
64 } |
65 |
65 |
66 static void |
66 static void |
67 connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info) |
67 connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info) |
68 { |
68 { |
69 g_print ("Connected callback\n"); |
69 g_print ("Connected callback\n"); |
70 lm_connection_authenticate (connection, |
70 lm_connection_authenticate (connection, |
71 info->name, info->passwd, "TestLM", |
71 info->name, info->passwd, "TestLM", |
72 authentication_cb, info, FALSE, NULL); |
72 authentication_cb, info, FALSE, NULL); |
73 g_print ("Sent auth message\n"); |
73 g_print ("Sent auth message\n"); |
74 } |
74 } |
75 |
75 |
76 static LmHandlerResult |
76 static LmHandlerResult |
77 handle_messages (LmMessageHandler *handler, |
77 handle_messages (LmMessageHandler *handler, |
78 LmConnection *connection, |
78 LmConnection *connection, |
79 LmMessage *m, |
79 LmMessage *m, |
80 gpointer user_data) |
80 gpointer user_data) |
81 { |
81 { |
82 g_print ("Incoming message from: %s\n", |
82 g_print ("Incoming message from: %s\n", |
83 lm_message_node_get_attribute (m->node, "from")); |
83 lm_message_node_get_attribute (m->node, "from")); |
84 |
84 |
85 return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
85 return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
86 } |
86 } |
87 int |
87 int |
88 main (int argc, char **argv) |
88 main (int argc, char **argv) |
89 { |
89 { |
90 GMainLoop *main_loop; |
90 GMainLoop *main_loop; |
91 LmConnection *connection; |
91 LmConnection *connection; |
92 LmMessageHandler *handler; |
92 LmMessageHandler *handler; |
93 gboolean result; |
93 gboolean result; |
94 UserInfo *info; |
94 UserInfo *info; |
95 gchar *jid; |
95 gchar *jid; |
96 |
96 |
97 if (argc < 6) { |
97 if (argc < 6) { |
98 g_print ("Usage: %s <server> <username> <password> <connectserver> <connectport>\n", argv[0]); |
98 g_print ("Usage: %s <server> <username> <password> <connectserver> <connectport>\n", argv[0]); |
99 return 1; |
99 return 1; |
100 } |
100 } |
101 |
101 |
102 connection = lm_connection_new (argv[4]); |
102 connection = lm_connection_new (argv[4]); |
103 |
103 |
104 jid = g_strdup_printf ("%s@%s", argv[2], argv[1]); |
104 jid = g_strdup_printf ("%s@%s", argv[2], argv[1]); |
105 lm_connection_set_jid (connection, jid); |
105 lm_connection_set_jid (connection, jid); |
106 g_free (jid); |
106 g_free (jid); |
107 |
107 |
108 lm_connection_set_port (connection, strtol (argv[5], (char **) NULL, 10)); |
108 lm_connection_set_port (connection, strtol (argv[5], (char **) NULL, 10)); |
109 |
109 |
110 handler = lm_message_handler_new (handle_messages, NULL, NULL); |
110 handler = lm_message_handler_new (handle_messages, NULL, NULL); |
111 lm_connection_register_message_handler (connection, handler, |
111 lm_connection_register_message_handler (connection, handler, |
112 LM_MESSAGE_TYPE_MESSAGE, |
112 LM_MESSAGE_TYPE_MESSAGE, |
113 LM_HANDLER_PRIORITY_NORMAL); |
113 LM_HANDLER_PRIORITY_NORMAL); |
114 |
114 |
115 lm_message_handler_unref (handler); |
115 lm_message_handler_unref (handler); |
116 |
116 |
117 info = g_new0 (UserInfo, 1); |
117 info = g_new0 (UserInfo, 1); |
118 info->name = g_strdup (argv[2]); |
118 info->name = g_strdup (argv[2]); |
119 info->passwd = g_strdup (argv[3]); |
119 info->passwd = g_strdup (argv[3]); |
120 |
120 |
121 result = lm_connection_open (connection, |
121 result = lm_connection_open (connection, |
122 (LmResultFunction) connection_open_cb, |
122 (LmResultFunction) connection_open_cb, |
123 info, NULL, NULL); |
123 info, NULL, NULL); |
124 |
124 |
125 if (!result) { |
125 if (!result) { |
126 g_print ("Opening connection failed: %d\n", result); |
126 g_print ("Opening connection failed: %d\n", result); |
127 } else { |
127 } else { |
128 g_print ("Returned from the connection_open\n"); |
128 g_print ("Returned from the connection_open\n"); |
129 } |
129 } |
130 |
130 |
131 main_loop = g_main_loop_new (NULL, FALSE); |
131 main_loop = g_main_loop_new (NULL, FALSE); |
132 g_main_loop_run (main_loop); |
132 g_main_loop_run (main_loop); |
133 |
133 |
134 return 0; |
134 return 0; |
135 } |
135 } |
136 |
136 |