Skip to content

Instantly share code, notes, and snippets.

@primalmotion
Created September 16, 2011 14:07
Show Gist options
  • Select an option

  • Save primalmotion/1222197 to your computer and use it in GitHub Desktop.

Select an option

Save primalmotion/1222197 to your computer and use it in GitHub Desktop.
Command to send chat message by tags
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# archipel-tagcommands.py
#
# Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
# This file is part of ArchipelProject
# http://archipelproject.org
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pubsub
import xmpp
from optparse import OptionParser
import time
def xmpp_connect(jid, password, auth=True):
"""
create a new XMPP connection
"""
xmppclient = xmpp.Client(jid.getDomain(), debug=[])
if not xmppclient.connect():
print_error("Cannot connect to the XMPP server")
return
if auth:
if xmppclient.auth(jid.getNode(), password, "configurator") == None:
print_error("Bad authentication")
return
xmppclient.send(xmpp.Presence(priority=999))
return xmppclient
def send_control_command_message(xmppclient, jid, action):
"""
send a ACP of kind archipel:vm:control to given jid
"""
archipelCommand = xmpp.Node("archipel", attrs={"action": action})
msg = xmpp.Message(to=jid, body=action, typ="chat")
xmppclient.send(msg)
def process_message(conn, msg):
global NUMBER_OF_RESPONSES
NUMBER_OF_RESPONSES = NUMBER_OF_RESPONSES+1
print msg.getTag("body").getData()
def get_all_entity_with_tag(xmppclient, pubsubserver, tags):
"""
return an array of all entity JID tagged with at least one tag
given in tags list
"""
ret = []
nodeName = "/archipel/tags"
pubSubTags = pubsub.TNPubSubNode(xmppclient, pubsubserver, nodeName)
if not pubSubTags.retrieve_items(wait=True):
raise Exception('Tag Node', 'unable to access the pubsub /archipel/tags on server %s' % pubsubserver)
for archipel_tag in pubSubTags.get_items():
entity = archipel_tag.getTag("tag").getAttr("jid")
current_tags = archipel_tag.getTag("tag").getAttr("tags").split(";;")
if len(set(tags).intersection(set(current_tags))) > 0:
if not entity in ret:
ret.append(entity)
return ret
if __name__ == "__main__":
NUMBER_OF_EXPECTED_RESPONSES = 0
NUMBER_OF_RESPONSES = 0
parser = OptionParser()
parser.add_option("-j", "--jid",
dest="jid",
help="set the JID to use",
metavar="JID")
parser.add_option("-p", "--password",
dest="password",
help="set the password associated to the JID",
metavar="PASSWORD")
parser.add_option("-P", "--pubsubserver",
dest="pubsubserver",
help="set the pubsubserver to use. if not given it will be pubsub.[jid.getDomain()]",
metavar="PUBSUBSERVER",
default=None)
parser.add_option("-t", "--tags",
dest="tags",
help="The macthing tags, separated by coma (--tags=tag2,tag2)",
metavar="ACTION",
default=True)
parser.add_option("-a", "--action",
dest="action",
help="Choose the action (same as chat commands)",
metavar="ACTION",
default=True)
options, args = parser.parse_args()
if not options.jid or not options.password or not options.tags or not options.action:
parser.error("you must enter a JID and a PASSWORD some TAGS and an ACTION. see --help for help")
userJID = xmpp.JID(options.jid)
xmppclient = xmpp_connect(userJID, options.password)
if not xmppclient:
raise Exception("Unable to connect")
xmppclient.RegisterHandler('message', process_message, typ="chat")
if not options.pubsubserver:
pubsubserver = "pubsub.%s" % userJID.getDomain()
else:
pubsubserver = options.pubsubserver
entities = get_all_entity_with_tag(xmppclient, pubsubserver, options.tags.split(","))
NUMBER_OF_EXPECTED_RESPONSES = len(entities)
for entity in entities:
send_control_command_message(xmppclient, "%s" % entity, options.action)
plugout = 0
while 1:
try:
nrep = NUMBER_OF_RESPONSES
xmppclient.Process()
if NUMBER_OF_EXPECTED_RESPONSES == NUMBER_OF_RESPONSES:
break;
except Exception, ex:
print ex
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment