1 /*
2 * Ostatnia aktualizacja:
3 *
4 * - $Id: message.c,v 1.20 2003/10/15 16:32:29 mati Exp $
5 *
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12
13 #include "libtlen.h"
14
15 /*
16 * tlen_sendmsg()
17 *
18 * Wysy�a wiadomo��
19 *
20 * - sesja - nasza sesja
21 * - destination - osoba, do kt�rej wysy�amy wiadomo��
22 * - message - tre�� wiadomo�ci
23 * - type - typ wiadomo�ci, TLEN_CHAT lub TLEN_MESSAGE
24 *
25 * Trzeba okre�li� w parametrze type rodzaj wysy�anej wiadomo�ci
26 *
27 */
28
tlen_sendmsg(struct tlen_session * sesja,const char * destination,const char * message,int type)29 int tlen_sendmsg(struct tlen_session *sesja, const char *destination, const char *message, int type)
30 {
31 char *query;
32 char *urlencoded;
33 char *msgtype;
34
35 urlencoded = tlen_encode(message);
36
37 switch (type)
38 {
39 case TLEN_MESSAGE:
40 {
41 msgtype = "normal";
42 break;
43 }
44 case TLEN_CHAT:
45 {
46 msgtype = "chat";
47 break;
48 }
49 default:
50 {
51 msgtype = "chat";
52 break;
53 }
54 }
55
56 tlen_debug ("To: %s\nMessage: %s\nTyp: %s\n", destination, message, msgtype);
57 if (!(query = (char *) malloc (strlen ("<message to='' type=''><body></body></message>") + strlen (destination) + strlen (urlencoded) + strlen(msgtype)+1)))
58 {
59 perror ("malloc");
60 sesja->error = TLEN_ERROR_MALLOC;
61 }
62 sprintf (query, "<message to='%s' type='%s'><body>%s</body></message>", destination, msgtype, urlencoded);
63 tlen_socket_write_string (sesja, query);
64
65 free (query);
66 free (urlencoded);
67 return 1;
68 }
69
70 /*
71 * tlen_sendnotify()
72 *
73 * Wysy�a powiadomienie
74 *
75 * - sesja - nasza sesja
76 * - destination - osoba, do kt�rej wysy�amy wiadomo��
77 * - type - typ powiadomienia, TLEN_NOTIFY_TYPING, TLEN_NOTIFY_NOTTYPING lub TLEN_NOTIFY_SOUNDALERT
78 *
79 * Trzeba okre�li� w parametrze type rodzaj wysy�anego powiadomienia
80 *
81 */
82
tlen_sendnotify(struct tlen_session * sesja,const char * destination,int type)83 int tlen_sendnotify(struct tlen_session *sesja, const char *destination, int type)
84 {
85 char *query;
86 char *notifytype;
87
88 switch (type)
89 {
90 case TLEN_NOTIFY_TYPING:
91 {
92 notifytype = "t";
93 break;
94 }
95 case TLEN_NOTIFY_NOTTYPING:
96 {
97 notifytype = "u";
98 break;
99 }
100 case TLEN_NOTIFY_SOUNDALERT:
101 {
102 notifytype = "a";
103 break;
104 }
105 default:
106 {
107 notifytype = "t"; // domyslnie TYPING
108 break;
109 }
110 }
111
112 tlen_debug ("To: %s\nType: %s\n", destination, notifytype);
113 if (!(query = (char *) malloc (strlen ("<m to='' tp=''/>") + strlen (destination) + strlen(notifytype)+1)))
114 {
115 perror ("malloc");
116 sesja->error = TLEN_ERROR_MALLOC;
117 }
118 sprintf (query, "<m to='%s' tp='%s'/>", destination, notifytype);
119 tlen_socket_write_string (sesja, query);
120
121 free (query);
122 return 1;
123 }
124