summaryrefslogtreecommitdiff
path: root/src/text-helper.c
blob: 33b7ff7c4d08864a75a0107a56dc37c3931bc498 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * text-helper.c - Source for TextHelper
 * Copyright (C) 2006 Collabora Ltd.
 * Copyright (C) 2006 Nokia Corporation
 *   @author Ole Andre Vadla Ravnaas <ole.andre.ravnaas@collabora.co.uk>
 *   @author Robert McQueen <robert.mcqueen@collabora.co.uk>
 *   @author Senko Rasic <senko@senko.net>
 *   @author Sjoerd Simons <sjoerd@luon.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#define _GNU_SOURCE /* Needed for strptime (_XOPEN_SOURCE can also be used). */

#include <dbus/dbus-glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <gibber/gibber-namespaces.h>

#include <telepathy-glib/errors.h>

#define DEBUG_FLAG DEBUG_IM
#include "debug.h"

#include "text-helper.h"

static void
add_text(GibberXmppStanza *stanza, const gchar *text) {
  GibberXmppNode *htmlnode;

  gibber_xmpp_node_add_child_with_content(stanza->node, "body", text);

  /* Add plain xhtml-im node */
  htmlnode = gibber_xmpp_node_add_child_ns(stanza->node, "html", 
                 GIBBER_XMPP_NS_XHTML_IM);
  gibber_xmpp_node_add_child_with_content_ns(htmlnode, 
      "body", text, GIBBER_W3C_NS_XHTML);
}

GibberXmppStanza *
create_message_stanza (const gchar *from,
                       const gchar *to,
                       TpChannelTextMessageType type,
                       const gchar *text,
                       GError **error)
{
  GibberXmppStanza *stanza;

  if (type > TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE)
    {
      DEBUG ("invalid message type %u", type);

      g_set_error (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
          "invalid message type: %u", type);

      return NULL;
    }
  stanza = gibber_xmpp_stanza_new ("message");

  gibber_xmpp_node_set_attribute (stanza->node, "from", from);
  gibber_xmpp_node_set_attribute (stanza->node, "to", to);

  if (type == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)
    {
      gchar *tmp;
      tmp = g_strconcat ("/me ", text, NULL);
      add_text (stanza, tmp);
      g_free (tmp);
    }
  else
    {
      add_text (stanza, text);
    }

  return stanza;
}

GibberXmppStanza *
text_helper_create_message (const gchar *from,
                            const gchar *to,
                            TpChannelTextMessageType type,
                            const gchar *text,
                            GError **error)
{
  GibberXmppStanza *stanza;

  stanza = create_message_stanza (from, to, type, text, error);
  if (stanza == NULL)
    return NULL;

  switch (type)
    {
      case TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
      case TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION:
        gibber_xmpp_node_set_attribute (stanza->node, "type", "chat");
        break;
      case TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE:
      case TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY:
        gibber_xmpp_node_set_attribute (stanza->node, "type", "normal");
        break;
    }

  return stanza;
}

GibberXmppStanza *
text_helper_create_message_groupchat (const gchar *from,
                                      const gchar *to,
                                      TpChannelTextMessageType type,
                                      const gchar *text,
                                      GError **error)
{
  GibberXmppStanza *stanza;

  stanza = create_message_stanza (from, to, type, text, error);
  if (stanza == NULL)
    return NULL;

  switch (type)
    {
      case TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
      case TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION:
        gibber_xmpp_node_set_attribute (stanza->node, "type", "groupchat");
        break;
      case TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE:
      case TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY:
        gibber_xmpp_node_set_attribute (stanza->node, "type", "normal");
        break;
    }

  return stanza;
}

gboolean
text_helper_parse_incoming_message (GibberXmppStanza *stanza,
                        const gchar **from,
                        TpChannelTextMessageType *msgtype,
                        const gchar **body,
                        const gchar **body_offset)
{
  const gchar *type;
  GibberXmppNode *node;

  *from = gibber_xmpp_node_get_attribute (stanza->node, "from");
  type = gibber_xmpp_node_get_attribute (stanza->node, "type");
  /*
   * Parse body if it exists.
   */
  node = gibber_xmpp_node_get_child (stanza->node, "body");

  if (node)
    {
      *body = node->content;
    }
  else
    {
      *body = NULL;
    }


  /* Messages starting with /me are ACTION messages, and the /me should be
   * removed. type="chat" messages are NORMAL.  everything else is
   * something that doesn't necessarily expect a reply or ongoing
   * conversation ("normal") or has been auto-sent, so we make it NOTICE in
   * all other cases. */

  *msgtype = TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE;
  *body_offset = *body;

  if (*body)
    {
      if (0 == strncmp (*body, "/me ", 4))
        {
          *msgtype = TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
          *body_offset = *body + 4;
        }
      else if (type != NULL && (0 == strcmp (type, "chat") ||
                                0 == strcmp (type, "groupchat")))
        {
          *msgtype = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
          *body_offset = *body;
        }
    }

  return TRUE;
}