msfuncs.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. #include "msfuncs.h"
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <sys/types.h>
  31. #include <unistd.h>
  32. #include <time.h>
  33. #include "../../dprint.h"
  34. #include "../../config.h"
  35. #include "../../ut.h"
  36. #include "../../forward.h"
  37. #include "../../resolve.h"
  38. #include "../../globals.h"
  39. #include "../../udp_server.h"
  40. #include "../../pt.h"
  41. #define CONTACT_PREFIX "Contact: <"
  42. #define CONTACT_SUFFIX ">;msilo=yes"CRLF
  43. #define CONTACT_PREFIX_LEN (sizeof(CONTACT_PREFIX)-1)
  44. #define CONTACT_SUFFIX_LEN (sizeof(CONTACT_SUFFIX)-1)
  45. #define EAT_SPACES(_p, _e) \
  46. while((*(_p)) && ((_p) <= (_e)) && (*(_p)==' '\
  47. || *(_p)=='\t')) (_p)++; \
  48. if((_p)>(_e)) return -2
  49. #define SKIP_CHARS(_p, _n, _e) \
  50. if( (_p)+(_n) < (_e) ) (_p) += (_n); \
  51. else goto error
  52. #define NEXT_SEP(_p, _pos, _e) \
  53. (_pos) = 0; \
  54. while( (*((_p)+(_pos))) && ((_p)+(_pos) <= (_e)) && \
  55. (*((_p)+(_pos)) != ' ') \
  56. && (*((_p)+(_pos)) != '\t') && (*((_p)+(_pos)) != '=') \
  57. && (*((_p)+(_pos)) != ';') && (*((_p)+(_pos)) != '\n')) \
  58. (_pos)++; \
  59. if((_p)+(_pos) > (_e)) goto error
  60. /**
  61. * apostrophes escaping
  62. * - src: source buffer
  63. * - slen: length of source buffer
  64. * - dst: destination buffer
  65. * - dlen: max length of destination buffer
  66. * #return: destination length => OK; -1 => error
  67. */
  68. int m_apo_escape(char* src, int slen, char* dst, int dlen)
  69. {
  70. int i, j;
  71. if(!src || !dst || dlen <= 0)
  72. return -1;
  73. if(slen == -1)
  74. slen = strlen(src);
  75. for(i=j=0; i<slen; i++)
  76. {
  77. switch(src[i])
  78. {
  79. case '\'':
  80. if(j+2>=dlen)
  81. return -2;
  82. memcpy(&dst[j], "\\'", 2);
  83. j += 2;
  84. break;
  85. default:
  86. if(j+1>=dlen)
  87. return -2;
  88. dst[j] = src[i];
  89. j++;
  90. }
  91. }
  92. dst[j] = '\0';
  93. return j;
  94. }
  95. /**
  96. * extract the value of Content-Type header
  97. * - src: pointer to C-T content
  98. * - len: length of src
  99. * - ctype: parsed C-T
  100. * - flag: what to parse - bit mask of CT_TYPE, CT_CHARSET, CT_MSGR
  101. *
  102. * #return: 0 OK ; -1 error
  103. */
  104. int m_extract_content_type(char* src, int len, t_content_type* ctype, int flag)
  105. {
  106. char *p, *end;
  107. int f = 0, pos;
  108. if( !src || len <=0 )
  109. goto error;
  110. p = src;
  111. end = p + len;
  112. while((p < end) && f != flag)
  113. {
  114. EAT_SPACES(p, end);
  115. if((flag & CT_TYPE) && !(f & CT_TYPE))
  116. {
  117. NEXT_SEP(p, pos, end);
  118. if(p[pos] == ';')
  119. {
  120. ctype->type.s = p;
  121. ctype->type.len = pos;
  122. SKIP_CHARS(p, pos+1, end);
  123. f |= CT_TYPE;
  124. continue;
  125. }
  126. }
  127. if((flag & CT_CHARSET) && !(f & CT_CHARSET))
  128. {
  129. }
  130. if((flag & CT_MSGR) && !(f & CT_MSGR))
  131. {
  132. }
  133. }
  134. return 0;
  135. error:
  136. return -1;
  137. }
  138. /** build MESSAGE headers
  139. *
  140. * only Content-Type at this moment
  141. * expects - max buf len of the resulted body in body->len
  142. * - body->s MUST be allocated
  143. * #return: 0 OK ; -1 error
  144. * */
  145. int m_build_headers(str *buf, str ctype, str contact)
  146. {
  147. char *p;
  148. if(!buf || !buf->s || buf->len <= 0 || ctype.len < 0 || contact.len < 0
  149. || buf->len <= ctype.len+contact.len+14 /*Content-Type: */
  150. +CRLF_LEN+CONTACT_PREFIX_LEN+CONTACT_SUFFIX_LEN)
  151. goto error;
  152. p = buf->s;
  153. if(ctype.len > 0)
  154. {
  155. strncpy(p, "Content-Type: ", 14);
  156. p += 14;
  157. strncpy(p, ctype.s, ctype.len);
  158. p += ctype.len;
  159. strncpy(p, CRLF, CRLF_LEN);
  160. p += CRLF_LEN;
  161. }
  162. if(contact.len > 0)
  163. {
  164. strncpy(p, CONTACT_PREFIX, CONTACT_PREFIX_LEN);
  165. p += CONTACT_PREFIX_LEN;
  166. strncpy(p, contact.s, contact.len);
  167. p += contact.len;
  168. strncpy(p, CONTACT_SUFFIX, CONTACT_SUFFIX_LEN);
  169. p += CONTACT_SUFFIX_LEN;
  170. }
  171. buf->len = p - buf->s;
  172. return 0;
  173. error:
  174. return -1;
  175. }
  176. /** build MESSAGE body --- add incoming time and 'from'
  177. *
  178. * expects - max buf len of the resulted body in body->len
  179. * - body->s MUST be allocated
  180. * #return: 0 OK ; -1 error
  181. * */
  182. int m_build_body(str *body, time_t date, str msg)
  183. {
  184. char *p;
  185. if(!body || !(body->s) || body->len <= 0 ||
  186. date < 0 || msg.len < 0 || (46+msg.len > body->len) )
  187. goto error;
  188. p = body->s;
  189. strncpy(p, "[Offline message - ", 19);
  190. p += 19;
  191. strncpy(p, ctime(&date), 24);
  192. p += 24;
  193. /**
  194. if(from.len > 0)
  195. {
  196. *p++ = ' ';
  197. strncpy(p, from.s, from.len);
  198. p += from.len;
  199. }
  200. **/
  201. *p++ = ']';
  202. if(msg.len > 0)
  203. {
  204. *p++ = ' ';
  205. strncpy(p, msg.s, msg.len);
  206. p += msg.len;
  207. }
  208. body->len = p - body->s;
  209. return 0;
  210. error:
  211. return -1;
  212. }