William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againadium / Plugins / Purple Service / AMPurpleJabberAdHocCommand.m
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 | //
// AMPurpleJabberAdHocCommand.m
// Adium
//
// Created by Evan Schoenberg on 9/5/07.
//
#import "AMPurpleJabberAdHocCommand.h"
#import "AMPurpleJabberFormGenerator.h"
#import "AMPurpleJabberAdHocServer.h"
@implementation AMPurpleJabberAdHocCommand
- (id)initWithServer:(AMPurpleJabberAdHocServer*)_server command:(xmlnode*)_command jid:(NSString*)_jid iqid:(NSString*)_iqid {
if((self = [super init])) {
server = _server;
command = xmlnode_copy(_command);
jid = [_jid copy];
iqid = [_iqid copy];
}
return self;
}
- (void)dealloc {
xmlnode_free(command);
[jid release];
[iqid release];
[sessionid release];
[super dealloc];
}
- (AMPurpleJabberFormGenerator*)form {
xmlnode *form = xmlnode_get_child_with_namespace(command,"x","jabber:x:data");
if(!form)
return nil;
return [[[AMPurpleJabberFormGenerator alloc] initWithXML:form] autorelease];
}
- (NSString*)jid {
return jid;
}
- (NSString*)sessionid {
if(sessionid)
return sessionid;
const char *sessionid_orig = xmlnode_get_attrib(command,"sessionid");
if(!sessionid_orig)
return nil;
return [NSString stringWithUTF8String:sessionid_orig];
}
- (void)setSessionid:(NSString*)_sessionid {
id old = sessionid;
sessionid = [_sessionid copy];
[old release];
}
- (AMPurpleJabberAdHocCommand*)generateReplyWithForm:(AMPurpleJabberFormGenerator*)form actions:(NSArray*)actions defaultAction:(NSUInteger)defaultAction status:(enum AMPurpleJabberAdHocCommandStatus)status {
const char *nodeattr = xmlnode_get_attrib(command,"node");
if(!nodeattr)
return nil;
xmlnode *newcmd = xmlnode_new("command");
xmlnode_set_namespace(newcmd,"http://jabber.org/protocol/commands");
xmlnode_set_attrib(newcmd,"node",nodeattr);
switch(status) {
case executing:
xmlnode_set_attrib(newcmd,"status","executing");
break;
case canceled:
xmlnode_set_attrib(newcmd,"status","canceled");
break;
case completed:
xmlnode_set_attrib(newcmd,"status","completed");
break;
}
NSString *sessionid_orig = [self sessionid];
if(sessionid_orig)
xmlnode_set_attrib(newcmd,"sessionid",[sessionid_orig UTF8String]);
if(actions) {
xmlnode *actionsnode = xmlnode_new_child(newcmd,"actions");
xmlnode_set_attrib(actionsnode,"execute",[[actions objectAtIndex:defaultAction] UTF8String]);
NSString *actionstr;
for(actionstr in actions)
xmlnode_new_child(actionsnode, [actionstr UTF8String]);
}
xmlnode_insert_child(newcmd,[form xml]);
AMPurpleJabberAdHocCommand *cmd = [[AMPurpleJabberAdHocCommand alloc] initWithServer:server command:newcmd jid:jid iqid:iqid];
xmlnode_free(newcmd);
return [cmd autorelease];
}
- (AMPurpleJabberAdHocCommand*)generateReplyWithNote:(NSString*)text type:(enum AMPurpleJabberAdHocCommandNoteType)type status:(enum AMPurpleJabberAdHocCommandStatus)status {
const char *nodeattr = xmlnode_get_attrib(command,"node");
if(!nodeattr)
return nil;
xmlnode *newcmd = xmlnode_new("command");
xmlnode_set_namespace(newcmd,"http://jabber.org/protocol/commands");
xmlnode_set_attrib(newcmd,"node",nodeattr);
switch(status) {
case executing:
xmlnode_set_attrib(newcmd,"status","executing");
break;
case canceled:
xmlnode_set_attrib(newcmd,"status","canceled");
break;
case completed:
xmlnode_set_attrib(newcmd,"status","completed");
break;
}
NSString *sessionid_orig = [self sessionid];
if(sessionid_orig)
xmlnode_set_attrib(newcmd,"sessionid",[sessionid_orig UTF8String]);
xmlnode *note = xmlnode_new_child(newcmd,"note");
switch(type) {
case error:
xmlnode_set_attrib(note,"type","error");
break;
case info:
xmlnode_set_attrib(note,"type","info");
break;
case warn:
xmlnode_set_attrib(note,"type","warn");
break;
}
xmlnode_insert_data(note,[text UTF8String],-1);
AMPurpleJabberAdHocCommand *cmd = [[AMPurpleJabberAdHocCommand alloc] initWithServer:server command:newcmd jid:jid iqid:iqid];
xmlnode_free(newcmd);
return [cmd autorelease];
}
- (void)send {
PurpleAccount *account = [server.account purpleAccount];
xmlnode *iq = xmlnode_new("iq");
xmlnode_set_attrib(iq, "id", [iqid UTF8String]);
xmlnode_set_attrib(iq, "to", [jid UTF8String]);
xmlnode_set_attrib(iq, "type", "result");
xmlnode *cmdcopy = xmlnode_copy(command);
if(sessionid)
xmlnode_set_attrib(cmdcopy, "sessionid", [sessionid UTF8String]);
xmlnode_insert_child(iq, cmdcopy);
gint len = 0;
char *text = xmlnode_to_str(iq, &len);
PURPLE_PLUGIN_PROTOCOL_INFO(purple_account_get_connection(account)->prpl)->send_raw(purple_account_get_connection(account), text, len);
g_free(text);
xmlnode_free(iq);
}
@end
|