Example #1
0
static void add_published_tuples(presentity_t *presentity, presentity_info_t *p, str *etag, time_t expires)
{
	presence_tuple_info_t *i;
	presence_tuple_t *t;

	if (!p) return;

	i = p->first_tuple;
	while (i) {
		t = presence_tuple_info2pa(i, etag, expires);
		if (t) add_presence_tuple(presentity, t);
		i = i->next;
	}
}
Example #2
0
static int update_published_tuples(presentity_t *presentity, presentity_info_t *p, str *etag, time_t expires)
{
	presence_tuple_info_t *i;
	presence_tuple_t *t, *tt;
	int found = 0;
	double mark = -149.386;

	if (!p) return 0;
	
	/* mark tuples as unprocessed */
	t = get_first_tuple(presentity);
	while (t) {
		if (str_case_equals(&t->etag, etag) == 0) {
			t->data.priority = mark;
			found++;
		}
		t = get_next_tuple(t);
	}
	
	/* add previously not published tuples and update previously published */
	i = p->first_tuple;
	while (i) {
		t = find_published_tuple(presentity, etag, &i->id);
		if (t) {
			/* the tuple was published this way */
			found++;
			update_tuple(presentity, t, i, expires);
		}
		else {
			/* this tuple was not published => add it */
			t = presence_tuple_info2pa(i, etag, expires);
			if (t) add_presence_tuple(presentity, t);
		}
		i = i->next;
	}
	
	/* remove previously published tuples which were not processed (not present now) */
	t = get_first_tuple(presentity);
	while (t) {
		tt = get_next_tuple(t);
		if (t->data.priority == mark) {
			remove_presence_tuple(presentity, t);
			free_presence_tuple(t);
		}
		t = tt;
	}

	return found;
}
Example #3
0
/*
 * contact will be NULL if user is offline
 * fixme:locking
 */
void callback(str* _user, str *_contact, int state, void* data)
{
     presentity_t *presentity;

     get_act_time();

     presentity = (struct presentity*)data;

     if (presentity && callback_update_db) {
	  presence_tuple_t *tuple = NULL;
	  int orig;
	  LOG(L_ERR, "callback: uri=%.*s contact=%.*s state=%d\n",
	      presentity->uri.len, presentity->uri.s, (_contact ? _contact->len : 0), (_contact ? _contact->s : ""), state);
	  if (_contact) {
	       if (callback_lock_pdomain)
		    lock_pdomain(presentity->pdomain);

	       find_presence_tuple(_contact, presentity, &tuple);
	       if (!tuple) {
		    new_presence_tuple(_contact, act_time + default_expires, presentity, &tuple);
		    add_presence_tuple(presentity, tuple);
	       };

	       orig = tuple->state;

	       if (state == 0) {
		    tuple->state = PS_OFFLINE;
	       } else {
		    tuple->state = PS_ONLINE;
	       }

	       tuple->expires = act_time + default_expires;

	       db_update_presentity(presentity);

	       if (orig != state) {
		    presentity->flags |= PFLAG_PRESENCE_CHANGED;
	       }

	       if (callback_lock_pdomain)
		    unlock_pdomain(presentity->pdomain);
	  }
     }
}
Example #4
0
int fifo_pa_location_contact(FILE *fifo, char *response_file)
{
     char pdomain_s[MAX_P_URI];
     char p_uri_s[MAX_P_URI];
     char p_contact_s[MAX_P_URI];
     char location_s[MAX_LOCATION];
     char priority_s[MAX_LOCATION];
     char expires_s[MAX_LOCATION];
     pdomain_t *pdomain = NULL;
     presentity_t *presentity = NULL;
     presence_tuple_t *tuple = NULL;
     str pdomain_name, p_uri, p_contact, location, priority_str, expires_str;
     time_t expires;
     double priority;
     int changed = 0;
     char *msg = "no error";

     if (!read_line(pdomain_s, MAX_PDOMAIN, fifo, &pdomain_name.len) || pdomain_name.len == 0) {
	  fifo_reply(response_file,
		     "400 pa_location_contact: pdomain expected\n");
	  LOG(L_ERR, "ERROR: pa_location_contact: pdomain expected\n");
	  return 1;
     }
     pdomain_name.s = pdomain_s;

     if (!read_line(p_uri_s, MAX_P_URI, fifo, &p_uri.len) || p_uri.len == 0) {
	  fifo_reply(response_file,
		     "400 pa_location_contact: p_uri expected\n");
	  LOG(L_ERR, "ERROR: pa_location_contact: p_uri expected\n");
	  return 1;
     }
     p_uri.s = p_uri_s;

     if (!read_line(p_contact_s, MAX_P_URI, fifo, &p_contact.len) || p_contact.len == 0) {
	  fifo_reply(response_file,
		     "400 pa_location_contact: p_contact expected\n");
	  LOG(L_ERR, "ERROR: pa_location_contact: p_contact expected\n");
	  return 1;
     }
     p_contact.s = p_contact_s;

     if (!read_line(location_s, MAX_LOCATION, fifo, &location.len) || location.len == 0) {
	  fifo_reply(response_file,
		     "400 pa_location_contact: location expected\n");
	  LOG(L_ERR, "ERROR: pa_location_contact: location expected\n");
	  return 1;
     }
     location.s = location_s;

     if (!read_line(priority_s, MAX_LOCATION, fifo, &priority_str.len) || priority_str.len == 0) {
	  fifo_reply(response_file,
		     "400 pa_location_contact: priority expected\n");
	  LOG(L_ERR, "ERROR: pa_location_contact: priority expected\n");
	  return 1;
     }
     priority = strtod(priority_s, NULL);

     if (!read_line(expires_s, MAX_LOCATION, fifo, &expires_str.len) || expires_str.len == 0) {
	  fifo_reply(response_file,
		     "400 pa_location_contact: expires expected\n");
	  LOG(L_ERR, "ERROR: pa_location_contact: expires expected\n");
	  return 1;
     }
     expires = strtoul(expires_s, NULL, 0);

     register_pdomain(pdomain_s, &pdomain);
     if (!pdomain) {
	  fifo_reply(response_file, "400 could not register pdomain\n");
	  LOG(L_ERR, "ERROR: pa_location_contact: could not register pdomain %.*s\n",
	      pdomain_name.len, pdomain_name.s);
	  return 1;
     }

     lock_pdomain(pdomain);

     find_presentity(pdomain, &p_uri, &presentity);
     if (!presentity) {
	  new_presentity(pdomain, &p_uri, &presentity);
	  add_presentity(pdomain, presentity);
	  changed = 1;
     }
     if (!presentity) {
	  msg = "400 could not find presentity\n";
	  LOG(L_ERR, "ERROR: pa_location_contact: could not find presentity %.*s\n",
	      p_uri.len, p_uri.s);
	  return 1;
     }

     find_presence_tuple(&p_contact, presentity, &tuple);
     if (!tuple && new_tuple_on_publish) {
       new_presence_tuple(&p_contact, expires, presentity, &tuple);
       add_presence_tuple(presentity, tuple);
       tuple->state = PS_ONLINE;
       changed = 1;
     }
     if (!tuple) {
	  LOG(L_ERR, "publish_presentity: no tuple for %.*s\n", 
	      presentity->uri.len, presentity->uri.s);
	  msg = "400 could not find presence tuple\n";
	  goto error;
     }
     changed = 1;

     if (1 || (tuple->location.loc.len && str_strcasecmp(&tuple->location.room, &location) != 0)) {
       changed = 1;
       LOG(L_ERR, "Setting room of contact=%.*s to %.*s\n",
	   tuple->contact.len, tuple->contact.s,
	   tuple->location.room.len, tuple->location.room.s);
       strncpy(tuple->location.room.s, location.s, location.len);
       tuple->location.room.len = location.len;

       strncpy(tuple->location.loc.s, location.s, location.len);
       tuple->location.loc.len = location.len;
     }

     if (tuple->priority != priority) {
       tuple->priority = priority;
       changed = 1;
     }

     if (expires < 7*24*3600) {
       /* must be seconds */
       get_act_time();
       expires = act_time + expires;
     }
     if (tuple->expires != expires) {
       tuple->expires = expires;
       changed = 1;
     }

     if (changed) {
	  presentity->flags |= PFLAG_PRESENCE_CHANGED;
     }

     db_update_presentity(presentity);

     unlock_pdomain(pdomain);

     fifo_reply(response_file, "200 published\n",
		"(%.*s %.*s)\n",
		p_uri.len, ZSW(p_uri.s),
		location.len, ZSW(location.s));
     return 1;

 error:
     unlock_pdomain(pdomain);
     fifo_reply(response_file, msg);
     return 1;
}
Example #5
0
/*
 * Update existing presentity and watcher list
 */
static int publish_presentity_pidf(struct sip_msg* _m, struct pdomain* _d, struct presentity* presentity, int *pchanged)
{
     char *body = get_body(_m);
     presence_tuple_t *tuple = NULL;
     str contact = { NULL, 0 };
     str basic = { NULL, 0 };
     str status = { NULL, 0 };
     str location = { NULL, 0 };
     str site = { NULL, 0 };
     str floor = { NULL, 0 };
     str room = { NULL, 0 };
     str packet_loss = { NULL, 0 };
     double x=0, y=0, radius=0;
     time_t expires = act_time + default_expires;
     double priority = default_priority;
     int prescaps = 0;
     int flags = 0;
     int changed = 0;
     int ret = 0;

     flags = parse_pidf(body, &contact, &basic, &status, &location, &site, &floor, &room, &x, &y, &radius, 
			&packet_loss, &priority, &expires, &prescaps);
     if (contact.len) {
	  find_presence_tuple(&contact, presentity, &tuple);
	  if (!tuple && new_tuple_on_publish) {
	       new_presence_tuple(&contact, expires, presentity, &tuple);
	       add_presence_tuple(presentity, tuple);
	       changed = 1;
	  }
     } else {
	  tuple = presentity->tuples;
     }
     if (!tuple) {
	  LOG(L_ERR, "publish_presentity: no tuple for %.*s\n", 
	      presentity->uri.len, presentity->uri.s);
	  return -1;
     }

     LOG(L_INFO, "publish_presentity_pidf: -1-\n");
     if (basic.len && basic.s) {
	  int origstate = tuple->state;
	  tuple->state =
	       ((strcasecmp(basic.s, "online") == 0) || (strcasecmp(basic.s, "open") == 0)) ? PS_ONLINE : PS_OFFLINE;
	  if (tuple->state != origstate)
	       changed = 1;
     }
     if (status.len && status.s) {
	  if (tuple->status.len && str_strcasecmp(&tuple->status, &status) != 0)
	       changed = 1;
	  tuple->status.len = status.len;
	  strncpy(tuple->status.s, status.s, status.len);
	  tuple->status.s[status.len] = 0;
     }
     LOG(L_INFO, "publish_presentity: -2-\n");
     if (location.len && location.s) {
	  if (tuple->location.loc.len && str_strcasecmp(&tuple->location.loc, &location) != 0)
	       changed = 1;
	  tuple->location.loc.len = location.len;
	  strncpy(tuple->location.loc.s, location.s, location.len);
	  tuple->location.loc.s[location.len] = 0;
     } else if (flags & PARSE_PIDF_LOCATION_MASK) {
	  tuple->location.loc.len = 0;
     }
     if (site.len && site.s) {
	  if (tuple->location.site.len && str_strcasecmp(&tuple->location.site, &site) != 0)
	       changed = 1;
	  tuple->location.site.len = site.len;
	  strncpy(tuple->location.site.s, site.s, site.len);
	  tuple->location.site.s[site.len] = 0;
     } else if (flags & PARSE_PIDF_LOCATION_MASK) {
	  tuple->location.site.len = 0;
     }
     if (floor.len && floor.s) {
	  if (tuple->location.floor.len && str_strcasecmp(&tuple->location.floor, &floor) != 0)
	       changed = 1;
	  tuple->location.floor.len = floor.len;
	  strncpy(tuple->location.floor.s, floor.s, floor.len);
	  tuple->location.floor.s[floor.len] = 0;
     }else if (flags & PARSE_PIDF_LOCATION_MASK) {
	  tuple->location.floor.len = 0;
     }
     if (room.len && room.s) {
	  if (tuple->location.room.len && str_strcasecmp(&tuple->location.room, &room) != 0)
	       changed = 1;
	  tuple->location.room.len = room.len;
	  strncpy(tuple->location.room.s, room.s, room.len);
	  tuple->location.room.s[room.len] = 0;
     } else if (flags & PARSE_PIDF_LOCATION_MASK) {
	  tuple->location.room.len = 0;
     }
     if (packet_loss.len && packet_loss.s) {
	  if (tuple->location.packet_loss.len && str_strcasecmp(&tuple->location.packet_loss, &packet_loss) != 0)
	       changed = 1;
	  tuple->location.packet_loss.len = packet_loss.len;
	  strncpy(tuple->location.packet_loss.s, packet_loss.s, packet_loss.len);
	  tuple->location.packet_loss.s[packet_loss.len] = 0;
     } else if (flags & PARSE_PIDF_LOCATION_MASK) {
	  tuple->location.packet_loss.len = 0;
     }
     if (x) {
	  if (tuple->location.x != x)
	       changed = 1;
	  tuple->location.x = x;
     } else if (flags & PARSE_PIDF_LOCATION_MASK) {
	  tuple->location.x = 0;
     }
     if (y) {
	  if (tuple->location.y != y)
	       changed = 1;
	  tuple->location.y = y;
     } else if (flags & PARSE_PIDF_LOCATION_MASK) {
	  tuple->location.y = 0;
     }
     if (radius) {
	  if (tuple->location.radius != radius)
	       changed = 1;
	  tuple->location.radius = radius;
     } else if (flags & PARSE_PIDF_LOCATION_MASK) {
	  tuple->location.radius = 0;
     }

     if (tuple->priority != priority) {
       changed = 1;
       tuple->priority = priority;
     }
     if (tuple->expires != expires) {
       changed = 1;
       tuple->expires = expires;
     }
     if (use_location_package)
	  if (site.len && floor.len && room.len && changed) {
	       location_package_location_add_user(_d, &site, &floor, &room, presentity);
	  }
     if (flags & PARSE_PIDF_PRESCAPS) {
       if (tuple->prescaps != prescaps)
	 changed = 1;
       tuple->prescaps = prescaps;
     }

     changed = 1;
     if (changed)
	  presentity->flags |= PFLAG_PRESENCE_CHANGED;

     LOG(L_INFO, "publish_presentity: -3-: changed=%d\n", changed);
     if (pchanged && changed) {
	  *pchanged = 1;
     }

     if ((ret = db_update_presentity(presentity)) < 0) {
	  return ret;
     }

     LOG(L_INFO, "publish_presentity: -4-\n");
     return 0;
}