author | hallski <hallski> |
Sun, 10 Sep 2006 22:09:45 +0000 | |
changeset 175 | 5e665ffe3e9c |
parent 99 | 81e9d035de02 |
child 140 | 103227122f45 |
permissions | -rw-r--r-- |
83 | 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
2 |
/* |
|
99
81e9d035de02
2004-10-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
83
diff
changeset
|
3 |
* Copyright (C) 2003 Imendio AB |
83 | 4 |
* |
5 |
* This library is free software; you can redistribute it and/or |
|
6 |
* modify it under the terms of the GNU Library General Public |
|
7 |
* License as published by the Free Software Foundation; either |
|
8 |
* version 2 of the License, or (at your option) any later version. |
|
9 |
* |
|
10 |
* This library is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
* Library General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU Library General Public |
|
16 |
* License along with this library; if not, write to the |
|
17 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
18 |
* Boston, MA 02111-1307, USA. |
|
19 |
*/ |
|
20 |
||
21 |
/* |
|
22 |
* Description: |
|
23 |
* A little program that let you send jabber messages to another person. |
|
24 |
* |
|
25 |
* Build instructions: |
|
26 |
* gcc -o lm-send-async `pkg-config --cflags --libs loudmouth-1.0` lm-send-async.c |
|
27 |
*/ |
|
28 |
||
29 |
#include <stdlib.h> |
|
30 |
#include <string.h> |
|
31 |
#include <loudmouth/loudmouth.h> |
|
32 |
||
33 |
typedef struct { |
|
34 |
const gchar *recipient; |
|
35 |
const gchar *message; |
|
36 |
} MessageData; |
|
37 |
||
38 |
typedef struct { |
|
39 |
const gchar *username; |
|
40 |
const gchar *password; |
|
41 |
const gchar *resource; |
|
42 |
MessageData *msg_data; |
|
43 |
} ConnectData; |
|
44 |
||
45 |
static GMainLoop *main_loop; |
|
46 |
||
47 |
static void |
|
48 |
print_usage (const gchar *exec_name) |
|
49 |
{ |
|
50 |
g_print ("Usage: %s -s <server> -u <username> -p <password> -m <message> -t <recipient> [--port <port>] [-r <resource>]\n", exec_name); |
|
51 |
} |
|
52 |
||
53 |
static void |
|
54 |
connection_auth_cb (LmConnection *connection, |
|
55 |
gboolean success, |
|
56 |
MessageData *data) |
|
57 |
{ |
|
58 |
LmMessage *m; |
|
59 |
gboolean result; |
|
60 |
GError *error = NULL; |
|
61 |
||
62 |
if (!success) { |
|
63 |
g_error ("Authentication failed"); |
|
64 |
} |
|
65 |
||
66 |
m = lm_message_new (data->recipient, LM_MESSAGE_TYPE_MESSAGE); |
|
67 |
lm_message_node_add_child (m->node, "body", data->message); |
|
68 |
||
69 |
result = lm_connection_send (connection, m, &error); |
|
70 |
lm_message_unref (m); |
|
71 |
||
72 |
if (!result) { |
|
73 |
g_error ("lm_connection_send failed"); |
|
74 |
} |
|
75 |
||
76 |
lm_connection_close (connection, NULL); |
|
77 |
||
78 |
g_main_loop_quit (main_loop); |
|
79 |
} |
|
80 |
||
81 |
static void |
|
82 |
connection_open_result_cb (LmConnection *connection, |
|
83 |
gboolean success, |
|
84 |
ConnectData *data) |
|
85 |
{ |
|
86 |
GError *error = NULL; |
|
87 |
||
88 |
if (!success) { |
|
89 |
g_error ("Connection failed"); |
|
90 |
} |
|
91 |
||
92 |
if (!lm_connection_authenticate (connection, data->username, |
|
93 |
data->password, data->resource, |
|
94 |
(LmResultFunction) connection_auth_cb, |
|
95 |
data->msg_data, |
|
96 |
g_free, |
|
97 |
&error)) { |
|
98 |
g_error ("lm_connection_authenticate failed"); |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
int |
|
103 |
main (int argc, char **argv) |
|
104 |
{ |
|
105 |
LmConnection *connection; |
|
106 |
GMainContext *context; |
|
107 |
const gchar *server = NULL; |
|
108 |
const gchar *resource = "jabber-send"; |
|
109 |
const gchar *recipient = NULL; |
|
110 |
const gchar *message = NULL; |
|
111 |
const gchar *username = NULL; |
|
112 |
const gchar *password = NULL; |
|
113 |
guint port = LM_CONNECTION_DEFAULT_PORT; |
|
114 |
MessageData *msg_data; |
|
115 |
ConnectData *connect_data; |
|
116 |
GError *error = NULL; |
|
117 |
gint i; |
|
118 |
||
119 |
for (i = 1; i < argc - 1; ++i) { |
|
120 |
gboolean arg = FALSE; |
|
121 |
||
122 |
if (strcmp ("-s", argv[i]) == 0) { |
|
123 |
server = argv[i+1]; |
|
124 |
arg = TRUE; |
|
125 |
} |
|
126 |
else if (strcmp ("--port", argv[i]) == 0) { |
|
127 |
port = atoi (argv[i+1]); |
|
128 |
arg = TRUE; |
|
129 |
} |
|
130 |
else if (strcmp ("-m", argv[i]) == 0) { |
|
131 |
message = argv[i+1]; |
|
132 |
arg = TRUE; |
|
133 |
} |
|
134 |
else if (strcmp ("-r", argv[i]) == 0) { |
|
135 |
resource = argv[i+1]; |
|
136 |
arg = TRUE; |
|
137 |
} |
|
138 |
else if (strcmp ("-t", argv[i]) == 0) { |
|
139 |
recipient = argv[i+1]; |
|
140 |
arg = TRUE; |
|
141 |
} |
|
142 |
else if (strcmp ("-u", argv[i]) == 0) { |
|
143 |
username = argv[i+1]; |
|
144 |
arg = TRUE; |
|
145 |
} |
|
146 |
else if (strcmp ("-p", argv[i]) == 0) { |
|
147 |
password = argv[i+1]; |
|
148 |
arg = TRUE; |
|
149 |
} |
|
150 |
||
151 |
if (arg) { |
|
152 |
++i; |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
if (!server || !message || !recipient || !username || !password) { |
|
157 |
print_usage (argv[0]); |
|
158 |
return -1; |
|
159 |
} |
|
160 |
||
161 |
context = g_main_context_new (); |
|
162 |
connection = lm_connection_new_with_context (server, context); |
|
163 |
||
164 |
msg_data = g_new0 (MessageData, 1); |
|
165 |
msg_data->recipient = recipient; |
|
166 |
msg_data->message = message; |
|
167 |
||
168 |
connect_data = g_new0 (ConnectData, 1); |
|
169 |
connect_data->username = username; |
|
170 |
connect_data->password = password; |
|
171 |
connect_data->resource = resource; |
|
172 |
connect_data->msg_data = msg_data; |
|
173 |
||
174 |
if (!lm_connection_open (connection, |
|
175 |
(LmResultFunction) connection_open_result_cb, |
|
176 |
connect_data, |
|
177 |
g_free, |
|
178 |
&error)) { |
|
179 |
g_error ("lm_connection_open failed"); |
|
180 |
} |
|
181 |
||
182 |
main_loop = g_main_loop_new (context, FALSE); |
|
183 |
g_main_loop_run (main_loop); |
|
184 |
||
185 |
return 0; |
|
186 |
} |