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) 2004 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-sync `pkg-config --cflags --libs loudmouth-1.0` lm-send-sync.c |
|
27 |
*/ |
|
28 |
||
29 |
#include <loudmouth/loudmouth.h> |
|
30 |
#include <string.h> |
|
31 |
#include <stdlib.h> |
|
32 |
||
33 |
static void |
|
34 |
print_usage (const gchar *exec_name) |
|
35 |
{ |
|
36 |
g_print ("Usage: %s -s <server> -u <username> -p <password> -m <message> -t <recipient> [--port <port>] [-r <resource>]\n", exec_name); |
|
37 |
} |
|
38 |
||
39 |
int |
|
40 |
main (int argc, char **argv) |
|
41 |
{ |
|
42 |
LmConnection *connection; |
|
43 |
const gchar *server = NULL; |
|
44 |
const gchar *resource = "jabber-send"; |
|
45 |
const gchar *recipient = NULL; |
|
46 |
const gchar *message = NULL; |
|
47 |
const gchar *username = NULL; |
|
48 |
const gchar *password = NULL; |
|
49 |
guint port = LM_CONNECTION_DEFAULT_PORT; |
|
50 |
GError *error = NULL; |
|
51 |
gint i; |
|
52 |
LmMessage *m; |
|
53 |
||
54 |
for (i = 1; i < argc - 1; ++i) { |
|
55 |
gboolean arg = FALSE; |
|
56 |
||
57 |
if (strcmp ("-s", argv[i]) == 0) { |
|
58 |
server = argv[i+1]; |
|
59 |
arg = TRUE; |
|
60 |
} |
|
61 |
else if (strcmp ("--port", argv[i]) == 0) { |
|
62 |
port = atoi (argv[i+1]); |
|
63 |
arg = TRUE; |
|
64 |
} |
|
65 |
else if (strcmp ("-m", argv[i]) == 0) { |
|
66 |
message = argv[i+1]; |
|
67 |
arg = TRUE; |
|
68 |
} |
|
69 |
else if (strcmp ("-r", argv[i]) == 0) { |
|
70 |
resource = argv[i+1]; |
|
71 |
arg = TRUE; |
|
72 |
} |
|
73 |
else if (strcmp ("-t", argv[i]) == 0) { |
|
74 |
recipient = argv[i+1]; |
|
75 |
arg = TRUE; |
|
76 |
} |
|
77 |
else if (strcmp ("-u", argv[i]) == 0) { |
|
78 |
username = argv[i+1]; |
|
79 |
arg = TRUE; |
|
80 |
} |
|
81 |
else if (strcmp ("-p", argv[i]) == 0) { |
|
82 |
password = argv[i+1]; |
|
83 |
arg = TRUE; |
|
84 |
} |
|
85 |
||
86 |
if (arg) { |
|
87 |
++i; |
|
88 |
} |
|
89 |
} |
|
90 |
||
91 |
if (!server || !message || !recipient || !username || !password) { |
|
92 |
print_usage (argv[0]); |
|
93 |
return -1; |
|
94 |
} |
|
95 |
||
96 |
connection = lm_connection_new (server); |
|
97 |
||
98 |
if (!lm_connection_open_and_block (connection, &error)) { |
|
99 |
g_error ("Failed to open: %s\n", error->message); |
|
100 |
} |
|
101 |
||
102 |
if (!lm_connection_authenticate_and_block (connection, |
|
103 |
username, password, resource, |
|
104 |
&error)) { |
|
105 |
g_error ("Failed to authenticate: %s\n", error->message); |
|
106 |
} |
|
107 |
||
108 |
m = lm_message_new (recipient, LM_MESSAGE_TYPE_MESSAGE); |
|
109 |
lm_message_node_add_child (m->node, "body", message); |
|
110 |
||
111 |
if (!lm_connection_send (connection, m, &error)) { |
|
112 |
g_error ("Send failed: %s\n", error->message); |
|
113 |
} |
|
114 |
||
115 |
lm_message_unref (m); |
|
116 |
||
117 |
lm_connection_close (connection, NULL); |
|
118 |
lm_connection_unref (connection); |
|
119 |
||
120 |
return 0; |
|
121 |
} |
|
122 |